webmcp
diff libraries/mondelefant/mondelefant_native.c @ 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 | 3a6fe8663b26 |
children | 3d43a5cf17c1 |
line diff
1.1 --- a/libraries/mondelefant/mondelefant_native.c Sat Oct 16 17:43:28 2010 +0200 1.2 +++ b/libraries/mondelefant/mondelefant_native.c Sat Oct 16 17:49:11 2010 +0200 1.3 @@ -210,50 +210,57 @@ 1.4 "Only database engine 'postgresql' is supported." 1.5 ); 1.6 } 1.7 - // create 'conninfo' string for PQconnectdb function, which contains all 1.8 - // options except "engine" option: 1.9 + // copy conninfo string for PQconnectdb function from argument table to 1.10 + // stack position 2: 1.11 lua_settop(L, 1); 1.12 - lua_pushnil(L); // slot for key at stack position 2 1.13 - lua_pushnil(L); // slot for value at stack position 3 1.14 - luaL_buffinit(L, &buf); 1.15 - { 1.16 - int need_seperator = 0; 1.17 - while (lua_pushvalue(L, 2), lua_next(L, 1)) { 1.18 - lua_replace(L, 3); 1.19 - lua_replace(L, 2); 1.20 - // NOTE: numbers will be converted to strings automatically here, 1.21 - // but perhaps this will change in future versions of lua 1.22 - luaL_argcheck(L, 1.23 - lua_isstring(L, 2) && lua_isstring(L, 3), 1, "non-string contained" 1.24 - ); 1.25 - lua_pushvalue(L, 2); 1.26 - lua_pushliteral(L, "engine"); 1.27 - if (!lua_rawequal(L, -2, -1)) { 1.28 - const char *value; 1.29 - size_t value_len; 1.30 - size_t value_pos = 0; 1.31 - lua_pop(L, 1); 1.32 - if (need_seperator) luaL_addchar(&buf, ' '); 1.33 - luaL_addvalue(&buf); 1.34 - luaL_addchar(&buf, '='); 1.35 - luaL_addchar(&buf, '\''); 1.36 - value = lua_tolstring(L, 3, &value_len); 1.37 - do { 1.38 - char c; 1.39 - c = value[value_pos++]; 1.40 - if (c == '\'') luaL_addchar(&buf, '\\'); 1.41 - luaL_addchar(&buf, c); 1.42 - } while (value_pos < value_len); 1.43 - luaL_addchar(&buf, '\''); 1.44 - need_seperator = 1; 1.45 - } else { 1.46 - lua_pop(L, 1); 1.47 + lua_getfield(L, 1, "conninfo"); // 2 1.48 + // if no conninfo string was found, then assemble one from the named 1.49 + // options except "engine" option: 1.50 + if (!lua_toboolean(L, 2)) { 1.51 + lua_settop(L, 1); 1.52 + lua_pushnil(L); // slot for key at stack position 2 1.53 + lua_pushnil(L); // slot for value at stack position 3 1.54 + luaL_buffinit(L, &buf); 1.55 + { 1.56 + int need_seperator = 0; 1.57 + while (lua_pushvalue(L, 2), lua_next(L, 1)) { 1.58 + lua_replace(L, 3); 1.59 + lua_replace(L, 2); 1.60 + // NOTE: numbers will be converted to strings automatically here, 1.61 + // but perhaps this will change in future versions of lua 1.62 + luaL_argcheck(L, 1.63 + lua_isstring(L, 2) && lua_isstring(L, 3), 1, "non-string contained" 1.64 + ); 1.65 + lua_pushvalue(L, 2); 1.66 + lua_pushliteral(L, "engine"); 1.67 + if (!lua_rawequal(L, -2, -1)) { 1.68 + const char *value; 1.69 + size_t value_len; 1.70 + size_t value_pos = 0; 1.71 + lua_pop(L, 1); 1.72 + if (need_seperator) luaL_addchar(&buf, ' '); 1.73 + luaL_addvalue(&buf); 1.74 + luaL_addchar(&buf, '='); 1.75 + luaL_addchar(&buf, '\''); 1.76 + value = lua_tolstring(L, 3, &value_len); 1.77 + do { 1.78 + char c; 1.79 + c = value[value_pos++]; 1.80 + if (c == '\'') luaL_addchar(&buf, '\\'); 1.81 + luaL_addchar(&buf, c); 1.82 + } while (value_pos < value_len); 1.83 + luaL_addchar(&buf, '\''); 1.84 + need_seperator = 1; 1.85 + } else { 1.86 + lua_pop(L, 1); 1.87 + } 1.88 } 1.89 } 1.90 + luaL_pushresult(&buf); 1.91 + lua_replace(L, 2); 1.92 + lua_settop(L, 2); 1.93 } 1.94 - luaL_pushresult(&buf); 1.95 - lua_replace(L, 2); 1.96 - lua_settop(L, 2); 1.97 + // use conninfo string on stack position 2: 1.98 conninfo = lua_tostring(L, 2); 1.99 // call PQconnectdb function of libpq: 1.100 pgconn = PQconnectdb(conninfo);