[Xfce4-commits] <midori:master> Introduce midori_array_query to unify bookmark queries

Christian Dywan noreply at xfce.org
Wed Nov 23 00:26:01 CET 2011


Updating branch refs/heads/master
         to e4963aa324bb8021550366eca814346baecda03d (commit)
       from 6ff2109adbfe6fea4c3a4b05ecab851fdf1f72f7 (commit)

commit e4963aa324bb8021550366eca814346baecda03d
Author: Christian Dywan <christian at twotoasts.de>
Date:   Wed Nov 23 00:23:41 2011 +0100

    Introduce midori_array_query to unify bookmark queries
    
    This ensures querying is consistently quoted and ordered.

 midori/midori-array.c     |   46 +++++++++++++++++++++++++++++++++++++++++++++
 midori/midori-array.h     |    6 +++++
 midori/midori-browser.c   |   35 +++++++--------------------------
 panels/midori-bookmarks.c |   45 ++++++-------------------------------------
 4 files changed, 67 insertions(+), 65 deletions(-)

diff --git a/midori/midori-array.c b/midori/midori-array.c
index fe40f90..66f089c 100644
--- a/midori/midori-array.c
+++ b/midori/midori-array.c
@@ -1065,3 +1065,49 @@ katze_array_from_sqlite (sqlite3*     db,
 
     return katze_array_from_statement (stmt);
 }
+
+/**
+ * midori_array_query:
+ * @array: the main bookmark array
+ * @fields: comma separated list of fields
+ * @condition: condition, like "folder = '%q'"
+ * @value: a value to be inserted if @condition contains %q
+ *
+ * Stores the result in a #KatzeArray.
+ *
+ * Return value: a #KatzeArray on success, %NULL otherwise
+ *
+ * Since: 0.4.3
+ **/
+KatzeArray*
+midori_array_query (KatzeArray*  bookmarks,
+                    const gchar* fields,
+                    const gchar* condition,
+                    const gchar* value)
+{
+    sqlite3* db;
+    gchar* sqlcmd;
+    char* sqlcmd_value;
+    KatzeArray* array;
+
+    g_return_val_if_fail (KATZE_IS_ARRAY (bookmarks), NULL);
+    g_return_val_if_fail (fields, NULL);
+    g_return_val_if_fail (condition, NULL);
+    db = g_object_get_data (G_OBJECT (bookmarks), "db");
+    if (db == NULL)
+        return NULL;
+
+    sqlcmd = g_strdup_printf ("SELECT %s FROM bookmarks WHERE %s "
+                              "ORDER BY title DESC", fields, condition);
+    if (strstr (condition, "%q"))
+    {
+        sqlcmd_value = sqlite3_mprintf (sqlcmd, value ? value : "");
+        array = katze_array_from_sqlite (db, sqlcmd_value);
+        sqlite3_free (sqlcmd_value);
+    }
+    else
+        array = katze_array_from_sqlite (db, sqlcmd);
+    g_free (sqlcmd);
+    return array;
+}
+
diff --git a/midori/midori-array.h b/midori/midori-array.h
index a0cac61..35b4e17 100644
--- a/midori/midori-array.h
+++ b/midori/midori-array.h
@@ -31,6 +31,12 @@ KatzeArray*
 katze_array_from_statement (sqlite3_stmt* stmt);
 
 KatzeArray*
+midori_array_query     (KatzeArray*  array,
+                        const gchar* fields,
+                        const gchar* condition,
+                        const gchar* value);
+
+KatzeArray*
 katze_array_from_sqlite (sqlite3*     db,
                          const gchar* sqlcmd);
 
diff --git a/midori/midori-browser.c b/midori/midori-browser.c
index f93a66c..6878f1e 100644
--- a/midori/midori-browser.c
+++ b/midori/midori-browser.c
@@ -2951,26 +2951,20 @@ _action_bookmarks_populate_folder (GtkAction*     action,
                                    KatzeArray*    folder,
                                    MidoriBrowser* browser)
 {
-    const char* sqlcmd = "SELECT uri, title, app, folder "
-                         "FROM bookmarks WHERE folder = '%q' ORDER BY uri ASC";
-    sqlite3* db = g_object_get_data (G_OBJECT (browser->bookmarks), "db");
     const gchar* folder_name;
-    char* sqlcmd_folder;
     KatzeArray* bookmarks;
     GtkWidget* menuitem;
 
-    if (!db)
+    folder_name = katze_item_get_name (KATZE_ITEM (folder));
+    if (!(bookmarks = midori_array_query (browser->bookmarks,
+          "uri, title, app, folder", "folder = '%q'", folder_name)))
         return FALSE;
 
     /* Clear items from dummy array here */
     gtk_container_foreach (GTK_CONTAINER (menu),
         (GtkCallback)(gtk_widget_destroy), NULL);
 
-    folder_name = katze_item_get_name (KATZE_ITEM (folder));
-    sqlcmd_folder = sqlite3_mprintf (sqlcmd, folder_name ? folder_name : "");
-    bookmarks = katze_array_from_sqlite (db, sqlcmd_folder);
-    sqlite3_free (sqlcmd_folder);
-    if (!bookmarks || katze_array_is_empty (bookmarks))
+    if (katze_array_is_empty (bookmarks))
     {
         menuitem = gtk_image_menu_item_new_with_label (_("Empty"));
         gtk_widget_set_sensitive (menuitem, FALSE);
@@ -6741,8 +6735,6 @@ midori_bookmarkbar_remove_item_cb (KatzeArray*    bookmarks,
 static void
 midori_bookmarkbar_populate (MidoriBrowser* browser)
 {
-    sqlite3* db;
-    const gchar* sqlcmd;
     KatzeArray* array;
     KatzeItem* item;
 
@@ -6752,14 +6744,8 @@ midori_bookmarkbar_populate (MidoriBrowser* browser)
     gtk_toolbar_insert (GTK_TOOLBAR (browser->bookmarkbar),
                         gtk_separator_tool_item_new (), -1);
 
-    db = g_object_get_data (G_OBJECT (browser->bookmarks), "db");
-    if (!db)
-        return;
-
-    sqlcmd = "SELECT uri, title, desc, app, folder, toolbar FROM bookmarks WHERE "
-             " toolbar = 1 ORDER BY uri ASC";
-
-    array = katze_array_from_sqlite (db, sqlcmd);
+    array = midori_array_query (browser->bookmarks,
+        "uri, title, desc, app, folder, toolbar", "toolbar = 1", NULL);
     if (!array)
     {
         _action_set_sensitive (browser, "BookmarkAdd", FALSE);
@@ -6773,15 +6759,10 @@ midori_bookmarkbar_populate (MidoriBrowser* browser)
             midori_bookmarkbar_insert_item (browser->bookmarkbar, item);
         else
         {
-            KatzeArray* subfolder;
-            gchar* subsqlcmd;
-
-            subsqlcmd = g_strdup_printf ("SELECT uri, title, desc, app FROM bookmarks WHERE "
-                                         " folder = '%s' and uri != ''", katze_item_get_name (item));
-            subfolder = katze_array_from_sqlite (db, subsqlcmd);
+            KatzeArray* subfolder = midori_array_query (browser->bookmarks,
+                "uri, title, desc, app", "folder = '%q' AND uri != ''", katze_item_get_name (item));
             katze_item_set_name (KATZE_ITEM (subfolder), katze_item_get_name (item));
             midori_bookmarkbar_insert_item (browser->bookmarkbar, KATZE_ITEM (subfolder));
-            g_free (subsqlcmd);
         }
     }
     _action_set_sensitive (browser, "BookmarkAdd", TRUE);
diff --git a/panels/midori-bookmarks.c b/panels/midori-bookmarks.c
index 15d27b2..ec6d321 100644
--- a/panels/midori-bookmarks.c
+++ b/panels/midori-bookmarks.c
@@ -124,19 +124,13 @@ midori_bookmarks_export_array_db (sqlite3*     db,
                                   KatzeArray*  array,
                                   const gchar* folder)
 {
-    gchar* sqlcmd;
     KatzeArray* root_array;
     KatzeArray* subarray;
     KatzeItem* item;
     GList* list;
 
-    if (!db)
+    if (!(root_array = midori_array_query (array, "*", "folder='%q'", folder)))
         return;
-
-    sqlcmd = g_strdup_printf ("SELECT * FROM bookmarks where folder='%s'", folder);
-    root_array = katze_array_from_sqlite (db, sqlcmd);
-    g_free (sqlcmd);
-
     KATZE_ARRAY_FOREACH_ITEM_L (item, root_array, list)
     {
         if (KATZE_ITEM_IS_FOLDER (item))
@@ -177,40 +171,15 @@ midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
                                const gchar*     folder,
                                const gchar*     keyword)
 {
-    sqlite3* db;
-    sqlite3_stmt* statement;
-    gint result;
-    const gchar* sqlcmd;
-
-    db = g_object_get_data (G_OBJECT (bookmarks->array), "db");
-
-    if (!db)
-        return katze_array_new (KATZE_TYPE_ITEM);
+    KatzeArray* array;
 
     if (keyword && *keyword)
-    {
-        gchar* filterstr;
-        sqlcmd = "SELECT uri, title, desc, app, toolbar, folder from bookmarks where "
-                 " title like ? ORDER BY uri DESC";
-        result = sqlite3_prepare_v2 (db, sqlcmd, -1, &statement, NULL);
-        filterstr = g_strdup_printf ("%%%s%%", keyword);
-        sqlite3_bind_text (statement, 1, g_strdup (filterstr), -1, g_free);
-        g_free (filterstr);
-    }
+        array = midori_array_query (bookmarks->array,
+           "uri, title, desc, app, toolbar, folder", "title LIKE '%%%q%%'", keyword);
     else
-    {
-        if (!folder)
-            folder = "";
-        sqlcmd = "SELECT uri, title, desc, app, toolbar, folder from bookmarks where "
-                 " folder = ? ORDER BY title DESC";
-        result = sqlite3_prepare_v2 (db, sqlcmd, -1, &statement, NULL);
-        sqlite3_bind_text (statement, 1, g_strdup (folder), -1, g_free);
-    }
-
-    if (result != SQLITE_OK)
-        return katze_array_new (KATZE_TYPE_ITEM);
-
-    return katze_array_from_statement (statement);
+        array = midori_array_query (bookmarks->array,
+           "uri, title, desc, app, toolbar, folder", "folder = '%q'", folder);
+    return array ? array : katze_array_new (KATZE_TYPE_ITEM);
 }
 
 static void


More information about the Xfce4-commits mailing list