Add query functionality to the IMDb SQLite store
authorPhilipp Zabel <philipp.zabel@gmail.com>
Thu, 15 Oct 2009 13:31:54 +0000 (15:31 +0200)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Mon, 2 Nov 2009 18:15:00 +0000 (19:15 +0100)
src/imdb/imdb-sqlite.vala

index c8e6cbc..ebb9d02 100644 (file)
@@ -22,6 +22,8 @@ class IMDbSqlite : Object {
        Database db;
        List<string> genres;
 
+       public delegate void ReceiveMovieFunction (string title, int year, int rating);
+
        public IMDbSqlite (string filename) {
                int rc;
 
@@ -154,4 +156,28 @@ class IMDbSqlite : Object {
 
                return 0;
        }
+
+       public int query (string filter, ReceiveMovieFunction receive_movie) {
+               var sql = "SELECT Title, Year, Rating FROM Movies WHERE Title LIKE \"%s%%\" LIMIT 10;".printf (filter);
+               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 1;
+               }
+
+               do {
+                       rc = stmt.step ();
+                       if (rc == Sqlite.ROW) {
+                               string title = stmt.column_text (0);
+                               int year = stmt.column_int (1);
+                               int rating = stmt.column_int (2);
+                               receive_movie (title, year, rating);
+                       }
+               } while (rc == Sqlite.ROW);
+
+               return 0;
+       }
 }