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 |
Related bugs: |
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
Description of the change
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 | 350 | namespace Local { | 350 | namespace Local { |
6 | 351 | private class Session : Base.Session { | 351 | private class Session : Base.Session { |
7 | 352 | public int64 id { get; private set; } | 352 | public int64 id { get; private set; } |
9 | 353 | private unowned Sqlite.Database db; | 353 | private Midori.Database database; |
10 | 354 | 354 | ||
11 | 355 | public override void add_item (Katze.Item item) { | 355 | public override void add_item (Katze.Item item) { |
12 | 356 | GLib.DateTime time = new DateTime.now_local (); | 356 | GLib.DateTime time = new DateTime.now_local (); |
14 | 357 | string? sorting = item.get_meta_string ("sorting"); | 357 | string? sorting = item.get_meta_string ("sorting") ?? "1"; |
15 | 358 | string sqlcmd = "INSERT INTO `tabs` (`crdate`, `tstamp`, `session_id`, `uri`, `title`, `sorting`) VALUES (:tstamp, :tstamp, :session_id, :uri, :title, :sorting);"; | 358 | string sqlcmd = "INSERT INTO `tabs` (`crdate`, `tstamp`, `session_id`, `uri`, `title`, `sorting`) VALUES (:tstamp, :tstamp, :session_id, :uri, :title, :sorting);"; |
16 | 359 | Sqlite.Statement stmt; | ||
17 | 360 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | ||
18 | 361 | critical (_("Failed to update database: %s"), db.errmsg); | ||
19 | 362 | stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ()); | ||
20 | 363 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | ||
21 | 364 | stmt.bind_text (stmt.bind_parameter_index (":uri"), item.uri); | ||
22 | 365 | stmt.bind_text (stmt.bind_parameter_index (":title"), item.name); | ||
23 | 366 | if (sorting == null) | ||
24 | 367 | stmt.bind_double (stmt.bind_parameter_index (":sorting"), double.parse ("1")); | ||
25 | 368 | else | ||
26 | 369 | stmt.bind_double (stmt.bind_parameter_index (":sorting"), double.parse (sorting)); | ||
27 | 370 | 359 | ||
32 | 371 | if (stmt.step () != Sqlite.DONE) | 360 | try { |
33 | 372 | critical (_("Failed to update database: %s"), db.errmsg); | 361 | var statement = database.prepare (sqlcmd, |
34 | 373 | else { | 362 | ":tstamp", typeof (int64), time.to_unix (), |
35 | 374 | int64 tab_id = this.db.last_insert_rowid (); | 363 | ":session_id", typeof (int64), this.id, |
36 | 364 | ":uri", typeof (string), item.uri, | ||
37 | 365 | ":title", typeof (string), item.name, | ||
38 | 366 | ":sorting", typeof (double), double.parse (sorting)); | ||
39 | 367 | statement.exec (); | ||
40 | 368 | int64 tab_id = statement.row_id (); | ||
41 | 375 | item.set_meta_integer ("tabby-id", tab_id); | 369 | item.set_meta_integer ("tabby-id", tab_id); |
42 | 370 | } catch (Error error) { | ||
43 | 371 | critical (_("Failed to update database: %s"), error.message); | ||
44 | 376 | } | 372 | } |
45 | 377 | } | 373 | } |
46 | 378 | 374 | ||
47 | @@ -380,28 +376,28 @@ | |||
48 | 380 | unowned Katze.Item item = view.get_proxy_item (); | 376 | unowned Katze.Item item = view.get_proxy_item (); |
49 | 381 | int64 tab_id = item.get_meta_integer ("tabby-id"); | 377 | int64 tab_id = item.get_meta_integer ("tabby-id"); |
50 | 382 | string sqlcmd = "UPDATE `tabs` SET uri = :uri WHERE session_id = :session_id AND id = :tab_id;"; | 378 | string sqlcmd = "UPDATE `tabs` SET uri = :uri WHERE session_id = :session_id AND id = :tab_id;"; |
59 | 383 | Sqlite.Statement stmt; | 379 | try { |
60 | 384 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 380 | database.prepare (sqlcmd, |
61 | 385 | critical (_("Failed to update database: %s"), db.errmsg ()); | 381 | ":uri", typeof (string), uri, |
62 | 386 | stmt.bind_text (stmt.bind_parameter_index (":uri"), uri); | 382 | ":session_id", typeof (int64), this.id, |
63 | 387 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 383 | ":tab_id", typeof (int64), tab_id).exec (); |
64 | 388 | stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id); | 384 | } catch (Error error) { |
65 | 389 | if (stmt.step () != Sqlite.DONE) | 385 | critical (_("Failed to update database: %s"), error.message); |
66 | 390 | critical (_("Failed to update database: %s"), db.errmsg ()); | 386 | } |
67 | 391 | } | 387 | } |
68 | 392 | 388 | ||
69 | 393 | protected override void data_changed (Midori.View view) { | 389 | protected override void data_changed (Midori.View view) { |
70 | 394 | unowned Katze.Item item = view.get_proxy_item (); | 390 | unowned Katze.Item item = view.get_proxy_item (); |
71 | 395 | int64 tab_id = item.get_meta_integer ("tabby-id"); | 391 | int64 tab_id = item.get_meta_integer ("tabby-id"); |
72 | 396 | string sqlcmd = "UPDATE `tabs` SET title = :title WHERE session_id = :session_id AND id = :tab_id;"; | 392 | string sqlcmd = "UPDATE `tabs` SET title = :title WHERE session_id = :session_id AND id = :tab_id;"; |
81 | 397 | Sqlite.Statement stmt; | 393 | try { |
82 | 398 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 394 | database.prepare (sqlcmd, |
83 | 399 | critical (_("Failed to update database: %s"), db.errmsg ()); | 395 | ":title", typeof (string), view.get_display_title (), |
84 | 400 | stmt.bind_text (stmt.bind_parameter_index (":title"), view.get_display_title ()); | 396 | ":session_id", typeof (int64), this.id, |
85 | 401 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 397 | ":tab_id", typeof (int64), tab_id).exec (); |
86 | 402 | stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id); | 398 | } catch (Error error) { |
87 | 403 | if (stmt.step () != Sqlite.DONE) | 399 | critical (_("Failed to update database: %s"), error.message); |
88 | 404 | critical (_("Failed to update database: %s"), db.errmsg ()); | 400 | } |
89 | 405 | } | 401 | } |
90 | 406 | 402 | ||
91 | 407 | protected override void tab_added (Midori.Browser browser, Midori.View view) { | 403 | protected override void tab_added (Midori.Browser browser, Midori.View view) { |
92 | @@ -419,13 +415,13 @@ | |||
93 | 419 | int64 tab_id = item.get_meta_integer ("tabby-id"); | 415 | int64 tab_id = item.get_meta_integer ("tabby-id"); |
94 | 420 | /* FixMe: mark as deleted */ | 416 | /* FixMe: mark as deleted */ |
95 | 421 | string sqlcmd = "DELETE FROM `tabs` WHERE session_id = :session_id AND id = :tab_id;"; | 417 | string sqlcmd = "DELETE FROM `tabs` WHERE session_id = :session_id AND id = :tab_id;"; |
103 | 422 | Sqlite.Statement stmt; | 418 | try { |
104 | 423 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 419 | database.prepare (sqlcmd, |
105 | 424 | critical (_("Failed to update database: %s"), db.errmsg ()); | 420 | ":session_id", typeof (int64), this.id, |
106 | 425 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 421 | ":tab_id", typeof (int64), tab_id).exec (); |
107 | 426 | stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id); | 422 | } catch (Error error) { |
108 | 427 | if (stmt.step () != Sqlite.DONE) | 423 | critical (_("Failed to update database: %s"), error.message); |
109 | 428 | critical (_("Failed to update database: %s"), db.errmsg ()); | 424 | } |
110 | 429 | } | 425 | } |
111 | 430 | 426 | ||
112 | 431 | protected override void tab_switched (Midori.View? old_view, Midori.View? new_view) { | 427 | protected override void tab_switched (Midori.View? old_view, Midori.View? new_view) { |
113 | @@ -433,14 +429,14 @@ | |||
114 | 433 | unowned Katze.Item item = new_view.get_proxy_item (); | 429 | unowned Katze.Item item = new_view.get_proxy_item (); |
115 | 434 | int64 tab_id = item.get_meta_integer ("tabby-id"); | 430 | int64 tab_id = item.get_meta_integer ("tabby-id"); |
116 | 435 | string sqlcmd = "UPDATE `tabs` SET tstamp = :tstamp WHERE session_id = :session_id AND id = :tab_id;"; | 431 | string sqlcmd = "UPDATE `tabs` SET tstamp = :tstamp WHERE session_id = :session_id AND id = :tab_id;"; |
125 | 436 | Sqlite.Statement stmt; | 432 | try { |
126 | 437 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 433 | database.prepare (sqlcmd, |
127 | 438 | critical (_("Failed to update database: %s"), db.errmsg ()); | 434 | ":session_id", typeof (int64), this.id, |
128 | 439 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 435 | ":tab_id", typeof (int64), tab_id, |
129 | 440 | stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id); | 436 | ":tstamp", typeof (int64), time.to_unix ()).exec (); |
130 | 441 | stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ()); | 437 | } catch (Error error) { |
131 | 442 | if (stmt.step () != Sqlite.DONE) | 438 | critical (_("Failed to update database: %s"), error.message); |
132 | 443 | critical (_("Failed to update database: %s"), db.errmsg ()); | 439 | } |
133 | 444 | } | 440 | } |
134 | 445 | 441 | ||
135 | 446 | protected override void tab_reordered (Gtk.Widget tab, uint pos) { | 442 | protected override void tab_reordered (Gtk.Widget tab, uint pos) { |
136 | @@ -450,36 +446,29 @@ | |||
137 | 450 | unowned Katze.Item item = view.get_proxy_item (); | 446 | unowned Katze.Item item = view.get_proxy_item (); |
138 | 451 | int64 tab_id = item.get_meta_integer ("tabby-id"); | 447 | int64 tab_id = item.get_meta_integer ("tabby-id"); |
139 | 452 | string sqlcmd = "UPDATE `tabs` SET sorting = :sorting WHERE session_id = :session_id AND id = :tab_id;"; | 448 | string sqlcmd = "UPDATE `tabs` SET sorting = :sorting WHERE session_id = :session_id AND id = :tab_id;"; |
149 | 453 | Sqlite.Statement stmt; | 449 | try { |
150 | 454 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 450 | database.prepare (sqlcmd, |
151 | 455 | critical (_("Failed to update database: %s"), db.errmsg ()); | 451 | ":session_id", typeof (int64), this.id, |
152 | 456 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 452 | ":tab_id", typeof (int64), tab_id, |
153 | 457 | stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id); | 453 | ":sorting", typeof (double), sorting).exec (); |
154 | 458 | stmt.bind_double (stmt.bind_parameter_index (":sorting"), sorting); | 454 | } catch (Error error) { |
155 | 459 | 455 | critical (_("Failed to update database: %s"), error.message); | |
156 | 460 | if (stmt.step () != Sqlite.DONE) | 456 | } |
148 | 461 | critical (_("Failed to update database: %s"), db.errmsg ()); | ||
157 | 462 | 457 | ||
158 | 463 | item.set_meta_string ("sorting", sorting.to_string ()); | 458 | item.set_meta_string ("sorting", sorting.to_string ()); |
159 | 464 | } | 459 | } |
160 | 465 | 460 | ||
161 | 466 | public override void remove() { | 461 | public override void remove() { |
178 | 467 | string sqlcmd = "DELETE FROM `tabs` WHERE session_id = :session_id;"; | 462 | string sqlcmd = """ |
179 | 468 | Sqlite.Statement stmt; | 463 | DELETE FROM `tabs` WHERE session_id = :session_id; |
180 | 469 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 464 | DELETE FROM `sessions` WHERE id = :session_id; |
181 | 470 | critical (_("Failed to update database: %s"), db.errmsg ()); | 465 | """; |
182 | 471 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 466 | try { |
183 | 472 | 467 | database.prepare (sqlcmd, | |
184 | 473 | if (stmt.step () != Sqlite.DONE) | 468 | ":session_id", typeof (int64), this.id). exec (); |
185 | 474 | critical (_("Failed to update database: %s"), db.errmsg ()); | 469 | } catch (Error error) { |
186 | 475 | 470 | critical (_("Failed to update database: %s"), error.message); | |
187 | 476 | sqlcmd = "DELETE FROM `sessions` WHERE id = :session_id;"; | 471 | } |
172 | 477 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | ||
173 | 478 | critical (_("Failed to update database: %s"), db.errmsg ()); | ||
174 | 479 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | ||
175 | 480 | |||
176 | 481 | if (stmt.step () != Sqlite.DONE) | ||
177 | 482 | critical (_("Failed to update database: %s"), db.errmsg ()); | ||
188 | 483 | } | 472 | } |
189 | 484 | 473 | ||
190 | 485 | public override void close() { | 474 | public override void close() { |
191 | @@ -501,104 +490,98 @@ | |||
192 | 501 | 490 | ||
193 | 502 | GLib.DateTime time = new DateTime.now_local (); | 491 | GLib.DateTime time = new DateTime.now_local (); |
194 | 503 | string sqlcmd = "UPDATE `sessions` SET closed = 1, tstamp = :tstamp WHERE id = :session_id;"; | 492 | string sqlcmd = "UPDATE `sessions` SET closed = 1, tstamp = :tstamp WHERE id = :session_id;"; |
203 | 504 | Sqlite.Statement stmt; | 493 | try { |
204 | 505 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 494 | database.prepare (sqlcmd, |
205 | 506 | critical (_("Failed to update database: %s"), db.errmsg ()); | 495 | ":session_id", typeof (int64), this.id, |
206 | 507 | 496 | ":tstamp", typeof (int64), time.to_unix ()).exec (); | |
207 | 508 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 497 | } catch (Error error) { |
208 | 509 | stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ()); | 498 | critical (_("Failed to update database: %s"), error.message); |
209 | 510 | if (stmt.step () != Sqlite.DONE) | 499 | } |
202 | 511 | critical (_("Failed to update database: %s"), db.errmsg ()); | ||
210 | 512 | } | 500 | } |
211 | 513 | 501 | ||
212 | 514 | public override Katze.Array get_tabs() { | 502 | public override Katze.Array get_tabs() { |
213 | 515 | Katze.Array tabs = new Katze.Array (typeof (Katze.Item)); | 503 | Katze.Array tabs = new Katze.Array (typeof (Katze.Item)); |
214 | 516 | 504 | ||
215 | 517 | string sqlcmd = "SELECT id, uri, title, sorting FROM tabs WHERE session_id = :session_id ORDER BY tstamp DESC"; | 505 | string sqlcmd = "SELECT id, uri, title, sorting FROM tabs WHERE session_id = :session_id ORDER BY tstamp DESC"; |
224 | 518 | Sqlite.Statement stmt; | 506 | try { |
225 | 519 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 507 | var statement = database.prepare (sqlcmd, |
226 | 520 | critical (_("Failed to select from database: %s"), db.errmsg ()); | 508 | ":session_id", typeof (int64), this.id); |
227 | 521 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 509 | while (statement.step ()) { |
228 | 522 | int result = stmt.step (); | 510 | Katze.Item item = new Katze.Item (); |
229 | 523 | if (!(result == Sqlite.DONE || result == Sqlite.ROW)) { | 511 | int64 id = statement.get_int64 ("id"); |
230 | 524 | critical (_("Failed to select from database: %s"), db.errmsg ()); | 512 | string uri = statement.get_string ("uri"); |
231 | 525 | return tabs; | 513 | string title = statement.get_string ("title"); |
232 | 514 | double sorting = statement.get_double ("sorting"); | ||
233 | 515 | item.uri = uri; | ||
234 | 516 | item.name = title; | ||
235 | 517 | item.set_meta_integer ("tabby-id", id); | ||
236 | 518 | item.set_meta_string ("sorting", sorting.to_string ()); | ||
237 | 519 | tabs.add_item (item); | ||
238 | 520 | } | ||
239 | 521 | } catch (Error error) { | ||
240 | 522 | critical (_("Failed to select from database: %s"), error.message); | ||
241 | 526 | } | 523 | } |
257 | 527 | 524 | return tabs; | |
243 | 528 | while (result == Sqlite.ROW) { | ||
244 | 529 | Katze.Item item = new Katze.Item (); | ||
245 | 530 | int64 id = stmt.column_int64 (0); | ||
246 | 531 | string uri = stmt.column_text (1); | ||
247 | 532 | string title = stmt.column_text (2); | ||
248 | 533 | item.uri = uri; | ||
249 | 534 | item.name = title; | ||
250 | 535 | item.set_meta_integer ("tabby-id", id); | ||
251 | 536 | item.set_meta_string ("sorting", stmt.column_double (3).to_string ()); | ||
252 | 537 | tabs.add_item (item); | ||
253 | 538 | result = stmt.step (); | ||
254 | 539 | } | ||
255 | 540 | |||
256 | 541 | return tabs; | ||
258 | 542 | } | 525 | } |
259 | 543 | 526 | ||
260 | 544 | public override double? get_max_sorting () { | 527 | public override double? get_max_sorting () { |
261 | 545 | string sqlcmd = "SELECT MAX(sorting) FROM tabs WHERE session_id = :session_id"; | 528 | string sqlcmd = "SELECT MAX(sorting) FROM tabs WHERE session_id = :session_id"; |
270 | 546 | Sqlite.Statement stmt; | 529 | try { |
271 | 547 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 530 | var statement = database.prepare (sqlcmd, |
272 | 548 | critical (_("Failed to select from database: %s"), db.errmsg ()); | 531 | ":session_id", typeof (int64), this.id); |
273 | 549 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 532 | statement.step (); |
266 | 550 | int result = stmt.step (); | ||
267 | 551 | if (!(result == Sqlite.DONE || result == Sqlite.ROW)) { | ||
268 | 552 | critical (_("Failed to select from database: %s"), db.errmsg ()); | ||
269 | 553 | } else if (result == Sqlite.ROW) { | ||
274 | 554 | double? sorting; | 533 | double? sorting; |
277 | 555 | string? sorting_string = stmt.column_double (0).to_string (); | 534 | string? sorting_string = statement.get_int64 ("MAX(sorting)").to_string (); |
278 | 556 | if (sorting_string != null) { /* we have to use a seperate if condition to avoid a `possibly unassigned local variable` error */ | 535 | if (sorting_string != null) { |
279 | 536 | /* we have to use a seperate if condition to avoid | ||
280 | 537 | a `possibly unassigned local variable` error */ | ||
281 | 557 | if (double.try_parse (sorting_string, out sorting)) { | 538 | if (double.try_parse (sorting_string, out sorting)) { |
282 | 558 | return sorting; | 539 | return sorting; |
283 | 559 | } | 540 | } |
284 | 560 | } | 541 | } |
286 | 561 | } | 542 | } catch (Error error) { |
287 | 543 | critical (_("Failed to select from database: %s"), error.message); | ||
288 | 544 | } | ||
289 | 562 | 545 | ||
290 | 563 | return double.parse ("0"); | 546 | return double.parse ("0"); |
291 | 564 | } | 547 | } |
292 | 565 | 548 | ||
295 | 566 | internal Session (Sqlite.Database db) { | 549 | internal Session (Midori.Database database) { |
296 | 567 | this.db = db; | 550 | this.database = database; |
297 | 568 | 551 | ||
298 | 569 | GLib.DateTime time = new DateTime.now_local (); | 552 | GLib.DateTime time = new DateTime.now_local (); |
299 | 570 | 553 | ||
300 | 571 | string sqlcmd = "INSERT INTO `sessions` (`tstamp`) VALUES (:tstamp);"; | 554 | string sqlcmd = "INSERT INTO `sessions` (`tstamp`) VALUES (:tstamp);"; |
309 | 572 | Sqlite.Statement stmt; | 555 | |
310 | 573 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 556 | try { |
311 | 574 | critical (_("Failed to update database: %s"), db.errmsg); | 557 | var statement = database.prepare (sqlcmd, |
312 | 575 | stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ()); | 558 | ":tstamp", typeof (int64), time.to_unix ()); |
313 | 576 | if (stmt.step () != Sqlite.DONE) | 559 | statement.exec (); |
314 | 577 | critical (_("Failed to update database: %s"), db.errmsg); | 560 | this.id = statement.row_id (); |
315 | 578 | else | 561 | } catch (Error error) { |
316 | 579 | this.id = this.db.last_insert_rowid (); | 562 | critical (_("Failed to update database: %s"), error.message); |
317 | 563 | } | ||
318 | 580 | } | 564 | } |
319 | 581 | 565 | ||
322 | 582 | internal Session.with_id (Sqlite.Database db, int64 id) { | 566 | internal Session.with_id (Midori.Database database, int64 id) { |
323 | 583 | this.db = db; | 567 | this.database = database; |
324 | 584 | this.id = id; | 568 | this.id = id; |
325 | 585 | 569 | ||
326 | 586 | GLib.DateTime time = new DateTime.now_local (); | 570 | GLib.DateTime time = new DateTime.now_local (); |
327 | 587 | string sqlcmd = "UPDATE `sessions` SET closed = 0, tstamp = :tstamp WHERE id = :session_id;"; | 571 | string sqlcmd = "UPDATE `sessions` SET closed = 0, tstamp = :tstamp WHERE id = :session_id;"; |
328 | 588 | Sqlite.Statement stmt; | ||
329 | 589 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | ||
330 | 590 | critical (_("Failed to update database: %s"), db.errmsg); | ||
331 | 591 | 572 | ||
336 | 592 | stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id); | 573 | try { |
337 | 593 | stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ()); | 574 | database.prepare (sqlcmd, |
338 | 594 | if (stmt.step () != Sqlite.DONE) | 575 | ":session_id", typeof (int64), this.id, |
339 | 595 | critical (_("Failed to update database: %s"), db.errmsg); | 576 | ":tstamp", typeof (int64), time.to_unix ()).exec (); |
340 | 577 | } catch (Error error) { | ||
341 | 578 | critical (_("Failed to update database: %s"), error.message); | ||
342 | 579 | } | ||
343 | 596 | } | 580 | } |
344 | 597 | } | 581 | } |
345 | 598 | 582 | ||
346 | 599 | private class Storage : Base.Storage { | 583 | private class Storage : Base.Storage { |
347 | 600 | private Midori.Database database; | 584 | private Midori.Database database; |
348 | 601 | private unowned Sqlite.Database db; | ||
349 | 602 | 585 | ||
350 | 603 | public override Katze.Array get_sessions () { | 586 | public override Katze.Array get_sessions () { |
351 | 604 | Katze.Array sessions = new Katze.Array (typeof (Session)); | 587 | Katze.Array sessions = new Katze.Array (typeof (Session)); |
352 | @@ -609,39 +592,38 @@ | |||
353 | 609 | SELECT * FROM (SELECT id, closed FROM sessions WHERE closed = 1 ORDER BY tstamp DESC LIMIT 1) | 592 | SELECT * FROM (SELECT id, closed FROM sessions WHERE closed = 1 ORDER BY tstamp DESC LIMIT 1) |
354 | 610 | ORDER BY closed; | 593 | ORDER BY closed; |
355 | 611 | """; | 594 | """; |
370 | 612 | Sqlite.Statement stmt; | 595 | try { |
371 | 613 | if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK) | 596 | var statement = database.prepare (sqlcmd); |
372 | 614 | critical (_("Failed to select from database: %s"), db.errmsg); | 597 | while (statement.step ()) { |
373 | 615 | int result = stmt.step (); | 598 | int64 id = statement.get_int64 ("id"); |
374 | 616 | if (!(result == Sqlite.DONE || result == Sqlite.ROW)) { | 599 | int64 closed = statement.get_int64 ("closed"); |
375 | 617 | critical (_("Failed to select from database: %s"), db.errmsg); | 600 | if (closed == 0 || sessions.is_empty ()) { |
376 | 618 | return sessions; | 601 | sessions.add_item (new Session.with_id (this.database, id)); |
377 | 619 | } | 602 | } |
364 | 620 | |||
365 | 621 | while (result == Sqlite.ROW) { | ||
366 | 622 | int64 id = stmt.column_int64 (0); | ||
367 | 623 | int64 closed = stmt.column_int64 (1); | ||
368 | 624 | if (closed == 0 || sessions.is_empty ()) { | ||
369 | 625 | sessions.add_item (new Session.with_id (this.db, id)); | ||
378 | 626 | } | 603 | } |
381 | 627 | result = stmt.step (); | 604 | } catch (Error error) { |
382 | 628 | } | 605 | critical (_("Failed to select from database: %s"), error.message); |
383 | 606 | } | ||
384 | 629 | 607 | ||
385 | 630 | if (sessions.is_empty ()) { | 608 | if (sessions.is_empty ()) { |
387 | 631 | sessions.add_item (new Session (this.db)); | 609 | sessions.add_item (new Session (this.database)); |
388 | 632 | } | 610 | } |
389 | 633 | 611 | ||
390 | 634 | return sessions; | 612 | return sessions; |
391 | 635 | } | 613 | } |
392 | 636 | 614 | ||
393 | 637 | public override void import_session (Katze.Array tabs) { | 615 | public override void import_session (Katze.Array tabs) { |
397 | 638 | this.db.exec ("BEGIN;"); | 616 | try { |
398 | 639 | base.import_session(tabs); | 617 | database.transaction (()=>{ |
399 | 640 | this.db.exec("COMMIT;"); | 618 | base.import_session(tabs); return true; |
400 | 619 | }); | ||
401 | 620 | } catch (Error error) { | ||
402 | 621 | critical (_("Failed to select from database: %s"), error.message); | ||
403 | 622 | } | ||
404 | 641 | } | 623 | } |
405 | 642 | 624 | ||
406 | 643 | public override Base.Session get_new_session () { | 625 | public override Base.Session get_new_session () { |
408 | 644 | return new Session (this.db) as Base.Session; | 626 | return new Session (this.database) as Base.Session; |
409 | 645 | } | 627 | } |
410 | 646 | 628 | ||
411 | 647 | internal Storage (Midori.App app) { | 629 | internal Storage (Midori.App app) { |
412 | @@ -652,7 +634,6 @@ | |||
413 | 652 | } catch (Midori.DatabaseError schema_error) { | 634 | } catch (Midori.DatabaseError schema_error) { |
414 | 653 | error (schema_error.message); | 635 | error (schema_error.message); |
415 | 654 | } | 636 | } |
416 | 655 | db = database.db; | ||
417 | 656 | 637 | ||
418 | 657 | if (database.first_use) { | 638 | if (database.first_use) { |
419 | 658 | string config_file = Midori.Paths.get_config_filename_for_reading ("session.xbel"); | 639 | string config_file = Midori.Paths.get_config_filename_for_reading ("session.xbel"); |
420 | 659 | 640 | ||
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 | 23 | } | 23 | } |
426 | 24 | 24 | ||
427 | 25 | /* | 25 | /* |
428 | 26 | * Since: 0.5.8 | ||
429 | 27 | */ | ||
430 | 28 | public delegate bool DatabaseCallback () throws DatabaseError; | ||
431 | 29 | |||
432 | 30 | /* | ||
433 | 26 | * Since: 0.5.7 | 31 | * Since: 0.5.7 |
434 | 27 | */ | 32 | */ |
435 | 28 | public class DatabaseStatement : GLib.Object, GLib.Initable { | 33 | public class DatabaseStatement : GLib.Object, GLib.Initable { |
436 | @@ -30,6 +35,7 @@ | |||
437 | 30 | protected Sqlite.Statement _stmt = null; | 35 | protected Sqlite.Statement _stmt = null; |
438 | 31 | public Database? database { get; set construct; } | 36 | public Database? database { get; set construct; } |
439 | 32 | public string? query { get; set construct; } | 37 | public string? query { get; set construct; } |
440 | 38 | private int64 last_row_id = -1; | ||
441 | 33 | 39 | ||
442 | 34 | public DatabaseStatement (Database database, string query) throws DatabaseError { | 40 | public DatabaseStatement (Database database, string query) throws DatabaseError { |
443 | 35 | Object (database: database, query: query); | 41 | Object (database: database, query: query); |
444 | @@ -79,9 +85,21 @@ | |||
445 | 79 | int result = stmt.step (); | 85 | int result = stmt.step (); |
446 | 80 | if (result != Sqlite.DONE && result != Sqlite.ROW) | 86 | if (result != Sqlite.DONE && result != Sqlite.ROW) |
447 | 81 | throw new DatabaseError.EXECUTE (database.db.errmsg ()); | 87 | throw new DatabaseError.EXECUTE (database.db.errmsg ()); |
448 | 88 | last_row_id = database.db.last_insert_rowid (); | ||
449 | 82 | return result == Sqlite.ROW; | 89 | return result == Sqlite.ROW; |
450 | 83 | } | 90 | } |
451 | 84 | 91 | ||
452 | 92 | /* | ||
453 | 93 | * Returns the id of the last inserted row. | ||
454 | 94 | * It is an error to ask for an id without having inserted a row. | ||
455 | 95 | * Since: 0.5.8 | ||
456 | 96 | */ | ||
457 | 97 | public int64 row_id () throws DatabaseError { | ||
458 | 98 | if (last_row_id == -1) | ||
459 | 99 | throw new DatabaseError.EXECUTE ("No row id"); | ||
460 | 100 | return last_row_id; | ||
461 | 101 | } | ||
462 | 102 | |||
463 | 85 | private int column_index (string name) throws DatabaseError { | 103 | private int column_index (string name) throws DatabaseError { |
464 | 86 | for (int i = 0; i < stmt.column_count (); i++) { | 104 | for (int i = 0; i < stmt.column_count (); i++) { |
465 | 87 | if (name == stmt.column_name (i)) | 105 | if (name == stmt.column_name (i)) |
466 | @@ -219,9 +237,14 @@ | |||
467 | 219 | } catch (Error error) { | 237 | } catch (Error error) { |
468 | 220 | throw new DatabaseError.FILENAME ("Failed to open schema: %s".printf (schema_filename)); | 238 | throw new DatabaseError.FILENAME ("Failed to open schema: %s".printf (schema_filename)); |
469 | 221 | } | 239 | } |
473 | 222 | schema = "BEGIN TRANSACTION; %s; COMMIT;".printf (schema); | 240 | transaction (()=> { return exec (schema); }); |
474 | 223 | if (db.exec (schema) != Sqlite.OK) | 241 | return true; |
475 | 224 | throw new DatabaseError.EXECUTE ("Failed to execute schema: %s".printf (schema)); | 242 | } |
476 | 243 | |||
477 | 244 | public bool transaction (DatabaseCallback callback) throws DatabaseError { | ||
478 | 245 | exec ("BEGIN TRANSACTION;"); | ||
479 | 246 | callback (); | ||
480 | 247 | exec ("COMMIT;"); | ||
481 | 225 | return true; | 248 | return true; |
482 | 226 | } | 249 | } |
483 | 227 | 250 |