From 056d72865db63d99f74ee73b7efcadcd572d52a8 Mon Sep 17 00:00:00 2001 From: Ben Turner Date: Sun, 23 Jan 2011 10:16:00 -0800 Subject: [PATCH] Bug 628063 - 'IndexedDB: Indexes created after data are added are sometimes improperly populated'. r=sicking, a=blocking. --- dom/indexedDB/IDBIndex.h | 2 +- dom/indexedDB/IDBObjectStore.cpp | 50 +++++++----- dom/indexedDB/test/Makefile.in | 1 + .../test_create_index_with_integer_keys.html | 76 +++++++++++++++++++ 4 files changed, 109 insertions(+), 20 deletions(-) create mode 100644 dom/indexedDB/test/test_create_index_with_integer_keys.html diff --git a/dom/indexedDB/IDBIndex.h b/dom/indexedDB/IDBIndex.h index 64ec998794e2..b62c1cf8b36c 100644 --- a/dom/indexedDB/IDBIndex.h +++ b/dom/indexedDB/IDBIndex.h @@ -94,7 +94,7 @@ public: const nsString& KeyPath() const { - return mName; + return mKeyPath; } private: diff --git a/dom/indexedDB/IDBObjectStore.cpp b/dom/indexedDB/IDBObjectStore.cpp index f7fb6e97fcfb..4c432d43b8a6 100644 --- a/dom/indexedDB/IDBObjectStore.cpp +++ b/dom/indexedDB/IDBObjectStore.cpp @@ -251,9 +251,6 @@ private: // In-params. nsRefPtr mIndex; - - // Out-params. - PRInt64 mId; }; class DeleteIndexHelper : public AsyncConnectionHelper @@ -2129,8 +2126,13 @@ CreateIndexHelper::DoDatabaseWork(mozIStorageConnection* aConnection) return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR; } - // Get the id of this object store, and store it for future use. - (void)aConnection->GetLastInsertRowID(&mId); +#ifdef DEBUG + { + PRInt64 id; + aConnection->GetLastInsertRowID(&id); + NS_ASSERTION(mIndex->Id() == id, "Bad index id!"); + } +#endif // Now we need to populate the index with data from the object store. rv = InsertDataFromObjectStore(aConnection); @@ -2144,11 +2146,9 @@ CreateIndexHelper::DoDatabaseWork(mozIStorageConnection* aConnection) nsresult CreateIndexHelper::InsertDataFromObjectStore(mozIStorageConnection* aConnection) { - bool autoIncrement = mIndex->IsAutoIncrement(); - nsCAutoString table; nsCAutoString columns; - if (autoIncrement) { + if (mIndex->IsAutoIncrement()) { table.AssignLiteral("ai_object_data"); columns.AssignLiteral("id, data"); } @@ -2173,27 +2173,40 @@ CreateIndexHelper::InsertDataFromObjectStore(mozIStorageConnection* aConnection) PRBool hasResult; while (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) { nsCOMPtr insertStmt = - mTransaction->IndexUpdateStatement(autoIncrement, mIndex->IsUnique(), - false); + mTransaction->IndexUpdateStatement(mIndex->IsAutoIncrement(), + mIndex->IsUnique(), false); NS_ENSURE_TRUE(insertStmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); mozStorageStatementScoper scoper2(insertStmt); - rv = insertStmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"), mId); + rv = insertStmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"), + mIndex->Id()); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); rv = insertStmt->BindInt64ByName(NS_LITERAL_CSTRING("object_data_id"), stmt->AsInt64(0)); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); - if (!autoIncrement) { - // XXX does this cause problems with the affinity? - nsString key; - rv = stmt->GetString(2, key); + if (!mIndex->IsAutoIncrement()) { + NS_NAMED_LITERAL_CSTRING(objectDataKey, "object_data_key"); + + PRInt32 keyType; + rv = stmt->GetTypeOfIndex(2, &keyType); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); - rv = insertStmt->BindStringByName(NS_LITERAL_CSTRING("object_data_key"), - key); + if (keyType == mozIStorageStatement::VALUE_TYPE_INTEGER) { + rv = insertStmt->BindInt64ByName(objectDataKey, stmt->AsInt64(2)); + } + else if (keyType == mozIStorageStatement::VALUE_TYPE_TEXT) { + nsString stringKey; + rv = stmt->GetString(2, stringKey); + NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); + + rv = insertStmt->BindStringByName(objectDataKey, stringKey); + } + else { + NS_NOTREACHED("Bad SQLite type!"); + } NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR); } @@ -2209,12 +2222,11 @@ CreateIndexHelper::InsertDataFromObjectStore(mozIStorageConnection* aConnection) &cx, key); NS_ENSURE_SUCCESS(rv, rv); - NS_NAMED_LITERAL_CSTRING(value, "value"); - if (key.IsUnset()) { continue; } + NS_NAMED_LITERAL_CSTRING(value, "value"); if (key.IsInt()) { rv = insertStmt->BindInt64ByName(value, key.IntValue()); } diff --git a/dom/indexedDB/test/Makefile.in b/dom/indexedDB/test/Makefile.in index 00d8cfcd59bb..07989d4c3924 100644 --- a/dom/indexedDB/test/Makefile.in +++ b/dom/indexedDB/test/Makefile.in @@ -56,6 +56,7 @@ TEST_FILES = \ test_bfcache.html \ test_clear.html \ test_create_index.html \ + test_create_index_with_integer_keys.html \ test_create_objectStore.html \ test_cursors.html \ test_cursor_mutation.html \ diff --git a/dom/indexedDB/test/test_create_index_with_integer_keys.html b/dom/indexedDB/test/test_create_index_with_integer_keys.html new file mode 100644 index 000000000000..eacebd06025e --- /dev/null +++ b/dom/indexedDB/test/test_create_index_with_integer_keys.html @@ -0,0 +1,76 @@ + + + + Indexed Database Test + + + + + + + + + + + +