summaryrefslogtreecommitdiff
path: root/midori/midori-bookmarksdatabase.vala
blob: 95dec9b79298693e1959d49b290d2f61f2c3a8f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 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 (GLib.FileUtils.test (dbfile_v2, GLib.FileTest.EXISTS)) {
                if (GLib.FileUtils.test (dbfile, GLib.FileTest.EXISTS)) {
                    if (GLib.FileUtils.test (olddbfile, GLib.FileTest.EXISTS))
                        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 (GLib.FileUtils.test (dbfile, GLib.FileTest.EXISTS)) {

                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 (GLib.FileUtils.test (olddbfile, GLib.FileTest.EXISTS))
                        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");

                    attach (olddbfile, "old_db");
                    
                    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;
                }
            }
        }
    }
}