]> jfr.im git - irc/quakenet/newserv.git/blobdiff - dbapi2/dbapi2.c
Port to git.
[irc/quakenet/newserv.git] / dbapi2 / dbapi2.c
index 726100d18b9f69ed009f7f4adaa2685a833aea22..a960b74c9e0fd1b5cfe778497d8c9541e7ff6faa 100644 (file)
@@ -8,12 +8,17 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <time.h>
+#include <stdint.h>
 
 #include "../core/error.h"
 #include "../lib/strlfunc.h"
 #include "../lib/stringbuf.h"
+#include "../lib/version.h"
 #include "dbapi2.h"
 
+MODULE_VERSION("");
+
 struct DBAPIProviderData {
   char name[PROVIDER_NAME_LEN+1];
 };
@@ -96,10 +101,17 @@ static void dbunsafecreatetable(const DBAPIConn *db, DBAPIQueryCallback cb, DBAP
 
 static void dbunsafesimplequery(const DBAPIConn *db, const char *format, ...) {
   va_list ap;
+  char buf[QUERYBUFLEN];
+  size_t ret;
 
   va_start(ap, format);
-  dbunsafequery(db, NULL, NULL, format, ap);
+  ret = vsnprintf(buf, sizeof(buf), format, ap);
   va_end(ap);
+
+  if(ret >= sizeof(buf))
+    Error("dbapi2", ERR_STOP, "Query truncated in dbunsafequery, format: '%s', database: %s", format, db->name);
+
+  db->__query(db, NULL, NULL, buf);
 }
 
 static void dbsafequery(const DBAPIConn *db, DBAPIQueryCallback cb, DBAPIUserData data, const char *format, const char *types, ...) {
@@ -126,10 +138,39 @@ static void dbsafecreatetable(const DBAPIConn *db, DBAPIQueryCallback cb, DBAPIU
 
 static void dbsafesimplequery(const DBAPIConn *db, const char *format, const char *types, ...) {
   va_list ap;
+  char buf[QUERYBUFLEN];
+
+  va_start(ap, types);
+  dbvsnprintf(db, buf, sizeof(buf), format, types, ap);
+  va_end(ap);
+
+  db->__query(db, NULL, NULL, buf);
+}
+
+static void dbloadtable(const DBAPIConn *db, DBAPIQueryCallback init, DBAPIQueryCallback data, DBAPIQueryCallback fini, DBAPIUserData tag, const char *tablename) {
+  db->__loadtable(db, init, data, fini, tag, db->tablename(db, tablename));
+}
+
+static void dbcall(const DBAPIConn *db, DBAPIQueryCallback cb, DBAPIUserData data, const char *function, const char *format, const char *types, ...) {
+  va_list ap;
+  char buf[QUERYBUFLEN];
+
+  va_start(ap, types);
+  dbvsnprintf(db, buf, sizeof(buf), format, types, ap);
+  va_end(ap);
+
+  db->__call(db, cb, data, function, buf);
+}
+
+static void dbsimplecall(const DBAPIConn *db, const char *function, const char *format, const char *types, ...) {
+  va_list ap;
+  char buf[QUERYBUFLEN];
 
   va_start(ap, types);
-  dbsafequery(db, NULL, NULL, format, types, ap);
+  dbvsnprintf(db, buf, sizeof(buf), format, types, ap);
   va_end(ap);
+
+  db->__call(db, NULL, NULL, function, buf);
 }
 
 DBAPIConn *dbapi2open(const char *provider, const char *database) {
@@ -172,17 +213,21 @@ DBAPIConn *dbapi2open(const char *provider, const char *database) {
   db->query = dbsafequery;
   db->createtable = dbsafecreatetable;
   db->squery = dbsafesimplequery;
-  db->loadtable = p->loadtable;
+  db->loadtable = dbloadtable;
   db->escapestring = p->escapestring;
   db->tablename = p->tablename;
   db->unsafequery = dbunsafequery;
   db->unsafesquery = dbunsafesimplequery;
   db->unsafecreatetable = dbunsafecreatetable;
+  db->call = dbcall;
+  db->scall = dbsimplecall;
 
   db->__query = p->query;
   db->__close = p->close;
   db->__quotestring = p->quotestring;
   db->__createtable = p->createtable;
+  db->__loadtable = p->loadtable;
+  db->__call = p->call;
 
   strlcpy(db->name, database, DBNAME_LEN);
 
@@ -216,6 +261,9 @@ static void dbvsnprintf(const DBAPIConn *db, char *buf, size_t size, const char
     unsigned int u;
     size_t l;
     int fallthrough;
+    time_t t;
+    unsigned long ul;
+    long _l;
 
     for(i=0;i<VSNPF_MAXARGS;i++)
       convbuf[i][0] = '\0';
@@ -233,7 +281,8 @@ static void dbvsnprintf(const DBAPIConn *db, char *buf, size_t size, const char
       switch(*types) {
         case 's':
           s = va_arg(ap, char *);
-          l = strlen(s);
+          if(s)
+            l = strlen(s);
           fallthrough = 1;
 
         /* falling through */
@@ -243,9 +292,11 @@ static void dbvsnprintf(const DBAPIConn *db, char *buf, size_t size, const char
             l = va_arg(ap, size_t);
           }
 
-          /* now... this is a guess, but we should catch it most of the time */
-          if((l > (VSNPF_MAXARGLEN / 2)) || !db->__quotestring(db, cb, sizeof(convbuf[0]), s, l)) {
-            Error("dbapi2", ERR_WARNING, "Long string truncated, format: '%s', database: %s", format, db->name);
+          if(!s) {
+            strlcpy(cb, "NULL", sizeof(convbuf[0]));
+          } else if((l > (VSNPF_MAXARGLEN / 2)) || !db->__quotestring(db, cb, sizeof(convbuf[0]), s, l)) {
+            /* now... this is a guess, but we should catch it most of the time */
+            Error("dbapi2", ERR_STOP, "Long string truncated, format: '%s', database: %s", format, db->name);
             l = VSNPF_MAXARGLEN;
           }
 
@@ -268,6 +319,18 @@ static void dbvsnprintf(const DBAPIConn *db, char *buf, size_t size, const char
           u = va_arg(ap, unsigned int);
           snprintf(cb, VSNPF_MAXARGLEN, "%u", u);
           break;
+        case 't':
+          t = va_arg(ap, time_t);
+          snprintf(cb, VSNPF_MAXARGLEN, "%jd", (intmax_t)t);
+          break;
+        case 'D':
+          _l = va_arg(ap, long);
+          snprintf(cb, VSNPF_MAXARGLEN, "%ld", _l);
+          break;
+        case 'U':
+          ul = va_arg(ap, unsigned long);
+          snprintf(cb, VSNPF_MAXARGLEN, "%lu", ul);
+          break;
         case 'g':
           g = va_arg(ap, double);
           snprintf(cb, VSNPF_MAXARGLEN, "%.1f", g);
@@ -282,7 +345,10 @@ static void dbvsnprintf(const DBAPIConn *db, char *buf, size_t size, const char
   sbinit(&b, buf, size);
 
   for(arg=0,p=format;*p;p++) {
-    if(*p != '?') {
+    if (*p == '\\' && *(p + 1) == '?')
+      continue;
+
+    if((p != format && *(p - 1) == '\\') || *p != '?') {
       if(!sbaddchar(&b, *p))
         break;
       continue;