2010-06-23 23:46:08 +04: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 22:51:06 +04:00
|
|
|
#include "IDBCursor.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#include "nsIVariant.h"
|
|
|
|
|
2010-08-27 00:57:25 +04:00
|
|
|
#include "jscntxt.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
2010-11-11 02:26:03 +03:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsJSON.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "AsyncConnectionHelper.h"
|
|
|
|
#include "DatabaseInfo.h"
|
|
|
|
#include "IDBEvents.h"
|
2010-06-28 22:51:06 +04:00
|
|
|
#include "IDBIndex.h"
|
2010-06-28 22:51:06 +04:00
|
|
|
#include "IDBObjectStore.h"
|
2010-06-28 20:46:21 +04:00
|
|
|
#include "IDBTransaction.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "TransactionThreadPool.h"
|
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class UpdateHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 20:46:21 +04:00
|
|
|
UpdateHelper(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
IDBRequest* aRequest,
|
|
|
|
PRInt64 aObjectStoreID,
|
|
|
|
const nsAString& aValue,
|
|
|
|
const Key& aKey,
|
|
|
|
bool aAutoIncrement,
|
|
|
|
nsTArray<IndexUpdateInfo>& aIndexUpdateInfo)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mOSID(aObjectStoreID),
|
|
|
|
mValue(aValue), mKey(aKey), mAutoIncrement(aAutoIncrement)
|
|
|
|
{
|
|
|
|
mIndexUpdateInfo.SwapElements(aIndexUpdateInfo);
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult GetSuccessResult(nsIWritableVariant* aResult);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// In-params.
|
|
|
|
const PRInt64 mOSID;
|
|
|
|
const nsString mValue;
|
|
|
|
const Key mKey;
|
|
|
|
const bool mAutoIncrement;
|
|
|
|
nsTArray<IndexUpdateInfo> mIndexUpdateInfo;
|
|
|
|
};
|
|
|
|
|
2010-11-11 02:25:57 +03:00
|
|
|
class DeleteHelper : public AsyncConnectionHelper
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-11-11 02:25:57 +03:00
|
|
|
DeleteHelper(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
IDBRequest* aRequest,
|
|
|
|
PRInt64 aObjectStoreID,
|
|
|
|
const Key& aKey,
|
|
|
|
bool aAutoIncrement)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mOSID(aObjectStoreID),
|
|
|
|
mKey(aKey), mAutoIncrement(aAutoIncrement)
|
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult GetSuccessResult(nsIWritableVariant* aResult);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// In-params.
|
|
|
|
const PRInt64 mOSID;
|
|
|
|
const nsString mValue;
|
|
|
|
const Key mKey;
|
|
|
|
const bool mAutoIncrement;
|
|
|
|
};
|
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
inline
|
|
|
|
already_AddRefed<IDBRequest>
|
|
|
|
GenerateRequest(IDBCursor* aCursor)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
IDBDatabase* database = aCursor->Transaction()->Database();
|
2010-11-11 02:26:03 +03:00
|
|
|
return IDBRequest::Create(aCursor, database->ScriptContext(),
|
|
|
|
database->Owner(), aCursor->Transaction());
|
2010-09-10 02:15:38 +04:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
BEGIN_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
class ContinueRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
ContinueRunnable(IDBCursor* aCursor,
|
2010-06-23 23:46:08 +04:00
|
|
|
const Key& aKey)
|
|
|
|
: mCursor(aCursor), mKey(aKey)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBCursor> mCursor;
|
2010-06-23 23:46:08 +04:00
|
|
|
const Key mKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
END_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
// static
|
2010-06-28 22:51:06 +04:00
|
|
|
already_AddRefed<IDBCursor>
|
|
|
|
IDBCursor::Create(IDBRequest* aRequest,
|
|
|
|
IDBTransaction* aTransaction,
|
|
|
|
IDBObjectStore* aObjectStore,
|
|
|
|
PRUint16 aDirection,
|
|
|
|
nsTArray<KeyValuePair>& aData)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aObjectStore, "Null pointer!");
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBCursor> cursor =
|
|
|
|
IDBCursor::CreateCommon(aRequest, aTransaction, aDirection);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
cursor->mObjectStore = aObjectStore;
|
|
|
|
|
|
|
|
if (!cursor->mData.SwapElements(aData)) {
|
|
|
|
NS_ERROR("Out of memory?!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(!cursor->mData.IsEmpty(), "Should never have an empty set!");
|
|
|
|
|
|
|
|
cursor->mDataIndex = cursor->mData.Length() - 1;
|
|
|
|
cursor->mType = OBJECTSTORE;
|
|
|
|
|
|
|
|
return cursor.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2010-06-28 22:51:06 +04:00
|
|
|
already_AddRefed<IDBCursor>
|
|
|
|
IDBCursor::Create(IDBRequest* aRequest,
|
|
|
|
IDBTransaction* aTransaction,
|
|
|
|
IDBIndex* aIndex,
|
|
|
|
PRUint16 aDirection,
|
|
|
|
nsTArray<KeyKeyPair>& aData)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aIndex, "Null pointer!");
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBCursor> cursor =
|
|
|
|
IDBCursor::CreateCommon(aRequest, aTransaction, aDirection);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
cursor->mObjectStore = aIndex->ObjectStore();
|
|
|
|
cursor->mIndex = aIndex;
|
|
|
|
|
|
|
|
if (!cursor->mKeyData.SwapElements(aData)) {
|
|
|
|
NS_ERROR("Out of memory?!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(!cursor->mKeyData.IsEmpty(), "Should never have an empty set!");
|
|
|
|
|
|
|
|
cursor->mDataIndex = cursor->mKeyData.Length() - 1;
|
|
|
|
cursor->mType = INDEX;
|
|
|
|
|
|
|
|
return cursor.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2010-06-28 22:51:06 +04:00
|
|
|
already_AddRefed<IDBCursor>
|
|
|
|
IDBCursor::Create(IDBRequest* aRequest,
|
|
|
|
IDBTransaction* aTransaction,
|
|
|
|
IDBIndex* aIndex,
|
|
|
|
PRUint16 aDirection,
|
|
|
|
nsTArray<KeyValuePair>& aData)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aIndex, "Null pointer!");
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBCursor> cursor =
|
|
|
|
IDBCursor::CreateCommon(aRequest, aTransaction, aDirection);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
cursor->mObjectStore = aIndex->ObjectStore();
|
|
|
|
cursor->mIndex = aIndex;
|
|
|
|
|
|
|
|
if (!cursor->mData.SwapElements(aData)) {
|
|
|
|
NS_ERROR("Out of memory?!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(!cursor->mData.IsEmpty(), "Should never have an empty set!");
|
|
|
|
|
|
|
|
cursor->mDataIndex = cursor->mData.Length() - 1;
|
|
|
|
cursor->mType = INDEXOBJECT;
|
|
|
|
|
|
|
|
return cursor.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2010-06-28 22:51:06 +04:00
|
|
|
already_AddRefed<IDBCursor>
|
|
|
|
IDBCursor::CreateCommon(IDBRequest* aRequest,
|
|
|
|
IDBTransaction* aTransaction,
|
|
|
|
PRUint16 aDirection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aRequest, "Null pointer!");
|
|
|
|
NS_ASSERTION(aTransaction, "Null pointer!");
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBCursor> cursor(new IDBCursor());
|
2010-09-10 02:15:38 +04:00
|
|
|
|
|
|
|
cursor->mScriptContext = aTransaction->Database()->ScriptContext();
|
|
|
|
cursor->mOwner = aTransaction->Database()->Owner();
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
cursor->mRequest = aRequest;
|
|
|
|
cursor->mTransaction = aTransaction;
|
|
|
|
cursor->mDirection = aDirection;
|
|
|
|
|
|
|
|
return cursor.forget();
|
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBCursor::IDBCursor()
|
2010-06-23 23:46:08 +04:00
|
|
|
: mDirection(nsIIDBCursor::NEXT),
|
|
|
|
mCachedValue(JSVAL_VOID),
|
|
|
|
mHaveCachedValue(false),
|
2010-09-14 02:32:56 +04:00
|
|
|
mValueRooted(false),
|
2010-06-23 23:46:08 +04:00
|
|
|
mContinueCalled(false),
|
|
|
|
mDataIndex(0),
|
|
|
|
mType(OBJECTSTORE)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBCursor::~IDBCursor()
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-09-14 02:32:56 +04:00
|
|
|
if (mValueRooted) {
|
|
|
|
NS_DROP_JS_OBJECTS(this, IDBCursor);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-09-10 02:15:38 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBCursor)
|
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBCursor)
|
2010-09-14 02:32:56 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mRequest,
|
|
|
|
nsPIDOMEventTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mTransaction,
|
|
|
|
nsPIDOMEventTarget)
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mObjectStore)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mIndex)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mScriptContext)
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2010-09-14 02:32:56 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_BEGIN(IDBCursor)
|
|
|
|
if (tmp->mValueRooted) {
|
|
|
|
NS_DROP_JS_OBJECTS(tmp, IDBCursor);
|
|
|
|
tmp->mCachedValue = JSVAL_VOID;
|
|
|
|
tmp->mHaveCachedValue = false;
|
|
|
|
tmp->mValueRooted = false;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(IDBCursor)
|
|
|
|
if (JSVAL_IS_GCTHING(tmp->mCachedValue)) {
|
|
|
|
void *gcThing = JSVAL_TO_GCTHING(tmp->mCachedValue);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_CALLBACK(gcThing)
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IDBCursor)
|
2010-09-10 02:15:38 +04:00
|
|
|
// Don't unlink mObjectStore, mIndex, or mTransaction!
|
2010-09-14 02:32:56 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mRequest)
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mScriptContext)
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBCursor)
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBCursor)
|
2010-06-28 22:51:06 +04:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBCursor)
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
2010-09-10 02:15:38 +04:00
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBCursor)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBCursor)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
DOMCI_DATA(IDBCursor, IDBCursor)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBCursor::GetDirection(PRUint16* aDirection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
*aDirection = mDirection;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBCursor::GetKey(nsIVariant** aKey)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (!mCachedKey) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIWritableVariant> variant =
|
|
|
|
do_CreateInstance(NS_VARIANT_CONTRACTID, &rv);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
const Key& key = mType == INDEX ?
|
|
|
|
mKeyData[mDataIndex].key :
|
|
|
|
mData[mDataIndex].key;
|
|
|
|
NS_ASSERTION(!key.IsUnset() && !key.IsNull(), "Bad key!");
|
|
|
|
|
|
|
|
if (key.IsString()) {
|
|
|
|
rv = variant->SetAsAString(key.StringValue());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else if (key.IsInt()) {
|
|
|
|
rv = variant->SetAsInt64(key.IntValue());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Huh?!");
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = variant->SetWritable(PR_FALSE);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsIWritableVariant* result;
|
|
|
|
variant.forget(&result);
|
|
|
|
|
|
|
|
mCachedKey = dont_AddRef(static_cast<nsIVariant*>(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIVariant> result(mCachedKey);
|
|
|
|
result.forget(aKey);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-29 02:22:41 +04:00
|
|
|
IDBCursor::GetValue(JSContext* aCx,
|
|
|
|
jsval* aValue)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mType == INDEX) {
|
|
|
|
const Key& value = mKeyData[mDataIndex].value;
|
|
|
|
NS_ASSERTION(!value.IsUnset() && !value.IsNull(), "Bad key!");
|
|
|
|
|
2010-06-29 02:22:41 +04:00
|
|
|
rv = IDBObjectStore::GetJSValFromKey(value, aCx, aValue);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mHaveCachedValue) {
|
2010-06-29 02:22:41 +04:00
|
|
|
JSAutoRequest ar(aCx);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIJSON> json(new nsJSON());
|
2010-06-29 02:22:41 +04:00
|
|
|
rv = json->DecodeToJSVal(mData[mDataIndex].value, aCx, &mCachedValue);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_SERIAL_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-14 02:32:56 +04:00
|
|
|
if (!mValueRooted) {
|
|
|
|
NS_HOLD_JS_OBJECTS(this, IDBCursor);
|
|
|
|
mValueRooted = true;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
mHaveCachedValue = true;
|
|
|
|
}
|
|
|
|
|
2010-06-29 02:22:41 +04:00
|
|
|
*aValue = mCachedValue;
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-07-15 10:19:36 +04:00
|
|
|
IDBCursor::Continue(const jsval &aKey,
|
2010-06-29 02:22:41 +04:00
|
|
|
JSContext* aCx,
|
2010-06-28 22:51:06 +04:00
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
PRBool* _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
if (!mTransaction->TransactionIsOpen()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mContinueCalled) {
|
2010-11-11 02:25:44 +03:00
|
|
|
// XXX Update the spec for precise behavior here.
|
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Key key;
|
2010-06-29 02:22:41 +04:00
|
|
|
nsresult rv = IDBObjectStore::GetKeyFromJSVal(aKey, key);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (key.IsNull()) {
|
|
|
|
if (aOptionalArgCount) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
key = Key::UNSETKEY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TransactionThreadPool* pool = TransactionThreadPool::GetOrCreate();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(pool, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsRefPtr<ContinueRunnable> runnable(new ContinueRunnable(this, key));
|
|
|
|
|
|
|
|
rv = pool->Dispatch(mTransaction, runnable, false, nsnull);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
mTransaction->OnNewRequest();
|
|
|
|
|
|
|
|
mContinueCalled = true;
|
|
|
|
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-11-11 02:25:44 +03:00
|
|
|
IDBCursor::Update(const jsval& aValue,
|
2010-06-29 02:22:41 +04:00
|
|
|
JSContext* aCx,
|
2010-06-28 22:51:06 +04:00
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
if (!mTransaction->TransactionIsOpen() || !mTransaction->IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
if (mType != OBJECTSTORE) {
|
|
|
|
NS_WARNING("Update for non-objectStore cursors is not implemented!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ASSERTION(mObjectStore, "This cannot be null!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
const Key& key = mData[mDataIndex].key;
|
|
|
|
NS_ASSERTION(!key.IsUnset() && !key.IsNull(), "Bad key!");
|
|
|
|
|
2010-06-29 02:22:41 +04:00
|
|
|
JSAutoRequest ar(aCx);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-11 02:35:42 +04:00
|
|
|
js::AutoValueRooter clone(aCx, aValue);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult rv;
|
2010-06-23 23:46:08 +04:00
|
|
|
if (!mObjectStore->KeyPath().IsEmpty()) {
|
|
|
|
// Make sure the object given has the correct keyPath value set on it or
|
|
|
|
// we will add it.
|
|
|
|
const nsString& keyPath = mObjectStore->KeyPath();
|
|
|
|
const jschar* keyPathChars = reinterpret_cast<const jschar*>(keyPath.get());
|
|
|
|
const size_t keyPathLen = keyPath.Length();
|
|
|
|
|
2010-06-29 02:22:41 +04:00
|
|
|
js::AutoValueRooter prop(aCx);
|
2010-07-15 10:19:36 +04:00
|
|
|
JSBool ok = JS_GetUCProperty(aCx, JSVAL_TO_OBJECT(clone.jsval_value()),
|
|
|
|
keyPathChars, keyPathLen, prop.jsval_addr());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
if (JSVAL_IS_VOID(prop.jsval_value())) {
|
|
|
|
rv = IDBObjectStore::GetJSValFromKey(key, aCx, prop.jsval_addr());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-11 02:35:42 +04:00
|
|
|
ok = JS_StructuredClone(aCx, clone.jsval_value(), clone.jsval_addr());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_SERIAL_ERR);
|
2010-10-11 02:35:42 +04:00
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
ok = JS_DefineUCProperty(aCx, JSVAL_TO_OBJECT(clone.jsval_value()),
|
|
|
|
keyPathChars, keyPathLen, prop.jsval_value(), nsnull,
|
2010-06-29 02:22:41 +04:00
|
|
|
nsnull, JSPROP_ENUMERATE);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-06-29 02:22:41 +04:00
|
|
|
else {
|
|
|
|
Key newKey;
|
2010-07-15 10:19:36 +04:00
|
|
|
rv = IDBObjectStore::GetKeyFromJSVal(prop.jsval_value(), newKey);
|
2010-06-29 02:22:41 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-06-29 02:22:41 +04:00
|
|
|
if (newKey.IsUnset() || newKey.IsNull() || newKey != key) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
ObjectStoreInfo* info;
|
|
|
|
if (!ObjectStoreInfo::Get(mTransaction->Database()->Id(),
|
|
|
|
mObjectStore->Name(), &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsTArray<IndexUpdateInfo> indexUpdateInfo;
|
2010-11-11 02:25:44 +03:00
|
|
|
rv = IDBObjectStore::GetIndexUpdateInfo(info, aCx, clone.jsval_value(),
|
|
|
|
indexUpdateInfo);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIJSON> json(new nsJSON());
|
|
|
|
|
|
|
|
nsString jsonValue;
|
2010-07-15 10:19:36 +04:00
|
|
|
rv = json->EncodeFromJSVal(clone.jsval_addr(), aCx, jsonValue);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_SERIAL_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsRefPtr<UpdateHelper> helper =
|
|
|
|
new UpdateHelper(mTransaction, request, mObjectStore->Id(), jsonValue, key,
|
|
|
|
mObjectStore->IsAutoIncrement(), indexUpdateInfo);
|
2010-11-11 02:25:44 +03:00
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
rv = helper->DispatchToTransactionPool();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-11-11 02:25:57 +03:00
|
|
|
IDBCursor::Delete(nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
if (!mTransaction->TransactionIsOpen() || !mTransaction->IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
if (mType != OBJECTSTORE) {
|
|
|
|
NS_WARNING("Delete for non-objectStore cursors is not implemented!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ASSERTION(mObjectStore, "This cannot be null!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
const Key& key = mData[mDataIndex].key;
|
|
|
|
NS_ASSERTION(!key.IsUnset() && !key.IsNull(), "Bad key!");
|
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:57 +03:00
|
|
|
nsRefPtr<DeleteHelper> helper =
|
|
|
|
new DeleteHelper(mTransaction, request, mObjectStore->Id(), key,
|
2010-06-23 23:46:08 +04:00
|
|
|
mObjectStore->IsAutoIncrement());
|
2010-11-11 02:25:44 +03:00
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsresult rv = helper->DispatchToTransactionPool();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-06-23 23:46:08 +04:00
|
|
|
UpdateHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
NS_ASSERTION(!mKey.IsUnset() && !mKey.IsNull(), "Badness!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->AddStatement(false, true, mAutoIncrement);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(keyValue, "key_value");
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), mOSID);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (mKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(keyValue, mKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(keyValue, mKey.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown key type!");
|
|
|
|
}
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("data"), mValue);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(stmt->Execute())) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update our indexes if needed.
|
|
|
|
if (!mIndexUpdateInfo.IsEmpty()) {
|
|
|
|
PRInt64 objectDataId = mAutoIncrement ? mKey.IntValue() : LL_MININT;
|
2010-06-28 22:51:06 +04:00
|
|
|
rv = IDBObjectStore::UpdateIndexes(mTransaction, mOSID, mKey,
|
|
|
|
mAutoIncrement, true,
|
|
|
|
objectDataId, mIndexUpdateInfo);
|
2010-06-23 23:46:08 +04:00
|
|
|
if (rv == NS_ERROR_STORAGE_CONSTRAINT) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-06-23 23:46:08 +04:00
|
|
|
UpdateHelper::GetSuccessResult(nsIWritableVariant* aResult)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mKey.IsUnset() && !mKey.IsNull(), "Badness!");
|
|
|
|
|
|
|
|
if (mKey.IsString()) {
|
|
|
|
aResult->SetAsAString(mKey.StringValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsInt()) {
|
|
|
|
aResult->SetAsInt64(mKey.IntValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad key!");
|
|
|
|
}
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-11-11 02:25:57 +03:00
|
|
|
DeleteHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
2010-11-11 02:25:57 +03:00
|
|
|
mTransaction->DeleteStatement(mAutoIncrement);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), mOSID);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(!mKey.IsUnset() && !mKey.IsNull(), "Must have a key here!");
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(key_value, "key_value");
|
|
|
|
|
|
|
|
if (mKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(key_value, mKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(key_value, mKey.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown key type!");
|
|
|
|
}
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
// Search for it!
|
|
|
|
rv = stmt->Execute();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-11-11 02:25:57 +03:00
|
|
|
DeleteHelper::GetSuccessResult(nsIWritableVariant* aResult)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!mKey.IsUnset() && !mKey.IsNull(), "Badness!");
|
|
|
|
|
|
|
|
if (mKey.IsString()) {
|
|
|
|
aResult->SetAsAString(mKey.StringValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsInt()) {
|
|
|
|
aResult->SetAsInt64(mKey.IntValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown key type!");
|
|
|
|
}
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ContinueRunnable::Run()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
rv = NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
// mCursor must be null after this method finishes. Swap out now.
|
|
|
|
nsRefPtr<IDBCursor> cursor;
|
|
|
|
cursor.swap(mCursor);
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
// Remove cached stuff from last time.
|
2010-09-10 08:54:25 +04:00
|
|
|
cursor->mCachedKey = nsnull;
|
|
|
|
cursor->mCachedValue = JSVAL_VOID;
|
|
|
|
cursor->mHaveCachedValue = false;
|
|
|
|
cursor->mContinueCalled = false;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
if (cursor->mType == IDBCursor::INDEX) {
|
|
|
|
cursor->mKeyData.RemoveElementAt(cursor->mDataIndex);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
2010-09-10 08:54:25 +04:00
|
|
|
cursor->mData.RemoveElementAt(cursor->mDataIndex);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-09-10 08:54:25 +04:00
|
|
|
if (cursor->mDataIndex) {
|
|
|
|
cursor->mDataIndex--;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWritableVariant> variant =
|
|
|
|
do_CreateInstance(NS_VARIANT_CONTRACTID);
|
|
|
|
if (!variant) {
|
|
|
|
NS_ERROR("Couldn't create variant!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
PRBool empty = cursor->mType == IDBCursor::INDEX ?
|
|
|
|
cursor->mKeyData.IsEmpty() :
|
|
|
|
cursor->mData.IsEmpty();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (empty) {
|
|
|
|
rv = variant->SetAsEmpty();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!mKey.IsUnset()) {
|
|
|
|
NS_ASSERTION(!mKey.IsNull(), "Huh?!");
|
|
|
|
|
|
|
|
NS_WARNING("Using a slow O(n) search for continue(key), do something "
|
|
|
|
"smarter!");
|
|
|
|
|
|
|
|
// Skip ahead to our next key match.
|
2010-09-10 08:54:25 +04:00
|
|
|
PRInt32 index = PRInt32(cursor->mDataIndex);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
if (cursor->mType == IDBCursor::INDEX) {
|
2010-06-23 23:46:08 +04:00
|
|
|
while (index >= 0) {
|
2010-09-10 08:54:25 +04:00
|
|
|
const Key& key = cursor->mKeyData[index].key;
|
2010-06-23 23:46:08 +04:00
|
|
|
if (mKey == key) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (key < mKey) {
|
|
|
|
index--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
index = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= 0) {
|
2010-09-10 08:54:25 +04:00
|
|
|
cursor->mDataIndex = PRUint32(index);
|
|
|
|
cursor->mKeyData.RemoveElementsAt(index + 1,
|
|
|
|
cursor->mKeyData.Length() - index
|
|
|
|
- 1);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (index >= 0) {
|
2010-09-10 08:54:25 +04:00
|
|
|
const Key& key = cursor->mData[index].key;
|
2010-06-23 23:46:08 +04:00
|
|
|
if (mKey == key) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (key < mKey) {
|
|
|
|
index--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
index = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= 0) {
|
2010-09-10 08:54:25 +04:00
|
|
|
cursor->mDataIndex = PRUint32(index);
|
|
|
|
cursor->mData.RemoveElementsAt(index + 1,
|
|
|
|
cursor->mData.Length() - index - 1);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
rv = variant->SetAsISupports(cursor);
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = variant->SetWritable(PR_FALSE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event =
|
2010-09-10 08:54:25 +04:00
|
|
|
IDBSuccessEvent::Create(cursor->mRequest, variant, cursor->mTransaction);
|
2010-06-23 23:46:08 +04:00
|
|
|
if (!event) {
|
|
|
|
NS_ERROR("Failed to create event!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool dummy;
|
2010-09-10 08:54:25 +04:00
|
|
|
cursor->mRequest->DispatchEvent(event, &dummy);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
cursor->mTransaction->OnRequestFinished();
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|