summaryrefslogtreecommitdiff
path: root/midori
diff options
context:
space:
mode:
authorAndré Auzi <aauzi@free.fr>2013-10-16 23:02:21 +0000
committerTarmac <>2013-10-16 23:02:21 +0000
commit19b6b425ca20bc970ec7579bf62e45e080dd1301 (patch)
treee7f0f527ce4bdd939b016354e84af71a98806f05 /midori
parent8e73e5df9346273b4ab4e0e06606c48df53fd4d1 (diff)
parent0b3ab37cf7a8874f054109e7ed0a18d424d9fb08 (diff)
downloadmidori-19b6b425ca20bc970ec7579bf62e45e080dd1301.tar.gz
Implement MidoriBookmarksDatabase class by inheritence from MidoriDatabase
Diffstat (limited to 'midori')
-rw-r--r--midori/midori-bookmarks-db.c247
-rw-r--r--midori/midori-bookmarksdatabase.vala101
-rw-r--r--midori/midori-historydatabase.vala4
3 files changed, 120 insertions, 232 deletions
diff --git a/midori/midori-bookmarks-db.c b/midori/midori-bookmarks-db.c
index deecd794..da110277 100644
--- a/midori/midori-bookmarks-db.c
+++ b/midori/midori-bookmarks-db.c
@@ -285,7 +285,6 @@ _midori_bookmarks_db_move_item (KatzeArray* array,
gpointer item,
gint position)
{
- MidoriBookmarksDb *bookmarks;
KatzeArray* parent;
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
@@ -581,73 +580,6 @@ midori_bookmarks_db_remove_item (MidoriBookmarksDb* bookmarks, KatzeItem* item)
katze_array_remove_item (KATZE_ARRAY (bookmarks), item);
}
-#define _APPEND_TO_SQL_ERRORMSG(custom_errmsg) \
- do { \
- if (sql_errmsg) \
- { \
- g_string_append_printf (errmsg_str, "%s : %s\n", custom_errmsg, sql_errmsg); \
- sqlite3_free (sql_errmsg); \
- } \
- else \
- g_string_append (errmsg_str, custom_errmsg); \
- } while (0)
-
-static gboolean
-midori_bookmarks_db_import_from_old_db (sqlite3* db,
- const gchar* oldfile,
- gchar** errmsg)
-{
- gint sql_errcode;
- gboolean failure = FALSE;
- gchar* sql_errmsg = NULL;
- GString* errmsg_str = g_string_new (NULL);
- gchar* attach_stmt = sqlite3_mprintf ("ATTACH DATABASE %Q AS old_db;", oldfile);
- const gchar* convert_stmts =
- "BEGIN TRANSACTION;"
- "INSERT INTO main.bookmarks (parentid, title, uri, desc, app, toolbar) "
- "SELECT NULL AS parentid, title, uri, desc, app, toolbar "
- "FROM old_db.bookmarks;"
- "UPDATE main.bookmarks SET parentid = ("
- "SELECT id FROM main.bookmarks AS b1 WHERE b1.title = ("
- "SELECT folder FROM old_db.bookmarks WHERE title = main.bookmarks.title));"
- "COMMIT;";
- const gchar* detach_stmt = "DETACH DATABASE old_db;";
-
- *errmsg = NULL;
- sql_errcode = sqlite3_exec (db, attach_stmt, NULL, NULL, &sql_errmsg);
- sqlite3_free (attach_stmt);
-
- if (sql_errcode != SQLITE_OK)
- {
- _APPEND_TO_SQL_ERRORMSG (_("failed to ATTACH old db"));
- goto convert_failed;
- }
-
- if (sqlite3_exec (db, convert_stmts, NULL, NULL, &sql_errmsg) != SQLITE_OK)
- {
- failure = TRUE;
- _APPEND_TO_SQL_ERRORMSG (_("failed to import from old db"));
-
- /* try to get back to previous state */
- if (sqlite3_exec (db, "ROLLBACK TRANSACTION;", NULL, NULL, &sql_errmsg) != SQLITE_OK)
- _APPEND_TO_SQL_ERRORMSG (_("failed to rollback the transaction"));
- }
-
- if (sqlite3_exec (db, detach_stmt, NULL, NULL, &sql_errmsg) != SQLITE_OK)
- _APPEND_TO_SQL_ERRORMSG (_("failed to DETACH "));
-
- if (failure)
- {
- convert_failed:
- *errmsg = g_string_free (errmsg_str, FALSE);
- g_print ("ERRORR: %s\n", errmsg_str->str);
- return FALSE;
- }
-
- return TRUE;
-}
-#undef _APPEND_TO_SQL_ERRORMSG
-
static void
midori_bookmarks_db_dbtracer (void* dummy,
const char* query)
@@ -667,177 +599,33 @@ midori_bookmarks_db_dbtracer (void* dummy,
MidoriBookmarksDb*
midori_bookmarks_db_new (char** errmsg)
{
- sqlite3* db;
- gchar* oldfile;
- gchar* newfile;
- gboolean newfile_did_exist, oldfile_exists;
- const gchar* create_stmt;
- gchar* sql_errmsg = NULL;
- gchar* import_errmsg = NULL;
- KatzeArray* array;
- MidoriBookmarksDb* bookmarks;
+ MidoriBookmarksDatabase* database;
+ GError* error = NULL;
+ sqlite3* db;
+ MidoriBookmarksDb* bookmarks;
g_return_val_if_fail (errmsg != NULL, NULL);
- oldfile = midori_paths_get_config_filename_for_writing ("bookmarks.db");
- oldfile_exists = g_access (oldfile, F_OK) == 0;
- newfile = midori_paths_get_config_filename_for_writing ("bookmarks_v2.db");
- newfile_did_exist = g_access (newfile, F_OK) == 0;
-
- /* sqlite3_open will create the file if it did not exists already */
- if (sqlite3_open (newfile, &db) != SQLITE_OK)
+ database = midori_bookmarks_database_new (&error);
+
+ if (error != NULL)
{
- *errmsg = g_strdup_printf (_("Failed to open database: %s\n"),
- db ? sqlite3_errmsg (db) : "(db = NULL)");
- goto init_failed;
+ *errmsg = g_strdup (error->message);
+ g_error_free (error);
+ return NULL;
}
+ db = midori_database_get_db (MIDORI_DATABASE (database));
+ g_return_val_if_fail (db != NULL, NULL);
+
if (midori_debug ("bookmarks"))
sqlite3_trace (db, midori_bookmarks_db_dbtracer, NULL);
- create_stmt = /* Table structure */
- "CREATE TABLE IF NOT EXISTS bookmarks "
- "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
- "parentid INTEGER DEFAULT NULL, "
- "title TEXT, uri TEXT, desc TEXT, app INTEGER, toolbar INTEGER, "
- "pos_panel INTEGER, pos_bar INTEGER, "
- "created DATE DEFAULT CURRENT_TIMESTAMP, "
- "last_visit DATE, visit_count INTEGER DEFAULT 0, "
- "nick TEXT, "
- "FOREIGN KEY(parentid) REFERENCES bookmarks(id) "
- "ON DELETE CASCADE); PRAGMA foreign_keys = ON;"
-
- /* trigger: insert panel position */
- "CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosPanel "
- "AFTER INSERT ON bookmarks FOR EACH ROW "
- "BEGIN UPDATE bookmarks SET pos_panel = ("
- "SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks WHERE "
- "(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
- "OR (NEW.parentid IS NULL AND parentid IS NULL)) "
- "WHERE id = NEW.id; END;"
-
- /* trigger: insert Bookmarkbar position */
- "CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosBar "
- "AFTER INSERT ON bookmarks FOR EACH ROW WHEN NEW.toolbar=1 "
- "BEGIN UPDATE bookmarks SET pos_bar = ("
- "SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
- "((NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
- "OR (NEW.parentid IS NULL AND parentid IS NULL)) AND toolbar=1) "
- "WHERE id = NEW.id; END;"
-
- /* trigger: update panel position */
- "CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosPanel "
- "BEFORE UPDATE OF parentid ON bookmarks FOR EACH ROW "
- "WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
- "AND NEW.parentid IS NOT OLD.parentid) OR "
- "((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
- "AND NEW.parentid!=OLD.parentid) "
- "BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
- "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
- "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; "
- "UPDATE bookmarks SET pos_panel = ("
- "SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks "
- "WHERE (NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
- "OR (NEW.parentid IS NULL AND parentid IS NULL)) "
- "WHERE id = OLD.id; END;"
-
- /* trigger: update Bookmarkbar position */
- "CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar0 "
- "AFTER UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
- "WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
- "AND NEW.parentid IS NOT OLD.parentid) "
- "OR ((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
- "AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=1 AND NEW.toolbar=0) "
- "BEGIN UPDATE bookmarks SET pos_bar = NULL WHERE id = NEW.id; "
- "UPDATE bookmarks SET pos_bar = pos_bar-1 "
- "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
- "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;"
-
- /* trigger: update Bookmarkbar position */
- "CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar1 "
- "BEFORE UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
- "WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
- "AND NEW.parentid IS NOT OLD.parentid) OR "
- "((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
- "AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=0 AND NEW.toolbar=1) "
- "BEGIN UPDATE bookmarks SET pos_bar = ("
- "SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
- "(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
- "OR (NEW.parentid IS NULL AND parentid IS NULL)) "
- "WHERE id = OLD.id; END;"
-
- /* trigger: delete panel position */
- "CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosPanel "
- "AFTER DELETE ON bookmarks FOR EACH ROW "
- "BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
- "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
- "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; END;"
-
- /* trigger: delete Bookmarkbar position */
- "CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosBar "
- "AFTER DELETE ON bookmarks FOR EACH ROW WHEN OLD.toolbar=1 "
- "BEGIN UPDATE bookmarks SET pos_bar = pos_bar-1 "
- "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
- "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;";
-
-
- if (newfile_did_exist)
- {
- const gchar* setup_stmt = "PRAGMA foreign_keys = ON;";
- /* initial setup */
- if (sqlite3_exec (db, setup_stmt, NULL, NULL, &sql_errmsg) != SQLITE_OK)
- {
- *errmsg = g_strdup_printf (_("Couldn't setup bookmarks: %s\n"),
- sql_errmsg ? sql_errmsg : "(err = NULL)");
- sqlite3_free (sql_errmsg);
- goto init_failed;
- }
-
- /* we are done */
- goto init_success;
- }
- else
- {
- /* initial creation */
- if (sqlite3_exec (db, create_stmt, NULL, NULL, &sql_errmsg) != SQLITE_OK)
- {
- *errmsg = g_strdup_printf (_("Couldn't create bookmarks table: %s\n"),
- sql_errmsg ? sql_errmsg : "(err = NULL)");
- sqlite3_free (sql_errmsg);
-
- /* we can as well remove the new file */
- g_unlink (newfile);
- goto init_failed;
- }
+ bookmarks = MIDORI_BOOKMARKS_DB (g_object_new (TYPE_MIDORI_BOOKMARKS_DB, NULL));
+ bookmarks->db = db;
- }
-
- if (oldfile_exists)
- /* import from old db */
- if (!midori_bookmarks_db_import_from_old_db (db, oldfile, &import_errmsg))
- {
- *errmsg = g_strdup_printf (_("Couldn't import from old database: %s\n"),
- import_errmsg ? import_errmsg : "(err = NULL)");
- g_free (import_errmsg);
- }
-
- init_success:
- g_free (newfile);
- g_free (oldfile);
- bookmarks = MIDORI_BOOKMARKS_DB (g_object_new (TYPE_MIDORI_BOOKMARKS_DB, NULL));
- bookmarks->db = db;
-
- g_object_set_data (G_OBJECT (bookmarks), "db", db);
- return bookmarks;
-
- init_failed:
- g_free (newfile);
- g_free (oldfile);
-
- if (db)
- sqlite3_close (db);
-
- return NULL;
+ g_object_set_data (G_OBJECT (bookmarks), "db", db);
+ return bookmarks;
}
/**
@@ -923,7 +711,6 @@ midori_bookmarks_db_array_from_statement (sqlite3_stmt* stmt,
{
gint i;
KatzeItem* item;
- KatzeItem* found;
item = katze_item_new ();
for (i = 0; i < cols; i++)
diff --git a/midori/midori-bookmarksdatabase.vala b/midori/midori-bookmarksdatabase.vala
new file mode 100644
index 00000000..c678035b
--- /dev/null
+++ b/midori/midori-bookmarksdatabase.vala
@@ -0,0 +1,101 @@
+/*
+ Copyright (C) 2013 Andre Auzi <aauzi@free.fr>
+ Copyright (C) 2013 Christian Dywan <christian@twotoats.de>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ See the file COPYING for the full license text.
+*/
+
+namespace Midori {
+ public class BookmarksDatabase : Midori.Database {
+ public BookmarksDatabase () throws DatabaseError {
+ Object (path: "bookmarks.db");
+ preinit ();
+ init ();
+ exec ("PRAGMA foreign_keys = ON;");
+ }
+
+ protected void preinit () throws DatabaseError {
+ string dbfile = Paths.get_config_filename_for_writing (path);
+ string olddbfile = dbfile + ".old";
+ string dbfile_v2 = Paths.get_config_filename_for_reading ("bookmarks_v2.db");
+
+ if (Posix.access (dbfile_v2, Posix.F_OK) == 0) {
+ if (Posix.access (dbfile, Posix.F_OK) == 0) {
+ if (Posix.access (olddbfile, Posix.F_OK) == 0)
+ Posix.unlink (olddbfile);
+ GLib.FileUtils.rename (dbfile, olddbfile);
+ }
+
+ GLib.FileUtils.rename (dbfile_v2, dbfile);
+
+ if (Sqlite.Database.open_v2 (dbfile, out _db) != Sqlite.OK)
+ throw new DatabaseError.OPEN ("Failed to open database %s".printf (path));
+
+ Sqlite.Statement stmt;
+ if (db.prepare_v2 ("PRAGMA user_version;", -1, out stmt, null) != Sqlite.OK)
+ throw new DatabaseError.EXECUTE ("Failed to compile statement %s".printf (db.errmsg ()));
+ if (stmt.step () != Sqlite.ROW)
+ throw new DatabaseError.EXECUTE ("Failed to get row %s".printf (db.errmsg ()));
+ int64 user_version = stmt.column_int64 (0);
+
+ if (user_version == 0) {
+ exec ("PRAGMA user_version = 1;");
+ }
+
+ _db = null;
+ } else if (Posix.access (dbfile, Posix.F_OK) == 0) {
+
+ if (Sqlite.Database.open_v2 (dbfile, out _db) != Sqlite.OK)
+ throw new DatabaseError.OPEN ("Failed to open database %s".printf (path));
+
+ Sqlite.Statement stmt;
+ if (db.prepare_v2 ("PRAGMA user_version;", -1, out stmt, null) != Sqlite.OK)
+ throw new DatabaseError.EXECUTE ("Failed to compile statement %s".printf (db.errmsg ()));
+ if (stmt.step () != Sqlite.ROW)
+ throw new DatabaseError.EXECUTE ("Failed to get row %s".printf (db.errmsg ()));
+ int64 user_version = stmt.column_int64 (0);
+
+ _db = null;
+
+ if (user_version == 0) {
+ if (Posix.access (olddbfile, Posix.F_OK) == 0)
+ Posix.unlink (olddbfile);
+
+ GLib.FileUtils.rename (dbfile, olddbfile);
+
+ if (Sqlite.Database.open_v2 (dbfile, out _db) != Sqlite.OK)
+ throw new DatabaseError.OPEN ("Failed to open database %s".printf (path));
+
+ exec_script ("Create");
+
+ if (db.exec ("ATTACH DATABASE '%s' AS old_db;".printf (olddbfile)) != Sqlite.OK)
+ throw new DatabaseError.EXECUTE ("Failed to attach old database : %s (%s)".printf (olddbfile, db.errmsg ()));
+
+ bool failure = false;
+ try {
+ exec_script ("Import_old_db_bookmarks");
+ } catch (DatabaseError error) {
+ if (error is DatabaseError.EXECUTE)
+ failure = true;
+ else
+ throw error;
+ }
+
+ /* try to get back to previous state */
+ if (failure)
+ exec ("ROLLBACK TRANSACTION;");
+
+ exec ("DETACH DATABASE old_db;");
+ exec ("PRAGMA user_version = 1;");
+
+ _db = null;
+ }
+ }
+ }
+ }
+}
diff --git a/midori/midori-historydatabase.vala b/midori/midori-historydatabase.vala
index 825f7e49..aeb630a7 100644
--- a/midori/midori-historydatabase.vala
+++ b/midori/midori-historydatabase.vala
@@ -39,8 +39,8 @@ namespace Midori {
public HistoryDatabase (GLib.Object? app) throws DatabaseError {
Object (path: "history.db");
init ();
- string bookmarks_filename = Midori.Paths.get_config_filename_for_writing ("bookmarks_v2.db");
- exec ("ATTACH DATABASE '%s' AS bookmarks".printf (bookmarks_filename));
+ Midori.BookmarksDatabase bookmarks_database = new Midori.BookmarksDatabase ();
+ exec ("ATTACH DATABASE '%s' AS bookmarks".printf (bookmarks_database.path));
try {
exec ("SELECT day FROM history LIMIT 1");