]> jfr.im git - irc/quakenet/newserv.git/commitdiff
LUA: port luadb to dbapi2 to drop postgres dependency 61/head
authorChris Porter <redacted>
Mon, 6 Nov 2017 01:55:55 +0000 (01:55 +0000)
committerChris Porter <redacted>
Mon, 6 Nov 2017 01:55:55 +0000 (01:55 +0000)
lua/Makefile
lua/lib/db.lua
lua/lua.c
lua/luadb.c

index c516ff5fced0d317ee82eeac84197afc69e81d38..b17f59d3a0df4f6a28f417bf6b5447cb25394f41 100644 (file)
@@ -1,7 +1,7 @@
 include ../build.mk
 
-CFLAGS+=$(INCLUA) $(INCDBAPI)
-LDFLAGS+=$(LIBLUA) $(LIBDBAPI)
+CFLAGS+=$(INCLUA)
+LDFLAGS+=$(LIBLUA)
 
 .PHONY: all
 all: lua.so nterfacer_lua.so
index 6bcf90e63233ce2a97155fc05d94cbe66e0a14e9..d86f8bf5799e580a274b58304c703c0dc17a3efc 100644 (file)
@@ -1,21 +1,19 @@
 function db_queryiter()
-  local c = db_numrows()
-  local i = -1
-  local f = db_numfields()
+  local f = db_numfields() - 1
   local gb = db_getvalue
 
   return function()
-    i = i + 1
-    if i == c then
+    if not db_nextrow() then
       return nil
     end
 
     local t = {}
     for j=0,f do
-      table.insert(t, gb(i, j))
+      local v = gb(j)
+      table.insert(t, v)
     end
 
-    return t
+    return unpack(t)
   end
 end
 
index 5263f26a02f504b39f4d24e687d421ad295982e7..6199da1674e6881868ad24255d35cbd037ce73d3 100644 (file)
--- a/lua/lua.c
+++ b/lua/lua.c
@@ -44,6 +44,7 @@ void lua_startbot(void *arg);
 void lua_destroybot(void);
 void lua_startcontrol(void);
 void lua_destroycontrol(void);
+void lua_destroydb(void);
 
 void lua_onunload(lua_State *l);
 void lua_onload(lua_State *l);
@@ -151,6 +152,7 @@ void _fini() {
 
     lua_destroybot();
     lua_destroycontrol();
+    lua_destroydb();
   }
 
   freesstring(cpath);
index d0c6e4031b12b425ef992c0aa82002eaa6f79249..64aed912806f452591ae74bd33522473dbe2c516 100644 (file)
 #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) {
@@ -89,12 +110,15 @@ 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);
   }
 
@@ -106,7 +130,7 @@ static int lua_dbquery(lua_State *ps) {
   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);
 }
@@ -116,6 +140,9 @@ static int lua_dbescape(lua_State *ps) {
   char *s = (char *)lua_tostring(ps, 1);
   int len;
 
+  if(!luadb)
+    return 0;
+
   if(!s)
     return 0;
 
@@ -123,26 +150,33 @@ static int lua_dbescape(lua_State *ps) {
   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);
+}