From 789b10bd191b5df2e20517fb1b43afa1d481379b Mon Sep 17 00:00:00 2001 From: Shawn Wilsher Date: Fri, 4 Jun 2010 10:09:53 -0700 Subject: [PATCH] Actually populate data on index creation. --- dom/indexedDB/IDBObjectStoreRequest.cpp | 51 ++++++++++++++----------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/dom/indexedDB/IDBObjectStoreRequest.cpp b/dom/indexedDB/IDBObjectStoreRequest.cpp index 0a4cef2dee9..3490992ed34 100644 --- a/dom/indexedDB/IDBObjectStoreRequest.cpp +++ b/dom/indexedDB/IDBObjectStoreRequest.cpp @@ -193,14 +193,14 @@ private: class CreateIndexHelper : public AsyncConnectionHelper { public: - CreateIndexHelper(IDBTransactionRequest* aDatabase, + CreateIndexHelper(IDBTransactionRequest* aTransaction, IDBRequest* aRequest, const nsAString& aName, const nsAString& aKeyPath, bool aUnique, bool aAutoIncrement, IDBObjectStoreRequest* aObjectStore) - : AsyncConnectionHelper(aDatabase, aRequest), mName(aName), + : AsyncConnectionHelper(aTransaction, aRequest), mName(aName), mKeyPath(aKeyPath), mUnique(aUnique), mAutoIncrement(aAutoIncrement), mObjectStore(aObjectStore), mId(LL_MININT) { } @@ -209,15 +209,7 @@ public: PRUint16 GetSuccessResult(nsIWritableVariant* aResult); private: - struct IndexData - { - PRInt64 odid; - nsString odkey; - nsString value; - }; - - PRUint16 GetDataFromObjectStore(mozIStorageConnection* aConnection, - nsTArray& _retval); + PRUint16 InsertDataFromObjectStore(mozIStorageConnection* aConnection); // In-params. nsString mName; @@ -1648,16 +1640,13 @@ CreateIndexHelper::DoDatabaseWork(mozIStorageConnection* aConnection) (void)aConnection->GetLastInsertRowID(&mId); // Now we need to populate the index with data from the object store. - nsTArray values; - GetDataFromObjectStore(aConnection, values); - // TODO insert the data from values into the db + InsertDataFromObjectStore(aConnection); return OK; } PRUint16 -CreateIndexHelper::GetDataFromObjectStore(mozIStorageConnection* aConnection, - nsTArray& _retval) +CreateIndexHelper::InsertDataFromObjectStore(mozIStorageConnection* aConnection) { nsCAutoString table; nsCAutoString columns; @@ -1681,24 +1670,42 @@ CreateIndexHelper::GetDataFromObjectStore(mozIStorageConnection* aConnection, PRBool hasResult; while (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) { - IndexData data; - data.odid = stmt->AsInt64(0); + nsCOMPtr insertStmt = + mTransaction->IndexUpdateStatement(mAutoIncrement, mUnique); + NS_ENSURE_TRUE(insertStmt, nsIIDBDatabaseException::UNKNOWN_ERR); + mozStorageStatementScoper scoper(insertStmt); + + rv = insertStmt->BindInt64ByName(NS_LITERAL_CSTRING("object_data_id"), + stmt->AsInt64(0)); + NS_ENSURE_SUCCESS(rv, nsIIDBDatabaseException::UNKNOWN_ERR); + if (!mAutoIncrement) { // XXX does this cause problems with the affinity? - rv = stmt->GetString(2, data.odkey); + nsString key; + rv = stmt->GetString(2, key); + NS_ENSURE_SUCCESS(rv, nsIIDBDatabaseException::UNKNOWN_ERR); + + rv = insertStmt->BindStringByName(NS_LITERAL_CSTRING("object_data_key"), + key); NS_ENSURE_SUCCESS(rv, nsIIDBDatabaseException::UNKNOWN_ERR); } - nsAutoString json; + nsString json; rv = stmt->GetString(1, json); NS_ENSURE_SUCCESS(rv, nsIIDBDatabaseException::UNKNOWN_ERR); + nsString value; JSContext* cx = nsnull; rv = IDBObjectStoreRequest::GetKeyPathValueFromJSON(json, mKeyPath, &cx, - data.value); + value); // XXX this should be a constraint error maybe? NS_ENSURE_SUCCESS(rv, nsIIDBDatabaseException::UNKNOWN_ERR); - _retval.AppendElement(data); + + rv = insertStmt->BindStringByName(NS_LITERAL_CSTRING("value"), value); + NS_ENSURE_SUCCESS(rv, nsIIDBDatabaseException::UNKNOWN_ERR); + + rv = insertStmt->Execute(); + NS_ENSURE_SUCCESS(rv, nsIIDBDatabaseException::UNKNOWN_ERR); } return OK;