Add IMDb SQLite storage class
[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 IMDbSqlite (string filename) {
26                 int rc;
27
28                 genres = new List<string> ();
29
30                 rc = Database.open (filename, out db);
31                 if (rc != Sqlite.OK) {
32                         stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ());
33                         return;
34                 }
35
36                 rc = db.exec ("PRAGMA journal_mode = OFF;", callback, null);
37                 if (rc != Sqlite.OK) {
38                         stderr.printf ("Can't turn off journal mode: %d, %s\n", rc, db.errmsg ());
39                         return;
40                 }
41
42                 rc = db.exec ("PRAGMA locking_mode = EXCLUSIVE;", callback, null);
43                 if (rc != Sqlite.OK) {
44                         stderr.printf ("Can't get exclusive lock: %d, %s\n", rc, db.errmsg ());
45                         return;
46                 }
47
48                 rc = db.exec ("PRAGMA synchronous = OFF;", callback, null);
49                 if (rc != Sqlite.OK)
50                         stderr.printf ("Can't turn off synchronous access: %d, %s\n", rc, db.errmsg ());
51
52         }
53
54         public static int callback (int n_columns, string[] values,
55                                     string[] column_names) {
56                 for (int i = 0; i < n_columns; i++) {
57                         stdout.printf ("%s = %s\n", column_names[i], values[i]);
58                 }
59                 stdout.printf ("\n");
60
61                 return 0;
62         }
63
64         public int add_movie (string _title, int year) {
65                 string md5 = Checksum.compute_for_string (ChecksumType.MD5, _title);
66                 uint64 half_md5 = (md5.ndup (16)).to_uint64 (null, 16);
67
68                 string title = strip_year (_title, year);
69
70                 string sql = "INSERT INTO Movies(ID, Title, Year) VALUES (%lld, \"%s\", %d);".printf ((int64) half_md5, title, year);
71                 int rc;
72
73                 rc = db.exec (sql, callback, null);
74                 if (rc != Sqlite.OK) {
75                         stderr.printf ("Failed to insert movie \"%s\" (%d): %d, %s\n", title, year, rc, db.errmsg ());
76                         return 1;
77                 }
78
79                 return 0;
80         }
81
82         private string strip_year (string title, int year) {
83                 string year_suffix = " (%d)".printf (year);
84                 if (title.has_suffix (year_suffix)) {
85                         return title.substring (0, title.length - year_suffix.length);
86                 } else {
87                         return title.dup ();
88                 }
89         }
90
91         public int movie_set_rating (string title, int rating) {
92                 string md5 = Checksum.compute_for_string (ChecksumType.MD5, title);
93                 uint64 half_md5 = (md5.ndup (16)).to_uint64 (null, 16);
94
95                 var sql = "UPDATE Movies SET Rating=%d WHERE ID=%lld;".printf (rating, (int64) half_md5);
96                 int rc;
97
98                 rc = db.exec (sql, callback, null);
99                 if (rc != Sqlite.OK) {
100                         stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
101                         return 1;
102                 }
103
104                 return 0;
105         }
106
107         public int movie_add_genre (string title, string genre) {
108                 string md5 = Checksum.compute_for_string (ChecksumType.MD5, title);
109                 uint64 half_md5 = (md5.ndup (16)).to_uint64 (null, 16);
110                 string sql;
111                 int bit;
112                 int rc;
113
114                 bit = genre_bit (genre);
115                 if (bit == 0) {
116                         genres.append (genre);
117                         bit = genre_bit (genre);
118
119                         sql = "INSERT INTO Genres(Bit, Genre) VALUES (%d, \"%s\");".printf (bit, genre);
120
121                         rc = db.exec (sql, callback, null);
122                         if (rc != Sqlite.OK) {
123                                 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
124                                 return 1;
125                         }
126                 }
127
128                 sql = "UPDATE Movies SET Genres=Genres|%d WHERE ID=%lld;".printf (bit, (int64) half_md5);
129                 rc = db.exec (sql, callback, null);
130                 if (rc != Sqlite.OK) {
131                         stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
132                         return 1;
133                 }
134
135                 return 0;
136         }
137
138         int genre_bit (string genre) {
139                 for (int i = 0; i < genres.length (); i++) {
140                         if (genres.nth_data (i) == genre)
141                                 return 1 << i;
142                 }
143                 return 0;
144         }
145
146         public int clear () {
147                 int rc;
148
149                 rc = db.exec ("DROP TABLE IF EXISTS Movies; CREATE TABLE Movies (ID INTEGER PRIMARY KEY, Title TEXT NOT NULL, Year INTEGER, Rating 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);
150                 if (rc != Sqlite.OK) {
151                         stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
152                         return 1;
153                 }
154
155                 return 0;
156         }
157 }