IMDb: Use SQLite indices instead of (half) MD5 hashes for lookup
[cinaest] / src / imdb / imdb-sqlite.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2009 Philipp Zabel
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 using Sqlite;
20
21 class IMDbSqlite : Object {
22         Database db;
23         List<string> genres;
24
25         public delegate void ReceiveMovieFunction (string title, int year, int rating, int genres);
26
27         public IMDbSqlite (string filename) {
28                 int rc;
29
30                 genres = new List<string> ();
31
32                 rc = Database.open (filename, out db);
33                 if (rc != Sqlite.OK) {
34                         stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ());
35                         return;
36                 }
37
38                 rc = db.exec ("PRAGMA journal_mode = OFF;", callback, null);
39                 if (rc != Sqlite.OK) {
40                         stderr.printf ("Can't turn off journal mode: %d, %s\n", rc, db.errmsg ());
41                         return;
42                 }
43
44                 rc = db.exec ("PRAGMA locking_mode = EXCLUSIVE;", callback, null);
45                 if (rc != Sqlite.OK) {
46                         stderr.printf ("Can't get exclusive lock: %d, %s\n", rc, db.errmsg ());
47                         return;
48                 }
49
50                 rc = db.exec ("PRAGMA synchronous = OFF;", callback, null);
51                 if (rc != Sqlite.OK)
52                         stderr.printf ("Can't turn off synchronous access: %d, %s\n", rc, db.errmsg ());
53
54         }
55
56         public static int callback (int n_columns, string[] values,
57                                     string[] column_names) {
58                 for (int i = 0; i < n_columns; i++) {
59                         stdout.printf ("%s = %s\n", column_names[i], values[i]);
60                 }
61                 stdout.printf ("\n");
62
63                 return 0;
64         }
65
66         public int add_movie (string title, int year) {
67                 string sql = "INSERT INTO Movies(Title, Year) VALUES (\"%s\", %d);".printf (title, year);
68                 int rc;
69
70                 rc = db.exec (sql, callback, null);
71                 if (rc != Sqlite.OK) {
72                         stderr.printf ("Failed to insert movie \"%s\" (%d): %d, %s\n", title, year, rc, db.errmsg ());
73                         return 1;
74                 }
75
76                 return 0;
77         }
78
79         public int movie_set_rating (string title, int rating, int votes) {
80                 var sql = "UPDATE Movies SET Rating=%d, Votes=%d WHERE Title=\"%s\";".printf (rating, votes, title);
81                 int rc;
82
83                 rc = db.exec (sql, callback, null);
84                 if (rc != Sqlite.OK) {
85                         stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
86                         return 1;
87                 }
88
89                 return 0;
90         }
91
92         public int movie_add_genre (string title, string genre) {
93                 string sql;
94                 int bit;
95                 int rc;
96
97                 bit = genre_bit (genre);
98                 if (bit == 0) {
99                         genres.append (genre);
100                         bit = genre_bit (genre);
101
102                         sql = "INSERT INTO Genres(Bit, Genre) VALUES (%d, \"%s\");".printf (bit, genre);
103
104                         rc = db.exec (sql, callback, null);
105                         if (rc != Sqlite.OK) {
106                                 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
107                                 return 1;
108                         }
109                 }
110
111                 sql = "UPDATE Movies SET Genres=Genres|%d WHERE Title=\"%s\";".printf (bit, title);
112                 rc = db.exec (sql, callback, null);
113                 if (rc != Sqlite.OK) {
114                         stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
115                         return 1;
116                 }
117
118                 return 0;
119         }
120
121         int genre_bit (string genre) {
122                 for (int i = 0; i < genres.length (); i++) {
123                         if (genres.nth_data (i) == genre)
124                                 return 1 << i;
125                 }
126                 return 0;
127         }
128
129         public int clear () {
130                 int rc;
131
132                 rc = db.exec ("DROP TABLE IF EXISTS Movies; CREATE TABLE Movies (Title TEXT NOT NULL, Year INTEGER, Rating INTEGER, Votes INTEGER, Genres INTEGER NOT NULL DEFAULT 0); DROP TABLE IF EXISTS Genres; CREATE TABLE Genres (Bit INTEGER PRIMARY KEY, Genre TEXT NOT NULL);", callback, null);
133                 if (rc != Sqlite.OK) {
134                         stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
135                         return 1;
136                 }
137
138                 return 0;
139         }
140
141         public int create_title_index () {
142                 int rc;
143
144                 rc = db.exec ("CREATE INDEX MovieTitles ON Movies(Title);", callback, null);
145                 if (rc != Sqlite.OK) {
146                         stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
147                         return 1;
148                 }
149
150                 return 0;
151         }
152
153         public int create_votes_index () {
154                 int rc;
155
156                 rc = db.exec ("CREATE INDEX MovieVotes ON Movies(Votes);", callback, null);
157                 if (rc != Sqlite.OK) {
158                         stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
159                         return 1;
160                 }
161
162                 return 0;
163         }
164
165         public int query (MovieFilter filter, ReceiveMovieFunction receive_movie) {
166                 var sql = "SELECT Title, Year, Rating, Genres FROM Movies";
167                 var sep = " WHERE ";
168                 Statement stmt;
169                 int rc;
170
171                 if (filter.title != null && filter.title != "") {
172                         if ("*" in filter.title)
173                                 sql += sep + "Title GLOB \"%s (*)\"".printf (filter.title);
174                         else
175                                 sql += sep + "Title LIKE \"%s%%\"".printf (filter.title);
176                         sep = " AND ";
177                 }
178                 if (filter.year_min > 0) {
179                         sql += sep + "Year >= %d".printf (filter.year_min);
180                         sep = " AND ";
181                 }
182                 if (filter.year_max > 0) {
183                         sql += sep + "Year <= %d".printf (filter.year_max);
184                         sep = " AND ";
185                 }
186                 if (filter.rating_min > 0) {
187                         sql += sep + "Rating >= = %d".printf (filter.rating_min);
188                         sep = " AND ";
189                 }
190                 if (filter.genres.field != 0) {
191                         sql += sep + "Genres&%d = %d".printf (filter.genres.field, filter.genres.field);
192                 }
193                 sql += " ORDER BY Votes DESC LIMIT %d;".printf (100);
194
195                 stdout.printf("SQL: \"%s\"\n", sql);
196
197                 rc = db.prepare_v2 (sql, -1, out stmt);
198                 if (rc != Sqlite.OK) {
199                         stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
200                         return 1;
201                 }
202
203                 do {
204                         rc = stmt.step ();
205                         if (rc == Sqlite.ROW) {
206                                 int year = stmt.column_int (1);
207                                 string title = stmt.column_text (0);
208                                 int rating = stmt.column_int (2);
209                                 int genres = stmt.column_int (3);
210                                 receive_movie (strip_year (title, year), year, rating, genres);
211                         }
212                 } while (rc == Sqlite.ROW);
213
214                 return 0;
215         }
216
217         private string strip_year (string title, int year) {
218                 string year_suffix = " (%d)".printf (year);
219                 if (title.has_suffix (year_suffix))
220                         return title.substring (0, title.length - year_suffix.length);
221                 year_suffix = " (%d/I)".printf (year);
222                 if (title.has_suffix (year_suffix))
223                         return title.substring (0, title.length - year_suffix.length);
224                 year_suffix = " (%d/II)".printf (year);
225                 if (title.has_suffix (year_suffix))
226                         return title.substring (0, title.length - year_suffix.length);
227                 return title.dup ();
228         }
229 }