From 8231aea4b0685a409e7c2952c42b1218426a9060 Mon Sep 17 00:00:00 2001 From: Tristan Van Berkom Date: Tue, 16 Apr 2013 17:18:53 +0900 Subject: [PATCH] EBookBackendSqliteDB: Avoid summary introspection on initial creation. The summary introspection does not introspect whether regular prefix indexes are created, which is acceptable since it does not effect queries. However since we introspect before creating the indexes, we lose the configured index information which is needed to initially create those indexes. This patch simply checks if the table exists when initially creating the summary and avoids the introspection if the table is being created for the first time. --- .../libedata-book/e-book-backend-sqlitedb.c | 50 +++++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/addressbook/libedata-book/e-book-backend-sqlitedb.c b/addressbook/libedata-book/e-book-backend-sqlitedb.c index 0a1ba71..e26d9bd 100644 --- a/addressbook/libedata-book/e-book-backend-sqlitedb.c +++ b/addressbook/libedata-book/e-book-backend-sqlitedb.c @@ -698,6 +698,47 @@ collect_columns_cb (gpointer ref, return 0; } +static gint +get_count_cb (gpointer ref, + gint n_cols, + gchar **cols, + gchar **name) +{ + gint64 count = 0; + gint *ret = ref; + gint i; + + for (i = 0; i < n_cols; i++) { + if (g_strcmp0 (name[i], "count(*)") == 0) { + count = g_ascii_strtoll (cols[i], NULL, 10); + } + } + + *ret = count; + + return 0; +} + +static gboolean +check_folderid_exists (EBookBackendSqliteDB *ebsdb, + const gchar *folderid, + gboolean *exists, + GError **error) +{ + gboolean success; + gint count = 0; + gchar *stmt; + + stmt = sqlite3_mprintf ("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=%Q;", folderid); + + success = book_backend_sql_exec (ebsdb->priv->db, stmt, get_count_cb, &count, error); + sqlite3_free (stmt); + + *exists = (count > 0); + + return success; +} + static gboolean introspect_summary (EBookBackendSqliteDB *ebsdb, const gchar *folderid, @@ -847,8 +888,12 @@ create_contacts_table (EBookBackendSqliteDB *ebsdb, gboolean success; gchar *stmt, *tmp; GString *string; + gboolean already_exists = FALSE; + + success = check_folderid_exists (ebsdb, folderid, &already_exists, error); + if (!success) + return FALSE; - /* Construct the create statement from the summary fields table */ string = g_string_new ( "CREATE TABLE IF NOT EXISTS %Q ( uid TEXT PRIMARY KEY, "); @@ -931,7 +976,8 @@ create_contacts_table (EBookBackendSqliteDB *ebsdb, g_free (tmp); } - if (success) + /* Dont introspect the summary if the table did not yet exist */ + if (success && already_exists) success = introspect_summary (ebsdb, folderid, error); /* Create indexes on the summary fields configured for indexing */ -- 2.7.4