X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/blobdiff_plain/55cee062c2130e4a6f0737e7a9899ba5eec99d46..211567363bd8bfbd70cef81e4208ea02d49ebb55:/lua/luadb.c diff --git a/lua/luadb.c b/lua/luadb.c index e47ca2d6..d0c6e403 100644 --- a/lua/luadb.c +++ b/lua/luadb.c @@ -1,14 +1,20 @@ -#include "../pqsql/pqsql.h" #include "lua.h" #include "luabot.h" +#include "../config.h" + +#ifdef HAVE_PGSQL +#include "../dbapi/dbapi.h" + +#warning Needs testing with new db API. + static int lua_dbcreatequery(lua_State *ps) { char *s = (char *)lua_tostring(ps, 1); if(!s) LUA_RETURN(ps, LUA_FAIL); - pqcreatequery(s); + dbcreatequery("%s", s); LUA_RETURN(ps, LUA_OK); } @@ -28,53 +34,44 @@ typedef struct lua_callback { } lua_callback; /* hack */ -PGresult *pgres; +DBResult *pgres; static int lua_dbnumfields(lua_State *ps) { - lua_pushint(ps, PQnfields(pgres)); + lua_pushint(ps, dbnumfields(pgres)); return 1; } -static int lua_dbnumrows(lua_State *ps) { - lua_pushint(ps, PQntuples(pgres)); - - return 1; -} - -static int lua_dbgetvalue(lua_State *ps) { +/* TODO */ +/* +static int lua_dbgetrow(lua_State *ps) { char *r; int tuple, field, len; - if(!lua_isint(ps, 1) || !lua_isint(ps, 2)) + if(!lua_isint(ps, 1)) return 0; - tuple = lua_toint(ps, 1); - field = lua_toint(ps, 2); - - r = PQgetvalue(pgres, lua_toint(ps, 1), lua_toint(ps, 2)); - len = PQgetlength(pgres, tuple, field); - - lua_pushlstring(ps, r, len); /* safe?! */ + lua_pushstring(ps, dbgetvalue(pgres, lua_toint(ps, 1)), len); return 1; } +*/ -void lua_dbcallback(PGconn *dbconn, void *arg) { - pgres = PQgetResult(dbconn); +void lua_dbcallback(DBConn *dbconn, void *arg) { + pgres = dbgetresult(dbconn); lua_callback *c = (lua_callback *)arg; if(!lua_listexists(c->l)) { - free(c); + luafree(c); return; } - if(PQresultStatus(pgres) != PGRES_TUPLES_OK) { + if(!dbquerysuccessful(pgres)) { _lua_vpcall(c->l->l, (void *)c->handler, LUA_POINTERMODE, "bR", 0, c->args); luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->handler); luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->args); - free(c); + luafree(c); return; } @@ -82,8 +79,8 @@ void lua_dbcallback(PGconn *dbconn, void *arg) { luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->handler); luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->args); - free(c); - PQclear(pgres); + luafree(c); + dbclear(pgres); pgres = NULL; } @@ -97,11 +94,11 @@ static int lua_dbquery(lua_State *ps) { LUA_RETURN(ps, LUA_FAIL); if(!lua_isfunction(ps, 2)) { - pqquery(q); + dbquery("%s", q); LUA_RETURN(ps, LUA_OK); } - cb = (lua_callback *)malloc(sizeof(lua_callback)); + cb = (lua_callback *)luamalloc(sizeof(lua_callback)); if(!cb) LUA_RETURN(ps, LUA_FAIL); @@ -109,16 +106,15 @@ static int lua_dbquery(lua_State *ps) { cb->args = luaL_ref(ps, LUA_REGISTRYINDEX); cb->handler = luaL_ref(ps, LUA_REGISTRYINDEX); - pqasyncquery(lua_dbcallback, cb, q); + dbasyncquery(lua_dbcallback, cb, "%s", q); LUA_RETURN(ps, LUA_OK); } static int lua_dbescape(lua_State *ps) { - char ebuf[8192 * 2]; + char ebuf[8192 * 2 + 1]; char *s = (char *)lua_tostring(ps, 1); int len; - size_t elen; if(!s) return 0; @@ -127,8 +123,8 @@ static int lua_dbescape(lua_State *ps) { if(len > sizeof(ebuf) / 2 - 5) return 0; - elen = PQescapeString(ebuf, s, len); - lua_pushlstring(ps, ebuf, elen); + dbescapestring(ebuf, s, len); + lua_pushstring(ps, ebuf); return 1; } @@ -141,6 +137,12 @@ void lua_registerdbcommands(lua_State *l) { lua_register(l, "db_query", lua_dbquery); lua_register(l, "db_escape", lua_dbescape); lua_register(l, "db_numfields", lua_dbnumfields); - lua_register(l, "db_numrows", lua_dbnumrows); - lua_register(l, "db_getvalue", lua_dbgetvalue); +/* lua_register(l, "db_getvalue", lua_dbgetvalue);*/ +} + +#else + +void lua_registerdbcommands(lua_State *l) { } + +#endif