1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
5 * Cinaest is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Cinaest is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
21 class CatalogSqlite : Object {
24 public delegate void ReceiveMovieFunction (string title, int year, int rating, int genres);
26 public CatalogSqlite (string filename) {
29 rc = Database.open (filename, out db);
30 if (rc != Sqlite.OK) {
31 stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ());
35 rc = db.exec ("PRAGMA locking_mode = EXCLUSIVE;", callback, null);
36 if (rc != Sqlite.OK) {
37 stderr.printf ("Can't get exclusive lock: %d, %s\n", rc, db.errmsg ());
41 rc = db.exec ("PRAGMA synchronous = OFF;", callback, null);
43 stderr.printf ("Can't turn off synchronous access: %d, %s\n", rc, db.errmsg ());
48 public static int callback (int n_columns, string[] values,
49 string[] column_names) {
50 for (int i = 0; i < n_columns; i++) {
51 stdout.printf ("%s = %s\n", column_names[i], values[i]);
58 public int add_movie (string table, Movie movie) {
59 string sql = "INSERT INTO %s(Title, Year, Rating, Genres) VALUES (\"%s\", %d, %d, %d);".printf (table, movie.title, movie.year, movie.rating, movie.genres.field);
62 rc = db.exec (sql, callback, null);
63 if (rc != Sqlite.OK) {
64 stderr.printf ("Failed to insert movie \"%s\" (%d): %d, %s\n", movie.title, movie.year, rc, db.errmsg ());
71 public int delete_movie (string table, Movie movie) {
72 string sql = "DELETE FROM %s WHERE Title=\"%s\" AND Year=%d".printf (table, movie.title, movie.year);
75 rc = db.exec (sql, callback, null);
76 if (rc != Sqlite.OK) {
77 stderr.printf ("Failed to delete movie \"%s\" (%d): %d, %s\n", movie.title, movie.year, rc, db.errmsg ());
84 public int count (string table) {
85 string sql = "SELECT count(*) FROM %s".printf (table);
90 rc = db.prepare_v2 (sql, -1, out stmt);
91 if (rc != Sqlite.OK) {
92 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
93 db.progress_handler (0, null);
99 if (rc == Sqlite.ROW) {
100 count = stmt.column_int (0);
102 } while (rc == Sqlite.ROW);
107 public bool contains (string table, Movie movie) {
108 string sql = "SELECT count(*) FROM %s WHERE Title=\"%s\" AND Year=%d".printf (table, movie.title, movie.year);
113 rc = db.prepare_v2 (sql, -1, out stmt);
114 if (rc != Sqlite.OK) {
115 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
116 db.progress_handler (0, null);
122 if (rc == Sqlite.ROW) {
123 count = stmt.column_int (0);
125 } while (rc == Sqlite.ROW);
130 private int prepare () {
134 rc = db.exec ("CREATE TABLE IF NOT EXISTS Collection (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); " +
135 "CREATE TABLE IF NOT EXISTS Loaned (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); " +
136 "CREATE TABLE IF NOT EXISTS Watched (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0, Date INTEGER); " +
137 "CREATE TABLE IF NOT EXISTS Watchlist (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0);",
139 if (rc != Sqlite.OK) {
140 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
144 // Add a date column to the Watched table
145 rc = db.prepare_v2 ("SELECT sql FROM sqlite_master WHERE (type = 'table' AND name = 'Watched');",
147 if (rc != Sqlite.OK) {
148 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
153 if (rc == Sqlite.ROW) {
154 var sql = stmt.column_text (0);
155 if (sql == "CREATE TABLE Watched (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0)") {
156 rc = db.exec ("ALTER TABLE Watched ADD COLUMN Date INTEGER;",
158 if (rc != Sqlite.OK) {
159 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
165 } while (rc == Sqlite.ROW);
170 public int clear () {
173 rc = db.exec ("DROP TABLE IF EXISTS Collection; CREATE TABLE Collection (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); DROP TABLE IF EXISTS Loaned; CREATE TABLE Loaned (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); DROP TABLE IF EXISTS Watchlist; CREATE TABLE Watchlist (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0);", callback, null);
174 if (rc != Sqlite.OK) {
175 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
182 private Cancellable? _cancellable;
183 public async int query (string table, MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
184 var sql = new StringBuilder ();
185 sql.append ("SELECT Title, Year, Rating, Genres");
186 if (table == "Watched")
187 sql.append (", Date");
188 sql.append (" FROM ");
194 // FIXME - how many opcodes until main loop iteration for best responsivity?
195 _cancellable = cancellable;
196 db.progress_handler (1000, progress_handler);
198 if (filter.title != null && filter.title != "") {
199 if ("*" in filter.title) {
201 sql.append_printf ("Title GLOB \"%s\"", filter.title);
204 sql.append_printf ("Title LIKE \"%s%%\"", filter.title);
208 if (filter.year_min > 0) {
210 sql.append_printf ("Year >= %d", filter.year_min);
213 if (filter.year_max > 0) {
215 sql.append_printf ("Year <= %d", filter.year_max);
218 if (filter.rating_min > 0) {
220 sql.append_printf ("Rating >= %d", filter.rating_min);
223 if (filter.genres.field != 0) {
225 sql.append_printf ("Genres&%d = %d", filter.genres.field, filter.genres.field);
227 sql.append_printf (" LIMIT %d;", limit);
229 stdout.printf("SQL: \"%s\"\n", sql.str);
231 rc = db.prepare_v2 (sql.str, -1, out stmt);
232 if (rc != Sqlite.OK) {
233 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
234 db.progress_handler (0, null);
239 Idle.add (query.callback);
242 if (rc == Sqlite.ROW) {
243 var movie = new Movie ();
244 movie.year = stmt.column_int (1);
245 movie.title = stmt.column_text (0);
246 movie.rating = stmt.column_int (2);
247 movie.genres.field = stmt.column_int (3);
248 // TODO - depending on settings, this could be something else, like director info or runtime
249 movie.secondary = movie.genres.to_string ();
252 } while (rc == Sqlite.ROW);
254 db.progress_handler (0, null);
258 private int progress_handler () {
259 ((MainContext) null).iteration (false);
260 return (int) _cancellable.is_cancelled ();