Bug 472963 - TableExists and IndexExists does not work for temporary tables, r=mak
authorMark Capella <markcapella@twcny.rr.com>
Fri, 27 Sep 2013 17:04:17 -0400
changeset 162909 46583beb7d1bea492c3021aedbd3b3aa52f47ddc
parent 162908 17453b973aa50314c9c36a672ceb2ad4c0d6c4ea
child 162910 d3c5c7c61894138b26c658d6d3a3c77cbbb2b8fd
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs472963
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 472963 - TableExists and IndexExists does not work for temporary tables, r=mak
storage/src/mozStorageConnection.cpp
storage/test/unit/test_storage_connection.js
toolkit/modules/Sqlite.jsm
toolkit/modules/tests/xpcshell/test_sqlite.js
--- a/storage/src/mozStorageConnection.cpp
+++ b/storage/src/mozStorageConnection.cpp
@@ -699,17 +699,19 @@ Connection::initializeInternal(nsIFile* 
 
 nsresult
 Connection::databaseElementExists(enum DatabaseElementType aElementType,
                                   const nsACString &aElementName,
                                   bool *_exists)
 {
   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");
       break;
     case TABLE:
       query.Append("table");
       break;
   }
--- a/storage/test/unit/test_storage_connection.js
+++ b/storage/test/unit/test_storage_connection.js
@@ -103,16 +103,29 @@ add_task(function test_tableExists_not_c
 });
 
 add_task(function test_indexExists_not_created()
 {
   var msc = getOpenedDatabase();
   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();
   msc.createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
   do_check_true(msc.tableExists("test"));
 });
 
 add_task(function test_indexExists_created()
--- a/toolkit/modules/Sqlite.jsm
+++ b/toolkit/modules/Sqlite.jsm
@@ -595,48 +595,48 @@ OpenedConnection.prototype = Object.free
         deferred.reject(error);
       }.bind(this)
     );
 
     return deferred.promise;
   },
 
   /**
-   * 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.
    *
    * @return Promise<bool>
    */
   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);
       }
     );
   },
 
   /**
-   * 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.
    *
    * @return Promise<bool>
    */
   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);
       }
     );
   },
 
   /**
--- a/toolkit/modules/tests/xpcshell/test_sqlite.js
+++ b/toolkit/modules/tests/xpcshell/test_sqlite.js
@@ -55,16 +55,32 @@ function getDummyDatabase(name, extraOpt
   for (let [k, v] in Iterator(TABLES)) {
     yield c.execute("CREATE TABLE " + k + "(" + v + ")");
     c._initialStatementCount++;
   }
 
   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");
   initTestLogging("Trace");
 
   run_next_test();
 }
 
@@ -198,16 +214,37 @@ add_task(function test_index_exists() {
   do_check_false(yield c.indexExists("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_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");
 
   yield c.executeCached("SELECT * FROM dirs");
   yield c.executeCached("SELECT * FROM files");
 
   yield c.close();
 });