X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/blobdiff_plain/2729984e3c95b8a12d68dbeae568432fea550f0a..8855bb48b449ed06cfd3ce528b3c0a77c37cb24b:/sqlite/sqlite.c diff --git a/sqlite/sqlite.c b/sqlite/sqlite.c index 7693c311..fa852571 100644 --- a/sqlite/sqlite.c +++ b/sqlite/sqlite.c @@ -7,7 +7,10 @@ #include #include +#ifndef __USE_POSIX199309 #define __USE_POSIX199309 +#endif + #include #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;iinit) - (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); }