From 6680ca574d1b82821c580a44c22337151227e4aa Mon Sep 17 00:00:00 2001
From: Eugene Livis <elivis@basistech.com>
Date: Mon, 30 Mar 2015 12:05:30 -0400
Subject: [PATCH] Fixed build error for non-PostgreSQL builds

---
 tsk/auto/case_db.cpp       | 17 ++++++++++++-----
 tsk/auto/db_postgresql.cpp | 11 +++++++----
 tsk/auto/tsk_case_db.h     |  1 -
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/tsk/auto/case_db.cpp b/tsk/auto/case_db.cpp
index 57f1491ef..94a91d7c4 100644
--- a/tsk/auto/case_db.cpp
+++ b/tsk/auto/case_db.cpp
@@ -54,15 +54,19 @@ TskCaseDb::~TskCaseDb()
 TskCaseDb *
 TskCaseDb::newDb(const TSK_TCHAR * const path)
 {
-
+    // ELTODO: optimize this
 #ifdef HAVE_POSTGRESQL
 #ifdef TSK_WIN32
 
     // ELTODO: check here which database to initialize
     //TskDb *db = new TskDbSqlite(path, true);
 
-    TskDb *db = new TskDbPostgreSQL(path, true);
+    return TskDbPostgreSQL(path, true);
+#else
+    TskDb *db = new TskDbSqlite(path, true);
 #endif // TSK_WIN32
+#else
+    TskDb *db = new TskDbSqlite(path, true);
 #endif // HAVE_POSTGRESQL
 
     // Check if the database already exsists
@@ -92,15 +96,19 @@ TskCaseDb::newDb(const TSK_TCHAR * const path)
 TskCaseDb *
 TskCaseDb::openDb(const TSK_TCHAR * path)
 {
-
+    // ELTODO: optimize this
 #ifdef HAVE_POSTGRESQL
 #ifdef TSK_WIN32
 
     // ELTODO: check here which database to initialize
     //TskDb *db = new TskDbSqlite(path, true);
 
-    TskDb *db = new TskDbPostgreSQL(path, true);
+    return TskDbPostgreSQL(path, true);
+#else
+    TskDb *db = new TskDbSqlite(path, true);
 #endif // TSK_WIN32
+#else
+    TskDb *db = new TskDbSqlite(path, true);
 #endif // HAVE_POSTGRESQL
 
     // Confirm that database already exsists
@@ -121,7 +129,6 @@ TskCaseDb::openDb(const TSK_TCHAR * path)
     return new TskCaseDb(db);
 }
 
-
 /**
  * Prepares the process to add an image to the database. This method
  * allows the caller to specify options to be used during the ingest.
diff --git a/tsk/auto/db_postgresql.cpp b/tsk/auto/db_postgresql.cpp
index b7ae6dfcf..2d8b654ff 100755
--- a/tsk/auto/db_postgresql.cpp
+++ b/tsk/auto/db_postgresql.cpp
@@ -133,7 +133,7 @@ int TskDbPostgreSQL::open(bool createDbFlag)
     }
 
     // ELTODO: delete this:
-    test();
+    //test();
 
     return 0;
 }
@@ -290,7 +290,7 @@ int TskDbPostgreSQL::initialize() {
     // http://www.postgresql.org/docs/current/interactive/datatype-numeric.html
 
     if (attempt_exec
-        ("CREATE TABLE tsk_objects (obj_id INTEGER PRIMARY KEY, par_obj_id INTEGER, type INTEGER NOT NULL);",
+        ("CREATE TABLE tsk_objects (obj_id SERIAL PRIMARY KEY, par_obj_id INTEGER, type INTEGER NOT NULL);",
         "Error creating tsk_objects table: %s\n")
         ||
         attempt_exec
@@ -359,7 +359,8 @@ int TskDbPostgreSQL::initialize() {
         "FOREIGN KEY(artifact_id) REFERENCES blackboard_artifacts(artifact_id), FOREIGN KEY(artifact_type_id) REFERENCES blackboard_artifact_types(artifact_type_id), FOREIGN KEY(attribute_type_id) REFERENCES blackboard_attribute_types(attribute_type_id))",
         "Error creating blackboard_attribute table: %s\n")
         ||
-        /* In PostgreSQL "desc" indicates "descending order" so I had to rename "desc TEXT" to "descr TEXT" */
+        /* In PostgreSQL "desc" indicates "descending order" so I had to rename "desc TEXT" to "descr TEXT" 
+        ELTODO: make sure all insert queries have "descr". Should I also make this change for SQLite?*/
         attempt_exec
         ("CREATE TABLE tsk_vs_parts (obj_id INTEGER PRIMARY KEY, addr INTEGER NOT NULL, start INTEGER NOT NULL, length INTEGER NOT NULL, descr TEXT, flags INTEGER NOT NULL, FOREIGN KEY(obj_id) REFERENCES tsk_objects(obj_id));",
         "Error creating tsk_vol_info table: %s\n")
@@ -419,7 +420,9 @@ uint8_t
 {
     char stmt[1024];
 
-    snprintf(stmt, 1024, "INSERT INTO tsk_objects (obj_id, par_obj_id, type) VALUES (NULL, %d, %d)", 
+//    snprintf(stmt, 1024, "INSERT INTO tsk_objects (obj_id, par_obj_id, type) VALUES (NULL, %d, %d)", 
+//        parObjId, type);
+    snprintf(stmt, 1024, "INSERT INTO tsk_objects (par_obj_id, type) VALUES (%d, %d)", 
         parObjId, type);
 
     if (attempt_exec(stmt, "TskDbSqlite::addObj: Error adding object to row: %s (result code %d)\n")) {
diff --git a/tsk/auto/tsk_case_db.h b/tsk/auto/tsk_case_db.h
index 512b2fea4..9315ad422 100644
--- a/tsk/auto/tsk_case_db.h
+++ b/tsk/auto/tsk_case_db.h
@@ -186,7 +186,6 @@ class TskCaseDb {
     // prevent copying until we add proper logic to handle it
     TskCaseDb(const TskCaseDb&);
     TskCaseDb & operator=(const TskCaseDb&);
-
     TskCaseDb(TskDb * a_db);
     TskDb *m_db;
     TSK_HDB_INFO * m_NSRLDb;
-- 
GitLab