Introduce midori_array_query to unify bookmark queries

This ensures querying is consistently quoted and ordered.
This commit is contained in:
Christian Dywan 2011-11-23 00:23:41 +01:00
parent 6ff2109adb
commit e4963aa324
4 changed files with 67 additions and 65 deletions

View file

@ -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;
}

View file

@ -30,6 +30,12 @@ midori_array_to_file (KatzeArray* 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);

View file

@ -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);

View file

@ -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