webmcp
changeset 40:ed00b972f40e
Allow mondelefant.connect to be called with an explicit "conninfo" string
author | jbe |
---|---|
date | Sat Oct 16 17:49:11 2010 +0200 (2010-10-16) |
parents | 56648d7917b1 |
children | 0bbfee4d4aed |
files | libraries/mondelefant/mondelefant_native.autodoc.lua libraries/mondelefant/mondelefant_native.c |
line diff
1.1 --- a/libraries/mondelefant/mondelefant_native.autodoc.lua Sat Oct 16 17:43:28 2010 +0200 1.2 +++ b/libraries/mondelefant/mondelefant_native.autodoc.lua Sat Oct 16 17:49:11 2010 +0200 1.3 @@ -5,6 +5,7 @@ 1.4 errcode = -- error code 1.5 mondelefant.connect{ 1.6 engine = "postgresql", -- no other engine is supported 1.7 + conninfo = conninfo, -- string passed directly to PostgreSQL's libpq 1.8 host = host, -- hostname or directory with leading slash where Unix-domain socket resides 1.9 hostaddr = hostaddr, -- IPv4, or IPv6 address if supported 1.10 port = port, -- TCP port or socket file name extension 1.11 @@ -15,7 +16,7 @@ 1.12 ... 1.13 } 1.14 1.15 -Opens a new database connection and returns a handle for that connection. 1.16 +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. 1.17 1.18 --]]-- 1.19 -- implemented in mondelefant_native.c as
2.1 --- a/libraries/mondelefant/mondelefant_native.c Sat Oct 16 17:43:28 2010 +0200 2.2 +++ b/libraries/mondelefant/mondelefant_native.c Sat Oct 16 17:49:11 2010 +0200 2.3 @@ -210,50 +210,57 @@ 2.4 "Only database engine 'postgresql' is supported." 2.5 ); 2.6 } 2.7 - // create 'conninfo' string for PQconnectdb function, which contains all 2.8 - // options except "engine" option: 2.9 + // copy conninfo string for PQconnectdb function from argument table to 2.10 + // stack position 2: 2.11 lua_settop(L, 1); 2.12 - lua_pushnil(L); // slot for key at stack position 2 2.13 - lua_pushnil(L); // slot for value at stack position 3 2.14 - luaL_buffinit(L, &buf); 2.15 - { 2.16 - int need_seperator = 0; 2.17 - while (lua_pushvalue(L, 2), lua_next(L, 1)) { 2.18 - lua_replace(L, 3); 2.19 - lua_replace(L, 2); 2.20 - // NOTE: numbers will be converted to strings automatically here, 2.21 - // but perhaps this will change in future versions of lua 2.22 - luaL_argcheck(L, 2.23 - lua_isstring(L, 2) && lua_isstring(L, 3), 1, "non-string contained" 2.24 - ); 2.25 - lua_pushvalue(L, 2); 2.26 - lua_pushliteral(L, "engine"); 2.27 - if (!lua_rawequal(L, -2, -1)) { 2.28 - const char *value; 2.29 - size_t value_len; 2.30 - size_t value_pos = 0; 2.31 - lua_pop(L, 1); 2.32 - if (need_seperator) luaL_addchar(&buf, ' '); 2.33 - luaL_addvalue(&buf); 2.34 - luaL_addchar(&buf, '='); 2.35 - luaL_addchar(&buf, '\''); 2.36 - value = lua_tolstring(L, 3, &value_len); 2.37 - do { 2.38 - char c; 2.39 - c = value[value_pos++]; 2.40 - if (c == '\'') luaL_addchar(&buf, '\\'); 2.41 - luaL_addchar(&buf, c); 2.42 - } while (value_pos < value_len); 2.43 - luaL_addchar(&buf, '\''); 2.44 - need_seperator = 1; 2.45 - } else { 2.46 - lua_pop(L, 1); 2.47 + lua_getfield(L, 1, "conninfo"); // 2 2.48 + // if no conninfo string was found, then assemble one from the named 2.49 + // options except "engine" option: 2.50 + if (!lua_toboolean(L, 2)) { 2.51 + lua_settop(L, 1); 2.52 + lua_pushnil(L); // slot for key at stack position 2 2.53 + lua_pushnil(L); // slot for value at stack position 3 2.54 + luaL_buffinit(L, &buf); 2.55 + { 2.56 + int need_seperator = 0; 2.57 + while (lua_pushvalue(L, 2), lua_next(L, 1)) { 2.58 + lua_replace(L, 3); 2.59 + lua_replace(L, 2); 2.60 + // NOTE: numbers will be converted to strings automatically here, 2.61 + // but perhaps this will change in future versions of lua 2.62 + luaL_argcheck(L, 2.63 + lua_isstring(L, 2) && lua_isstring(L, 3), 1, "non-string contained" 2.64 + ); 2.65 + lua_pushvalue(L, 2); 2.66 + lua_pushliteral(L, "engine"); 2.67 + if (!lua_rawequal(L, -2, -1)) { 2.68 + const char *value; 2.69 + size_t value_len; 2.70 + size_t value_pos = 0; 2.71 + lua_pop(L, 1); 2.72 + if (need_seperator) luaL_addchar(&buf, ' '); 2.73 + luaL_addvalue(&buf); 2.74 + luaL_addchar(&buf, '='); 2.75 + luaL_addchar(&buf, '\''); 2.76 + value = lua_tolstring(L, 3, &value_len); 2.77 + do { 2.78 + char c; 2.79 + c = value[value_pos++]; 2.80 + if (c == '\'') luaL_addchar(&buf, '\\'); 2.81 + luaL_addchar(&buf, c); 2.82 + } while (value_pos < value_len); 2.83 + luaL_addchar(&buf, '\''); 2.84 + need_seperator = 1; 2.85 + } else { 2.86 + lua_pop(L, 1); 2.87 + } 2.88 } 2.89 } 2.90 + luaL_pushresult(&buf); 2.91 + lua_replace(L, 2); 2.92 + lua_settop(L, 2); 2.93 } 2.94 - luaL_pushresult(&buf); 2.95 - lua_replace(L, 2); 2.96 - lua_settop(L, 2); 2.97 + // use conninfo string on stack position 2: 2.98 conninfo = lua_tostring(L, 2); 2.99 // call PQconnectdb function of libpq: 2.100 pgconn = PQconnectdb(conninfo);