[yelp: 3/17] yelp-sqlite-storage: Hooking up update and search



commit 65ad43b5b7cf6caa819bb0cbd39402e54e1d5f79
Author: Shaun McCance <shaunm gnome org>
Date:   Thu Feb 10 17:52:01 2011 -0500

    yelp-sqlite-storage: Hooking up update and search

 libyelp/yelp-sqlite-storage.c |   86 +++++++++++++++++++++++++++++++++++-----
 1 files changed, 75 insertions(+), 11 deletions(-)
---
diff --git a/libyelp/yelp-sqlite-storage.c b/libyelp/yelp-sqlite-storage.c
index 17a987d..57ad188 100644
--- a/libyelp/yelp-sqlite-storage.c
+++ b/libyelp/yelp-sqlite-storage.c
@@ -55,6 +55,7 @@ typedef struct _YelpSqliteStoragePrivate YelpSqliteStoragePrivate;
 struct _YelpSqliteStoragePrivate {
     gchar   *filename;
     sqlite3 *db;
+    GMutex *mutex;
 };
 
 enum {  
@@ -78,25 +79,45 @@ yelp_sqlite_storage_finalize (GObject *object)
     if (priv->db)
         sqlite3_close (priv->db);
 
+    if (priv->mutex)
+        g_mutex_free (priv->mutex);
+
     G_OBJECT_CLASS (yelp_sqlite_storage_parent_class)->finalize (object);
 }
 
 static void
 yelp_sqlite_storage_init (YelpSqliteStorage *storage)
 {
+    YelpSqliteStoragePrivate *priv = GET_PRIV (storage);
+    priv->mutex = g_mutex_new ();
 }
 
 static void
 yelp_sqlite_storage_constructed (GObject *object)
 {
+    int status;
+    sqlite3_stmt *stmt = NULL;
     YelpSqliteStoragePrivate *priv = GET_PRIV (object);
 
     if (priv->filename != NULL)
-        sqlite3_open (priv->filename, &(priv->db));
+        status = sqlite3_open (priv->filename, &(priv->db));
     else
-        sqlite3_open (":memory:", &(priv->db));
-
-    /* FIXME: create tables if necessary */
+        status = sqlite3_open (":memory:", &(priv->db));
+
+    if (status != SQLITE_OK)
+        return;
+
+    status = sqlite3_prepare_v2 (priv->db,
+                                 "create virtual table pages using fts4("
+                                 " doc_uri, lang, full_uri,"
+                                 " title, desc, icon, body"
+                                 ");",
+                                 -1, &stmt, NULL);
+    if (status != SQLITE_OK)
+        return;
+
+    sqlite3_step (stmt);
+    sqlite3_finalize (stmt);
 }
 
 static void
@@ -187,6 +208,35 @@ yelp_sqlite_storage_update (YelpStorage   *storage,
                             const gchar   *icon,
                             const gchar   *text)
 {
+    sqlite3_stmt *stmt = NULL;
+    YelpSqliteStoragePrivate *priv = GET_PRIV (storage);
+
+    g_mutex_lock (priv->mutex);
+
+    sqlite3_prepare_v2 (priv->db,
+                        "delete from pages where doc_uri = ? and lang = ? and full_uri = ?;",
+                        -1, &stmt, NULL);
+    sqlite3_bind_text (stmt, 1, doc_uri, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 2, g_get_language_names()[0], -1, SQLITE_STATIC);
+    sqlite3_bind_text (stmt, 3, full_uri, -1, SQLITE_TRANSIENT);
+    sqlite3_step (stmt);
+    sqlite3_finalize (stmt);
+
+    sqlite3_prepare_v2 (priv->db,
+                        "insert into pages (doc_uri, lang, full_uri, title, desc, icon, body)"
+                        " values (?, ?, ?, ?, ?, ?, ?);",
+                        -1, &stmt, NULL);
+    sqlite3_bind_text (stmt, 1, doc_uri, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 2, g_get_language_names()[0], -1, SQLITE_STATIC);
+    sqlite3_bind_text (stmt, 3, full_uri, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 4, title, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 5, desc, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 6, icon, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 7, text, -1, SQLITE_TRANSIENT);
+    sqlite3_step (stmt);
+    sqlite3_finalize (stmt);
+
+    g_mutex_unlock (priv->mutex);
 }
 
 static GVariant *
@@ -194,19 +244,33 @@ yelp_sqlite_storage_search (YelpStorage   *storage,
                             const gchar   *doc_uri,
                             const gchar   *text)
 {
+    sqlite3_stmt *stmt = NULL;
     GVariantBuilder builder;
     GVariant *ret;
-    int i;
+    YelpSqliteStoragePrivate *priv = GET_PRIV (storage);
+
+    g_mutex_lock (priv->mutex);
+
+    sqlite3_prepare_v2 (priv->db,
+                        "select full_uri, title, desc, icon from pages where"
+                        " doc_uri = ? and lang = ? and body match ?;",
+                        -1, &stmt, NULL);
+    sqlite3_bind_text (stmt, 1, doc_uri, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 2, g_get_language_names()[0], -1, SQLITE_STATIC);
+    sqlite3_bind_text (stmt, 3, text, -1, SQLITE_TRANSIENT);
 
-    /* FIXME */
     g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ssss)"));
-    for (i = 0; i < 1; i++) {
+    while (sqlite3_step (stmt) == SQLITE_ROW) {
         g_variant_builder_add (&builder, "(ssss)",
-                               doc_uri,
-                               doc_uri,
-                               "Description",
-                               "help-contents");
+                               sqlite3_column_text (stmt, 0),
+                               sqlite3_column_text (stmt, 1),
+                               sqlite3_column_text (stmt, 2),
+                               sqlite3_column_text (stmt, 3));
     }
+    sqlite3_finalize (stmt);
     ret = g_variant_new ("a(ssss)", &builder);
+
+    g_mutex_unlock (priv->mutex);
+
     return ret;
 }



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