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 {
26 public delegate void ReceiveMovieFunction (string title, int year, int rating, int genres);
28 public CatalogSqlite (string filename) {
31 rc = Database.open (filename, out db);
32 if (rc != Sqlite.OK) {
33 stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ());
37 rc = db.exec ("PRAGMA locking_mode = EXCLUSIVE;", callback, null);
38 if (rc != Sqlite.OK) {
39 stderr.printf ("Can't get exclusive lock: %d, %s\n", rc, db.errmsg ());
43 rc = db.exec ("PRAGMA synchronous = OFF;", callback, null);
45 stderr.printf ("Can't turn off synchronous access: %d, %s\n", rc, db.errmsg ());
50 public static int callback (int n_columns, string[] values,
51 string[] column_names) {
52 for (int i = 0; i < n_columns; i++) {
53 stdout.printf ("%s = %s\n", column_names[i], values[i]);
60 public int add_movie (string table, Movie movie) {
61 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);
64 rc = db.exec (sql, callback, null);
65 if (rc != Sqlite.OK) {
66 stderr.printf ("Failed to insert movie \"%s\" (%d): %d, %s\n", movie.title, movie.year, rc, db.errmsg ());
73 public int delete_movie (string table, Movie movie) {
74 string sql = "DELETE FROM %s WHERE Title=\"%s\" AND Year=%d".printf (table, movie.title, movie.year);
77 rc = db.exec (sql, callback, null);
78 if (rc != Sqlite.OK) {
79 stderr.printf ("Failed to delete movie \"%s\" (%d): %d, %s\n", movie.title, movie.year, rc, db.errmsg ());
86 public int count (string table) {
87 string sql = "SELECT count(*) FROM %s".printf (table);
92 rc = db.prepare_v2 (sql, -1, out stmt);
93 if (rc != Sqlite.OK) {
94 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
95 db.progress_handler (0, null);
101 if (rc == Sqlite.ROW) {
102 count = stmt.column_int (0);
104 } while (rc == Sqlite.ROW);
109 public bool contains (string table, Movie movie) {
110 string sql = "SELECT count(*) FROM %s WHERE Title=\"%s\" AND Year=%d".printf (table, movie.title, movie.year);
115 rc = db.prepare_v2 (sql, -1, out stmt);
116 if (rc != Sqlite.OK) {
117 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
118 db.progress_handler (0, null);
124 if (rc == Sqlite.ROW) {
125 count = stmt.column_int (0);
127 } while (rc == Sqlite.ROW);
132 private int prepare () {
136 rc = db.exec ("CREATE TABLE IF NOT EXISTS Collection (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); " +
137 "CREATE TABLE IF NOT EXISTS Loaned (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0); " +
138 "CREATE TABLE IF NOT EXISTS Watched (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0, Date INTEGER); " +
139 "CREATE TABLE IF NOT EXISTS Watchlist (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0);",
141 if (rc != Sqlite.OK) {
142 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
146 // Add a date column to the Watched table
147 rc = db.prepare_v2 ("SELECT sql FROM sqlite_master WHERE (type = 'table' AND name = 'Watched');",
149 if (rc != Sqlite.OK) {
150 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
155 if (rc == Sqlite.ROW) {
156 var sql = stmt.column_text (0);
157 if (sql == "CREATE TABLE Watched (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Genres INTEGER NOT NULL DEFAULT 0)") {
158 rc = db.exec ("ALTER TABLE Watched ADD COLUMN Date INTEGER;",
160 if (rc != Sqlite.OK) {
161 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
167 } while (rc == Sqlite.ROW);
172 public int clear () {
175 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);
176 if (rc != Sqlite.OK) {
177 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
184 private Cancellable? _cancellable;
185 public async int query (string table, MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
186 var sql = new StringBuilder ();
187 sql.append ("SELECT Title, Year, Rating, Genres");
188 if (table == "Watched")
189 sql.append (", Date");
190 sql.append (" FROM ");
196 // FIXME - how many opcodes until main loop iteration for best responsivity?
197 _cancellable = cancellable;
198 db.progress_handler (1000, progress_handler);
200 if (filter.title != null && filter.title != "") {
201 if ("*" in filter.title) {
203 sql.append_printf ("Title GLOB \"%s\"", filter.title);
206 sql.append_printf ("Title LIKE \"%s%%\"", filter.title);
210 if (filter.year_min > 0) {
212 sql.append_printf ("Year >= %d", filter.year_min);
215 if (filter.year_max > 0) {
217 sql.append_printf ("Year <= %d", filter.year_max);
220 if (filter.rating_min > 0) {
222 sql.append_printf ("Rating >= %d", filter.rating_min);
225 if (filter.genres.field != 0) {
227 sql.append_printf ("Genres&%d = %d", filter.genres.field, filter.genres.field);
229 sql.append_printf (" LIMIT %d;", limit);
231 stdout.printf("SQL: \"%s\"\n", sql.str);
233 rc = db.prepare_v2 (sql.str, -1, out stmt);
234 if (rc != Sqlite.OK) {
235 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
236 db.progress_handler (0, null);
240 result = new SList<Movie> ();
244 Idle.add (query.callback);
247 if (rc == Sqlite.ROW) {
248 var movie = new Movie ();
249 movie.year = stmt.column_int (1);
250 movie.title = stmt.column_text (0);
251 movie.rating = stmt.column_int (2);
252 movie.genres.field = stmt.column_int (3);
253 // TODO - depending on settings, this could be something else, like director info or runtime
254 movie.secondary = movie.genres.to_string ();
255 result.append (movie);
256 if (++results_waiting >= 10) {
258 result = new SList<Movie> ();
262 } while (rc == Sqlite.ROW);
264 if (results_waiting > 0)
266 result = new SList<Movie> ();
268 db.progress_handler (0, null);
272 private int progress_handler () {
273 ((MainContext) null).iteration (false);
274 return (int) _cancellable.is_cancelled ();