Catalog plugin: add Date column to Watched table
authorPhilipp Zabel <philipp.zabel@gmail.com>
Thu, 8 Jul 2010 19:25:54 +0000 (21:25 +0200)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Wed, 14 Jul 2010 21:34:08 +0000 (23:34 +0200)
src/plugins/catalog-sqlite.vala

index 151fc1c..b8d5105 100644 (file)
@@ -128,13 +128,41 @@ class CatalogSqlite : Object {
        }
 
        private int prepare () {
+               Statement stmt;
                int rc;
 
-               rc = db.exec ("CREATE TABLE IF NOT EXISTS Collection (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); CREATE TABLE IF NOT EXISTS Loaned (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); CREATE TABLE IF NOT EXISTS Watched (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); CREATE TABLE IF NOT EXISTS Watchlist (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0);", callback, null);
+               rc = db.exec ("CREATE TABLE IF NOT EXISTS Collection (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); " +
+                             "CREATE TABLE IF NOT EXISTS Loaned (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); " +
+                             "CREATE TABLE IF NOT EXISTS Watched (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0, Date INTEGER); " +
+                             "CREATE TABLE IF NOT EXISTS Watchlist (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0);",
+                             callback, null);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       return 1;
+               }
+
+               // Add a date column to the Watched table
+               rc = db.prepare_v2 ("SELECT sql FROM sqlite_master WHERE (type = 'table' AND name = 'Watched');",
+                                   -1, out stmt);
                if (rc != Sqlite.OK) {
                        stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
                        return 1;
                }
+               do {
+                       rc = stmt.step ();
+                       if (rc == Sqlite.ROW) {
+                               var sql = stmt.column_text (0);
+                               if (sql == "CREATE TABLE Watched (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0)") {
+                                       rc = db.exec ("ALTER TABLE Watched ADD COLUMN Date INTEGER;",
+                                                     callback, null);
+                                       if (rc != Sqlite.OK) {
+                                               stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                                               return 1;
+                                       }
+                               }
+                               break;
+                       }
+               } while (rc == Sqlite.ROW);
 
                return 0;
        }