Re: [PATCH] libsdb support in 1.3.1

[email protected]
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <OFB897DE79.8C78BAC9-ONCA256D57.00418FB9-CA256D57.0041903E@callista.com.au>
> thank you very much for fixing some stupid bugs introduced by me :(

Not a problem; the re-write is lots easier to work with.

John
sdb-ignore.diff (application/octet-stream, 10.3 KB)
? sdb.diff
? sdb-ignore.diff
? test/test_dbpool
? test/test_md5
? test/test_octstr_dump
? test/test_radius_acct
? test/test_radius_pdu
? utils/mtbatch
Index: contrib/sendsms
===================================================================
RCS file: /home/cvs/gateway/contrib/sendsms,v
retrieving revision 1.1
diff -u -a -u -b -r1.1 sendsms
--- contrib/sendsms	7 Dec 2000 18:04:30 -0000	1.1
+++ contrib/sendsms	2 Jul 2003 10:19:42 -0000
@@ -6,6 +6,7 @@
 PORT = 13013
 USERNAME = "tester"
 PASSWORD = "foobar"
+FROM     = "tester"
 
 NUMBERS = "~/.sendsms.dat"
 
@@ -37,8 +38,8 @@
 def sendsms():
     to = urllib.quote_plus(recipient(sys.argv[1]))
     text = urllib.quote_plus(string.join(sys.argv[2:], " "))
-    url="http://%s:%d/cgi-bin/sendsms?username=%s&password=%s&to=%s&text=%s" \
-	% (HOST, PORT, USERNAME, PASSWORD, to, text)
+    url="http://%s:%d/cgi-bin/sendsms?username=%s&password=%s&from=%s&to=%s&text=%s" \
+	% (HOST, PORT, USERNAME, PASSWORD, FROM, to, text)
     f = urllib.urlopen(url)
     print f.read()
     f.close()
Index: doc/examples/dlr-mysql.conf
===================================================================
RCS file: /home/cvs/gateway/doc/examples/dlr-mysql.conf,v
retrieving revision 1.2
diff -u -a -u -b -r1.2 dlr-mysql.conf
--- doc/examples/dlr-mysql.conf	5 Aug 2002 21:52:28 -0000	1.2
+++ doc/examples/dlr-mysql.conf	2 Jul 2003 10:19:43 -0000
@@ -24,6 +24,7 @@
 #     url varchar(255),
 #     mask int(10),
 #     status int(10)
+#     boxc varchar2(40)
 #   )
 #
 
@@ -37,4 +38,5 @@
 field-url = url
 field-mask = mask
 field-status = status
+field-boxc-id = boxc
 
Index: doc/examples/dlr-sdb.conf
===================================================================
RCS file: /home/cvs/gateway/doc/examples/dlr-sdb.conf,v
retrieving revision 1.2
diff -u -a -u -b -r1.2 dlr-sdb.conf
--- doc/examples/dlr-sdb.conf	5 Aug 2002 21:52:28 -0000	1.2
+++ doc/examples/dlr-sdb.conf	2 Jul 2003 10:19:43 -0000
@@ -39,6 +39,7 @@
 #     url varchar(255),
 #     mask int(10),
 #     status int(10)
+#     boxc varchar(40),
 #   )
 #
 # Other DB types may vary. Consult your DB admin for help in creating
@@ -55,4 +56,5 @@
 field-url = url
 field-mask = mask
 field-status = status
+field-boxc-id = boxc
 
Index: gw/dlr_mysql.c
===================================================================
RCS file: /home/cvs/gateway/gw/dlr_mysql.c,v
retrieving revision 1.1
diff -u -a -u -b -r1.1 dlr_mysql.c
--- gw/dlr_mysql.c	19 Jun 2003 22:11:50 -0000	1.1
+++ gw/dlr_mysql.c	2 Jul 2003 10:19:43 -0000
@@ -76,7 +76,7 @@
     MYSQL_RES *result;
     MYSQL_ROW row;
 
-    sql = octstr_format("SELECT %s, %s, %s, %s, %s FROM %s WHERE %s='%s' AND %s='%s';",
+    sql = octstr_format("SELECT %s, %s, %s, %s, %s, %s FROM %s WHERE %s='%s' AND %s='%s';",
                         octstr_get_cstr(fields->field_mask), octstr_get_cstr(fields->field_serv),
                         octstr_get_cstr(fields->field_url), octstr_get_cstr(fields->field_src),
                         octstr_get_cstr(fields->field_dst), octstr_get_cstr(fields->field_boxc),
Index: gw/dlr_sdb.c
===================================================================
RCS file: /home/cvs/gateway/gw/dlr_sdb.c,v
retrieving revision 1.1
diff -u -a -u -b -r1.1 dlr_sdb.c
--- gw/dlr_sdb.c	19 Jun 2003 22:11:50 -0000	1.1
+++ gw/dlr_sdb.c	2 Jul 2003 10:19:43 -0000
@@ -34,7 +34,7 @@
     mutex_destroy(dlr_mutex);
 }
 
-static void dlr_add_sdb(struct dlr_entry *dlr)
+static void dlr_sdb_add(struct dlr_entry *dlr)
 {
     Octstr *sql;
     int	state;
@@ -49,9 +49,13 @@
                         octstr_get_cstr(fields->field_status),
                         octstr_get_cstr(dlr->smsc), octstr_get_cstr(dlr->timestamp),
                         octstr_get_cstr(dlr->source), octstr_get_cstr(dlr->destination),
-                        octstr_get_cstr(dlr->service), octstr_get_cstr(dlr->url), mask,
+                        octstr_get_cstr(dlr->service), octstr_get_cstr(dlr->url), dlr->mask,
                         octstr_get_cstr(dlr->boxc_id), 0);
 
+#if defined(DLR_TRACE)
+    debug("dlr.sdb", 0, "sql: %s", octstr_get_cstr(sql));
+#endif
+
     mutex_lock(dlr_mutex);
     state = sdb_query(connection, octstr_get_cstr(sql), NULL, NULL);
     mutex_unlock(dlr_mutex);
@@ -62,34 +66,62 @@
     dlr_entry_destroy(dlr);
 }
 
-static int sdb_callback_add(int n, char **p, void *row)
+static int sdb_callback_add(int n, char **p, void *data)
 {
-	if (!n) {
-        debug("dlr.sdb", 0, "no rows found");
+    struct dlr_entry *res = (struct dlr_entry *) data;
+
+    if (n != 6) {
+        debug("dlr.sdb", 0, "Result has incorrect number of columns: %d", n);
         return 0;
     }
 
-    /* strip string into words */
-    row = octstr_split(octstr_imm(p[0]), octstr_imm(" "));
-    if (list_len(row) != 6) {
-        debug("dlr.sdb", 0, "Row has wrong length %ld", list_len(row));
+#if defined(DLR_TRACE)
+    debug("dlr.sdb", 0, "row=%s,%s,%s,%s,%s,%s",p[0],p[1],p[2],p[3],p[4],p[5]);
+#endif
+
+    if (res->service != NULL) {
+        debug("dlr.sdb", 0, "Row already stored");
         return 0;
     }
 
+    res->mask = atoi(p[0]);
+
+    res->service = octstr_create(p[1]);
+    res->url = octstr_create(p[2]);
+    res->source = octstr_create(p[3]);
+    res->destination = octstr_create(p[4]);
+    res->boxc_id = octstr_create(p[5]);
+
 	return 0;
 }
 
-static int sdb_callback_msgs(int n, char **p, void *row)
-{}
+static int sdb_callback_msgs(int n, char **p, void *data)
+{
+   long *count = (long *) data;
+
+   if (n != 1) {
+        debug("dlr.sdb", 0, "Result has incorrect number of columns: %d", n);
+        return 0;
+    }
+
+#if defined(DLR_TRACE)
+    debug("dlr.sdb", 0, "messages=%s",p[0]);
+#endif
+
+    *count = atol(p[0]);
+
+    return 0;
+}
 
 static struct dlr_entry*  dlr_sdb_get(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
 {
     Octstr *sql;
     int	state;
-    List *row;
-    struct dlr_entry *res = NULL;
+    struct dlr_entry *res = dlr_entry_create();
+
+    gw_assert(res);
 
-    sql = octstr_format("SELECT %s, %s, %s, %s, %s FROM %s WHERE %s='%s' AND %s='%s'",
+    sql = octstr_format("SELECT %s, %s, %s, %s, %s, %s FROM %s WHERE %s='%s' AND %s='%s';",
                         octstr_get_cstr(fields->field_mask), octstr_get_cstr(fields->field_serv),
                         octstr_get_cstr(fields->field_url), octstr_get_cstr(fields->field_src),
                         octstr_get_cstr(fields->field_dst), octstr_get_cstr(fields->field_boxc),
@@ -102,37 +134,19 @@
 #endif
 
     mutex_lock(dlr_mutex);
-    state = sdb_query(connection, octstr_get_cstr(sql), sdb_callback_add, row);
+    state = sdb_query(connection, octstr_get_cstr(sql), sdb_callback_add, res);
     mutex_unlock(dlr_mutex);
     octstr_destroy(sql);
     if (state == -1) {
+        dlr_entry_destroy(res);
         error(0, "SDB: error in finding DLR");
         return NULL;
     }
 
-    debug("dlr.sdb", 0, "Found entry, row[0]=%s, row[1]=%s, row[2]=%s, row[3]=%s row[4]=%s row[5]=%s",
-          octstr_get_cstr(list_get(row, 0)),
-          octstr_get_cstr(list_get(row, 1)),
-          octstr_get_cstr(list_get(row, 2)),
-          octstr_get_cstr(list_get(row, 3)),
-          octstr_get_cstr(list_get(row, 4)),
-          octstr_get_cstr(list_get(row, 5)));
-
-    res = dlr_entry_create();
-    gw_assert(res);
-    res->dlr_mask = atoi(octstr_get_cstr(list_get(row, 0)));
-    res->dlr_service = octstr_duplicate(list_get(row, 1));
-    res->dlr_url = octstr_duplicate(list_get(row, 2));
-    res->source = octstr_duplicate(list_get(row, 3));
-    res->destination = octstr_duplicate(list_get(row, 4));
-    res->boxc_id = octstr_duplicate(list_get(row, 5));
-
-    list_destroy(row, octstr_destroy_item);
-
     return res;
 }
 
-static void  dlr_sdb_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
+static void  dlr_sdb_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
 {
     Octstr *sql;
     int	state;
@@ -140,9 +154,9 @@
     debug("dlr.sdb", 0, "updating DLR status in database");
     sql = octstr_format("UPDATE %s SET %s=%d WHERE %s='%s' AND %s='%s'",
                         octstr_get_cstr(fields->table),
-                        octstr_get_cstr(fields->field_status), typ,
+                        octstr_get_cstr(fields->field_status), status,
                         octstr_get_cstr(fields->field_smsc), octstr_get_cstr(smsc),
-                        octstr_get_cstr(field_ts), octstr_get_cstr(ts));
+                        octstr_get_cstr(fields->field_ts), octstr_get_cstr(ts));
 
 #if defined(DLR_TRACE)
      debug("dlr.sdb", 0, "sql: %s", octstr_get_cstr(sql));
@@ -186,26 +200,21 @@
     int	state;
     long res;
 
-     /*
-      * XXXX select * ... is not efficient.
-     * Please use here SELECT count(*) ... , see mysql for example.
-     */
-    sql = octstr_format("SELECT * FROM %s", octstr_get_cstr(table));
+    sql = octstr_format("SELECT COUNT(*) FROM %s", octstr_get_cstr(fields->table));
 
 #if defined(DLR_TRACE)
     debug("dlr.sdb", 0, "sql: %s", octstr_get_cstr(sql));
 #endif
 
     mutex_lock(dlr_mutex);
-    state = sdb_query(connection, octstr_get_cstr(sql), sdb_callback_msgs, NULL);
+    state = sdb_query(connection, octstr_get_cstr(sql), sdb_callback_msgs, &res);
+    mutex_unlock(dlr_mutex);
     octstr_destroy(sql);
     if (state == -1) {
         error(0, "SDB: error in selecting ammount of waiting DLRs");
         mutex_unlock(dlr_mutex);
         return -1;
     }
-    res = (long) state;
-    mutex_unlock(dlr_mutex);
 
     return res;
 }
@@ -215,7 +224,7 @@
     Octstr *sql;
     int	state;
 
-    sql = octstr_format("DELETE FROM %s", octstr_get_cstr(table));
+    sql = octstr_format("DELETE FROM %s", octstr_get_cstr(fields->table));
 
 #if defined(DLR_TRACE)
      debug("dlr.sdb", 0, "sql: %s", octstr_get_cstr(sql));
@@ -242,7 +251,7 @@
     .dlr_flush = dlr_sdb_flush
 };
 
-static struct dlr_storage *dlr_init_sdb(Cfg* cfg)
+struct dlr_storage *dlr_init_sdb(Cfg* cfg)
 {
     CfgGroup *grp;
     List *grplist;
Index: gwlib/dbpool.h
===================================================================
RCS file: /home/cvs/gateway/gwlib/dbpool.h,v
retrieving revision 1.1
diff -u -a -u -b -r1.1 dbpool.h
--- gwlib/dbpool.h	11 Mar 2003 15:30:22 -0000	1.1
+++ gwlib/dbpool.h	2 Jul 2003 10:19:43 -0000
@@ -5,7 +5,7 @@
 #ifndef GWDBPOOL_H
 #define GWDBPOOL_H
 
-#if defined(HAVE_MYSQL) || defined(HAVE_SDB)
+#if defined(HAVE_MYSQL)
 #define HAVE_DBPOOL 1
 #endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.