]> jfr.im git - irc/quakenet/newserv.git/blobdiff - sqlite/sqlite.c
fixes for clang
[irc/quakenet/newserv.git] / sqlite / sqlite.c
index 7693c311f1a1dc9470819841faa1425bac7c7c6c..fa85257121488a0a70f5ea0b9a7a712a33dd0dda 100644 (file)
@@ -7,7 +7,10 @@
 #include <stdarg.h>
 #include <string.h>
 
+#ifndef __USE_POSIX199309
 #define __USE_POSIX199309
+#endif
+
 #include <time.h>
 
 #include "../core/config.h"
@@ -41,6 +44,7 @@ struct sqlitequeue {
 static struct sqlitequeue *head, *tail;
 static int queuesize;
 static void *processsched;
+static int inited;
 
 #define SYNC_MODE "OFF"
 
@@ -65,6 +69,15 @@ void _init(void) {
     return;
   }
 
+
+  if(sqlite3_initialize() != SQLITE_OK) {
+    Error("sqlite", ERR_ERROR, "Unable to initialise sqlite");
+    return;
+  }
+  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
+
+  inited = 1;
+
   rc = sqlite3_open(dbfile->content, &conn);
   freesstring(dbfile);
 
@@ -82,25 +95,31 @@ void _init(void) {
 
 void _fini(void) {
   struct sqlitequeue *q, *nq;
-  if(!sqliteconnected())
-    return;
 
-  deregisterhook(HOOK_CORE_STATSREQUEST, dbstatus);
-  deleteschedule(processsched, &sqlitequeueprocessor, NULL);
-
-  /* we assume every module that's being unloaded
-   * has us as a dependency and will have cleaned up
-   * their queries by using freeid..
-   */
-  for(q=head;q;q=nq) {
-    nq = q->next;
-    sqlite3_finalize(q->statement);
-    nsfree(POOL_SQLITE, q);
+  if(sqliteconnected()) {
+    deregisterhook(HOOK_CORE_STATSREQUEST, dbstatus);
+    deleteschedule(processsched, &sqlitequeueprocessor, NULL);
+
+    /* we assume every module that's being unloaded
+     * has us as a dependency and will have cleaned up
+     * their queries by using freeid..
+     */
+    for(q=head;q;q=nq) {
+      nq = q->next;
+      sqlite3_finalize(q->statement);
+      nsfree(POOL_SQLITE, q);
+    }
+
+    sqlite3_close(conn);
+
+    dbconnected = 0;
   }
 
-  sqlite3_close(conn);
+  if(inited) {
+    sqlite3_shutdown();
+    inited = 0;
+  }
 
-  dbconnected = 0;
   nscheckfreeall(POOL_SQLITE);
 }
 
@@ -177,11 +196,11 @@ static void popqueue(void) {
 
 void sqliteasyncqueryf(int identifier, SQLiteQueryHandler handler, void *tag, int flags, char *format, ...) {
   char querybuf[8192];
-  va_list va;
   int len;
   int rc;
   sqlite3_stmt *s;
-  
+  va_list va;
+
   if(!sqliteconnected())
     return;
 
@@ -216,16 +235,18 @@ int sqliteconnected(void) {
   return dbconnected;
 }
 
-void sqliteescapestring(char *buf, char *src, unsigned int len) {
+size_t sqliteescapestring(char *buf, char *src, unsigned int len) {
   unsigned int i;
-  char *p;
+  char *p, *d;
 
-  for(p=src,i=0;i<len;i++,p++) {
+  for(p=src,d=buf,i=0;i<len;i++,p++) {
     if(*p == '\'')
-      *buf++ = *p;
-    *buf++ = *p;
+      *d++ = *p;
+    *d++ = *p;
   }
-  *buf = '\0';
+  *d = '\0';
+
+  return d - buf;
 }
 
 SQLiteResult *sqlitegetresult(SQLiteConn *r) {
@@ -289,6 +310,7 @@ int sqlitequerysuccessful(SQLiteResult *r) {
 
 struct sqlitetableloader {
   SQLiteQueryHandler init, data, fini;
+  void *tag;
   char tablename[];
 };
 
@@ -302,12 +324,12 @@ static void loadtablerows(SQLiteConn *c, void *tag) {
 
   /* the handlers do all the checking and cleanup */
   if(t->init)
-    (t->init)(c, NULL);
+    (t->init)(NULL, t->tag);
 
-  (t->data)(c, NULL);
+  (t->data)(c, t->tag);
 
   if(t->fini)
-    (t->fini)(c, NULL);
+    (t->fini)(NULL, t->tag);
 
   nsfree(POOL_SQLITE, t);
 }
@@ -336,7 +358,7 @@ static void loadtablecount(SQLiteConn *c, void *tag) {
   sqliteasyncqueryf(0, loadtablerows, t, 0, "SELECT * FROM %s", t->tablename);
 }
 
-void sqliteloadtable(char *tablename, SQLiteQueryHandler init, SQLiteQueryHandler data, SQLiteQueryHandler fini) {
+void sqliteloadtable(char *tablename, SQLiteQueryHandler init, SQLiteQueryHandler data, SQLiteQueryHandler fini, void *tag) {
   struct sqlitetableloader *t;
   int len;
 
@@ -350,6 +372,7 @@ void sqliteloadtable(char *tablename, SQLiteQueryHandler init, SQLiteQueryHandle
   t->init = init;
   t->data = data;
   t->fini = fini;
+  t->tag = tag;
 
   sqliteasyncqueryf(0, loadtablecount, t, 0, "SELECT COUNT(*) FROM %s", tablename);
 }