IMDb SQLite database: add get_cast method
authorPhilipp Zabel <philipp.zabel@gmail.com>
Sun, 15 Aug 2010 10:14:04 +0000 (12:14 +0200)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Mon, 16 Aug 2010 13:11:07 +0000 (15:11 +0200)
src/imdb/imdb-sqlite.vala

index a49160c..ff9edcb 100644 (file)
@@ -406,6 +406,36 @@ class IMDbSqlite : Object {
                return "";
        }
 
+       public List<Role> get_cast (string title) {
+               int rowid;
+               string sql = "SELECT Name,Character FROM Actors,People WHERE TitleID IN (SELECT rowid FROM Movies WHERE Title=\"%s\") AND ActorID=People.rowid AND Number>0 ORDER BY NUMBER LIMIT 3".printf (title);
+               Statement stmt;
+               int rc;
+               int count = 0;
+               var cast = new List<Role> ();
+
+               if (!movie_exists (title, out rowid))
+                       return 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) {
+                               var role = new Role ();
+                               role.actor_name = stmt.column_text (0);
+                               role.character = stmt.column_text (1);
+                               cast.append (role);
+                       }
+               } while (rc == Sqlite.ROW);
+
+               return cast;
+       }
+
        public int clear () {
                int rc;