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
=== modified file 'extensions/tabby.vala'
--- extensions/tabby.vala 2014-01-12 18:15:53 +0000
+++ extensions/tabby.vala 2014-03-16 20:28:56 +0000
@@ -350,29 +350,25 @@
350 namespace Local {350 namespace Local {
351 private class Session : Base.Session {351 private class Session : Base.Session {
352 public int64 id { get; private set; }352 public int64 id { get; private set; }
353 private unowned Sqlite.Database db;353 private Midori.Database database;
354354
355 public override void add_item (Katze.Item item) {355 public override void add_item (Katze.Item item) {
356 GLib.DateTime time = new DateTime.now_local ();356 GLib.DateTime time = new DateTime.now_local ();
357 string? sorting = item.get_meta_string ("sorting");357 string? sorting = item.get_meta_string ("sorting") ?? "1";
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);";
359 Sqlite.Statement stmt;
360 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
361 critical (_("Failed to update database: %s"), db.errmsg);
362 stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());
363 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
364 stmt.bind_text (stmt.bind_parameter_index (":uri"), item.uri);
365 stmt.bind_text (stmt.bind_parameter_index (":title"), item.name);
366 if (sorting == null)
367 stmt.bind_double (stmt.bind_parameter_index (":sorting"), double.parse ("1"));
368 else
369 stmt.bind_double (stmt.bind_parameter_index (":sorting"), double.parse (sorting));
370359
371 if (stmt.step () != Sqlite.DONE)360 try {
372 critical (_("Failed to update database: %s"), db.errmsg);361 var statement = database.prepare (sqlcmd,
373 else {362 ":tstamp", typeof (int64), time.to_unix (),
374 int64 tab_id = this.db.last_insert_rowid ();363 ":session_id", typeof (int64), this.id,
364 ":uri", typeof (string), item.uri,
365 ":title", typeof (string), item.name,
366 ":sorting", typeof (double), double.parse (sorting));
367 statement.exec ();
368 int64 tab_id = statement.row_id ();
375 item.set_meta_integer ("tabby-id", tab_id);369 item.set_meta_integer ("tabby-id", tab_id);
370 } catch (Error error) {
371 critical (_("Failed to update database: %s"), error.message);
376 }372 }
377 }373 }
378374
@@ -380,28 +376,28 @@
380 unowned Katze.Item item = view.get_proxy_item ();376 unowned Katze.Item item = view.get_proxy_item ();
381 int64 tab_id = item.get_meta_integer ("tabby-id");377 int64 tab_id = item.get_meta_integer ("tabby-id");
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;";
383 Sqlite.Statement stmt;379 try {
384 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)380 database.prepare (sqlcmd,
385 critical (_("Failed to update database: %s"), db.errmsg ());381 ":uri", typeof (string), uri,
386 stmt.bind_text (stmt.bind_parameter_index (":uri"), uri);382 ":session_id", typeof (int64), this.id,
387 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);383 ":tab_id", typeof (int64), tab_id).exec ();
388 stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);384 } catch (Error error) {
389 if (stmt.step () != Sqlite.DONE)385 critical (_("Failed to update database: %s"), error.message);
390 critical (_("Failed to update database: %s"), db.errmsg ());386 }
391 }387 }
392388
393 protected override void data_changed (Midori.View view) {389 protected override void data_changed (Midori.View view) {
394 unowned Katze.Item item = view.get_proxy_item ();390 unowned Katze.Item item = view.get_proxy_item ();
395 int64 tab_id = item.get_meta_integer ("tabby-id");391 int64 tab_id = item.get_meta_integer ("tabby-id");
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;";
397 Sqlite.Statement stmt;393 try {
398 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)394 database.prepare (sqlcmd,
399 critical (_("Failed to update database: %s"), db.errmsg ());395 ":title", typeof (string), view.get_display_title (),
400 stmt.bind_text (stmt.bind_parameter_index (":title"), view.get_display_title ());396 ":session_id", typeof (int64), this.id,
401 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);397 ":tab_id", typeof (int64), tab_id).exec ();
402 stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);398 } catch (Error error) {
403 if (stmt.step () != Sqlite.DONE)399 critical (_("Failed to update database: %s"), error.message);
404 critical (_("Failed to update database: %s"), db.errmsg ());400 }
405 }401 }
406402
407 protected override void tab_added (Midori.Browser browser, Midori.View view) {403 protected override void tab_added (Midori.Browser browser, Midori.View view) {
@@ -419,13 +415,13 @@
419 int64 tab_id = item.get_meta_integer ("tabby-id");415 int64 tab_id = item.get_meta_integer ("tabby-id");
420 /* FixMe: mark as deleted */416 /* FixMe: mark as deleted */
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;";
422 Sqlite.Statement stmt;418 try {
423 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)419 database.prepare (sqlcmd,
424 critical (_("Failed to update database: %s"), db.errmsg ());420 ":session_id", typeof (int64), this.id,
425 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);421 ":tab_id", typeof (int64), tab_id).exec ();
426 stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);422 } catch (Error error) {
427 if (stmt.step () != Sqlite.DONE)423 critical (_("Failed to update database: %s"), error.message);
428 critical (_("Failed to update database: %s"), db.errmsg ());424 }
429 }425 }
430426
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) {
@@ -433,14 +429,14 @@
433 unowned Katze.Item item = new_view.get_proxy_item ();429 unowned Katze.Item item = new_view.get_proxy_item ();
434 int64 tab_id = item.get_meta_integer ("tabby-id");430 int64 tab_id = item.get_meta_integer ("tabby-id");
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;";
436 Sqlite.Statement stmt;432 try {
437 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)433 database.prepare (sqlcmd,
438 critical (_("Failed to update database: %s"), db.errmsg ());434 ":session_id", typeof (int64), this.id,
439 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);435 ":tab_id", typeof (int64), tab_id,
440 stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);436 ":tstamp", typeof (int64), time.to_unix ()).exec ();
441 stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());437 } catch (Error error) {
442 if (stmt.step () != Sqlite.DONE)438 critical (_("Failed to update database: %s"), error.message);
443 critical (_("Failed to update database: %s"), db.errmsg ());439 }
444 }440 }
445441
446 protected override void tab_reordered (Gtk.Widget tab, uint pos) {442 protected override void tab_reordered (Gtk.Widget tab, uint pos) {
@@ -450,36 +446,29 @@
450 unowned Katze.Item item = view.get_proxy_item ();446 unowned Katze.Item item = view.get_proxy_item ();
451 int64 tab_id = item.get_meta_integer ("tabby-id");447 int64 tab_id = item.get_meta_integer ("tabby-id");
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;";
453 Sqlite.Statement stmt;449 try {
454 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)450 database.prepare (sqlcmd,
455 critical (_("Failed to update database: %s"), db.errmsg ());451 ":session_id", typeof (int64), this.id,
456 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);452 ":tab_id", typeof (int64), tab_id,
457 stmt.bind_int64 (stmt.bind_parameter_index (":tab_id"), tab_id);453 ":sorting", typeof (double), sorting).exec ();
458 stmt.bind_double (stmt.bind_parameter_index (":sorting"), sorting);454 } catch (Error error) {
459455 critical (_("Failed to update database: %s"), error.message);
460 if (stmt.step () != Sqlite.DONE)456 }
461 critical (_("Failed to update database: %s"), db.errmsg ());
462457
463 item.set_meta_string ("sorting", sorting.to_string ());458 item.set_meta_string ("sorting", sorting.to_string ());
464 }459 }
465460
466 public override void remove() {461 public override void remove() {
467 string sqlcmd = "DELETE FROM `tabs` WHERE session_id = :session_id;";462 string sqlcmd = """
468 Sqlite.Statement stmt;463 DELETE FROM `tabs` WHERE session_id = :session_id;
469 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)464 DELETE FROM `sessions` WHERE id = :session_id;
470 critical (_("Failed to update database: %s"), db.errmsg ());465 """;
471 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);466 try {
472467 database.prepare (sqlcmd,
473 if (stmt.step () != Sqlite.DONE)468 ":session_id", typeof (int64), this.id). exec ();
474 critical (_("Failed to update database: %s"), db.errmsg ());469 } catch (Error error) {
475470 critical (_("Failed to update database: %s"), error.message);
476 sqlcmd = "DELETE FROM `sessions` WHERE id = :session_id;";471 }
477 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
478 critical (_("Failed to update database: %s"), db.errmsg ());
479 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);
480
481 if (stmt.step () != Sqlite.DONE)
482 critical (_("Failed to update database: %s"), db.errmsg ());
483 }472 }
484473
485 public override void close() {474 public override void close() {
@@ -501,104 +490,98 @@
501490
502 GLib.DateTime time = new DateTime.now_local ();491 GLib.DateTime time = new DateTime.now_local ();
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;";
504 Sqlite.Statement stmt;493 try {
505 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)494 database.prepare (sqlcmd,
506 critical (_("Failed to update database: %s"), db.errmsg ());495 ":session_id", typeof (int64), this.id,
507496 ":tstamp", typeof (int64), time.to_unix ()).exec ();
508 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);497 } catch (Error error) {
509 stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());498 critical (_("Failed to update database: %s"), error.message);
510 if (stmt.step () != Sqlite.DONE)499 }
511 critical (_("Failed to update database: %s"), db.errmsg ());
512 }500 }
513501
514 public override Katze.Array get_tabs() {502 public override Katze.Array get_tabs() {
515 Katze.Array tabs = new Katze.Array (typeof (Katze.Item));503 Katze.Array tabs = new Katze.Array (typeof (Katze.Item));
516504
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";
518 Sqlite.Statement stmt;506 try {
519 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)507 var statement = database.prepare (sqlcmd,
520 critical (_("Failed to select from database: %s"), db.errmsg ());508 ":session_id", typeof (int64), this.id);
521 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);509 while (statement.step ()) {
522 int result = stmt.step ();510 Katze.Item item = new Katze.Item ();
523 if (!(result == Sqlite.DONE || result == Sqlite.ROW)) {511 int64 id = statement.get_int64 ("id");
524 critical (_("Failed to select from database: %s"), db.errmsg ());512 string uri = statement.get_string ("uri");
525 return tabs;513 string title = statement.get_string ("title");
514 double sorting = statement.get_double ("sorting");
515 item.uri = uri;
516 item.name = title;
517 item.set_meta_integer ("tabby-id", id);
518 item.set_meta_string ("sorting", sorting.to_string ());
519 tabs.add_item (item);
520 }
521 } catch (Error error) {
522 critical (_("Failed to select from database: %s"), error.message);
526 }523 }
527524 return tabs;
528 while (result == Sqlite.ROW) {
529 Katze.Item item = new Katze.Item ();
530 int64 id = stmt.column_int64 (0);
531 string uri = stmt.column_text (1);
532 string title = stmt.column_text (2);
533 item.uri = uri;
534 item.name = title;
535 item.set_meta_integer ("tabby-id", id);
536 item.set_meta_string ("sorting", stmt.column_double (3).to_string ());
537 tabs.add_item (item);
538 result = stmt.step ();
539 }
540
541 return tabs;
542 }525 }
543526
544 public override double? get_max_sorting () {527 public override double? get_max_sorting () {
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";
546 Sqlite.Statement stmt;529 try {
547 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)530 var statement = database.prepare (sqlcmd,
548 critical (_("Failed to select from database: %s"), db.errmsg ());531 ":session_id", typeof (int64), this.id);
549 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);532 statement.step ();
550 int result = stmt.step ();
551 if (!(result == Sqlite.DONE || result == Sqlite.ROW)) {
552 critical (_("Failed to select from database: %s"), db.errmsg ());
553 } else if (result == Sqlite.ROW) {
554 double? sorting;533 double? sorting;
555 string? sorting_string = stmt.column_double (0).to_string ();534 string? sorting_string = statement.get_int64 ("MAX(sorting)").to_string ();
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) {
536 /* we have to use a seperate if condition to avoid
537 a `possibly unassigned local variable` error */
557 if (double.try_parse (sorting_string, out sorting)) {538 if (double.try_parse (sorting_string, out sorting)) {
558 return sorting;539 return sorting;
559 }540 }
560 }541 }
561 }542 } catch (Error error) {
543 critical (_("Failed to select from database: %s"), error.message);
544 }
562545
563 return double.parse ("0");546 return double.parse ("0");
564 }547 }
565548
566 internal Session (Sqlite.Database db) {549 internal Session (Midori.Database database) {
567 this.db = db;550 this.database = database;
568551
569 GLib.DateTime time = new DateTime.now_local ();552 GLib.DateTime time = new DateTime.now_local ();
570553
571 string sqlcmd = "INSERT INTO `sessions` (`tstamp`) VALUES (:tstamp);";554 string sqlcmd = "INSERT INTO `sessions` (`tstamp`) VALUES (:tstamp);";
572 Sqlite.Statement stmt;555
573 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)556 try {
574 critical (_("Failed to update database: %s"), db.errmsg);557 var statement = database.prepare (sqlcmd,
575 stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());558 ":tstamp", typeof (int64), time.to_unix ());
576 if (stmt.step () != Sqlite.DONE)559 statement.exec ();
577 critical (_("Failed to update database: %s"), db.errmsg);560 this.id = statement.row_id ();
578 else561 } catch (Error error) {
579 this.id = this.db.last_insert_rowid ();562 critical (_("Failed to update database: %s"), error.message);
563 }
580 }564 }
581565
582 internal Session.with_id (Sqlite.Database db, int64 id) {566 internal Session.with_id (Midori.Database database, int64 id) {
583 this.db = db;567 this.database = database;
584 this.id = id;568 this.id = id;
585569
586 GLib.DateTime time = new DateTime.now_local ();570 GLib.DateTime time = new DateTime.now_local ();
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;";
588 Sqlite.Statement stmt;
589 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)
590 critical (_("Failed to update database: %s"), db.errmsg);
591572
592 stmt.bind_int64 (stmt.bind_parameter_index (":session_id"), this.id);573 try {
593 stmt.bind_int64 (stmt.bind_parameter_index (":tstamp"), time.to_unix ());574 database.prepare (sqlcmd,
594 if (stmt.step () != Sqlite.DONE)575 ":session_id", typeof (int64), this.id,
595 critical (_("Failed to update database: %s"), db.errmsg);576 ":tstamp", typeof (int64), time.to_unix ()).exec ();
577 } catch (Error error) {
578 critical (_("Failed to update database: %s"), error.message);
579 }
596 }580 }
597 }581 }
598582
599 private class Storage : Base.Storage {583 private class Storage : Base.Storage {
600 private Midori.Database database;584 private Midori.Database database;
601 private unowned Sqlite.Database db;
602585
603 public override Katze.Array get_sessions () {586 public override Katze.Array get_sessions () {
604 Katze.Array sessions = new Katze.Array (typeof (Session));587 Katze.Array sessions = new Katze.Array (typeof (Session));
@@ -609,39 +592,38 @@
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)
610 ORDER BY closed;593 ORDER BY closed;
611 """;594 """;
612 Sqlite.Statement stmt;595 try {
613 if (this.db.prepare_v2 (sqlcmd, -1, out stmt, null) != Sqlite.OK)596 var statement = database.prepare (sqlcmd);
614 critical (_("Failed to select from database: %s"), db.errmsg);597 while (statement.step ()) {
615 int result = stmt.step ();598 int64 id = statement.get_int64 ("id");
616 if (!(result == Sqlite.DONE || result == Sqlite.ROW)) {599 int64 closed = statement.get_int64 ("closed");
617 critical (_("Failed to select from database: %s"), db.errmsg);600 if (closed == 0 || sessions.is_empty ()) {
618 return sessions;601 sessions.add_item (new Session.with_id (this.database, id));
619 }602 }
620
621 while (result == Sqlite.ROW) {
622 int64 id = stmt.column_int64 (0);
623 int64 closed = stmt.column_int64 (1);
624 if (closed == 0 || sessions.is_empty ()) {
625 sessions.add_item (new Session.with_id (this.db, id));
626 }603 }
627 result = stmt.step ();604 } catch (Error error) {
628 }605 critical (_("Failed to select from database: %s"), error.message);
606 }
629607
630 if (sessions.is_empty ()) {608 if (sessions.is_empty ()) {
631 sessions.add_item (new Session (this.db));609 sessions.add_item (new Session (this.database));
632 }610 }
633611
634 return sessions;612 return sessions;
635 }613 }
636614
637 public override void import_session (Katze.Array tabs) {615 public override void import_session (Katze.Array tabs) {
638 this.db.exec ("BEGIN;");616 try {
639 base.import_session(tabs);617 database.transaction (()=>{
640 this.db.exec("COMMIT;");618 base.import_session(tabs); return true;
619 });
620 } catch (Error error) {
621 critical (_("Failed to select from database: %s"), error.message);
622 }
641 }623 }
642624
643 public override Base.Session get_new_session () {625 public override Base.Session get_new_session () {
644 return new Session (this.db) as Base.Session;626 return new Session (this.database) as Base.Session;
645 }627 }
646628
647 internal Storage (Midori.App app) {629 internal Storage (Midori.App app) {
@@ -652,7 +634,6 @@
652 } catch (Midori.DatabaseError schema_error) {634 } catch (Midori.DatabaseError schema_error) {
653 error (schema_error.message);635 error (schema_error.message);
654 }636 }
655 db = database.db;
656637
657 if (database.first_use) {638 if (database.first_use) {
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");
659640
=== modified file 'midori/midori-database.vala'
--- midori/midori-database.vala 2014-01-29 21:52:10 +0000
+++ midori/midori-database.vala 2014-03-16 20:28:56 +0000
@@ -23,6 +23,11 @@
23 }23 }
2424
25 /*25 /*
26 * Since: 0.5.8
27 */
28 public delegate bool DatabaseCallback () throws DatabaseError;
29
30 /*
26 * Since: 0.5.731 * Since: 0.5.7
27 */32 */
28 public class DatabaseStatement : GLib.Object, GLib.Initable {33 public class DatabaseStatement : GLib.Object, GLib.Initable {
@@ -30,6 +35,7 @@
30 protected Sqlite.Statement _stmt = null;35 protected Sqlite.Statement _stmt = null;
31 public Database? database { get; set construct; }36 public Database? database { get; set construct; }
32 public string? query { get; set construct; }37 public string? query { get; set construct; }
38 private int64 last_row_id = -1;
3339
34 public DatabaseStatement (Database database, string query) throws DatabaseError {40 public DatabaseStatement (Database database, string query) throws DatabaseError {
35 Object (database: database, query: query);41 Object (database: database, query: query);
@@ -79,9 +85,21 @@
79 int result = stmt.step ();85 int result = stmt.step ();
80 if (result != Sqlite.DONE && result != Sqlite.ROW)86 if (result != Sqlite.DONE && result != Sqlite.ROW)
81 throw new DatabaseError.EXECUTE (database.db.errmsg ());87 throw new DatabaseError.EXECUTE (database.db.errmsg ());
88 last_row_id = database.db.last_insert_rowid ();
82 return result == Sqlite.ROW;89 return result == Sqlite.ROW;
83 }90 }
8491
92 /*
93 * Returns the id of the last inserted row.
94 * It is an error to ask for an id without having inserted a row.
95 * Since: 0.5.8
96 */
97 public int64 row_id () throws DatabaseError {
98 if (last_row_id == -1)
99 throw new DatabaseError.EXECUTE ("No row id");
100 return last_row_id;
101 }
102
85 private int column_index (string name) throws DatabaseError {103 private int column_index (string name) throws DatabaseError {
86 for (int i = 0; i < stmt.column_count (); i++) {104 for (int i = 0; i < stmt.column_count (); i++) {
87 if (name == stmt.column_name (i))105 if (name == stmt.column_name (i))
@@ -219,9 +237,14 @@
219 } catch (Error error) {237 } catch (Error error) {
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));
221 }239 }
222 schema = "BEGIN TRANSACTION; %s; COMMIT;".printf (schema);240 transaction (()=> { return exec (schema); });
223 if (db.exec (schema) != Sqlite.OK)241 return true;
224 throw new DatabaseError.EXECUTE ("Failed to execute schema: %s".printf (schema));242 }
243
244 public bool transaction (DatabaseCallback callback) throws DatabaseError {
245 exec ("BEGIN TRANSACTION;");
246 callback ();
247 exec ("COMMIT;");
225 return true;248 return true;
226 }249 }
227250

Subscribers

People subscribed via source and target branches

to all changes: