2010-06-23 19:46:08 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Indexed Database.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Ben Turner <bent.mozilla@gmail.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
#include "IDBTransaction.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2010-08-26 20:57:25 +00:00
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "mozilla/storage.h"
|
2011-10-03 19:11:31 +00:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2010-11-10 23:26:03 +00:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-08-26 20:57:25 +00:00
|
|
|
#include "nsPIDOMWindow.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2010-11-15 21:49:49 +00:00
|
|
|
#include "AsyncConnectionHelper.h"
|
2010-09-09 22:15:40 +00:00
|
|
|
#include "DatabaseInfo.h"
|
2010-06-28 18:51:06 +00:00
|
|
|
#include "IDBCursor.h"
|
2010-09-09 22:15:40 +00:00
|
|
|
#include "IDBEvents.h"
|
2010-06-28 16:44:30 +00:00
|
|
|
#include "IDBFactory.h"
|
2010-09-09 22:15:40 +00:00
|
|
|
#include "IDBObjectStore.h"
|
2011-08-26 08:21:35 +00:00
|
|
|
#include "IndexedDatabaseManager.h"
|
2010-06-23 19:46:08 +00:00
|
|
|
#include "TransactionThreadPool.h"
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
#define SAVEPOINT_NAME "savepoint"
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
PLDHashOperator
|
|
|
|
DoomCachedStatements(const nsACString& aQuery,
|
|
|
|
nsCOMPtr<mozIStorageStatement>& aStatement,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
CommitHelper* helper = static_cast<CommitHelper*>(aUserArg);
|
|
|
|
helper->AddDoomedObject(aStatement);
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// static
|
2010-06-28 16:46:21 +00:00
|
|
|
already_AddRefed<IDBTransaction>
|
2010-09-09 22:15:38 +00:00
|
|
|
IDBTransaction::Create(IDBDatabase* aDatabase,
|
2010-06-28 16:46:21 +00:00
|
|
|
nsTArray<nsString>& aObjectStoreNames,
|
|
|
|
PRUint16 aMode,
|
2010-11-15 21:49:49 +00:00
|
|
|
PRUint32 aTimeout,
|
|
|
|
bool aDispatchDelayed)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
nsRefPtr<IDBTransaction> transaction = new IDBTransaction();
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2010-09-09 22:15:38 +00:00
|
|
|
transaction->mScriptContext = aDatabase->ScriptContext();
|
|
|
|
transaction->mOwner = aDatabase->Owner();
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
transaction->mDatabase = aDatabase;
|
|
|
|
transaction->mMode = aMode;
|
|
|
|
transaction->mTimeout = aTimeout;
|
|
|
|
|
|
|
|
if (!transaction->mObjectStoreNames.AppendElements(aObjectStoreNames)) {
|
|
|
|
NS_ERROR("Out of memory!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!transaction->mCachedStatements.Init()) {
|
|
|
|
NS_ERROR("Failed to initialize hash!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-11-15 21:49:49 +00:00
|
|
|
if (!aDispatchDelayed) {
|
2011-06-24 21:12:54 +00:00
|
|
|
nsCOMPtr<nsIThreadInternal> thread =
|
2010-11-18 22:19:19 +00:00
|
|
|
do_QueryInterface(NS_GetCurrentThread());
|
|
|
|
NS_ENSURE_TRUE(thread, nsnull);
|
|
|
|
|
|
|
|
// We need the current recursion depth first.
|
|
|
|
PRUint32 depth;
|
|
|
|
nsresult rv = thread->GetRecursionDepth(&depth);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
|
|
|
NS_ASSERTION(depth, "This should never be 0!");
|
|
|
|
transaction->mCreatedRecursionDepth = depth - 1;
|
|
|
|
|
|
|
|
rv = thread->AddObserver(transaction);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
2010-11-15 21:49:49 +00:00
|
|
|
transaction->mCreating = true;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::IDBTransaction()
|
2010-06-23 19:46:08 +00:00
|
|
|
: mReadyState(nsIIDBTransaction::INITIAL),
|
|
|
|
mMode(nsIIDBTransaction::READ_ONLY),
|
|
|
|
mTimeout(0),
|
|
|
|
mPendingRequests(0),
|
2010-11-18 22:19:19 +00:00
|
|
|
mCreatedRecursionDepth(0),
|
2010-06-23 19:46:08 +00:00
|
|
|
mSavepointCount(0),
|
2010-11-10 23:26:03 +00:00
|
|
|
mAborted(false),
|
2010-11-15 21:49:49 +00:00
|
|
|
mCreating(false)
|
2010-12-15 21:21:17 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mFiredCompleteOrAbort(false)
|
|
|
|
#endif
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::~IDBTransaction()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!mPendingRequests, "Should have no pending requests here!");
|
|
|
|
NS_ASSERTION(!mSavepointCount, "Should have released them all!");
|
|
|
|
NS_ASSERTION(!mConnection, "Should have called CommitOrRollback!");
|
2010-11-15 21:49:49 +00:00
|
|
|
NS_ASSERTION(!mCreating, "Should have been cleared already!");
|
2010-12-15 21:21:17 +00:00
|
|
|
NS_ASSERTION(mFiredCompleteOrAbort, "Should have fired event!");
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
if (mListenerManager) {
|
|
|
|
mListenerManager->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::OnNewRequest()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
if (!mPendingRequests) {
|
|
|
|
NS_ASSERTION(mReadyState == nsIIDBTransaction::INITIAL,
|
|
|
|
"Reusing a transaction!");
|
|
|
|
mReadyState = nsIIDBTransaction::LOADING;
|
|
|
|
}
|
|
|
|
++mPendingRequests;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::OnRequestFinished()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(mPendingRequests, "Mismatched calls!");
|
|
|
|
--mPendingRequests;
|
|
|
|
if (!mPendingRequests) {
|
2011-01-27 21:47:35 +00:00
|
|
|
NS_ASSERTION(mAborted || mReadyState == nsIIDBTransaction::LOADING,
|
|
|
|
"Bad state!");
|
|
|
|
mReadyState = IDBTransaction::COMMITTING;
|
2010-06-23 19:46:08 +00:00
|
|
|
CommitOrRollback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::CommitOrRollback()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-10-20 00:46:37 +00:00
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
TransactionThreadPool* pool = TransactionThreadPool::GetOrCreate();
|
|
|
|
NS_ENSURE_STATE(pool);
|
|
|
|
|
|
|
|
nsRefPtr<CommitHelper> helper(new CommitHelper(this));
|
|
|
|
|
|
|
|
mCachedStatements.Enumerate(DoomCachedStatements, helper);
|
|
|
|
NS_ASSERTION(!mCachedStatements.Count(), "Statements left!");
|
|
|
|
|
|
|
|
nsresult rv = pool->Dispatch(this, helper, true, helper);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::StartSavepoint()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_PRECONDITION(mConnection, "No connection!");
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = GetCachedStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SAVEPOINT " SAVEPOINT_NAME
|
|
|
|
));
|
|
|
|
NS_ENSURE_TRUE(stmt, false);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
nsresult rv = stmt->Execute();
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
++mSavepointCount;
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::ReleaseSavepoint()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_PRECONDITION(mConnection, "No connection!");
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
NS_ASSERTION(mSavepointCount, "Mismatch!");
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = GetCachedStatement(NS_LITERAL_CSTRING(
|
|
|
|
"RELEASE SAVEPOINT " SAVEPOINT_NAME
|
|
|
|
));
|
|
|
|
NS_ENSURE_TRUE(stmt, false);
|
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
--mSavepointCount;
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::RollbackSavepoint()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_PRECONDITION(mConnection, "No connection!");
|
|
|
|
|
|
|
|
NS_ASSERTION(mSavepointCount == 1, "Mismatch!");
|
|
|
|
mSavepointCount = 0;
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = GetCachedStatement(NS_LITERAL_CSTRING(
|
|
|
|
"ROLLBACK TO SAVEPOINT " SAVEPOINT_NAME
|
|
|
|
));
|
|
|
|
NS_ENSURE_TRUE(stmt,);
|
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv,);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetOrCreateConnection(mozIStorageConnection** aResult)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-09-10 19:12:11 +00:00
|
|
|
if (mDatabase->IsInvalidated()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
if (!mConnection) {
|
|
|
|
nsCOMPtr<mozIStorageConnection> connection =
|
2010-06-28 16:44:30 +00:00
|
|
|
IDBFactory::GetConnection(mDatabase->FilePath());
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_ENSURE_TRUE(connection, NS_ERROR_FAILURE);
|
|
|
|
|
2011-01-13 20:11:26 +00:00
|
|
|
nsCString beginTransaction;
|
2011-09-28 10:04:21 +00:00
|
|
|
if (mMode != nsIIDBTransaction::READ_ONLY) {
|
2011-01-13 20:11:26 +00:00
|
|
|
beginTransaction.AssignLiteral("BEGIN IMMEDIATE TRANSACTION;");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
beginTransaction.AssignLiteral("BEGIN TRANSACTION;");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = connection->CreateStatement(beginTransaction,
|
|
|
|
getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
2010-10-19 17:58:52 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
connection.swap(mConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageConnection> result(mConnection);
|
|
|
|
result.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::AddStatement(bool aCreate,
|
|
|
|
bool aOverwrite,
|
|
|
|
bool aAutoIncrement)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (!aCreate) {
|
|
|
|
NS_ASSERTION(aOverwrite, "Bad param combo!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (aAutoIncrement) {
|
|
|
|
if (aCreate) {
|
|
|
|
if (aOverwrite) {
|
|
|
|
return GetCachedStatement(
|
2010-08-04 21:44:26 +00:00
|
|
|
"INSERT OR FAIL INTO ai_object_data (object_store_id, id, data) "
|
2010-06-23 19:46:08 +00:00
|
|
|
"VALUES (:osid, :key_value, :data)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT INTO ai_object_data (object_store_id, data) "
|
|
|
|
"VALUES (:osid, :data)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"UPDATE ai_object_data "
|
|
|
|
"SET data = :data "
|
|
|
|
"WHERE object_store_id = :osid "
|
|
|
|
"AND id = :key_value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (aCreate) {
|
|
|
|
if (aOverwrite) {
|
|
|
|
return GetCachedStatement(
|
2010-08-04 21:44:26 +00:00
|
|
|
"INSERT OR FAIL INTO object_data (object_store_id, key_value, data) "
|
2010-06-23 19:46:08 +00:00
|
|
|
"VALUES (:osid, :key_value, :data)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT INTO object_data (object_store_id, key_value, data) "
|
|
|
|
"VALUES (:osid, :key_value, :data)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"UPDATE object_data "
|
|
|
|
"SET data = :data "
|
|
|
|
"WHERE object_store_id = :osid "
|
|
|
|
"AND key_value = :key_value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
2010-11-10 23:25:57 +00:00
|
|
|
IDBTransaction::DeleteStatement(bool aAutoIncrement)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
if (aAutoIncrement) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"DELETE FROM ai_object_data "
|
|
|
|
"WHERE id = :key_value "
|
|
|
|
"AND object_store_id = :osid"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"DELETE FROM object_data "
|
|
|
|
"WHERE key_value = :key_value "
|
|
|
|
"AND object_store_id = :osid"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetStatement(bool aAutoIncrement)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
if (aAutoIncrement) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT data "
|
|
|
|
"FROM ai_object_data "
|
|
|
|
"WHERE id = :id "
|
|
|
|
"AND object_store_id = :osid"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT data "
|
|
|
|
"FROM object_data "
|
|
|
|
"WHERE key_value = :id "
|
|
|
|
"AND object_store_id = :osid"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::IndexGetStatement(bool aUnique,
|
|
|
|
bool aAutoIncrement)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
if (aAutoIncrement) {
|
|
|
|
if (aUnique) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT ai_object_data_id "
|
|
|
|
"FROM ai_unique_index_data "
|
|
|
|
"WHERE index_id = :index_id "
|
|
|
|
"AND value = :value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT ai_object_data_id "
|
|
|
|
"FROM ai_index_data "
|
|
|
|
"WHERE index_id = :index_id "
|
|
|
|
"AND value = :value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (aUnique) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT object_data_key "
|
|
|
|
"FROM unique_index_data "
|
|
|
|
"WHERE index_id = :index_id "
|
|
|
|
"AND value = :value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT object_data_key "
|
|
|
|
"FROM index_data "
|
|
|
|
"WHERE index_id = :index_id "
|
|
|
|
"AND value = :value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::IndexGetObjectStatement(bool aUnique,
|
|
|
|
bool aAutoIncrement)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
if (aAutoIncrement) {
|
|
|
|
if (aUnique) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT data "
|
|
|
|
"FROM ai_object_data "
|
|
|
|
"INNER JOIN ai_unique_index_data "
|
|
|
|
"ON ai_object_data.id = ai_unique_index_data.ai_object_data_id "
|
|
|
|
"WHERE index_id = :index_id "
|
|
|
|
"AND value = :value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT data "
|
|
|
|
"FROM ai_object_data "
|
|
|
|
"INNER JOIN ai_index_data "
|
|
|
|
"ON ai_object_data.id = ai_index_data.ai_object_data_id "
|
|
|
|
"WHERE index_id = :index_id "
|
|
|
|
"AND value = :value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (aUnique) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT data "
|
|
|
|
"FROM object_data "
|
|
|
|
"INNER JOIN unique_index_data "
|
|
|
|
"ON object_data.id = unique_index_data.object_data_id "
|
|
|
|
"WHERE index_id = :index_id "
|
|
|
|
"AND value = :value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"SELECT data "
|
|
|
|
"FROM object_data "
|
|
|
|
"INNER JOIN index_data "
|
|
|
|
"ON object_data.id = index_data.object_data_id "
|
|
|
|
"WHERE index_id = :index_id "
|
|
|
|
"AND value = :value"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::IndexUpdateStatement(bool aAutoIncrement,
|
|
|
|
bool aUnique,
|
|
|
|
bool aOverwrite)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
if (aAutoIncrement) {
|
|
|
|
if (aUnique) {
|
|
|
|
if (aOverwrite) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT OR REPLACE INTO ai_unique_index_data "
|
2011-04-13 07:26:20 +00:00
|
|
|
"(index_id, ai_object_data_id, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :value)"
|
2010-06-23 19:46:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT INTO ai_unique_index_data "
|
2011-04-13 07:26:20 +00:00
|
|
|
"(index_id, aI_object_data_id, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :value)"
|
2010-06-23 19:46:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (aOverwrite) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT OR REPLACE INTO ai_index_data "
|
2011-04-13 07:26:20 +00:00
|
|
|
"(index_id, ai_object_data_id, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :value)"
|
2010-06-23 19:46:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT INTO ai_index_data "
|
2011-04-13 07:26:20 +00:00
|
|
|
"(index_id, ai_object_data_id, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :value)"
|
2010-06-23 19:46:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (aUnique) {
|
|
|
|
if (aOverwrite) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT OR REPLACE INTO unique_index_data "
|
|
|
|
"(index_id, object_data_id, object_data_key, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :object_data_key, :value)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT INTO unique_index_data "
|
|
|
|
"(index_id, object_data_id, object_data_key, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :object_data_key, :value)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (aOverwrite) {
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT INTO index_data ("
|
|
|
|
"index_id, object_data_id, object_data_key, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :object_data_key, :value)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GetCachedStatement(
|
|
|
|
"INSERT INTO index_data ("
|
|
|
|
"index_id, object_data_id, object_data_key, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :object_data_key, :value)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetCachedStatement(const nsACString& aQuery)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!aQuery.IsEmpty(), "Empty sql statement!");
|
|
|
|
NS_ASSERTION(mConnection, "No connection!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
|
|
|
|
if (!mCachedStatements.Get(aQuery, getter_AddRefs(stmt))) {
|
|
|
|
nsresult rv = mConnection->CreateStatement(aQuery, getter_AddRefs(stmt));
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsCString error;
|
|
|
|
error.AppendLiteral("The statement `");
|
|
|
|
error.Append(aQuery);
|
|
|
|
error.AppendLiteral("` failed to compile with the error message `");
|
|
|
|
nsCString msg;
|
|
|
|
(void)mConnection->GetLastErrorString(msg);
|
|
|
|
error.Append(msg);
|
|
|
|
error.AppendLiteral("`.");
|
|
|
|
NS_ERROR(error.get());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
|
|
|
if (!mCachedStatements.Put(aQuery, stmt)) {
|
|
|
|
NS_ERROR("Out of memory?!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stmt.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-12-15 21:21:17 +00:00
|
|
|
IDBTransaction::IsOpen() const
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-11-15 21:49:49 +00:00
|
|
|
|
|
|
|
// If we haven't started anything then we're open.
|
|
|
|
if (mReadyState == nsIIDBTransaction::INITIAL) {
|
|
|
|
NS_ASSERTION(AsyncConnectionHelper::GetCurrentTransaction() != this,
|
|
|
|
"This should be some other transaction (or null)!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've already started then we need to check to see if we still have the
|
|
|
|
// mCreating flag set. If we do (i.e. we haven't returned to the event loop
|
|
|
|
// from the time we were created) then we are open. Otherwise check the
|
|
|
|
// currently running transaction to see if it's the same. We only allow other
|
|
|
|
// requests to be made if this transaction is currently running.
|
|
|
|
if (mReadyState == nsIIDBTransaction::LOADING) {
|
|
|
|
if (mCreating) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AsyncConnectionHelper::GetCurrentTransaction() == this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-10 23:25:40 +00:00
|
|
|
already_AddRefed<IDBObjectStore>
|
|
|
|
IDBTransaction::GetOrCreateObjectStore(const nsAString& aName,
|
|
|
|
ObjectStoreInfo* aObjectStoreInfo)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!aName.IsEmpty(), "Empty name!");
|
|
|
|
NS_ASSERTION(aObjectStoreInfo, "Null pointer!");
|
|
|
|
|
|
|
|
nsRefPtr<IDBObjectStore> retval;
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < mCreatedObjectStores.Length(); index++) {
|
|
|
|
nsRefPtr<IDBObjectStore>& objectStore = mCreatedObjectStores[index];
|
|
|
|
if (objectStore->Name() == aName) {
|
|
|
|
retval = objectStore;
|
|
|
|
return retval.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = IDBObjectStore::Create(this, aObjectStoreInfo);
|
|
|
|
NS_ENSURE_TRUE(retval, nsnull);
|
|
|
|
|
|
|
|
if (!mCreatedObjectStores.AppendElement(retval)) {
|
|
|
|
NS_WARNING("Out of memory!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval.forget();
|
|
|
|
}
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBTransaction)
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(IDBTransaction,
|
2010-06-23 19:46:08 +00:00
|
|
|
nsDOMEventTargetHelper)
|
2010-09-09 22:15:38 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mDatabase,
|
2011-06-24 02:18:00 +00:00
|
|
|
nsIDOMEventTarget)
|
2010-11-10 23:26:03 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnErrorListener)
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnCompleteListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnAbortListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnTimeoutListener)
|
2010-11-10 23:25:40 +00:00
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < tmp->mCreatedObjectStores.Length(); i++) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mCreatedObjectStores[i]");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIIDBObjectStore*>(
|
|
|
|
tmp->mCreatedObjectStores[i].get()));
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(IDBTransaction,
|
2010-06-23 19:46:08 +00:00
|
|
|
nsDOMEventTargetHelper)
|
2010-09-09 22:15:38 +00:00
|
|
|
// Don't unlink mDatabase!
|
2010-11-10 23:26:03 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnErrorListener)
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnCompleteListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnAbortListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnTimeoutListener)
|
2010-11-10 23:25:40 +00:00
|
|
|
|
|
|
|
tmp->mCreatedObjectStores.Clear();
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBTransaction)
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBTransaction)
|
2010-11-18 22:19:19 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIThreadObserver)
|
2010-06-28 16:46:21 +00:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBTransaction)
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(IDBTransaction, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(IDBTransaction, nsDOMEventTargetHelper)
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2010-06-28 16:46:21 +00:00
|
|
|
DOMCI_DATA(IDBTransaction, IDBTransaction)
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetDb(nsIIDBDatabase** aDB)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
NS_ADDREF(*aDB = mDatabase);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetReadyState(PRUint16* aReadyState)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
*aReadyState = mReadyState;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetMode(PRUint16* aMode)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
*aMode = mMode;
|
2010-06-23 19:46:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetObjectStoreNames(nsIDOMDOMStringList** aObjectStores)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<nsDOMStringList> list(new nsDOMStringList());
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
nsAutoTArray<nsString, 10> stackArray;
|
2010-06-23 19:46:08 +00:00
|
|
|
nsTArray<nsString>* arrayOfNames;
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
if (mMode == IDBTransaction::VERSION_CHANGE) {
|
2010-06-23 19:46:08 +00:00
|
|
|
DatabaseInfo* info;
|
|
|
|
if (!DatabaseInfo::Get(mDatabase->Id(), &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info->GetObjectStoreNames(stackArray)) {
|
|
|
|
NS_ERROR("Out of memory!");
|
2010-11-10 23:25:44 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
arrayOfNames = &stackArray;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
arrayOfNames = &mObjectStoreNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 count = arrayOfNames->Length();
|
|
|
|
for (PRUint32 index = 0; index < count; index++) {
|
|
|
|
NS_ENSURE_TRUE(list->Add(arrayOfNames->ElementAt(index)),
|
2010-11-10 23:25:44 +00:00
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
2010-11-10 23:25:44 +00:00
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
list.forget(aObjectStores);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::ObjectStore(const nsAString& aName,
|
2010-06-28 18:51:06 +00:00
|
|
|
nsIIDBObjectStore** _retval)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-15 21:21:17 +00:00
|
|
|
if (!IsOpen()) {
|
2010-11-10 23:25:44 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectStoreInfo* info = nsnull;
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
if (mMode == nsIIDBTransaction::VERSION_CHANGE ||
|
|
|
|
mObjectStoreNames.Contains(aName)) {
|
|
|
|
ObjectStoreInfo::Get(mDatabase->Id(), aName, &info);
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!info) {
|
2010-11-10 23:25:44 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-10 23:25:40 +00:00
|
|
|
nsRefPtr<IDBObjectStore> objectStore = GetOrCreateObjectStore(aName, info);
|
2010-11-10 23:25:44 +00:00
|
|
|
NS_ENSURE_TRUE(objectStore, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
|
|
|
objectStore.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::Abort()
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-01-27 21:47:35 +00:00
|
|
|
// We can't use IsOpen here since we need it to be possible to call Abort()
|
|
|
|
// even from outside of transaction callbacks.
|
|
|
|
if (mReadyState != IDBTransaction::INITIAL &&
|
|
|
|
mReadyState != IDBTransaction::LOADING) {
|
2010-11-10 23:25:44 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 21:21:17 +00:00
|
|
|
bool needToCommitOrRollback = mReadyState == nsIIDBTransaction::INITIAL;
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
mAborted = true;
|
|
|
|
mReadyState = nsIIDBTransaction::DONE;
|
2010-12-15 21:21:17 +00:00
|
|
|
|
|
|
|
// Fire the abort event if there are no outstanding requests. Otherwise the
|
|
|
|
// abort event will be fired when all outdtanding requests finish.
|
|
|
|
if (needToCommitOrRollback) {
|
|
|
|
return CommitOrRollback();
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-10 23:26:03 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBTransaction::SetOnerror(nsIDOMEventListener* aErrorListener)
|
|
|
|
{
|
|
|
|
return RemoveAddEventListener(NS_LITERAL_STRING(ERROR_EVT_STR),
|
|
|
|
mOnErrorListener, aErrorListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBTransaction::GetOnerror(nsIDOMEventListener** aErrorListener)
|
|
|
|
{
|
|
|
|
return GetInnerEventListener(mOnErrorListener, aErrorListener);
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetOncomplete(nsIDOMEventListener** aOncomplete)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return GetInnerEventListener(mOnCompleteListener, aOncomplete);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::SetOncomplete(nsIDOMEventListener* aOncomplete)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return RemoveAddEventListener(NS_LITERAL_STRING(COMPLETE_EVT_STR),
|
|
|
|
mOnCompleteListener, aOncomplete);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetOnabort(nsIDOMEventListener** aOnabort)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return GetInnerEventListener(mOnAbortListener, aOnabort);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::SetOnabort(nsIDOMEventListener* aOnabort)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return RemoveAddEventListener(NS_LITERAL_STRING(ABORT_EVT_STR),
|
|
|
|
mOnAbortListener, aOnabort);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::GetOntimeout(nsIDOMEventListener** aOntimeout)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return GetInnerEventListener(mOnTimeoutListener, aOntimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 16:46:21 +00:00
|
|
|
IDBTransaction::SetOntimeout(nsIDOMEventListener* aOntimeout)
|
2010-06-23 19:46:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return RemoveAddEventListener(NS_LITERAL_STRING(TIMEOUT_EVT_STR),
|
|
|
|
mOnTimeoutListener, aOntimeout);
|
|
|
|
}
|
|
|
|
|
2010-11-10 23:26:03 +00:00
|
|
|
nsresult
|
|
|
|
IDBTransaction::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
aVisitor.mCanHandle = true;
|
2010-11-10 23:26:03 +00:00
|
|
|
aVisitor.mParentTarget = mDatabase;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-18 22:19:19 +00:00
|
|
|
// XXX Once nsIThreadObserver gets split this method will disappear.
|
2010-11-15 21:49:49 +00:00
|
|
|
NS_IMETHODIMP
|
2010-11-18 22:19:19 +00:00
|
|
|
IDBTransaction::OnDispatchedEvent(nsIThreadInternal* aThread)
|
2010-11-15 21:49:49 +00:00
|
|
|
{
|
2010-11-18 22:19:19 +00:00
|
|
|
NS_NOTREACHED("Don't call me!");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-11-15 21:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-11-18 22:19:19 +00:00
|
|
|
IDBTransaction::OnProcessNextEvent(nsIThreadInternal* aThread,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aMayWait,
|
2010-11-15 21:49:49 +00:00
|
|
|
PRUint32 aRecursionDepth)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-11-18 22:19:19 +00:00
|
|
|
NS_ASSERTION(aRecursionDepth > mCreatedRecursionDepth,
|
|
|
|
"Should be impossible!");
|
|
|
|
NS_ASSERTION(mCreating, "Should be true!");
|
2010-11-15 21:49:49 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-11-18 22:19:19 +00:00
|
|
|
IDBTransaction::AfterProcessNextEvent(nsIThreadInternal* aThread,
|
2010-11-15 21:49:49 +00:00
|
|
|
PRUint32 aRecursionDepth)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aThread, "This should never be null!");
|
2010-11-18 22:19:19 +00:00
|
|
|
NS_ASSERTION(aRecursionDepth >= mCreatedRecursionDepth,
|
|
|
|
"Should be impossible!");
|
|
|
|
NS_ASSERTION(mCreating, "Should be true!");
|
2010-11-15 21:49:49 +00:00
|
|
|
|
2010-11-18 22:19:19 +00:00
|
|
|
if (aRecursionDepth == mCreatedRecursionDepth) {
|
|
|
|
// We're back at the event loop, no longer newborn.
|
|
|
|
mCreating = false;
|
2010-11-15 21:49:49 +00:00
|
|
|
|
2010-11-18 22:19:19 +00:00
|
|
|
// Maybe set the readyState to DONE if there were no requests generated.
|
|
|
|
if (mReadyState == nsIIDBTransaction::INITIAL) {
|
|
|
|
mReadyState = nsIIDBTransaction::DONE;
|
2010-12-15 21:21:17 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(CommitOrRollback())) {
|
|
|
|
NS_WARNING("Failed to commit!");
|
|
|
|
}
|
2010-11-18 22:19:19 +00:00
|
|
|
}
|
2010-11-15 21:49:49 +00:00
|
|
|
|
2010-11-18 22:19:19 +00:00
|
|
|
// No longer need to observe thread events.
|
2011-06-24 21:12:54 +00:00
|
|
|
if(NS_FAILED(aThread->RemoveObserver(this))) {
|
2010-11-18 22:19:19 +00:00
|
|
|
NS_ERROR("Failed to remove observer!");
|
|
|
|
}
|
2010-11-15 21:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-09 22:15:38 +00:00
|
|
|
CommitHelper::CommitHelper(IDBTransaction* aTransaction)
|
|
|
|
: mTransaction(aTransaction),
|
|
|
|
mAborted(!!aTransaction->mAborted),
|
2010-10-19 17:58:52 +00:00
|
|
|
mHaveMetadata(false)
|
2010-09-09 22:15:38 +00:00
|
|
|
{
|
|
|
|
mConnection.swap(aTransaction->mConnection);
|
|
|
|
}
|
|
|
|
|
2010-10-19 17:58:52 +00:00
|
|
|
CommitHelper::~CommitHelper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(CommitHelper, nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CommitHelper::Run()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
NS_ASSERTION(mDoomedObjects.IsEmpty(), "Didn't release doomed objects!");
|
|
|
|
|
2010-10-20 00:46:37 +00:00
|
|
|
mTransaction->mReadyState = nsIIDBTransaction::DONE;
|
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
if (mAborted) {
|
2010-10-19 17:58:52 +00:00
|
|
|
if (mHaveMetadata) {
|
|
|
|
NS_ASSERTION(mTransaction->Mode() == nsIIDBTransaction::VERSION_CHANGE,
|
|
|
|
"Bad transaction type!");
|
|
|
|
|
|
|
|
DatabaseInfo* dbInfo;
|
|
|
|
if (!DatabaseInfo::Get(mTransaction->Database()->Id(), &dbInfo)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(IDBFactory::UpdateDatabaseMetadata(dbInfo, mOldVersion,
|
|
|
|
mOldObjectStores))) {
|
|
|
|
NS_WARNING("Failed to update database metadata!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(mOldObjectStores.IsEmpty(), "Should have swapped!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 06:21:36 +00:00
|
|
|
event = CreateGenericEvent(NS_LITERAL_STRING(ABORT_EVT_STR));
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-01-07 06:21:36 +00:00
|
|
|
event = CreateGenericEvent(NS_LITERAL_STRING(COMPLETE_EVT_STR));
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
2010-11-10 23:25:44 +00:00
|
|
|
NS_ENSURE_TRUE(event, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool dummy;
|
2010-06-23 19:46:08 +00:00
|
|
|
if (NS_FAILED(mTransaction->DispatchEvent(event, &dummy))) {
|
|
|
|
NS_WARNING("Dispatch failed!");
|
|
|
|
}
|
|
|
|
|
2010-12-15 21:21:17 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
mTransaction->mFiredCompleteOrAbort = true;
|
|
|
|
#endif
|
2010-06-23 19:46:08 +00:00
|
|
|
mTransaction = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-10 19:12:11 +00:00
|
|
|
IDBDatabase* database = mTransaction->Database();
|
|
|
|
if (database->IsInvalidated()) {
|
|
|
|
mAborted = true;
|
|
|
|
}
|
|
|
|
|
2010-12-15 21:21:17 +00:00
|
|
|
if (mConnection) {
|
2011-08-26 08:21:35 +00:00
|
|
|
IndexedDatabaseManager::SetCurrentDatabase(database);
|
2010-09-09 22:15:40 +00:00
|
|
|
|
2010-12-15 21:21:17 +00:00
|
|
|
if (!mAborted) {
|
2011-09-28 10:04:21 +00:00
|
|
|
NS_NAMED_LITERAL_CSTRING(release, "COMMIT TRANSACTION");
|
2010-12-15 21:21:17 +00:00
|
|
|
if (NS_FAILED(mConnection->ExecuteSimpleSQL(release))) {
|
2011-01-27 21:47:35 +00:00
|
|
|
mAborted = true;
|
2010-12-15 21:21:17 +00:00
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 21:21:17 +00:00
|
|
|
if (mAborted) {
|
|
|
|
NS_NAMED_LITERAL_CSTRING(rollback, "ROLLBACK TRANSACTION");
|
|
|
|
if (NS_FAILED(mConnection->ExecuteSimpleSQL(rollback))) {
|
|
|
|
NS_WARNING("Failed to rollback transaction!");
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
2010-12-15 21:21:17 +00:00
|
|
|
|
|
|
|
if (mTransaction->Mode() == nsIIDBTransaction::VERSION_CHANGE) {
|
|
|
|
nsresult rv =
|
|
|
|
IDBFactory::LoadDatabaseInformation(mConnection,
|
|
|
|
mTransaction->Database()->Id(),
|
|
|
|
mOldVersion, mOldObjectStores);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mHaveMetadata = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("Failed to get database information!");
|
|
|
|
}
|
2010-10-19 17:58:52 +00:00
|
|
|
}
|
2010-06-23 19:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mDoomedObjects.Clear();
|
|
|
|
|
2010-12-15 21:21:17 +00:00
|
|
|
if (mConnection) {
|
|
|
|
mConnection->Close();
|
|
|
|
mConnection = nsnull;
|
2010-06-23 19:46:08 +00:00
|
|
|
|
2011-08-26 08:21:35 +00:00
|
|
|
IndexedDatabaseManager::SetCurrentDatabase(nsnull);
|
2010-12-15 21:21:17 +00:00
|
|
|
}
|
2010-09-09 22:15:40 +00:00
|
|
|
|
2010-06-23 19:46:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|