static struct sqlitequeue *head, *tail;
static int queuesize;
static void *processsched;
+static int inited;
#define SYNC_MODE "OFF"
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);
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);
}
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;
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) {
struct sqlitetableloader {
SQLiteQueryHandler init, data, fini;
+ void *tag;
char tablename[];
};
/* 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);
}
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;
t->init = init;
t->data = data;
t->fini = fini;
+ t->tag = tag;
sqliteasyncqueryf(0, loadtablecount, t, 0, "SELECT COUNT(*) FROM %s", tablename);
}