# HG changeset patch # User jbe # Date 1287244151 -7200 # Node ID ed00b972f40edb30707fffbb0a6ee8174f5a2236 # Parent 56648d7917b18800933874280b8c6ed24100f16d Allow mondelefant.connect to be called with an explicit "conninfo" string diff -r 56648d7917b1 -r ed00b972f40e libraries/mondelefant/mondelefant_native.autodoc.lua --- a/libraries/mondelefant/mondelefant_native.autodoc.lua Sat Oct 16 17:43:28 2010 +0200 +++ b/libraries/mondelefant/mondelefant_native.autodoc.lua Sat Oct 16 17:49:11 2010 +0200 @@ -5,6 +5,7 @@ errcode = -- error code mondelefant.connect{ engine = "postgresql", -- no other engine is supported + conninfo = conninfo, -- string passed directly to PostgreSQL's libpq host = host, -- hostname or directory with leading slash where Unix-domain socket resides hostaddr = hostaddr, -- IPv4, or IPv6 address if supported port = port, -- TCP port or socket file name extension @@ -15,7 +16,7 @@ ... } -Opens a new database connection and returns a handle for that connection. +Opens a new database connection and returns a handle for that connection. You may chose to specify host, port, dbname, etc. as seperated arguments, or to use a "conninfo" string, which is directly passed to PostgreSQL's libpq. --]]-- -- implemented in mondelefant_native.c as diff -r 56648d7917b1 -r ed00b972f40e libraries/mondelefant/mondelefant_native.c --- a/libraries/mondelefant/mondelefant_native.c Sat Oct 16 17:43:28 2010 +0200 +++ b/libraries/mondelefant/mondelefant_native.c Sat Oct 16 17:49:11 2010 +0200 @@ -210,50 +210,57 @@ "Only database engine 'postgresql' is supported." ); } - // create 'conninfo' string for PQconnectdb function, which contains all - // options except "engine" option: + // copy conninfo string for PQconnectdb function from argument table to + // stack position 2: lua_settop(L, 1); - lua_pushnil(L); // slot for key at stack position 2 - lua_pushnil(L); // slot for value at stack position 3 - luaL_buffinit(L, &buf); - { - int need_seperator = 0; - while (lua_pushvalue(L, 2), lua_next(L, 1)) { - lua_replace(L, 3); - lua_replace(L, 2); - // NOTE: numbers will be converted to strings automatically here, - // but perhaps this will change in future versions of lua - luaL_argcheck(L, - lua_isstring(L, 2) && lua_isstring(L, 3), 1, "non-string contained" - ); - lua_pushvalue(L, 2); - lua_pushliteral(L, "engine"); - if (!lua_rawequal(L, -2, -1)) { - const char *value; - size_t value_len; - size_t value_pos = 0; - lua_pop(L, 1); - if (need_seperator) luaL_addchar(&buf, ' '); - luaL_addvalue(&buf); - luaL_addchar(&buf, '='); - luaL_addchar(&buf, '\''); - value = lua_tolstring(L, 3, &value_len); - do { - char c; - c = value[value_pos++]; - if (c == '\'') luaL_addchar(&buf, '\\'); - luaL_addchar(&buf, c); - } while (value_pos < value_len); - luaL_addchar(&buf, '\''); - need_seperator = 1; - } else { - lua_pop(L, 1); + lua_getfield(L, 1, "conninfo"); // 2 + // if no conninfo string was found, then assemble one from the named + // options except "engine" option: + if (!lua_toboolean(L, 2)) { + lua_settop(L, 1); + lua_pushnil(L); // slot for key at stack position 2 + lua_pushnil(L); // slot for value at stack position 3 + luaL_buffinit(L, &buf); + { + int need_seperator = 0; + while (lua_pushvalue(L, 2), lua_next(L, 1)) { + lua_replace(L, 3); + lua_replace(L, 2); + // NOTE: numbers will be converted to strings automatically here, + // but perhaps this will change in future versions of lua + luaL_argcheck(L, + lua_isstring(L, 2) && lua_isstring(L, 3), 1, "non-string contained" + ); + lua_pushvalue(L, 2); + lua_pushliteral(L, "engine"); + if (!lua_rawequal(L, -2, -1)) { + const char *value; + size_t value_len; + size_t value_pos = 0; + lua_pop(L, 1); + if (need_seperator) luaL_addchar(&buf, ' '); + luaL_addvalue(&buf); + luaL_addchar(&buf, '='); + luaL_addchar(&buf, '\''); + value = lua_tolstring(L, 3, &value_len); + do { + char c; + c = value[value_pos++]; + if (c == '\'') luaL_addchar(&buf, '\\'); + luaL_addchar(&buf, c); + } while (value_pos < value_len); + luaL_addchar(&buf, '\''); + need_seperator = 1; + } else { + lua_pop(L, 1); + } } } + luaL_pushresult(&buf); + lua_replace(L, 2); + lua_settop(L, 2); } - luaL_pushresult(&buf); - lua_replace(L, 2); - lua_settop(L, 2); + // use conninfo string on stack position 2: conninfo = lua_tostring(L, 2); // call PQconnectdb function of libpq: pgconn = PQconnectdb(conninfo);