-#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);
}
} 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;
}
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;
}
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);
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;
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;
}
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