From e53f491d811f349dc29834e1384529c2ef50c903 Mon Sep 17 00:00:00 2001 From: Mark Capella Date: Fri, 27 Sep 2013 17:04:17 -0400 Subject: [PATCH] Bug 472963 - TableExists and IndexExists does not work for temporary tables, r=mak --- storage/src/mozStorageConnection.cpp | 4 +- storage/test/unit/test_storage_connection.js | 13 +++++++ toolkit/modules/Sqlite.jsm | 16 ++++---- toolkit/modules/tests/xpcshell/test_sqlite.js | 37 +++++++++++++++++++ 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/storage/src/mozStorageConnection.cpp b/storage/src/mozStorageConnection.cpp index af714e4b63e..11be0ad34a3 100644 --- a/storage/src/mozStorageConnection.cpp +++ b/storage/src/mozStorageConnection.cpp @@ -704,7 +704,9 @@ Connection::databaseElementExists(enum DatabaseElementType aElementType, { if (!mDBConn) return NS_ERROR_NOT_INITIALIZED; - nsAutoCString query("SELECT name FROM sqlite_master WHERE type = '"); + nsCString query("SELECT name FROM (SELECT * FROM sqlite_master UNION ALL " + "SELECT * FROM sqlite_temp_master) " + "WHERE type = '"); switch (aElementType) { case INDEX: query.Append("index"); diff --git a/storage/test/unit/test_storage_connection.js b/storage/test/unit/test_storage_connection.js index 540785321d2..2ad2f315cc9 100644 --- a/storage/test/unit/test_storage_connection.js +++ b/storage/test/unit/test_storage_connection.js @@ -108,6 +108,19 @@ add_task(function test_indexExists_not_created() do_check_false(msc.indexExists("foo")); }); +add_task(function test_temp_tableExists_and_indexExists() +{ + var msc = getOpenedDatabase(); + msc.executeSimpleSQL("CREATE TEMP TABLE test_temp(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)"); + do_check_true(msc.tableExists("test_temp")); + + msc.executeSimpleSQL("CREATE INDEX test_temp_ind ON test_temp (name)"); + do_check_true(msc.indexExists("test_temp_ind")); + + msc.executeSimpleSQL("DROP INDEX test_temp_ind"); + msc.executeSimpleSQL("DROP TABLE test_temp"); +}); + add_task(function test_createTable_not_created() { var msc = getOpenedDatabase(); diff --git a/toolkit/modules/Sqlite.jsm b/toolkit/modules/Sqlite.jsm index 2a7dff36bb6..737ccd44e53 100644 --- a/toolkit/modules/Sqlite.jsm +++ b/toolkit/modules/Sqlite.jsm @@ -600,9 +600,7 @@ OpenedConnection.prototype = Object.freeze({ }, /** - * Whether a table exists in the database. - * - * IMPROVEMENT: Look for temporary tables. + * Whether a table exists in the database (both persistent and temporary tables). * * @param name * (string) Name of the table. @@ -611,7 +609,9 @@ OpenedConnection.prototype = Object.freeze({ */ tableExists: function (name) { return this.execute( - "SELECT name FROM sqlite_master WHERE type='table' AND name=?", + "SELECT name FROM (SELECT * FROM sqlite_master UNION ALL " + + "SELECT * FROM sqlite_temp_master) " + + "WHERE type = 'table' AND name=?", [name]) .then(function onResult(rows) { return Promise.resolve(rows.length > 0); @@ -620,9 +620,7 @@ OpenedConnection.prototype = Object.freeze({ }, /** - * Whether a named index exists. - * - * IMPROVEMENT: Look for indexes in temporary tables. + * Whether a named index exists (both persistent and temporary tables). * * @param name * (string) Name of the index. @@ -631,7 +629,9 @@ OpenedConnection.prototype = Object.freeze({ */ indexExists: function (name) { return this.execute( - "SELECT name FROM sqlite_master WHERE type='index' AND name=?", + "SELECT name FROM (SELECT * FROM sqlite_master UNION ALL " + + "SELECT * FROM sqlite_temp_master) " + + "WHERE type = 'index' AND name=?", [name]) .then(function onResult(rows) { return Promise.resolve(rows.length > 0); diff --git a/toolkit/modules/tests/xpcshell/test_sqlite.js b/toolkit/modules/tests/xpcshell/test_sqlite.js index 7fcf947161e..ca5908dfa46 100644 --- a/toolkit/modules/tests/xpcshell/test_sqlite.js +++ b/toolkit/modules/tests/xpcshell/test_sqlite.js @@ -60,6 +60,22 @@ function getDummyDatabase(name, extraOptions={}) { throw new Task.Result(c); } +function getDummyTempDatabase(name, extraOptions={}) { + const TABLES = { + dirs: "id INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT", + files: "id INTEGER PRIMARY KEY AUTOINCREMENT, dir_id INTEGER, path TEXT", + }; + + let c = yield getConnection(name, extraOptions); + c._initialStatementCount = 0; + + for (let [k, v] in Iterator(TABLES)) { + yield c.execute("CREATE TEMP TABLE " + k + "(" + v + ")"); + c._initialStatementCount++; + } + + throw new Task.Result(c); +} function run_test() { Cu.import("resource://testing-common/services-common/logging.js"); @@ -203,6 +219,27 @@ add_task(function test_index_exists() { yield c.close(); }); +add_task(function test_temp_table_exists() { + let c = yield getDummyTempDatabase("temp_table_exists"); + + do_check_false(yield c.tableExists("temp_does_not_exist")); + do_check_true(yield c.tableExists("dirs")); + do_check_true(yield c.tableExists("files")); + + yield c.close(); +}); + +add_task(function test_temp_index_exists() { + let c = yield getDummyTempDatabase("temp_index_exists"); + + do_check_false(yield c.indexExists("temp_does_not_exist")); + + yield c.execute("CREATE INDEX my_index ON dirs (path)"); + do_check_true(yield c.indexExists("my_index")); + + yield c.close(); +}); + add_task(function test_close_cached() { let c = yield getDummyDatabase("close_cached");