Merge lp:~midori/midori/mackerel2 into lp:midori

Proposed by Cris Dywan
Status: Merged
Approved by: Paweł Forysiuk
Approved revision: 6519
Merged at revision: 6603
Proposed branch: lp:~midori/midori/mackerel2
Merge into: lp:midori
Diff against target: 482 lines (+164/-160)
2 files modified
extensions/tabby.vala (+138/-157)
midori/midori-database.vala (+26/-3)
To merge this branch: bzr merge lp:~midori/midori/mackerel2
Reviewer Review Type Date Requested Status
Paweł Forysiuk Approve
André Stösel Pending
Review via email: mp+200581@code.launchpad.net

Commit message

Port Tabby to DatabaseStatement API

To post a comment you must log in.
Revision history for this message
Paweł Forysiuk (tuxator) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'extensions/tabby.vala'
2--- extensions/tabby.vala 2014-01-12 18:15:53 +0000
3+++ extensions/tabby.vala 2014-03-16 20:28:56 +0000
4@@ -350,29 +350,25 @@
5 namespace Local {
6 private class Session : Base.Session {
7 public int64 id { get; private set; }
8- private unowned Sqlite.Database db;
9+ private Midori.Database database;
10
11 public override void add_item (Katze.Item item) {
12 GLib.DateTime time = new DateTime.now_local ();
13- string? sorting = item.get_meta_string ("sorting");
14+ string? sorting = item.get_meta_string ("sorting") ?? "1";
15 string sqlcmd = "INSERT INTO `tabs` (`crdate`, `tstamp`, `session_id`, `uri`, `title`, `sorting`) VALUES (:tstamp, :tstamp, :session_id, :uri, :title, :sorting);";
16- Sqlite.Statement stmt;
17- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
18- critical (_("Failed to update database: %s"), db.errmsg);
19- stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());
20- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
21- stmt.bind_text (stmt.bind_parameter_index (":uri"), item.uri);
22- stmt.bind_text (stmt.bind_parameter_index (":title"), item.name);
23- if (sorting == null)
24- stmt.bind_double (stmt.bind_parameter_index (":sorting"), double.parse ("1"));
25- else
26- stmt.bind_double (stmt.bind_parameter_index (":sorting"), double.parse (sorting));
27
28- if (stmt.step () != Sqlite.DONE)
29- critical (_("Failed to update database: %s"), db.errmsg);
30- else {
31- int64 tab_id = this.db.last_insert_rowid ();
32+ try {
33+ var statement = database.prepare (sqlcmd,
34+ ":tstamp", typeof (int64), time.to_unix (),
35+ ":session_id", typeof (int64), this.id,
36+ ":uri", typeof (string), item.uri,
37+ ":title", typeof (string), item.name,
38+ ":sorting", typeof (double), double.parse (sorting));
39+ statement.exec ();
40+ int64 tab_id = statement.row_id ();
41 item.set_meta_integer ("tabby-id", tab_id);
42+ } catch (Error error) {
43+ critical (_("Failed to update database: %s"), error.message);
44 }
45 }
46
47@@ -380,28 +376,28 @@
48 unowned Katze.Item item = view.get_proxy_item ();
49 int64 tab_id = item.get_meta_integer ("tabby-id");
50 string sqlcmd = "UPDATE `tabs` SET uri = :uri WHERE session_id = :session_id AND id = :tab_id;";
51- Sqlite.Statement stmt;
52- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
53- critical (_("Failed to update database: %s"), db.errmsg ());
54- stmt.bind_text (stmt.bind_parameter_index (":uri"), uri);
55- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
56- stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);
57- if (stmt.step () != Sqlite.DONE)
58- critical (_("Failed to update database: %s"), db.errmsg ());
59+ try {
60+ database.prepare (sqlcmd,
61+ ":uri", typeof (string), uri,
62+ ":session_id", typeof (int64), this.id,
63+ ":tab_id", typeof (int64), tab_id).exec ();
64+ } catch (Error error) {
65+ critical (_("Failed to update database: %s"), error.message);
66+ }
67 }
68
69 protected override void data_changed (Midori.View view) {
70 unowned Katze.Item item = view.get_proxy_item ();
71 int64 tab_id = item.get_meta_integer ("tabby-id");
72 string sqlcmd = "UPDATE `tabs` SET title = :title WHERE session_id = :session_id AND id = :tab_id;";
73- Sqlite.Statement stmt;
74- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
75- critical (_("Failed to update database: %s"), db.errmsg ());
76- stmt.bind_text (stmt.bind_parameter_index (":title"), view.get_display_title ());
77- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
78- stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);
79- if (stmt.step () != Sqlite.DONE)
80- critical (_("Failed to update database: %s"), db.errmsg ());
81+ try {
82+ database.prepare (sqlcmd,
83+ ":title", typeof (string), view.get_display_title (),
84+ ":session_id", typeof (int64), this.id,
85+ ":tab_id", typeof (int64), tab_id).exec ();
86+ } catch (Error error) {
87+ critical (_("Failed to update database: %s"), error.message);
88+ }
89 }
90
91 protected override void tab_added (Midori.Browser browser, Midori.View view) {
92@@ -419,13 +415,13 @@
93 int64 tab_id = item.get_meta_integer ("tabby-id");
94 /* FixMe: mark as deleted */
95 string sqlcmd = "DELETE FROM `tabs` WHERE session_id = :session_id AND id = :tab_id;";
96- Sqlite.Statement stmt;
97- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
98- critical (_("Failed to update database: %s"), db.errmsg ());
99- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
100- stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);
101- if (stmt.step () != Sqlite.DONE)
102- critical (_("Failed to update database: %s"), db.errmsg ());
103+ try {
104+ database.prepare (sqlcmd,
105+ ":session_id", typeof (int64), this.id,
106+ ":tab_id", typeof (int64), tab_id).exec ();
107+ } catch (Error error) {
108+ critical (_("Failed to update database: %s"), error.message);
109+ }
110 }
111
112 protected override void tab_switched (Midori.View? old_view, Midori.View? new_view) {
113@@ -433,14 +429,14 @@
114 unowned Katze.Item item = new_view.get_proxy_item ();
115 int64 tab_id = item.get_meta_integer ("tabby-id");
116 string sqlcmd = "UPDATE `tabs` SET tstamp = :tstamp WHERE session_id = :session_id AND id = :tab_id;";
117- Sqlite.Statement stmt;
118- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
119- critical (_("Failed to update database: %s"), db.errmsg ());
120- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
121- stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);
122- stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());
123- if (stmt.step () != Sqlite.DONE)
124- critical (_("Failed to update database: %s"), db.errmsg ());
125+ try {
126+ database.prepare (sqlcmd,
127+ ":session_id", typeof (int64), this.id,
128+ ":tab_id", typeof (int64), tab_id,
129+ ":tstamp", typeof (int64), time.to_unix ()).exec ();
130+ } catch (Error error) {
131+ critical (_("Failed to update database: %s"), error.message);
132+ }
133 }
134
135 protected override void tab_reordered (Gtk.Widget tab, uint pos) {
136@@ -450,36 +446,29 @@
137 unowned Katze.Item item = view.get_proxy_item ();
138 int64 tab_id = item.get_meta_integer ("tabby-id");
139 string sqlcmd = "UPDATE `tabs` SET sorting = :sorting WHERE session_id = :session_id AND id = :tab_id;";
140- Sqlite.Statement stmt;
141- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
142- critical (_("Failed to update database: %s"), db.errmsg ());
143- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
144- stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);
145- stmt.bind_double (stmt.bind_parameter_index (":sorting"), sorting);
146-
147- if (stmt.step () != Sqlite.DONE)
148- critical (_("Failed to update database: %s"), db.errmsg ());
149+ try {
150+ database.prepare (sqlcmd,
151+ ":session_id", typeof (int64), this.id,
152+ ":tab_id", typeof (int64), tab_id,
153+ ":sorting", typeof (double), sorting).exec ();
154+ } catch (Error error) {
155+ critical (_("Failed to update database: %s"), error.message);
156+ }
157
158 item.set_meta_string ("sorting", sorting.to_string ());
159 }
160
161 public override void remove() {
162- string sqlcmd = "DELETE FROM `tabs` WHERE session_id = :session_id;";
163- Sqlite.Statement stmt;
164- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
165- critical (_("Failed to update database: %s"), db.errmsg ());
166- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
167-
168- if (stmt.step () != Sqlite.DONE)
169- critical (_("Failed to update database: %s"), db.errmsg ());
170-
171- sqlcmd = "DELETE FROM `sessions` WHERE id = :session_id;";
172- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
173- critical (_("Failed to update database: %s"), db.errmsg ());
174- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
175-
176- if (stmt.step () != Sqlite.DONE)
177- critical (_("Failed to update database: %s"), db.errmsg ());
178+ string sqlcmd = """
179+ DELETE FROM `tabs` WHERE session_id = :session_id;
180+ DELETE FROM `sessions` WHERE id = :session_id;
181+ """;
182+ try {
183+ database.prepare (sqlcmd,
184+ ":session_id", typeof (int64), this.id). exec ();
185+ } catch (Error error) {
186+ critical (_("Failed to update database: %s"), error.message);
187+ }
188 }
189
190 public override void close() {
191@@ -501,104 +490,98 @@
192
193 GLib.DateTime time = new DateTime.now_local ();
194 string sqlcmd = "UPDATE `sessions` SET closed = 1, tstamp = :tstamp WHERE id = :session_id;";
195- Sqlite.Statement stmt;
196- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
197- critical (_("Failed to update database: %s"), db.errmsg ());
198-
199- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
200- stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());
201- if (stmt.step () != Sqlite.DONE)
202- critical (_("Failed to update database: %s"), db.errmsg ());
203+ try {
204+ database.prepare (sqlcmd,
205+ ":session_id", typeof (int64), this.id,
206+ ":tstamp", typeof (int64), time.to_unix ()).exec ();
207+ } catch (Error error) {
208+ critical (_("Failed to update database: %s"), error.message);
209+ }
210 }
211
212 public override Katze.Array get_tabs() {
213 Katze.Array tabs = new Katze.Array (typeof (Katze.Item));
214
215 string sqlcmd = "SELECT id, uri, title, sorting FROM tabs WHERE session_id = :session_id ORDER BY tstamp DESC";
216- Sqlite.Statement stmt;
217- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
218- critical (_("Failed to select from database: %s"), db.errmsg ());
219- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
220- int result = stmt.step ();
221- if (!(result == Sqlite.DONE || result == Sqlite.ROW)) {
222- critical (_("Failed to select from database: %s"), db.errmsg ());
223- return tabs;
224+ try {
225+ var statement = database.prepare (sqlcmd,
226+ ":session_id", typeof (int64), this.id);
227+ while (statement.step ()) {
228+ Katze.Item item = new Katze.Item ();
229+ int64 id = statement.get_int64 ("id");
230+ string uri = statement.get_string ("uri");
231+ string title = statement.get_string ("title");
232+ double sorting = statement.get_double ("sorting");
233+ item.uri = uri;
234+ item.name = title;
235+ item.set_meta_integer ("tabby-id", id);
236+ item.set_meta_string ("sorting", sorting.to_string ());
237+ tabs.add_item (item);
238+ }
239+ } catch (Error error) {
240+ critical (_("Failed to select from database: %s"), error.message);
241 }
242-
243- while (result == Sqlite.ROW) {
244- Katze.Item item = new Katze.Item ();
245- int64 id = stmt.column_int64 (0);
246- string uri = stmt.column_text (1);
247- string title = stmt.column_text (2);
248- item.uri = uri;
249- item.name = title;
250- item.set_meta_integer ("tabby-id", id);
251- item.set_meta_string ("sorting", stmt.column_double (3).to_string ());
252- tabs.add_item (item);
253- result = stmt.step ();
254- }
255-
256- return tabs;
257+ return tabs;
258 }
259
260 public override double? get_max_sorting () {
261 string sqlcmd = "SELECT MAX(sorting) FROM tabs WHERE session_id = :session_id";
262- Sqlite.Statement stmt;
263- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
264- critical (_("Failed to select from database: %s"), db.errmsg ());
265- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
266- int result = stmt.step ();
267- if (!(result == Sqlite.DONE || result == Sqlite.ROW)) {
268- critical (_("Failed to select from database: %s"), db.errmsg ());
269- } else if (result == Sqlite.ROW) {
270+ try {
271+ var statement = database.prepare (sqlcmd,
272+ ":session_id", typeof (int64), this.id);
273+ statement.step ();
274 double? sorting;
275- string? sorting_string = stmt.column_double (0).to_string ();
276- if (sorting_string != null) { /* we have to use a seperate if condition to avoid a `possibly unassigned local variable` error */
277+ string? sorting_string = statement.get_int64 ("MAX(sorting)").to_string ();
278+ if (sorting_string != null) {
279+ /* we have to use a seperate if condition to avoid
280+ a `possibly unassigned local variable` error */
281 if (double.try_parse (sorting_string, out sorting)) {
282 return sorting;
283 }
284 }
285- }
286+ } catch (Error error) {
287+ critical (_("Failed to select from database: %s"), error.message);
288+ }
289
290 return double.parse ("0");
291 }
292
293- internal Session (Sqlite.Database db) {
294- this.db = db;
295+ internal Session (Midori.Database database) {
296+ this.database = database;
297
298 GLib.DateTime time = new DateTime.now_local ();
299
300 string sqlcmd = "INSERT INTO `sessions` (`tstamp`) VALUES (:tstamp);";
301- Sqlite.Statement stmt;
302- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
303- critical (_("Failed to update database: %s"), db.errmsg);
304- stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());
305- if (stmt.step () != Sqlite.DONE)
306- critical (_("Failed to update database: %s"), db.errmsg);
307- else
308- this.id = this.db.last_insert_rowid ();
309+
310+ try {
311+ var statement = database.prepare (sqlcmd,
312+ ":tstamp", typeof (int64), time.to_unix ());
313+ statement.exec ();
314+ this.id = statement.row_id ();
315+ } catch (Error error) {
316+ critical (_("Failed to update database: %s"), error.message);
317+ }
318 }
319
320- internal Session.with_id (Sqlite.Database db, int64 id) {
321- this.db = db;
322+ internal Session.with_id (Midori.Database database, int64 id) {
323+ this.database = database;
324 this.id = id;
325
326 GLib.DateTime time = new DateTime.now_local ();
327 string sqlcmd = "UPDATE `sessions` SET closed = 0, tstamp = :tstamp WHERE id = :session_id;";
328- Sqlite.Statement stmt;
329- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
330- critical (_("Failed to update database: %s"), db.errmsg);
331
332- stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
333- stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());
334- if (stmt.step () != Sqlite.DONE)
335- critical (_("Failed to update database: %s"), db.errmsg);
336+ try {
337+ database.prepare (sqlcmd,
338+ ":session_id", typeof (int64), this.id,
339+ ":tstamp", typeof (int64), time.to_unix ()).exec ();
340+ } catch (Error error) {
341+ critical (_("Failed to update database: %s"), error.message);
342+ }
343 }
344 }
345
346 private class Storage : Base.Storage {
347 private Midori.Database database;
348- private unowned Sqlite.Database db;
349
350 public override Katze.Array get_sessions () {
351 Katze.Array sessions = new Katze.Array (typeof (Session));
352@@ -609,39 +592,38 @@
353 SELECT * FROM (SELECT id, closed FROM sessions WHERE closed = 1 ORDER BY tstamp DESC LIMIT 1)
354 ORDER BY closed;
355 """;
356- Sqlite.Statement stmt;
357- if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
358- critical (_("Failed to select from database: %s"), db.errmsg);
359- int result = stmt.step ();
360- if (!(result == Sqlite.DONE || result == Sqlite.ROW)) {
361- critical (_("Failed to select from database: %s"), db.errmsg);
362- return sessions;
363- }
364-
365- while (result == Sqlite.ROW) {
366- int64 id = stmt.column_int64 (0);
367- int64 closed = stmt.column_int64 (1);
368- if (closed == 0 || sessions.is_empty ()) {
369- sessions.add_item (new Session.with_id (this.db, id));
370+ try {
371+ var statement = database.prepare (sqlcmd);
372+ while (statement.step ()) {
373+ int64 id = statement.get_int64 ("id");
374+ int64 closed = statement.get_int64 ("closed");
375+ if (closed == 0 || sessions.is_empty ()) {
376+ sessions.add_item (new Session.with_id (this.database, id));
377+ }
378 }
379- result = stmt.step ();
380- }
381+ } catch (Error error) {
382+ critical (_("Failed to select from database: %s"), error.message);
383+ }
384
385 if (sessions.is_empty ()) {
386- sessions.add_item (new Session (this.db));
387+ sessions.add_item (new Session (this.database));
388 }
389
390 return sessions;
391 }
392
393 public override void import_session (Katze.Array tabs) {
394- this.db.exec ("BEGIN;");
395- base.import_session(tabs);
396- this.db.exec("COMMIT;");
397+ try {
398+ database.transaction (()=>{
399+ base.import_session(tabs); return true;
400+ });
401+ } catch (Error error) {
402+ critical (_("Failed to select from database: %s"), error.message);
403+ }
404 }
405
406 public override Base.Session get_new_session () {
407- return new Session (this.db) as Base.Session;
408+ return new Session (this.database) as Base.Session;
409 }
410
411 internal Storage (Midori.App app) {
412@@ -652,7 +634,6 @@
413 } catch (Midori.DatabaseError schema_error) {
414 error (schema_error.message);
415 }
416- db = database.db;
417
418 if (database.first_use) {
419 string config_file = Midori.Paths.get_config_filename_for_reading ("session.xbel");
420
421=== modified file 'midori/midori-database.vala'
422--- midori/midori-database.vala 2014-01-29 21:52:10 +0000
423+++ midori/midori-database.vala 2014-03-16 20:28:56 +0000
424@@ -23,6 +23,11 @@
425 }
426
427 /*
428+ * Since: 0.5.8
429+ */
430+ public delegate bool DatabaseCallback () throws DatabaseError;
431+
432+ /*
433 * Since: 0.5.7
434 */
435 public class DatabaseStatement : GLib.Object, GLib.Initable {
436@@ -30,6 +35,7 @@
437 protected Sqlite.Statement _stmt = null;
438 public Database? database { get; set construct; }
439 public string? query { get; set construct; }
440+ private int64 last_row_id = -1;
441
442 public DatabaseStatement (Database database, string query) throws DatabaseError {
443 Object (database: database, query: query);
444@@ -79,9 +85,21 @@
445 int result = stmt.step ();
446 if (result != Sqlite.DONE && result != Sqlite.ROW)
447 throw new DatabaseError.EXECUTE (database.db.errmsg ());
448+ last_row_id = database.db.last_insert_rowid ();
449 return result == Sqlite.ROW;
450 }
451
452+ /*
453+ * Returns the id of the last inserted row.
454+ * It is an error to ask for an id without having inserted a row.
455+ * Since: 0.5.8
456+ */
457+ public int64 row_id () throws DatabaseError {
458+ if (last_row_id == -1)
459+ throw new DatabaseError.EXECUTE ("No row id");
460+ return last_row_id;
461+ }
462+
463 private int column_index (string name) throws DatabaseError {
464 for (int i = 0; i < stmt.column_count (); i++) {
465 if (name == stmt.column_name (i))
466@@ -219,9 +237,14 @@
467 } catch (Error error) {
468 throw new DatabaseError.FILENAME ("Failed to open schema: %s".printf (schema_filename));
469 }
470- schema = "BEGIN TRANSACTION; %s; COMMIT;".printf (schema);
471- if (db.exec (schema) != Sqlite.OK)
472- throw new DatabaseError.EXECUTE ("Failed to execute schema: %s".printf (schema));
473+ transaction (()=> { return exec (schema); });
474+ return true;
475+ }
476+
477+ public bool transaction (DatabaseCallback callback) throws DatabaseError {
478+ exec ("BEGIN TRANSACTION;");
479+ callback ();
480+ exec ("COMMIT;");
481 return true;
482 }
483

Subscribers

People subscribed via source and target branches

to all changes: