[PATCH 07/13] cache: Add API to search content in cache.



Add grl_media_cache_search() function to get all GrlMedia that fulfils a
condition.

Signed-off-by: Juan A. Suarez Romero <jasuarez igalia com>
---
 src/grl-media-cache.c |   63 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/grl-media-cache.h |    4 +++
 2 files changed, 67 insertions(+), 0 deletions(-)

diff --git a/src/grl-media-cache.c b/src/grl-media-cache.c
index a71512c..b1d53ec 100644
--- a/src/grl-media-cache.c
+++ b/src/grl-media-cache.c
@@ -82,6 +82,10 @@ GRL_LOG_DOMAIN(media_cache_log_domain);
   "SELECT parent, updated, media from %s "      \
   "WHERE id='%s' "
 
+#define GRL_CACHE_SEARCH                        \
+  "SELECT cache.media from %s cache "           \
+  "%s %s"
+
 enum {
   PROP_0,
   PROP_CACHE_ID,
@@ -685,3 +689,62 @@ grl_media_cache_get_media (GrlMediaCache *cache,
                cache->priv->cache_id);
   return FALSE;
 }
+
+GList *
+grl_media_cache_search (GrlMediaCache *cache,
+                        const gchar *condition,
+                        GError **error)
+{
+  GList *medias = NULL;
+  gchar *sql_sentence;
+  gint r;
+  sqlite3_stmt *sql_stmt;
+
+  g_return_val_if_fail (GRL_IS_MEDIA_CACHE (cache), NULL);
+
+  sql_sentence = g_strdup_printf (GRL_CACHE_SEARCH,
+                                  cache->priv->cache_id,
+                                  condition? "WHERE": "",
+                                  condition? condition: "");
+
+  if (sqlite3_prepare_v2 (cache->priv->db,
+                          sql_sentence,
+                          strlen (sql_sentence),
+                          &sql_stmt,
+                          NULL) != SQLITE_OK) {
+    g_free (sql_sentence);
+    goto error;
+  }
+
+  g_free (sql_sentence);
+
+  /* Wait until it finish */
+  while ((r = sqlite3_step (sql_stmt)) == SQLITE_BUSY);
+
+  if (r == SQLITE_ERROR) {
+    sqlite3_finalize (sql_stmt);
+    goto error;
+  }
+
+  while (r == SQLITE_ROW) {
+    medias =
+      g_list_prepend (medias,
+                      grl_media_unserialize ((const gchar *) sqlite3_column_text (sql_stmt, 0)));
+    r = sqlite3_step (sql_stmt);
+  }
+
+  sqlite3_finalize (sql_stmt);
+
+  return medias;
+
+ error:
+  GRL_WARNING ("Failed to search in cache '%s': %s",
+               cache->priv->cache_id,
+               sqlite3_errmsg (cache->priv->db));
+  g_set_error (error,
+               GRL_CORE_ERROR,
+               GRL_CORE_ERROR_CACHE_FAILED,
+               "Unable to query cache '%s'",
+               cache->priv->cache_id);
+  return NULL;
+}
diff --git a/src/grl-media-cache.h b/src/grl-media-cache.h
index bae83ad..4160923 100644
--- a/src/grl-media-cache.h
+++ b/src/grl-media-cache.h
@@ -107,6 +107,10 @@ GrlMedia *grl_media_cache_get_media (GrlMediaCache *cache,
                                      GTimeVal *last_time_changed,
                                      GError **error);
 
+GList * grl_media_cache_search (GrlMediaCache *cache,
+                                const gchar *condition,
+                                GError **error);
+
 G_END_DECLS
 
 #endif /* _GRL_MEDIA_CACHE_H_ */
-- 
1.7.4



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]