]> jfr.im git - irc/quakenet/newserv.git/blobdiff - dbapi2/dbapi2.c
DBAPI2: add support for NULL values to string format.
[irc/quakenet/newserv.git] / dbapi2 / dbapi2.c
index 9a641fe252cdaaa5c77140745e97392819fce92c..1f8da497c49dd29c4c2355c62940f210a0951aa8 100644 (file)
@@ -8,6 +8,8 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <time.h>
+#include <stdint.h>
 
 #include "../core/error.h"
 #include "../lib/strlfunc.h"
@@ -145,6 +147,10 @@ static void dbsafesimplequery(const DBAPIConn *db, const char *format, const cha
   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));
+}
+
 DBAPIConn *dbapi2open(const char *provider, const char *database) {
   int i, found = -1;
   DBAPIConn *db;
@@ -185,7 +191,7 @@ 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;
@@ -196,6 +202,7 @@ DBAPIConn *dbapi2open(const char *provider, const char *database) {
   db->__close = p->close;
   db->__quotestring = p->quotestring;
   db->__createtable = p->createtable;
+  db->__loadtable = p->loadtable;
 
   strlcpy(db->name, database, DBNAME_LEN);
 
@@ -229,6 +236,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';
@@ -246,7 +256,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 */
@@ -256,8 +267,10 @@ 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)) {
+          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;
           }
@@ -281,6 +294,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);