← Back to team overview

zeitgeist team mailing list archive

[Branch ~zeitgeist/zeitgeist/bluebird] Rev 401: Prepare TableLookup.get_value in the constructor, and add a test

 

------------------------------------------------------------
revno: 401
committer: Siegfried-Angel Gevatter Pujals <siegfried@xxxxxxxxxxxx>
branch nick: bluebird
timestamp: Mon 2012-02-13 21:14:41 +0100
message:
  Prepare TableLookup.get_value in the constructor, and add a test
  to verify that it's working.
modified:
  src/table-lookup.vala
  test/direct/table-lookup-test.vala


--
lp:zeitgeist
https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird

Your team Zeitgeist Framework Team is subscribed to branch lp:zeitgeist.
To unsubscribe from this branch go to https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird/+edit-subscription
=== modified file 'src/table-lookup.vala'
--- src/table-lookup.vala	2012-02-10 11:28:05 +0000
+++ src/table-lookup.vala	2012-02-13 20:14:41 +0000
@@ -33,6 +33,7 @@
         private HashTable<int, string> id_to_value;
         private HashTable<string, int> value_to_id;
         private Sqlite.Statement insertion_stmt;
+        private Sqlite.Statement retrieval_stmt;
 
         public TableLookup (Database database, string table_name)
         {
@@ -42,6 +43,7 @@
             value_to_id = new HashTable<string, int>(str_hash, str_equal);
 
             int rc;
+            string sql;
 
             rc = db.exec ("SELECT id, value FROM " + table,
                 (n_columns, values, column_names) =>
@@ -56,10 +58,19 @@
                     rc, db.errmsg ());
             }
 
-            string sql = "INSERT INTO " + table + " (value) VALUES (?)";
+            sql = "INSERT INTO " + table + " (value) VALUES (?)";
             rc = db.prepare_v2 (sql, -1, out insertion_stmt);
             if (rc != Sqlite.OK)
             {
+                // FIXME: throw exception and propagate it up to
+                //        zeitgeist-daemon to abort with DB error?
+                critical ("SQL error: %d, %s\n", rc, db.errmsg ());
+            }
+
+            sql = "SELECT value FROM " + table + " WHERE id=?";
+            rc = db.prepare_v2 (sql, -1, out retrieval_stmt);
+            if (rc != Sqlite.OK)
+            {
                 critical ("SQL error: %d, %s\n", rc, db.errmsg ());
             }
         }
@@ -93,23 +104,27 @@
             unowned string val = id_to_value.lookup (id);
             if (val != null) return val;
 
-            // The above statement isn't exactly true. If this is a standalone
-            // reader in a separate process, the values won't be kept updated
-            // so we need to query the DB if we don't find it.
+            // Unless this is a standalone reader in a separate process, in
+            // which case the values won't be kept updated, so we need to
+            // query the DB if we don't find it.
             int rc;
+            string? text = null;
 
-            rc = db.exec ("SELECT value FROM %s WHERE id=%d".printf (table, id),
-                (n_columns, values, column_names) =>
-                {
-                    id_to_value.insert (id, values[0]);
-                    value_to_id.insert (values[0], id);
-                    return 0;
-                }, null);
-            if (rc != Sqlite.OK)
-            {
-                critical ("Can't get data from table %s: %d, %s\n", table,
+            retrieval_stmt.reset ();
+            retrieval_stmt.bind_int64 (1, id);
+            if ((rc = retrieval_stmt.step()) == Sqlite.ROW)
+            {
+                text = retrieval_stmt.column_text (0);
+                id_to_value.insert (id, text);
+                value_to_id.insert (text, id);
+                rc = retrieval_stmt.step ();
+            }
+            if (rc != Sqlite.DONE || text == null)
+            {
+                critical ("Error getting data from table: %d, %s\n",
                     rc, db.errmsg ());
             }
+
             return id_to_value.lookup (id);
         }
 

=== modified file 'test/direct/table-lookup-test.vala'
--- test/direct/table-lookup-test.vala	2012-02-13 19:43:15 +0000
+++ test/direct/table-lookup-test.vala	2012-02-13 20:14:41 +0000
@@ -38,6 +38,7 @@
 
     Test.add_func ("/WhereClause/basic", basic_test);
     Test.add_func ("/WhereClause/delete_hook", engine_test);
+    Test.add_func ("/WhereClause/get_value_query", get_value_with_query_test);
 
     return Test.run ();
 }
@@ -68,6 +69,18 @@
     assert_cmpint (table_lookup.get_id ("1st-actor"), OperatorType.EQUAL, id+2);
 }
 
+public void get_value_with_query_test ()
+{
+    Database database = new Zeitgeist.SQLite.Database ();
+    unowned Sqlite.Database db = database.database;
+    TableLookup table_lookup = new TableLookup (database, "actor");
+
+    int rc = db.exec ("INSERT INTO actor (id, value) VALUES (100, 'new-actor')");
+    assert (rc == Sqlite.OK);
+
+    assert_cmpstr (table_lookup.get_value (100), OperatorType.EQUAL, "new-actor");
+}
+
 public void engine_test ()
 {
     PublicEngine engine = new PublicEngine ();