]> jfr.im git - irc/quakenet/newserv.git/commitdiff
Refactor sqlite code in preparation for proper scheduling stuff.
authorChris Porter <redacted>
Thu, 20 Mar 2008 19:36:45 +0000 (19:36 +0000)
committerChris Porter <redacted>
Thu, 20 Mar 2008 19:36:45 +0000 (19:36 +0000)
dbapi/dbapi.h
sqlite/sqlite.c
sqlite/sqlite.h

index f29e1c265114e8ce9a6504fa0bfa7223838be623..7905702723945334c7764375c7ecfc3136339fd8 100644 (file)
@@ -52,8 +52,8 @@ typedef SQLiteQueryHandler DBQueryHandler;
 typedef SQLiteResult DBResult;
 
 #define dbconnected() sqliteconnected()
-#define dbgetid() sqliteconnected()
-#define dbfreeid(x) sqlitefreeid()
+#define dbgetid() sqlitegetid()
+#define dbfreeid(x) sqlitefreeid(x)
 
 #define dbcreateschema(schema) sqlitecreateschema(schema)
 #define dbescapestring(buf, src, len) sqliteescapestring(buf, src, len)
index 20385791d803d042ee952fce3f8026abdcb2f13e..13ad66c2cb195956605af9a776c3b9db4843d9c7 100644 (file)
@@ -27,6 +27,7 @@ MODULE_VERSION("");
 
 static int dbconnected = 0;
 static struct sqlite3 *conn;
+static SQLiteModuleIdentifier modid;
 
 #define SYNC_MODE "OFF"
 
@@ -166,47 +167,87 @@ int sqlitequerysuccessful(SQLiteResult *r) {
   return 0;
 }
 
-void sqliteloadtable(char *tablename, SQLiteQueryHandler init, SQLiteQueryHandler data, SQLiteQueryHandler fini) {
-  int rc;
-  sqlite3_stmt *s;
-  char buf[1024];
-  
-  if(!sqliteconnected())
-    return;
+struct sqlitetableloader {
+  SQLiteQueryHandler init, data, fini;
+  char tablename[];
+};
 
-  snprintf(buf, sizeof(buf), "SELECT COUNT(*) FROM %s", tablename);
+static void loadtablerows(SQLiteConn *c, void *tag) {
+  struct sqlitetableloader *t = (struct sqlitetableloader *)tag;
 
-  rc = sqlite3_prepare(conn, buf, -1, &s, NULL);
-  if(rc != SQLITE_OK) {
-    Error("sqlite", ERR_ERROR, "Error getting row count for %s.", tablename);
+  if(!c) { /* pqsql doesnt call the handlers so we don't either */
+    free(t);
     return;
   }
 
-  if(sqlitestep(s) != SQLITE_ROW) {
-    Error("sqlite", ERR_ERROR, "Error getting row count for %s.", tablename);
-    sqlite3_finalize(s);
+  /* the handlers do all the checking and cleanup */
+  if(t->init)
+    (t->init)(c, NULL);
+
+  (t->data)(c, NULL);
+
+  if(t->fini)
+    (t->fini)(c, NULL);
+
+  free(t);
+}
+
+static void loadtablecount(SQLiteConn *c, void *tag) {
+  struct sqlitetableloader *t = (struct sqlitetableloader *)tag;
+  SQLiteResult *r = NULL;
+
+  if(!c) { /* unloaded */
+    free(t);
+    return;
+  } 
+
+  if(!(r = sqlitegetresult(c)) || !sqlitefetchrow(r)) {
+    Error("sqlite", ERR_ERROR, "Error getting row count for %s.", t->tablename);
+    free(t);
+
+    if(r)
+      sqliteclear(r);
     return;
   }
 
-  Error("sqlite", ERR_INFO, "Found %s entries in table %s, loading...", (char *)sqlite3_column_text(s, 0), tablename);
-  sqlite3_finalize(s);
+  Error("sqlite", ERR_INFO, "Found %s entries in table %s, loading...", (char *)sqlite3_column_text(r->r, 0), t->tablename);
+  sqliteclear(r);
+  
+  sqliteasyncqueryf(0, loadtablerows, t, 0, "SELECT * FROM %s", t->tablename);
+}
 
-  snprintf(buf, sizeof(buf), "SELECT * FROM %s", tablename);
+void sqliteloadtable(char *tablename, SQLiteQueryHandler init, SQLiteQueryHandler data, SQLiteQueryHandler fini) {
+  struct sqlitetableloader *t;
+  int len;
 
-  rc = sqlite3_prepare(conn, buf, -1, &s, NULL);
-  if(rc != SQLITE_OK) {
-    Error("sqlite", ERR_ERROR, "Error loading table %s.", tablename);
+  if(!sqliteconnected())
     return;
-  }
 
-  if(init)
-    init(s, NULL);
-  data(s, NULL);
-  if(fini)
-    fini(s, NULL);
+  len = strlen(tablename);
+
+  t = (struct sqlitetableloader *)malloc(sizeof(struct sqlitetableloader) + len + 1);
+  memcpy(t->tablename, tablename, len + 1);
+  t->init = init;
+  t->data = data;
+  t->fini = fini;
+
+  sqliteasyncqueryf(0, loadtablecount, t, 0, "SELECT COUNT(*) FROM %s", tablename);
 }
 
 void sqlitecreateschema(char *schema) {
   sqliteasyncqueryf(0, NULL, NULL, 0, "ATTACH DATABASE '%s.db' AS %s", schema, schema);
   sqliteasyncqueryf(0, NULL, NULL, 0, "PRAGMA %s.synchronous=" SYNC_MODE ";", schema);
 }
+
+int sqlitegetid(void) {
+  modid++; 
+  if(modid == 0)
+    modid = 1;
+
+  return modid;
+}
+
+void sqlitefreeid(int id) {
+
+}
+
index 967255f2f4d0ceedba65e19e04058d9bb02a78d1..3bc9765f2e1a0219ecc54134ef6d908ba4f0ff4e 100644 (file)
@@ -15,8 +15,8 @@ void sqliteasyncqueryf(SQLiteModuleIdentifier identifier, SQLiteQueryHandler han
 
 int sqliteconnected(void);
 
-#define sqlitelitegetid(void) 0
-#define sqlitefreeid(x)
+int sqlitegetid(void);
+void sqlitefreeid(int);
 
 void sqliteescapestring(char *, char *, unsigned int);