From: Philipp Zabel Date: Thu, 8 Jul 2010 19:34:19 +0000 (+0200) Subject: Catalog plugin: use StringBuilder to build SQL query X-Git-Url: http://git.maemo.org/git/?p=cinaest;a=commitdiff_plain;h=d32979638361bf4b1a9015a509de2d6d6788ae4a Catalog plugin: use StringBuilder to build SQL query --- diff --git a/src/plugins/catalog-sqlite.vala b/src/plugins/catalog-sqlite.vala index b8d5105..be05605 100644 --- a/src/plugins/catalog-sqlite.vala +++ b/src/plugins/catalog-sqlite.vala @@ -181,7 +181,12 @@ class CatalogSqlite : Object { private Cancellable? _cancellable; public async int query (string table, MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) { - var sql = "SELECT Title, Year, Rating, Genres FROM %s".printf (table); + var sql = new StringBuilder (); + sql.append ("SELECT Title, Year, Rating, Genres"); + if (table == "Watched") + sql.append (", Date"); + sql.append (" FROM "); + sql.append (table); var sep = " WHERE "; Statement stmt; int rc; @@ -191,32 +196,39 @@ class CatalogSqlite : Object { db.progress_handler (1000, progress_handler); if (filter.title != null && filter.title != "") { - if ("*" in filter.title) - sql += sep + "Title GLOB \"%s (*)\"".printf (filter.title); - else - sql += sep + "Title LIKE \"%s%%\"".printf (filter.title); + if ("*" in filter.title) { + sql.append (sep); + sql.append_printf ("Title GLOB \"%s\"", filter.title); + } else { + sql.append (sep); + sql.append_printf ("Title LIKE \"%s%%\"", filter.title); + } sep = " AND "; } if (filter.year_min > 0) { - sql += sep + "Year >= %d".printf (filter.year_min); + sql.append (sep); + sql.append_printf ("Year >= %d", filter.year_min); sep = " AND "; } if (filter.year_max > 0) { - sql += sep + "Year <= %d".printf (filter.year_max); + sql.append (sep); + sql.append_printf ("Year <= %d", filter.year_max); sep = " AND "; } if (filter.rating_min > 0) { - sql += sep + "Rating >= %d".printf (filter.rating_min); + sql.append (sep); + sql.append_printf ("Rating >= %d", filter.rating_min); sep = " AND "; } if (filter.genres.field != 0) { - sql += sep + "Genres&%d = %d".printf (filter.genres.field, filter.genres.field); + sql.append (sep); + sql.append_printf ("Genres&%d = %d", filter.genres.field, filter.genres.field); } - sql += " LIMIT %d;".printf (limit); + sql.append_printf (" LIMIT %d;", limit); - stdout.printf("SQL: \"%s\"\n", sql); + stdout.printf("SQL: \"%s\"\n", sql.str); - rc = db.prepare_v2 (sql, -1, out stmt); + rc = db.prepare_v2 (sql.str, -1, out stmt); if (rc != Sqlite.OK) { stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ()); db.progress_handler (0, null);