Merge lp:~rainct/zeitgeist/table-lookup-get-value-query into lp:~zeitgeist/zeitgeist/bluebird

Proposed by Siegfried Gevatter
Status: Merged
Approved by: Michal Hruby
Approved revision: 401
Merged at revision: 401
Proposed branch: lp:~rainct/zeitgeist/table-lookup-get-value-query
Merge into: lp:~zeitgeist/zeitgeist/bluebird
Diff against target: 177 lines (+54/-28)
3 files modified
src/table-lookup.vala (+29/-14)
test/direct/Makefile.am (+7/-7)
test/direct/table-lookup-test.vala (+18/-7)
To merge this branch: bzr merge lp:~rainct/zeitgeist/table-lookup-get-value-query
Reviewer Review Type Date Requested Status
Michal Hruby (community) Approve
Review via email: mp+92843@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Michal Hruby (mhr3) wrote :

Looking good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/table-lookup.vala'
2--- src/table-lookup.vala 2012-02-10 11:28:05 +0000
3+++ src/table-lookup.vala 2012-02-13 20:22:18 +0000
4@@ -33,6 +33,7 @@
5 private HashTable<int, string> id_to_value;
6 private HashTable<string, int> value_to_id;
7 private Sqlite.Statement insertion_stmt;
8+ private Sqlite.Statement retrieval_stmt;
9
10 public TableLookup (Database database, string table_name)
11 {
12@@ -42,6 +43,7 @@
13 value_to_id = new HashTable<string, int>(str_hash, str_equal);
14
15 int rc;
16+ string sql;
17
18 rc = db.exec ("SELECT id, value FROM " + table,
19 (n_columns, values, column_names) =>
20@@ -56,10 +58,19 @@
21 rc, db.errmsg ());
22 }
23
24- string sql = "INSERT INTO " + table + " (value) VALUES (?)";
25+ sql = "INSERT INTO " + table + " (value) VALUES (?)";
26 rc = db.prepare_v2 (sql, -1, out insertion_stmt);
27 if (rc != Sqlite.OK)
28 {
29+ // FIXME: throw exception and propagate it up to
30+ // zeitgeist-daemon to abort with DB error?
31+ critical ("SQL error: %d, %s\n", rc, db.errmsg ());
32+ }
33+
34+ sql = "SELECT value FROM " + table + " WHERE id=?";
35+ rc = db.prepare_v2 (sql, -1, out retrieval_stmt);
36+ if (rc != Sqlite.OK)
37+ {
38 critical ("SQL error: %d, %s\n", rc, db.errmsg ());
39 }
40 }
41@@ -93,23 +104,27 @@
42 unowned string val = id_to_value.lookup (id);
43 if (val != null) return val;
44
45- // The above statement isn't exactly true. If this is a standalone
46- // reader in a separate process, the values won't be kept updated
47- // so we need to query the DB if we don't find it.
48+ // Unless this is a standalone reader in a separate process, in
49+ // which case the values won't be kept updated, so we need to
50+ // query the DB if we don't find it.
51 int rc;
52+ string? text = null;
53
54- rc = db.exec ("SELECT value FROM %s WHERE id=%d".printf (table, id),
55- (n_columns, values, column_names) =>
56- {
57- id_to_value.insert (id, values[0]);
58- value_to_id.insert (values[0], id);
59- return 0;
60- }, null);
61- if (rc != Sqlite.OK)
62- {
63- critical ("Can't get data from table %s: %d, %s\n", table,
64+ retrieval_stmt.reset ();
65+ retrieval_stmt.bind_int64 (1, id);
66+ if ((rc = retrieval_stmt.step()) == Sqlite.ROW)
67+ {
68+ text = retrieval_stmt.column_text (0);
69+ id_to_value.insert (id, text);
70+ value_to_id.insert (text, id);
71+ rc = retrieval_stmt.step ();
72+ }
73+ if (rc != Sqlite.DONE || text == null)
74+ {
75+ critical ("Error getting data from table: %d, %s\n",
76 rc, db.errmsg ());
77 }
78+
79 return id_to_value.lookup (id);
80 }
81
82
83=== modified file 'test/direct/Makefile.am'
84--- test/direct/Makefile.am 2012-02-05 14:52:13 +0000
85+++ test/direct/Makefile.am 2012-02-13 20:22:18 +0000
86@@ -10,10 +10,10 @@
87
88 TESTS = \
89 marshalling-test \
90+ mimetype-test \
91 query-operators-test \
92+ table-lookup-test \
93 where-clause-test \
94- table-lookup-test \
95- mimetype-test \
96 $(NULL)
97
98 SRC_FILES = \
99@@ -56,19 +56,19 @@
100
101 DISTCLEANFILES = \
102 marshalling-test \
103+ mimetype-test \
104 query-operators-test \
105+ table-lookup-test \
106 where-clause-test \
107- table-lookup-test \
108- mimetype-test \
109 $(NULL)
110
111 EXTRA_DIST = \
112+ assertions.vapi \
113 marshalling-test.vala \
114+ mimetype-test.vala \
115 query-operators-test.vala \
116+ table-lookup-test.vala \
117 where-clause-test.vala \
118- table-lookup-test.vala \
119- mimetype-test.vala \
120- assertions.vapi \
121 $(NULL)
122
123 VALA_V = $(VALA_V_$(V))
124
125=== modified file 'test/direct/table-lookup-test.vala'
126--- test/direct/table-lookup-test.vala 2012-02-05 14:52:13 +0000
127+++ test/direct/table-lookup-test.vala 2012-02-13 20:22:18 +0000
128@@ -38,6 +38,7 @@
129
130 Test.add_func ("/WhereClause/basic", basic_test);
131 Test.add_func ("/WhereClause/delete_hook", engine_test);
132+ Test.add_func ("/WhereClause/get_value_query", get_value_with_query_test);
133
134 return Test.run ();
135 }
136@@ -56,16 +57,28 @@
137 unowned Sqlite.Database db = database.database;
138 TableLookup table_lookup = new TableLookup (database, "actor");
139
140- assert_cmpint (table_lookup.get_id ("1st-actor"), OperatorType.EQUAL, 1);
141- assert_cmpint (table_lookup.get_id ("2nd-actor"), OperatorType.EQUAL, 2);
142- assert_cmpint (table_lookup.get_id ("1st-actor"), OperatorType.EQUAL, 1);
143+ int id = table_lookup.get_id ("1st-actor");
144+ assert_cmpint (table_lookup.get_id ("2nd-actor"), OperatorType.EQUAL, id+1);
145+ assert_cmpint (table_lookup.get_id ("1st-actor"), OperatorType.EQUAL, id);
146
147 int rc = db.exec ("DELETE FROM actor WHERE value='1st-actor'");
148 assert (rc == Sqlite.OK);
149
150 table_lookup.remove (1);
151- assert_cmpint (table_lookup.get_id ("2nd-actor"), OperatorType.EQUAL, 2);
152- assert_cmpint (table_lookup.get_id ("1st-actor"), OperatorType.EQUAL, 3);
153+ assert_cmpint (table_lookup.get_id ("2nd-actor"), OperatorType.EQUAL, id+1);
154+ assert_cmpint (table_lookup.get_id ("1st-actor"), OperatorType.EQUAL, id+2);
155+}
156+
157+public void get_value_with_query_test ()
158+{
159+ Database database = new Zeitgeist.SQLite.Database ();
160+ unowned Sqlite.Database db = database.database;
161+ TableLookup table_lookup = new TableLookup (database, "actor");
162+
163+ int rc = db.exec ("INSERT INTO actor (id, value) VALUES (100, 'new-actor')");
164+ assert (rc == Sqlite.OK);
165+
166+ assert_cmpstr (table_lookup.get_value (100), OperatorType.EQUAL, "new-actor");
167 }
168
169 public void engine_test ()
170@@ -82,8 +95,6 @@
171 int rc = db.exec ("DELETE FROM actor WHERE value='something'");
172 assert (rc == Sqlite.OK);
173
174- assert_cmpint (
175- table_lookup.get_id ("sqlite-reuses-the-id"), OperatorType.EQUAL, 1);
176 assert_cmpint (table_lookup.get_id ("something"), OperatorType.EQUAL, 2);
177 }
178

Subscribers

People subscribed via source and target branches