~midori/midori/gtkdoc

« back to all changes in this revision

Viewing changes to midori/midori-database.vala

  • Committer: Tarmac
  • Author(s): Christian Dywan, André Stösel
  • Date: 2013-09-18 16:20:29 UTC
  • mfrom: (6402.2.4 dbupdate)
  • Revision ID: tarmac-20130918162029-s5ue5homp1q4sqjh
Refactor excuting schema from file into a function

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
namespace Midori {
13
13
    public errordomain DatabaseError {
14
14
        OPEN,
15
 
        SCHEMA,
 
15
        NAMING,
 
16
        FILENAME,
16
17
        EXECUTE,
17
18
    }
18
19
 
48
49
 
49
50
            if (db.exec ("PRAGMA journal_mode = WAL; PRAGMA cache_size = 32100;") != Sqlite.OK)
50
51
                db.exec ("PRAGMA synchronous = NORMAL; PRAGMA temp_store = MEMORY;");
51
 
 
 
52
            db.exec ("PRAGMA count_changes = OFF;");
 
53
 
 
54
            int64 version, user_version;
 
55
            Sqlite.Statement stmt;
 
56
            if (db.prepare_v2 ("PRAGMA user_version;", -1, out stmt, null) != Sqlite.OK)
 
57
                throw new DatabaseError.EXECUTE ("Failed to compile statement %s".printf (db.errmsg ()));
 
58
            if (stmt.step () != Sqlite.ROW)
 
59
                throw new DatabaseError.EXECUTE ("Failed to get row %s".printf (db.errmsg ()));
 
60
            version = user_version = stmt.column_int64 (0);
 
61
 
 
62
            if (version == 0) {
 
63
                exec_script ("Create");
 
64
                user_version = version = 1;
 
65
                exec ("PRAGMA user_version = " + user_version.to_string ());
 
66
            }
 
67
 
 
68
            while (true) {
 
69
                int64 new_version = version + 1;
 
70
                try {
 
71
                    exec_script ("Update" + new_version.to_string ());
 
72
                } catch (DatabaseError error) {
 
73
                    if (error is DatabaseError.FILENAME)
 
74
                        break;
 
75
                    throw error;
 
76
                }
 
77
                user_version = new_version;
 
78
                exec ("PRAGMA user_version = " + user_version.to_string ());
 
79
            }
 
80
 
 
81
            first_use = !exists;
 
82
            return true;
 
83
        }
 
84
 
 
85
        public bool exec_script (string filename) throws DatabaseError {
52
86
            string basename = Path.get_basename (path);
53
87
            string[] parts = basename.split (".");
54
88
            if (!(parts != null && parts[0] != null && parts[1] != null))
55
 
                throw new DatabaseError.SCHEMA ("Failed to deduce schema filename from %s".printf (path));
56
 
            string schema_filename = Midori.Paths.get_res_filename (parts[0] + "/Create.sql");
 
89
                throw new DatabaseError.NAMING ("Failed to deduce schema filename from %s".printf (path));
 
90
            string schema_filename = Midori.Paths.get_res_filename (parts[0] + "/" + filename + ".sql");
57
91
            string schema;
58
92
            try {
59
93
                FileUtils.get_contents (schema_filename, out schema, null);
60
94
            } catch (Error error) {
61
 
                throw new DatabaseError.SCHEMA ("Failed to open schema: %s".printf (schema_filename));
 
95
                throw new DatabaseError.FILENAME ("Failed to open schema: %s".printf (schema_filename));
62
96
            }
 
97
            schema = "BEGIN TRANSACTION; %s; COMMIT;".printf (schema);
63
98
            if (db.exec (schema) != Sqlite.OK)
64
99
                throw new DatabaseError.EXECUTE ("Failed to execute schema: %s".printf (schema));
65
 
 
66
 
            first_use = !exists;
67
100
            return true;
68
101
        }
69
102