diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-07-23 09:06:59 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-07-23 09:06:59 +0200 |
commit | 4ea2cc3bd4a7d9b1c54a9d33e6a1cf82e7c8c21d (patch) | |
tree | d2e54377d14d604356c86862a326f64ae64dadd6 /src/db/VersionTable.vala |
Imported Upstream version 0.18.1upstream/0.18.1
Diffstat (limited to 'src/db/VersionTable.vala')
-rw-r--r-- | src/db/VersionTable.vala | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/db/VersionTable.vala b/src/db/VersionTable.vala new file mode 100644 index 0000000..9003b1b --- /dev/null +++ b/src/db/VersionTable.vala @@ -0,0 +1,98 @@ +/* Copyright 2011-2014 Yorba Foundation + * + * This software is licensed under the GNU Lesser General Public License + * (version 2.1 or later). See the COPYING file in this distribution. + */ + +public class VersionTable : DatabaseTable { + private static VersionTable instance = null; + + private VersionTable() { + Sqlite.Statement stmt; + int res = db.prepare_v2("CREATE TABLE IF NOT EXISTS VersionTable (" + + "id INTEGER PRIMARY KEY, " + + "schema_version INTEGER, " + + "app_version TEXT, " + + "user_data TEXT NULL" + + ")", -1, out stmt); + assert(res == Sqlite.OK); + + res = stmt.step(); + if (res != Sqlite.DONE) + fatal("create version table", res); + + set_table_name("VersionTable"); + } + + public static VersionTable get_instance() { + if (instance == null) + instance = new VersionTable(); + + return instance; + } + + public int get_version(out string app_version) { + Sqlite.Statement stmt; + int res = db.prepare_v2("SELECT schema_version, app_version FROM VersionTable ORDER BY schema_version DESC LIMIT 1", + -1, out stmt); + assert(res == Sqlite.OK); + + res = stmt.step(); + if (res != Sqlite.ROW) { + if (res != Sqlite.DONE) + fatal("get_version", res); + + app_version = null; + + return -1; + } + + app_version = stmt.column_text(1); + + return stmt.column_int(0); + } + + public void set_version(int version, string app_version, string? user_data = null) { + Sqlite.Statement stmt; + + string bitbucket; + if (get_version(out bitbucket) != -1) { + // overwrite existing row + int res = db.prepare_v2("UPDATE VersionTable SET schema_version=?, app_version=?, user_data=?", + -1, out stmt); + assert(res == Sqlite.OK); + } else { + // insert new row + int res = db.prepare_v2("INSERT INTO VersionTable (schema_version, app_version, user_data) VALUES (?,?, ?)", + -1, out stmt); + assert(res == Sqlite.OK); + } + + int res = stmt.bind_int(1, version); + assert(res == Sqlite.OK); + res = stmt.bind_text(2, app_version); + assert(res == Sqlite.OK); + res = stmt.bind_text(3, user_data); + assert(res == Sqlite.OK); + + res = stmt.step(); + if (res != Sqlite.DONE) + fatal("set_version %d %s %s".printf(version, app_version, user_data), res); + } + + public void update_version(int version, string app_version) { + Sqlite.Statement stmt; + int res = db.prepare_v2("UPDATE VersionTable SET schema_version=?, app_version=?", -1, out stmt); + assert(res == Sqlite.OK); + + res = stmt.bind_int(1, version); + assert(res == Sqlite.OK); + res = stmt.bind_text(2, app_version); + assert(res == Sqlite.OK); + + res = stmt.step(); + if (res != Sqlite.DONE) + fatal("update_version %d".printf(version), res); + } +} + |