Refactor location completion queries to use prepared statements

This commit is contained in:
Alexander Butenko 2010-02-04 19:22:35 +01:00 committed by Christian Dywan
parent db334e2921
commit 4461a16a79

View file

@ -339,9 +339,9 @@ midori_location_action_popup_timeout_cb (gpointer data)
GtkTreeViewColumn* column; GtkTreeViewColumn* column;
GtkListStore* store; GtkListStore* store;
sqlite3* db; sqlite3* db;
gchar* query;
gint result; gint result;
sqlite3_stmt* statement; static sqlite3_stmt* stmt;
const gchar* sqlcmd;
gint matches, searches, height, screen_height, sep; gint matches, searches, height, screen_height, sep;
if (!gtk_widget_has_focus (action->entry) || !action->history) if (!gtk_widget_has_focus (action->entry) || !action->history)
@ -360,27 +360,22 @@ midori_location_action_popup_timeout_cb (gpointer data)
} }
db = g_object_get_data (G_OBJECT (action->history), "db"); db = g_object_get_data (G_OBJECT (action->history), "db");
/* FIXME: Consider keeping the prepared statement with '...LIKE ?...' if (!stmt)
and prepending/ appending % to the key. */
query = sqlite3_mprintf ("SELECT uri, title FROM history WHERE "
"uri LIKE '%%%q%%' OR title LIKE '%%%q%%'"
"GROUP BY uri ORDER BY count() DESC LIMIT %d",
action->key, action->key, MAX_ITEMS);
result = sqlite3_prepare_v2 (db, query, -1, &statement, NULL);
sqlite3_free (query);
if (result != SQLITE_OK)
{ {
g_print (_("Failed to execute database statement: %s\n"), sqlcmd = "SELECT uri, title FROM history WHERE uri LIKE ? OR title LIKE ?"
sqlite3_errmsg (db)); " GROUP BY uri ORDER BY count() DESC LIMIT ?";
midori_location_action_popdown_completion (action); sqlite3_prepare_v2 (db, sqlcmd, -1, &stmt, NULL);
return FALSE;
} }
sqlite3_bind_text (stmt, 1, g_strdup_printf ("%%%s%%", action->key), -1, g_free);
sqlite3_bind_text (stmt, 2, g_strdup_printf ("%%%s%%", action->key), -1, g_free);
sqlite3_bind_int64 (stmt, 3, MAX_ITEMS);
result = sqlite3_step (statement); result = sqlite3_step (stmt);
if (result != SQLITE_ROW && !action->search_engines) if (result != SQLITE_ROW && !action->search_engines)
{ {
sqlite3_finalize (statement); g_print (_("Failed to select from history: %s\n"), sqlite3_errmsg (db));
sqlite3_reset (stmt);
sqlite3_clear_bindings (stmt);
midori_location_action_popdown_completion (action); midori_location_action_popdown_completion (action);
return FALSE; return FALSE;
} }
@ -442,8 +437,8 @@ midori_location_action_popup_timeout_cb (gpointer data)
matches = searches = 0; matches = searches = 0;
while (result == SQLITE_ROW) while (result == SQLITE_ROW)
{ {
const unsigned char* uri = sqlite3_column_text (statement, 0); const unsigned char* uri = sqlite3_column_text (stmt, 0);
const unsigned char* title = sqlite3_column_text (statement, 1); const unsigned char* title = sqlite3_column_text (stmt, 1);
GdkPixbuf* icon = katze_load_cached_icon ((gchar*)uri, NULL); GdkPixbuf* icon = katze_load_cached_icon ((gchar*)uri, NULL);
if (!icon) if (!icon)
icon = action->default_icon; icon = action->default_icon;
@ -451,8 +446,10 @@ midori_location_action_popup_timeout_cb (gpointer data)
URI_COL, uri, TITLE_COL, title, YALIGN_COL, 0.25, URI_COL, uri, TITLE_COL, title, YALIGN_COL, 0.25,
FAVICON_COL, icon, -1); FAVICON_COL, icon, -1);
matches++; matches++;
result = sqlite3_step (statement); result = sqlite3_step (stmt);
} }
sqlite3_reset (stmt);
sqlite3_clear_bindings (stmt);
if (action->search_engines) if (action->search_engines)
{ {
@ -1142,41 +1139,38 @@ midori_location_action_entry_popup_cb (GtkComboBox* combo_box,
#if HAVE_SQLITE #if HAVE_SQLITE
GtkListStore* store; GtkListStore* store;
sqlite3* db; sqlite3* db;
gchar* query;
gint result; gint result;
sqlite3_stmt* statement; const gchar* sqlcmd;
static sqlite3_stmt* stmt;
gint matches; gint matches;
store = GTK_LIST_STORE (gtk_combo_box_get_model (combo_box)); store = GTK_LIST_STORE (gtk_combo_box_get_model (combo_box));
gtk_list_store_clear (store); gtk_list_store_clear (store);
db = g_object_get_data (G_OBJECT (location_action->history), "db"); db = g_object_get_data (G_OBJECT (location_action->history), "db");
/* FIXME: Consider keeping the prepared statement with '...LIKE ?...' if (!stmt)
and prepending/ appending % to the key. */ {
query = sqlite3_mprintf ("SELECT uri, title FROM history GROUP BY uri " sqlcmd = "SELECT uri, title FROM history"
"ORDER BY count() DESC LIMIT %d", MAX_ITEMS); " GROUP BY uri ORDER BY count() DESC LIMIT ?";
result = sqlite3_prepare_v2 (db, query, -1, &statement, NULL); sqlite3_prepare_v2 (db, sqlcmd, -1, &stmt, NULL);
sqlite3_free (query); }
if (result != SQLITE_OK) sqlite3_bind_int64 (stmt, 1, MAX_ITEMS);
result = sqlite3_step (stmt);
if (result != SQLITE_ROW)
{ {
g_print (_("Failed to execute database statement: %s\n"), g_print (_("Failed to execute database statement: %s\n"),
sqlite3_errmsg (db)); sqlite3_errmsg (db));
return; sqlite3_reset (stmt);
} sqlite3_clear_bindings (stmt);
result = sqlite3_step (statement);
if (result != SQLITE_ROW)
{
sqlite3_finalize (statement);
return; return;
} }
matches = 0; matches = 0;
do do
{ {
const unsigned char* uri = sqlite3_column_text (statement, 0); const unsigned char* uri = sqlite3_column_text (stmt, 0);
const unsigned char* title = sqlite3_column_text (statement, 1); const unsigned char* title = sqlite3_column_text (stmt, 1);
GdkPixbuf* icon = katze_load_cached_icon ((gchar*)uri, NULL); GdkPixbuf* icon = katze_load_cached_icon ((gchar*)uri, NULL);
if (!icon) if (!icon)
icon = location_action->default_icon; icon = location_action->default_icon;
@ -1184,9 +1178,11 @@ midori_location_action_entry_popup_cb (GtkComboBox* combo_box,
URI_COL, uri, TITLE_COL, title, YALIGN_COL, 0.25, URI_COL, uri, TITLE_COL, title, YALIGN_COL, 0.25,
FAVICON_COL, icon, -1); FAVICON_COL, icon, -1);
matches++; matches++;
result = sqlite3_step (statement); result = sqlite3_step (stmt);
} }
while (result == SQLITE_ROW); while (result == SQLITE_ROW);
sqlite3_reset (stmt);
sqlite3_clear_bindings (stmt);
#endif #endif
} }