[3/3]: Patch smsd/sqlite: Several bug fixes / optimizations

Pedro Aguilar <[email protected]>
Newsgroups gmane.linux.drivers.gnokii
Message-ID <CAEWWn29eGD397LiA04eCU1bMZkd2sX4apWf98+n2d1z50HKBmw@mail.gmail.com>
Hi,

This patch fixes a corrupted date/time insert in table outbox.
Without this patch the inserted date had this format:
13-03-23 10:26:09
instead of
2013-03-23 10:26:09

Given that the 'insertdate' column in the same table outbox and the
date/time columns in the inbox tables use the format YYYY-MM-DD hh:mm:ss, I
assumed that the format YY-MM-DD hh:mm:ss was not valid.

Regards,
-- 
Pedro Aguilar
http://www.paguilar.org/blog

_______________________________________________
gnokii-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/gnokii-users
03_sqlite_corrupted_insert_date.patch (application/octet-stream, 2.7 KB)
--- a/smsd/sqlite.c	2013-03-21 12:03:43.109251034 +0100
+++ b/smsd/sqlite.c	2013-03-21 12:10:41.165260954 +0100
@@ -131,10 +131,10 @@
 
 GNOKII_API gint DB_Look(const gchar * const phone)
 {
-    GString *buf, *phnStr, *timebuf;
+    GString *buf, *phnStr;
+	gchar timebuf[32];
     gint ret1, numError, error;
     time_t rawtime;
-    struct tm * timeinfo;
     sqlite3_stmt * stmt;
     gint empty = 1;
 
@@ -145,12 +145,8 @@
         g_string_printf(phnStr, "AND phone = '%s'", phone);
     }
 
-    time(&rawtime);
-    timeinfo = localtime(&rawtime);
-
-    timebuf = g_string_sized_new(25);
-    g_string_printf(timebuf, "'%02d:%02d:%02d'",
-            timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
+    rawtime = time(NULL);
+	strftime(timebuf, 32, "%H:%M:%S", localtime(&rawtime));
 
     sqlite3_exec(ppDbOutbox, "BEGIN TRANSACTION;", NULL, NULL, NULL);
 
@@ -158,9 +154,9 @@
     buf = g_string_sized_new(256);
     g_string_printf(buf, "SELECT id, number, text, dreport FROM outbox \
                         WHERE processed=0 \
-                        AND %s >= not_before \
-                        AND %s <= not_after \
-                        %s", timebuf->str, timebuf->str, phnStr->str);
+                        AND '%s' >= not_before \
+                        AND '%s' <= not_after \
+                        %s", timebuf, timebuf, phnStr->str);
 
     g_string_free(phnStr, TRUE);
 
@@ -171,11 +167,7 @@
         return (SMSD_NOK);
     }
 
-    g_string_printf(timebuf, "'%02d-%02d-%02d %02d:%02d:%02d'",
-            timeinfo->tm_year, timeinfo->tm_mon,
-            timeinfo->tm_mday, timeinfo->tm_hour,
-            timeinfo->tm_min, timeinfo->tm_sec
-            );
+	strftime(timebuf, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime));
 
     ret1 = sqlite3_step(stmt);
     while (ret1 == SQLITE_ROW) {
@@ -214,9 +206,9 @@
 
         /* mark sended */
         g_string_printf(buf, "UPDATE outbox SET processed=1, error='%d', \
-                        processed_date=%s \
+                        processed_date='%s' \
                         WHERE id=%d",
-                gerror, timebuf->str, sqlite3_column_int(stmt, 0)
+                gerror, timebuf, sqlite3_column_int(stmt, 0)
                 );
 
         sqlite3_exec(ppDbOutbox, buf->str, NULL, NULL, NULL);
@@ -230,14 +222,12 @@
         sqlite3_finalize(stmt);
         sqlite3_exec(ppDbOutbox, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
 
-        g_string_free(timebuf, TRUE);
         g_string_free(buf, TRUE);
         return (SMSD_NOK);
     }
     sqlite3_finalize(stmt);
     sqlite3_exec(ppDbOutbox, "COMMIT;", NULL, NULL, NULL);
 
-    g_string_free(timebuf, TRUE);
     g_string_free(buf, TRUE);
     
     if (empty)
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.