From: Alexander Butenko Date: Thu, 1 Jul 2010 19:18:31 +0000 (-0400) Subject: Introduce katze_array_from_sqlite () and katze_array_from_statement () X-Git-Url: https://spindle.queued.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd97f069d4f60c10cc8edf4b2fd157611cb76d6a;p=midori Introduce katze_array_from_sqlite () and katze_array_from_statement () --- diff --git a/midori/midori-array.c b/midori/midori-array.c index 3e725462..ca8390de 100644 --- a/midori/midori-array.c +++ b/midori/midori-array.c @@ -17,6 +17,7 @@ #include #include +#include #if HAVE_LIBXML #include @@ -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 a41936a3..a0cac614 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 #include 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__ */