[Xfce4-commits] <midori:master> Introduce katze_array_from_sqlite () and katze_array_from_statement ()

Christian Dywan noreply at xfce.org
Thu Jul 1 23:38:02 CEST 2010


Updating branch refs/heads/master
         to bd97f069d4f60c10cc8edf4b2fd157611cb76d6a (commit)
       from a49f1f7814006db82f13622b97ad627026360b7d (commit)

commit bd97f069d4f60c10cc8edf4b2fd157611cb76d6a
Author: Alexander Butenko <a.butenka at gmail.com>
Date:   Thu Jul 1 15:18:31 2010 -0400

    Introduce katze_array_from_sqlite () and katze_array_from_statement ()

 midori/midori-array.c |  103 +++++++++++++++++++++++++++++++++++++++++++++++++
 midori/midori-array.h |    8 ++++
 2 files changed, 111 insertions(+), 0 deletions(-)

diff --git a/midori/midori-array.c b/midori/midori-array.c
index 3e72546..ca8390d 100644
--- a/midori/midori-array.c
+++ b/midori/midori-array.c
@@ -17,6 +17,7 @@
 
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
+#include <sqlite3.h>
 
 #if HAVE_LIBXML
     #include <libxml/parser.h>
@@ -752,3 +753,105 @@ midori_array_to_file (KatzeArray*  array,
     g_critical ("Cannot write KatzeArray to unknown format '%s'.", format);
     return FALSE;
 }
+
+static void
+katze_item_set_value_from_column (sqlite3_stmt* stmt,
+                                  gint          column,
+                                  KatzeItem*    item)
+{
+    const gchar* name;
+
+    name = sqlite3_column_name (stmt, column);
+    g_return_if_fail (name != NULL);
+
+    if (g_str_equal (name, "uri"))
+    {
+        const unsigned char* uri;
+        uri = sqlite3_column_text (stmt, column);
+        if (uri && uri[0])
+            katze_item_set_uri (item, (gchar*)uri);
+    }
+    else if (g_str_equal (name, "title") || g_str_equal (name, "name"))
+    {
+        const unsigned char* title;
+        title = sqlite3_column_text (stmt, column);
+        katze_item_set_name (item, (gchar*)title);
+    }
+    else if (g_str_equal (name, "date"))
+    {
+        gint date;
+        date = sqlite3_column_int64 (stmt, column);
+        katze_item_set_added (item, date);
+    }
+    else if (g_str_equal (name, "day") || g_str_equal (name, "app")
+          || g_str_equal (name, "toolbar") || g_str_equal (name, "type"))
+    {
+        gint value;
+        value = sqlite3_column_int64 (stmt, column);
+        katze_item_set_meta_integer (item, name, value);
+    }
+    else
+        g_warn_if_reached ();
+}
+
+/**
+ * midori_array_from_statement:
+ * @stmt: prepared statement
+ *
+ * Stores the result in a #KatzeArray.
+ *
+ * Return value: a #KatzeArray on success, %NULL otherwise
+ *
+ * Since: 0.2.7
+ **/
+KatzeArray*
+katze_array_from_statement (sqlite3_stmt* stmt)
+{
+    KatzeArray *array;
+    gint result;
+    gint cols;
+
+    array = katze_array_new (KATZE_TYPE_ITEM);
+    cols = sqlite3_column_count (stmt);
+
+    while ((result = sqlite3_step (stmt)) == SQLITE_ROW)
+    {
+        gint i;
+        KatzeItem* item;
+
+        item = katze_item_new ();
+        for (i = 0; i < cols; i++)
+            katze_item_set_value_from_column (stmt, i, item);
+        katze_array_add_item (array, item);
+    }
+
+    sqlite3_clear_bindings (stmt);
+    sqlite3_reset (stmt);
+    return array;
+}
+
+/**
+ * midori_array_from_sqlite:
+ * @db: opened database handler
+ * @sqlcmd: SQL query
+ *
+ * Stores the result in a #KatzeArray.
+ *
+ * Return value: a #KatzeArray on success, %NULL otherwise
+ *
+ * Since: 0.2.7
+ **/
+
+KatzeArray*
+katze_array_from_sqlite (sqlite3*     db,
+                         const gchar* sqlcmd)
+{
+    sqlite3_stmt* stmt;
+    gint result;
+
+    result = sqlite3_prepare_v2 (db, sqlcmd, -1, &stmt, NULL);
+    if (result != SQLITE_OK)
+        return NULL;
+
+    return katze_array_from_statement (stmt);
+}
diff --git a/midori/midori-array.h b/midori/midori-array.h
index a41936a..a0cac61 100644
--- a/midori/midori-array.h
+++ b/midori/midori-array.h
@@ -12,6 +12,7 @@
 #ifndef __MIDORI_ARRAY_H__
 #define __MIDORI_ARRAY_H__ 1
 
+#include <sqlite3.h>
 #include <katze/katze.h>
 
 gboolean
@@ -26,4 +27,11 @@ midori_array_to_file   (KatzeArray*  array,
                         const gchar* format,
                         GError**     error);
 
+KatzeArray*
+katze_array_from_statement (sqlite3_stmt* stmt);
+
+KatzeArray*
+katze_array_from_sqlite (sqlite3*     db,
+                         const gchar* sqlcmd);
+
 #endif /* !__MIDORI_ARRAY_H__ */



More information about the Xfce4-commits mailing list