#include "lua.h"
#include "luabot.h"
-#include "../config.h"
+#include "../dbapi2/dbapi2.h"
-#ifdef HAVE_PGSQL
-#include "../dbapi/dbapi.h"
+static DBAPIConn *luadb;
-#warning Needs testing with new db API.
+typedef struct lua_callback {
+ lua_list *l;
+ long handler, args;
+} lua_callback;
static int lua_dbcreatequery(lua_State *ps) {
char *s = (char *)lua_tostring(ps, 1);
+ if(!luadb)
+ LUA_RETURN(ps, LUA_FAIL);
+
if(!s)
LUA_RETURN(ps, LUA_FAIL);
- dbcreatequery("%s", s);
+ luadb->unsafecreatetable(luadb, NULL, NULL, "%s", s);
LUA_RETURN(ps, LUA_OK);
}
-/* TODO */
-/*
-static int lua_dbloadtable(lua_State *ps) {
- lua_list *l = lua_listfromstate(ps);
- if(!l)
+static int lua_dbtablename(lua_State *ps) {
+ char *s = (char *)lua_tostring(ps, 1);
+
+ if(!luadb)
LUA_RETURN(ps, LUA_FAIL);
+ if(!s)
+ LUA_RETURN(ps, LUA_FAIL);
+
+ char *tablename = luadb->tablename(luadb, s);
+ if(!tablename)
+ LUA_RETURN(ps, LUA_FAIL);
+
+ lua_pushstring(ps, tablename);
+ return 1;
}
-*/
-typedef struct lua_callback {
- lua_list *l;
- long handler, args;
-} lua_callback;
+const static DBAPIResult *active_result;
+static void lua_dbcallback(const DBAPIResult *result, void *tag) {
+ lua_callback *c = (lua_callback *)tag;
+
+ if(!lua_listexists(c->l)) {
+ luafree(c);
+ return;
+ }
+
+ active_result = result;
+ _lua_vpcall(c->l->l, (void *)c->handler, LUA_POINTERMODE, "bR", result && result->success ? 1 : 0, c->args);
+ active_result = NULL;
-/* hack */
-DBResult *pgres;
+ if(result)
+ result->clear(result);
+
+ luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->handler);
+ luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->args);
+ luafree(c);
+}
static int lua_dbnumfields(lua_State *ps) {
- lua_pushint(ps, dbnumfields(pgres));
+ if(!luadb)
+ return 0;
+
+ if(!active_result)
+ return 0;
+ lua_pushint(ps, active_result->fields);
return 1;
}
-/* TODO */
-/*
-static int lua_dbgetrow(lua_State *ps) {
- char *r;
- int tuple, field, len;
+static int lua_dbgetvalue(lua_State *ps) {
+ if(!luadb)
+ return 0;
+
+ if(!active_result)
+ return 0;
if(!lua_isint(ps, 1))
return 0;
- lua_pushstring(ps, dbgetvalue(pgres, lua_toint(ps, 1)), len);
+ int field = lua_toint(ps, 1);
+ if(field < 0 || field >= active_result->fields)
+ return 0;
+
+ char *value = active_result->get(active_result, field);
+ /* bit rubbish, no way to get the length... binary data is out then */
+ lua_pushstring(ps, value);
return 1;
}
-*/
-
-void lua_dbcallback(DBConn *dbconn, void *arg) {
- pgres = dbgetresult(dbconn);
- lua_callback *c = (lua_callback *)arg;
-
- if(!lua_listexists(c->l)) {
- luafree(c);
- return;
- }
-
- 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);
- luafree(c);
- return;
- }
+static int lua_dbnextrow(lua_State *ps) {
+ if(!luadb)
+ return 0;
- _lua_vpcall(c->l->l, (void *)c->handler, LUA_POINTERMODE, "bR", 1, c->args);
+ if(!active_result)
+ return 0;
- luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->handler);
- luaL_unref(c->l->l, LUA_REGISTRYINDEX, c->args);
- luafree(c);
- dbclear(pgres);
- pgres = NULL;
+ int r = active_result->next(active_result);
+ lua_pushboolean(ps, r?1:0);
+ return 1;
}
static int lua_dbquery(lua_State *ps) {
char *q = (char *)lua_tostring(ps, 1);
lua_callback *cb;
+ if(!luadb)
+ LUA_RETURN(ps, LUA_FAIL);
+
/* what happens if 3 isn't there? */
if(!l || !q)
LUA_RETURN(ps, LUA_FAIL);
if(!lua_isfunction(ps, 2)) {
- dbquery("%s", q);
+ luadb->unsafesquery(luadb, "%s", q);
LUA_RETURN(ps, LUA_OK);
}
cb->args = luaL_ref(ps, LUA_REGISTRYINDEX);
cb->handler = luaL_ref(ps, LUA_REGISTRYINDEX);
- dbasyncquery(lua_dbcallback, cb, "%s", q);
+ luadb->unsafequery(luadb, lua_dbcallback, cb, "%s", q);
LUA_RETURN(ps, LUA_OK);
}
char *s = (char *)lua_tostring(ps, 1);
int len;
+ if(!luadb)
+ return 0;
+
if(!s)
return 0;
if(len > sizeof(ebuf) / 2 - 5)
return 0;
- dbescapestring(ebuf, s, len);
+ luadb->escapestring(luadb, ebuf, s, len);
lua_pushstring(ps, ebuf);
return 1;
}
void lua_registerdbcommands(lua_State *l) {
- lua_register(l, "db_createquery", lua_dbcreatequery);
-
-/* lua_register(l, "db_loadtable", lua_dbloadtable); */
+ lua_register(l, "db_tablename", lua_dbtablename);
+ lua_register(l, "db_createquery", lua_dbcreatequery);
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_getvalue", lua_dbgetvalue);*/
-}
+ lua_register(l, "db_getvalue", lua_dbgetvalue);
+ lua_register(l, "db_nextrow", lua_dbnextrow);
-#else
+ luadb = dbapi2open(NULL, "lua");
-void lua_registerdbcommands(lua_State *l) {
+ /* TODO */
+ /* open gets you a unique db */
+ /* parameterised queries (huge pain due to no va_args in dbapi2) */
+ /* loadtable */
+ /* getrow */
}
-#endif
+void lua_destroydb(void) {
+ if(luadb)
+ luadb->close(luadb);
+}