From f38e5a051b4d57e1cb9c1e503d2a8895e8532172 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 29 Jul 2015 00:14:03 +0000
Subject: Handled NULL properly in SQL requests and fixed the recording of
 updated items.

git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@559 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
---
 ChangeLog                     |  7 +++++++
 src/analysis/db/collection.c  | 27 ++++++++++++++++++++-------
 src/analysis/db/misc/rlestr.c |  2 +-
 src/arch/vmpa.c               |  6 ++----
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 02dc216..c92a45e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+15-07-29  Cyrille Bagard <nocbos@gmail.com>
+
+	* src/analysis/db/collection.c:
+	* src/analysis/db/misc/rlestr.c:
+	* src/arch/vmpa.c:
+	Handle NULL properly in SQL requests and fix the recording of updated items.
+
 15-07-28  Cyrille Bagard <nocbos@gmail.com>
 
 	* src/analysis/binary.c:
diff --git a/src/analysis/db/collection.c b/src/analysis/db/collection.c
index 91f9872..5e5adb7 100644
--- a/src/analysis/db/collection.c
+++ b/src/analysis/db/collection.c
@@ -997,8 +997,14 @@ static bool g_db_collection_store_item(const GDbCollection *collec, const GDbIte
                 index++;
                 break;
 
+            case SQLITE_NULL:
+                ret = sqlite3_bind_null(stmt, index);
+                index++;
+                break;
+
             default:
-                ret = SQLITE_OK;
+                assert(false);
+                ret = SQLITE_ERROR;
                 break;
 
         }
@@ -1090,7 +1096,10 @@ static bool g_db_collection_store_updated_item(const GDbCollection *collec, cons
 
         sql = stradd(sql, values[i].name);
 
-        sql = stradd(sql, " = ");
+        if (values[i].type == SQLITE_NULL)
+            sql = stradd(sql, " IS ");
+        else
+            sql = stradd(sql, " = ");
 
         if (values[i].type == SQLITE_RAW)
             sql = stradd(sql, values[i].cstring);
@@ -1101,12 +1110,10 @@ static bool g_db_collection_store_updated_item(const GDbCollection *collec, cons
 
     sql = stradd(sql, ";");
 
-    fprintf(stderr, "UPDATE SQL '%s'\n", sql);
-
 	ret = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
 	if (ret != SQLITE_OK)
     {
-		fprintf(stderr, "Can't prepare INSERT statment '%s' (ret=%d): %s\n", sql, ret, sqlite3_errmsg(db));
+		fprintf(stderr, "Can't prepare UPDATE statment '%s' (ret=%d): %s\n", sql, ret, sqlite3_errmsg(db));
         goto gdcsi_exit;
 	}
 
@@ -1117,7 +1124,7 @@ static bool g_db_collection_store_updated_item(const GDbCollection *collec, cons
     timestamp = find_bound_value(values, count, "timestamp");
     assert(timestamp->type == SQLITE_INT64);
 
-    ret = sqlite3_bind_int64(stmt, index, values[i].integer64);
+    ret = sqlite3_bind_int64(stmt, index, timestamp->integer64);
     index++;
 
     if (ret != SQLITE_OK)
@@ -1144,8 +1151,14 @@ static bool g_db_collection_store_updated_item(const GDbCollection *collec, cons
                 index++;
                 break;
 
+            case SQLITE_NULL:
+                ret = sqlite3_bind_null(stmt, index);
+                index++;
+                break;
+
             default:
-                ret = SQLITE_OK;
+                assert(false);
+                ret = SQLITE_ERROR;
                 break;
 
         }
diff --git a/src/analysis/db/misc/rlestr.c b/src/analysis/db/misc/rlestr.c
index 9986967..cbf9291 100644
--- a/src/analysis/db/misc/rlestr.c
+++ b/src/analysis/db/misc/rlestr.c
@@ -268,7 +268,7 @@ bool prepare_db_statement_for_rle_string(const rle_string *str, const char *name
     value = &(*values)[*count - 1];
 
     value->name = name;
-    value->type = SQLITE_TEXT;
+    value->type = (get_rle_string(str) != NULL ? SQLITE_TEXT : SQLITE_NULL);
     value->cstring = get_rle_string(str);
     value->delete = SQLITE_STATIC;
 
diff --git a/src/arch/vmpa.c b/src/arch/vmpa.c
index 9f2245c..5030562 100644
--- a/src/arch/vmpa.c
+++ b/src/arch/vmpa.c
@@ -639,8 +639,7 @@ bool prepare_vmpa_db_statement(const vmpa2t *addr, bound_value **values, size_t
     }
     else
     {
-        value->type = SQLITE_RAW;
-        value->cstring = "NULL";
+        value->type = SQLITE_NULL;
     }
 
     *values = (bound_value *)realloc(*values, ++(*count) * sizeof(bound_value));
@@ -655,8 +654,7 @@ bool prepare_vmpa_db_statement(const vmpa2t *addr, bound_value **values, size_t
     }
     else
     {
-        value->type = SQLITE_RAW;
-        value->cstring = "NULL";
+        value->type = SQLITE_NULL;
     }
 
     return true;
-- 
cgit v0.11.2-87-g4458