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 IMDbSqlite : Object {
25 public IMDbSqlite (string filename) {
28 genres = new List<string> ();
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 ());
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 ());
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 ());
48 rc = db.exec ("PRAGMA synchronous = OFF;", callback, null);
50 stderr.printf ("Can't turn off synchronous access: %d, %s\n", rc, db.errmsg ());
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]);
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);
68 string title = strip_year (_title, year);
70 string sql = "INSERT INTO Movies(ID, Title, Year) VALUES (%lld, \"%s\", %d);".printf ((int64) half_md5, title, year);
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 ());
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);
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);
95 var sql = "UPDATE Movies SET Rating=%d WHERE ID=%lld;".printf (rating, (int64) half_md5);
98 rc = db.exec (sql, callback, null);
99 if (rc != Sqlite.OK) {
100 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
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);
114 bit = genre_bit (genre);
116 genres.append (genre);
117 bit = genre_bit (genre);
119 sql = "INSERT INTO Genres(Bit, Genre) VALUES (%d, \"%s\");".printf (bit, genre);
121 rc = db.exec (sql, callback, null);
122 if (rc != Sqlite.OK) {
123 stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
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 ());
138 int genre_bit (string genre) {
139 for (int i = 0; i < genres.length (); i++) {
140 if (genres.nth_data (i) == genre)
146 public int clear () {
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 ());