This ensures querying is consistently quoted and ordered.
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;
+}
+
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);
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);
static void
midori_bookmarkbar_populate (MidoriBrowser* browser)
{
- sqlite3* db;
- const gchar* sqlcmd;
KatzeArray* array;
KatzeItem* item;
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);
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);
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))
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