IMDb SQLite class: set rating to -1 to indicate a NULL value in the database
[cinaest] / src / imdb / imdb-sqlite.vala
index c8e6cbc..10065c0 100644 (file)
@@ -22,6 +22,8 @@ class IMDbSqlite : Object {
        Database db;
        List<string> genres;
 
+       public delegate void ReceiveMovieFunction (string title, string? aka, int year, int rating, int genres);
+
        public IMDbSqlite (string filename) {
                int rc;
 
@@ -61,13 +63,8 @@ class IMDbSqlite : Object {
                return 0;
        }
 
-       public int add_movie (string _title, int year) {
-               string md5 = Checksum.compute_for_string (ChecksumType.MD5, _title);
-               uint64 half_md5 = (md5.ndup (16)).to_uint64 (null, 16);
-
-               string title = strip_year (_title, year);
-
-               string sql = "INSERT INTO Movies(ID, Title, Year) VALUES (%lld, \"%s\", %d);".printf ((int64) half_md5, title, year);
+       public int add_movie (string title, int year) {
+               string sql = "INSERT INTO Movies(Title, Year) VALUES (\"%s\", %d);".printf (title, year);
                int rc;
 
                rc = db.exec (sql, callback, null);
@@ -79,20 +76,8 @@ class IMDbSqlite : Object {
                return 0;
        }
 
-       private string strip_year (string title, int year) {
-               string year_suffix = " (%d)".printf (year);
-               if (title.has_suffix (year_suffix)) {
-                       return title.substring (0, title.length - year_suffix.length);
-               } else {
-                       return title.dup ();
-               }
-       }
-
-       public int movie_set_rating (string title, int rating) {
-               string md5 = Checksum.compute_for_string (ChecksumType.MD5, title);
-               uint64 half_md5 = (md5.ndup (16)).to_uint64 (null, 16);
-
-               var sql = "UPDATE Movies SET Rating=%d WHERE ID=%lld;".printf (rating, (int64) half_md5);
+       public int movie_set_rating (string title, int rating, int votes) {
+               var sql = "UPDATE Movies SET Rating=%d, Votes=%d WHERE Title=\"%s\";".printf (rating, votes, title);
                int rc;
 
                rc = db.exec (sql, callback, null);
@@ -105,8 +90,6 @@ class IMDbSqlite : Object {
        }
 
        public int movie_add_genre (string title, string genre) {
-               string md5 = Checksum.compute_for_string (ChecksumType.MD5, title);
-               uint64 half_md5 = (md5.ndup (16)).to_uint64 (null, 16);
                string sql;
                int bit;
                int rc;
@@ -125,7 +108,7 @@ class IMDbSqlite : Object {
                        }
                }
 
-               sql = "UPDATE Movies SET Genres=Genres|%d WHERE ID=%lld;".printf (bit, (int64) half_md5);
+               sql = "UPDATE Movies SET Genres=Genres|%d WHERE Title=\"%s\";".printf (bit, title);
                rc = db.exec (sql, callback, null);
                if (rc != Sqlite.OK) {
                        stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
@@ -143,10 +126,145 @@ class IMDbSqlite : Object {
                return 0;
        }
 
+       public int add_aka (string title, string aka) {
+               int rowid;
+
+               if (!movie_exists (title, out rowid))
+                       return 1;
+
+               string sql = "INSERT INTO Akas(Aka, TitleID) VALUES (\"%s\", %d);".printf (aka, rowid);
+               int rc;
+               rc = db.exec (sql, callback, null);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       return 1;
+               }
+
+               return 0;
+       }
+
+       public int add_plot (string title, string plot, string? author) {
+               int rowid;
+
+               if (!movie_exists (title, out rowid))
+                       return 1;
+
+               string sql;
+               if (author != null) {
+                       sql = "INSERT INTO Plots(rowid, Plot, Author) VALUES (%d, \"%s\", \"%s\");".printf (rowid, plot.replace ("\"", "&quot;"), author.replace ("\"", "&quot;"));
+               } else {
+                       sql = "INSERT INTO Plots(rowid, Plot) VALUES (%d, \"%s\");".printf (rowid, plot.replace ("\"", "&quot;"));
+               }
+               int rc;
+               rc = db.exec (sql, callback, null);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       stderr.printf ("offending SQL: %s\n", sql);
+                       return 1;
+               }
+
+               return 0;
+       }
+
+       public bool movie_exists (string title, out int rowid = null) {
+               string sql = "SELECT rowid FROM Movies WHERE Title=\"%s\"".printf (title);
+               Statement stmt;
+               int rc;
+
+               rc = db.prepare_v2 (sql, -1, out stmt);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       return false;
+               }
+
+               do {
+                       rc = stmt.step ();
+                       if (rc == Sqlite.ROW) {
+                               if (&rowid != null) {
+                                       rowid = stmt.column_int (0);
+                               }
+                               return true;
+                       }
+               } while (rc == Sqlite.ROW);
+
+               return false;
+       }
+
+       public int count (string table) {
+               string sql = "SELECT count(*) FROM %s".printf (table);
+               Statement stmt;
+               int rc;
+               int count = 0;
+
+               rc = db.prepare_v2 (sql, -1, out stmt);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       db.progress_handler (0, null);
+                       return 0;
+               }
+
+               do {
+                       rc = stmt.step ();
+                       if (rc == Sqlite.ROW) {
+                               count = stmt.column_int (0);
+                       }
+               } while (rc == Sqlite.ROW);
+
+               return count;
+       }
+
+       public bool has_plots () {
+               return (count ("Plots") > 0);
+       }
+
+       public string get_plot (string title) {
+               string sql = "SELECT Plot FROM Plots WHERE rowid in (SELECT rowid FROM Movies WHERE Title=\"%s\")".printf (title);
+               Statement stmt;
+               int rc;
+               int count = 0;
+
+               rc = db.prepare_v2 (sql, -1, out stmt);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       db.progress_handler (0, null);
+                       return "";
+               }
+
+               do {
+                       rc = stmt.step ();
+                       if (rc == Sqlite.ROW) {
+                               return stmt.column_text (0).replace ("&quot;", "\"");
+                       }
+               } while (rc == Sqlite.ROW);
+
+               return "";
+       }
+
        public int clear () {
                int rc;
 
-               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);
+               rc = db.exec (
+                       "DROP TABLE IF EXISTS Movies;" +
+                       "CREATE TABLE Movies (Title TEXT PRIMARY KEY COLLATE NOCASE, Year INTEGER, Rating INTEGER, Votes INTEGER NOT NULL DEFAULT 0, Genres INTEGER NOT NULL DEFAULT 0);" +
+                       "DROP TABLE IF EXISTS Genres;" +
+                       "CREATE TABLE Genres (Bit INTEGER PRIMARY KEY, Genre TEXT NOT NULL);" +
+                       "DROP TABLE IF EXISTS Akas;" +
+                       "CREATE TABLE Akas (Aka TEXT NOT NULL COLLATE NOCASE, TitleID INTEGER NOT NULL);" +
+                       "DROP TABLE IF EXISTS Plots;" +
+                       "CREATE TABLE Plots (Plot TEXT NOT NULL, Author TEXT)",
+                       callback, null);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       return 1;
+               }
+
+               return 0;
+       }
+
+       public int create_aka_index () {
+               int rc;
+
+               rc = db.exec ("CREATE INDEX AkasAka ON Akas(Aka);", callback, null);
                if (rc != Sqlite.OK) {
                        stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
                        return 1;
@@ -154,4 +272,133 @@ class IMDbSqlite : Object {
 
                return 0;
        }
+
+       public int create_votes_index () {
+               int rc;
+
+               rc = db.exec ("CREATE INDEX MovieVotes ON Movies(Votes);", callback, null);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       return 1;
+               }
+
+               return 0;
+       }
+
+       private Cancellable? _cancellable;
+       public async int query (MovieFilter filter, ReceiveMovieFunction receive_movie, int limit, Cancellable? cancellable) {
+               var sql = "SELECT Title, Year, Rating, Genres FROM Movies";
+               var sep = " WHERE ";
+               string match = null;
+               Statement stmt;
+               int rc;
+
+               // FIXME - how many opcodes until main loop iteration for best responsivity?
+               _cancellable = cancellable;
+               db.progress_handler (1000, progress_handler);
+
+               if (filter.title != null && filter.title != "") {
+                       if ("*" in filter.title) {
+                               match = "GLOB \"%s (*)\"".printf (filter.title);
+                       } else {
+                               match = "LIKE \"%s%%\"".printf (filter.title);
+                       }
+                       sql += sep + "(Title %s OR rowid IN (SELECT TitleID FROM Akas WHERE Aka %s))".printf (match, match);
+                       sep = " AND ";
+               }
+               if (filter.year_min > 0) {
+                       sql += sep + "Year >= %d".printf (filter.year_min);
+                       sep = " AND ";
+               }
+               if (filter.year_max > 0) {
+                       sql += sep + "Year <= %d".printf (filter.year_max);
+                       sep = " AND ";
+               }
+               if (filter.rating_min > 0) {
+                       sql += sep + "Rating >= %d".printf (filter.rating_min);
+                       sep = " AND ";
+               }
+               if (filter.genres.field != 0) {
+                       sql += sep + "Genres&%d = %d".printf (filter.genres.field, filter.genres.field);
+               }
+               sql += " ORDER BY Votes DESC LIMIT %d;".printf (limit + 1);
+
+               stdout.printf("SQL: \"%s\"\n", sql);
+
+               rc = db.prepare_v2 (sql, -1, out stmt);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       db.progress_handler (0, null);
+                       return 0;
+               }
+
+               int n = 0;
+               do {
+                       if (++n > limit)
+                               break;
+                       Idle.add (query.callback);
+                       yield;
+                       rc = stmt.step ();
+                       if (rc == Sqlite.ROW) {
+                               int year = stmt.column_int (1);
+                               string title = stmt.column_text (0);
+                               int rating;
+                               if (stmt.column_type (2) != Sqlite.NULL)
+                                       rating = stmt.column_int (2);
+                               else
+                                       rating = -1;
+                               int genres = stmt.column_int (3);
+                               string aka = null;
+                               if (match != null && !(filter.matches_title (strip_year (title, year)))) {
+                                       aka = movie_aka (title, match);
+                                       if (aka != null)
+                                               aka = strip_year (aka, year);
+                               }
+                               receive_movie (strip_year (title, year), aka, year, rating, genres);
+                       }
+               } while (rc == Sqlite.ROW);
+
+               db.progress_handler (0, null);
+               return n;
+       }
+
+       private string? movie_aka (string title, string match) {
+               string sql = "SELECT Aka FROM Akas WHERE (TitleID = (SELECT rowid FROM Movies WHERE Title = \"%s\") AND Aka %s) LIMIT 1;".printf (title, match);
+               Statement stmt;
+               int rc;
+               string aka = null;
+
+               rc = db.prepare_v2 (sql, -1, out stmt);
+               if (rc != Sqlite.OK) {
+                       stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
+                       return null;
+               }
+
+               do {
+                       rc = stmt.step ();
+                       if (rc == Sqlite.ROW) {
+                               aka = stmt.column_text (0);
+                       }
+               } while (rc == Sqlite.ROW);
+
+               return aka;
+       }
+
+       private int progress_handler () {
+               ((MainContext) null).iteration (false);
+               return (int) _cancellable.is_cancelled ();
+       }
+
+       private string strip_year (string title, int year) {
+               string year_suffix = " (%d)".printf (year);
+               if (title.has_suffix (year_suffix))
+                       return title.substring (0, title.length - year_suffix.length);
+               year_suffix = " (%d/I)".printf (year);
+               if (title.has_suffix (year_suffix))
+                       return title.substring (0, title.length - year_suffix.length);
+               year_suffix = " (%d/II)".printf (year);
+               if (title.has_suffix (year_suffix))
+                       return title.substring (0, title.length - year_suffix.length);
+               return title.dup ();
+       }
 }