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):
|
|
|
|
* Shawn Wilsher <me@shawnwilsher.com>
|
2010-12-16 00:21:02 +03:00
|
|
|
* Ben Turner <bent.mozilla@gmail.com>
|
2010-06-23 23:46:08 +04:00
|
|
|
*
|
|
|
|
* 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 "IDBIndex.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#include "nsIIDBKeyRange.h"
|
2010-12-21 19:02:04 +03:00
|
|
|
#include "nsIJSContextStack.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#include "nsDOMClassInfo.h"
|
2010-11-11 02:26:03 +03:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "mozilla/storage.h"
|
|
|
|
|
|
|
|
#include "AsyncConnectionHelper.h"
|
2010-06-28 22:51:06 +04:00
|
|
|
#include "IDBCursor.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "IDBEvents.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 "DatabaseInfo.h"
|
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2010-12-16 00:21:02 +03:00
|
|
|
class GetKeyHelper : public AsyncConnectionHelper
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-16 00:21:02 +03:00
|
|
|
GetKeyHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
IDBIndex* aIndex,
|
|
|
|
const Key& aKey)
|
2010-10-19 21:58:52 +04:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mIndex(aIndex), mKey(aKey)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mIndex = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
protected:
|
|
|
|
// In-params.
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBIndex> mIndex;
|
2010-06-23 23:46:08 +04:00
|
|
|
Key mKey;
|
|
|
|
};
|
|
|
|
|
2010-12-16 00:21:02 +03:00
|
|
|
class GetHelper : public GetKeyHelper
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-16 00:21:02 +03:00
|
|
|
GetHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
IDBIndex* aIndex,
|
|
|
|
const Key& aKey)
|
|
|
|
: GetKeyHelper(aTransaction, aRequest, aIndex, aKey)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
~GetHelper()
|
2010-12-21 19:02:04 +03:00
|
|
|
{
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffer);
|
|
|
|
}
|
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
protected:
|
2010-12-21 19:02:04 +03:00
|
|
|
JSAutoStructuredCloneBuffer mCloneBuffer;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2010-12-16 00:21:02 +03:00
|
|
|
class GetAllKeysHelper : public GetKeyHelper
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-16 00:21:02 +03:00
|
|
|
GetAllKeysHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
IDBIndex* aIndex,
|
|
|
|
const Key& aKey,
|
|
|
|
const PRUint32 aLimit)
|
|
|
|
: GetKeyHelper(aTransaction, aRequest, aIndex, aKey), mLimit(aLimit)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
const PRUint32 mLimit;
|
|
|
|
nsTArray<Key> mKeys;
|
|
|
|
};
|
|
|
|
|
2010-12-16 00:21:02 +03:00
|
|
|
class GetAllHelper : public GetKeyHelper
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-16 00:21:02 +03:00
|
|
|
GetAllHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
IDBIndex* aIndex,
|
|
|
|
const Key& aKey,
|
|
|
|
const PRUint32 aLimit)
|
|
|
|
: GetKeyHelper(aTransaction, aRequest, aIndex, aKey), mLimit(aLimit)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
~GetAllHelper()
|
|
|
|
{
|
|
|
|
for (PRUint32 index = 0; index < mCloneBuffers.Length(); index++) {
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffers[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-21 19:02:04 +03:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
GetKeyHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
protected:
|
|
|
|
const PRUint32 mLimit;
|
2010-12-21 19:02:04 +03:00
|
|
|
nsTArray<JSAutoStructuredCloneBuffer> mCloneBuffers;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2010-12-16 00:21:02 +03:00
|
|
|
class OpenKeyCursorHelper : public AsyncConnectionHelper
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-16 00:21:02 +03:00
|
|
|
OpenKeyCursorHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
IDBIndex* aIndex,
|
|
|
|
const Key& aLowerKey,
|
|
|
|
const Key& aUpperKey,
|
|
|
|
PRBool aLowerOpen,
|
|
|
|
PRBool aUpperOpen,
|
|
|
|
PRUint16 aDirection)
|
2010-10-19 21:58:52 +04:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mIndex(aIndex),
|
2010-11-11 02:26:00 +03:00
|
|
|
mLowerKey(aLowerKey), mUpperKey(aUpperKey), mLowerOpen(aLowerOpen),
|
|
|
|
mUpperOpen(aUpperOpen), mDirection(aDirection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mIndex = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
private:
|
|
|
|
// In-params.
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBIndex> mIndex;
|
2010-11-11 02:26:00 +03:00
|
|
|
const Key mLowerKey;
|
|
|
|
const Key mUpperKey;
|
|
|
|
const PRPackedBool mLowerOpen;
|
|
|
|
const PRPackedBool mUpperOpen;
|
2010-06-23 23:46:08 +04:00
|
|
|
const PRUint16 mDirection;
|
|
|
|
|
|
|
|
// Out-params.
|
2010-12-10 05:15:00 +03:00
|
|
|
Key mKey;
|
|
|
|
Key mObjectKey;
|
|
|
|
nsCString mContinueQuery;
|
|
|
|
nsCString mContinueToQuery;
|
|
|
|
Key mRangeKey;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2010-12-16 00:21:02 +03:00
|
|
|
class OpenCursorHelper : public AsyncConnectionHelper
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-16 00:21:02 +03:00
|
|
|
OpenCursorHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
IDBIndex* aIndex,
|
|
|
|
const Key& aLowerKey,
|
|
|
|
const Key& aUpperKey,
|
|
|
|
PRBool aLowerOpen,
|
|
|
|
PRBool aUpperOpen,
|
|
|
|
PRUint16 aDirection)
|
2010-10-19 21:58:52 +04:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mIndex(aIndex),
|
2010-11-11 02:26:00 +03:00
|
|
|
mLowerKey(aLowerKey), mUpperKey(aUpperKey), mLowerOpen(aLowerOpen),
|
|
|
|
mUpperOpen(aUpperOpen), mDirection(aDirection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
~OpenCursorHelper()
|
|
|
|
{
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffer);
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mIndex = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
private:
|
|
|
|
// In-params.
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBIndex> mIndex;
|
2010-11-11 02:26:00 +03:00
|
|
|
const Key mLowerKey;
|
|
|
|
const Key mUpperKey;
|
|
|
|
const PRPackedBool mLowerOpen;
|
|
|
|
const PRPackedBool mUpperOpen;
|
2010-06-23 23:46:08 +04:00
|
|
|
const PRUint16 mDirection;
|
|
|
|
|
|
|
|
// Out-params.
|
2010-12-10 05:15:00 +03:00
|
|
|
Key mKey;
|
|
|
|
Key mObjectKey;
|
2010-12-21 19:02:04 +03:00
|
|
|
JSAutoStructuredCloneBuffer mCloneBuffer;
|
2010-12-10 05:15:00 +03:00
|
|
|
nsCString mContinueQuery;
|
|
|
|
nsCString mContinueToQuery;
|
|
|
|
Key mRangeKey;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
inline
|
|
|
|
already_AddRefed<IDBRequest>
|
|
|
|
GenerateRequest(IDBIndex* aIndex)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-11-11 02:26:03 +03:00
|
|
|
IDBTransaction* transaction = aIndex->ObjectStore()->Transaction();
|
|
|
|
IDBDatabase* database = transaction->Database();
|
|
|
|
return IDBRequest::Create(aIndex, database->ScriptContext(),
|
|
|
|
database->Owner(), transaction);
|
2010-09-10 02:15:38 +04:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// static
|
2010-06-28 22:51:06 +04:00
|
|
|
already_AddRefed<IDBIndex>
|
|
|
|
IDBIndex::Create(IDBObjectStore* aObjectStore,
|
|
|
|
const IndexInfo* aIndexInfo)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(aObjectStore, "Null pointer!");
|
|
|
|
NS_ASSERTION(aIndexInfo, "Null pointer!");
|
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
IDBDatabase* database = aObjectStore->Transaction()->Database();
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBIndex> index = new IDBIndex();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
index->mScriptContext = database->ScriptContext();
|
|
|
|
index->mOwner = database->Owner();
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
index->mObjectStore = aObjectStore;
|
|
|
|
index->mId = aIndexInfo->id;
|
|
|
|
index->mName = aIndexInfo->name;
|
|
|
|
index->mKeyPath = aIndexInfo->keyPath;
|
|
|
|
index->mUnique = aIndexInfo->unique;
|
|
|
|
index->mAutoIncrement = aIndexInfo->autoIncrement;
|
|
|
|
|
|
|
|
return index.forget();
|
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBIndex::IDBIndex()
|
2010-06-23 23:46:08 +04:00
|
|
|
: mId(LL_MININT),
|
|
|
|
mUnique(false),
|
|
|
|
mAutoIncrement(false)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBIndex::~IDBIndex()
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBIndex)
|
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBIndex)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mObjectStore)
|
|
|
|
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-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IDBIndex)
|
2010-09-10 02:15:38 +04:00
|
|
|
// Don't unlink mObjectStore!
|
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(IDBIndex)
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBIndex)
|
2010-06-28 22:51:06 +04:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBIndex)
|
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(IDBIndex)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBIndex)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
DOMCI_DATA(IDBIndex, IDBIndex)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBIndex::GetName(nsAString& aName)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
aName.Assign(mName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBIndex::GetStoreName(nsAString& aStoreName)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
return mObjectStore->GetName(aStoreName);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBIndex::GetKeyPath(nsAString& aKeyPath)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
aKeyPath.Assign(mKeyPath);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBIndex::GetUnique(PRBool* aUnique)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
*aUnique = mUnique;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-10 05:14:09 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBIndex::GetObjectStore(nsIIDBObjectStore** aObjectStore)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIDBObjectStore> objectStore(mObjectStore);
|
|
|
|
objectStore.forget(aObjectStore);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBIndex::Get(nsIVariant* aKey,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
IDBTransaction* transaction = mObjectStore->Transaction();
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!transaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-11-11 02:25:44 +03:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
Key key;
|
2010-06-28 22:51:06 +04:00
|
|
|
nsresult rv = IDBObjectStore::GetKeyFromVariant(aKey, key);
|
2010-12-16 00:21:07 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-16 00:21:07 +03:00
|
|
|
if (key.IsUnset()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_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
|
|
|
|
2010-12-16 00:21:02 +03:00
|
|
|
nsRefPtr<GetHelper> helper = new GetHelper(transaction, request, this, key);
|
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-12-16 00:21:02 +03:00
|
|
|
IDBIndex::GetKey(nsIVariant* aKey,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
IDBTransaction* transaction = mObjectStore->Transaction();
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!transaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-11-11 02:25:44 +03:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
Key key;
|
2010-06-28 22:51:06 +04:00
|
|
|
nsresult rv = IDBObjectStore::GetKeyFromVariant(aKey, key);
|
2010-12-16 00:21:07 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-16 00:21:07 +03:00
|
|
|
if (key.IsUnset()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_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
|
|
|
|
2010-12-16 00:21:02 +03:00
|
|
|
nsRefPtr<GetKeyHelper> helper =
|
|
|
|
new GetKeyHelper(transaction, request, this, key);
|
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-06-28 22:51:06 +04:00
|
|
|
IDBIndex::GetAll(nsIVariant* aKey,
|
|
|
|
PRUint32 aLimit,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
IDBTransaction* transaction = mObjectStore->Transaction();
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!transaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-11-11 02:25:44 +03:00
|
|
|
}
|
|
|
|
|
2010-12-16 00:21:07 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
Key key;
|
2010-12-16 00:21:07 +03:00
|
|
|
if (aOptionalArgCount &&
|
|
|
|
NS_FAILED(IDBObjectStore::GetKeyFromVariant(aKey, key))) {
|
|
|
|
PRUint16 type;
|
|
|
|
rv = aKey->GetDataType(&type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-16 00:21:07 +03:00
|
|
|
if (type != nsIDataType::VTYPE_EMPTY) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aOptionalArgCount < 2) {
|
|
|
|
aLimit = PR_UINT32_MAX;
|
|
|
|
}
|
|
|
|
|
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<GetAllHelper> helper =
|
2010-10-19 21:58:52 +04:00
|
|
|
new GetAllHelper(transaction, request, this, key, aLimit);
|
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-12-16 00:21:02 +03:00
|
|
|
IDBIndex::GetAllKeys(nsIVariant* aKey,
|
|
|
|
PRUint32 aLimit,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
IDBTransaction* transaction = mObjectStore->Transaction();
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!transaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-11-11 02:25:44 +03:00
|
|
|
}
|
|
|
|
|
2010-12-16 00:21:07 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
Key key;
|
2010-12-16 00:21:07 +03:00
|
|
|
if (aOptionalArgCount &&
|
|
|
|
NS_FAILED(IDBObjectStore::GetKeyFromVariant(aKey, key))) {
|
|
|
|
PRUint16 type;
|
|
|
|
rv = aKey->GetDataType(&type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-16 00:21:07 +03:00
|
|
|
if (type != nsIDataType::VTYPE_EMPTY) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aOptionalArgCount < 2) {
|
|
|
|
aLimit = PR_UINT32_MAX;
|
|
|
|
}
|
|
|
|
|
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-12-16 00:21:02 +03:00
|
|
|
nsRefPtr<GetAllKeysHelper> helper =
|
|
|
|
new GetAllKeysHelper(transaction, request, this, key, aLimit);
|
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-06-28 22:51:06 +04:00
|
|
|
IDBIndex::OpenCursor(nsIIDBKeyRange* aKeyRange,
|
|
|
|
PRUint16 aDirection,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
IDBTransaction* transaction = mObjectStore->Transaction();
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!transaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2010-11-11 02:26:00 +03:00
|
|
|
Key lowerKey, upperKey;
|
|
|
|
PRBool lowerOpen = PR_FALSE, upperOpen = PR_FALSE;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (aKeyRange) {
|
2010-11-11 02:26:00 +03:00
|
|
|
nsCOMPtr<nsIVariant> variant;
|
|
|
|
rv = aKeyRange->GetLower(getter_AddRefs(variant));
|
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:26:00 +03:00
|
|
|
rv = IDBObjectStore::GetKeyFromVariant(variant, lowerKey);
|
2010-12-16 00:21:07 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
|
|
|
|
rv = aKeyRange->GetUpper(getter_AddRefs(variant));
|
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:26:00 +03:00
|
|
|
rv = IDBObjectStore::GetKeyFromVariant(variant, upperKey);
|
2010-12-16 00:21:07 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:26:00 +03:00
|
|
|
rv = aKeyRange->GetLowerOpen(&lowerOpen);
|
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:26:00 +03:00
|
|
|
rv = aKeyRange->GetUpperOpen(&upperOpen);
|
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 (aOptionalArgCount >= 2) {
|
|
|
|
if (aDirection != nsIIDBCursor::NEXT &&
|
|
|
|
aDirection != nsIIDBCursor::NEXT_NO_DUPLICATE &&
|
|
|
|
aDirection != nsIIDBCursor::PREV &&
|
|
|
|
aDirection != nsIIDBCursor::PREV_NO_DUPLICATE) {
|
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 {
|
|
|
|
aDirection = nsIIDBCursor::NEXT;
|
|
|
|
}
|
|
|
|
|
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<OpenCursorHelper> helper =
|
2010-11-11 02:26:00 +03:00
|
|
|
new OpenCursorHelper(transaction, request, this, lowerKey, upperKey,
|
|
|
|
lowerOpen, upperOpen, aDirection);
|
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-12-16 00:21:02 +03:00
|
|
|
IDBIndex::OpenKeyCursor(nsIIDBKeyRange* aKeyRange,
|
|
|
|
PRUint16 aDirection,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
IDBTransaction* transaction = mObjectStore->Transaction();
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!transaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2010-11-11 02:26:00 +03:00
|
|
|
Key lowerKey, upperKey;
|
|
|
|
PRBool lowerOpen = PR_FALSE, upperOpen = PR_FALSE;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (aKeyRange) {
|
2010-11-11 02:26:00 +03:00
|
|
|
nsCOMPtr<nsIVariant> variant;
|
|
|
|
rv = aKeyRange->GetLower(getter_AddRefs(variant));
|
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:26:00 +03:00
|
|
|
rv = IDBObjectStore::GetKeyFromVariant(variant, lowerKey);
|
2010-12-16 00:21:07 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
|
|
|
|
rv = aKeyRange->GetUpper(getter_AddRefs(variant));
|
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:26:00 +03:00
|
|
|
rv = IDBObjectStore::GetKeyFromVariant(variant, upperKey);
|
2010-12-16 00:21:07 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:26:00 +03:00
|
|
|
rv = aKeyRange->GetLowerOpen(&lowerOpen);
|
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:26:00 +03:00
|
|
|
rv = aKeyRange->GetUpperOpen(&upperOpen);
|
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 (aOptionalArgCount >= 2) {
|
|
|
|
if (aDirection != nsIIDBCursor::NEXT &&
|
|
|
|
aDirection != nsIIDBCursor::NEXT_NO_DUPLICATE &&
|
|
|
|
aDirection != nsIIDBCursor::PREV &&
|
|
|
|
aDirection != nsIIDBCursor::PREV_NO_DUPLICATE) {
|
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 {
|
|
|
|
aDirection = nsIIDBCursor::NEXT;
|
|
|
|
}
|
|
|
|
|
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-12-16 00:21:02 +03:00
|
|
|
nsRefPtr<OpenKeyCursorHelper> helper =
|
|
|
|
new OpenKeyCursorHelper(transaction, request, this, lowerKey, upperKey,
|
|
|
|
lowerOpen, upperOpen, aDirection);
|
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;
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-12-16 00:21:02 +03:00
|
|
|
GetKeyHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
2010-10-19 21:58:52 +04:00
|
|
|
mTransaction->IndexGetStatement(mIndex->IsUnique(),
|
|
|
|
mIndex->IsAutoIncrement());
|
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);
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"),
|
|
|
|
mIndex->Id());
|
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_NAMED_LITERAL_CSTRING(value, "value");
|
|
|
|
|
|
|
|
if (mKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(value, mKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(value, mKey.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad 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
|
|
|
|
|
|
|
mKey = Key::UNSETKEY;
|
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
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 (hasResult) {
|
|
|
|
PRInt32 keyType;
|
|
|
|
rv = stmt->GetTypeOfIndex(0, &keyType);
|
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(keyType == mozIStorageStatement::VALUE_TYPE_INTEGER ||
|
|
|
|
keyType == mozIStorageStatement::VALUE_TYPE_TEXT,
|
|
|
|
"Bad key type!");
|
|
|
|
|
|
|
|
if (keyType == mozIStorageStatement::VALUE_TYPE_INTEGER) {
|
|
|
|
mKey = stmt->AsInt64(0);
|
|
|
|
}
|
|
|
|
else if (keyType == mozIStorageStatement::VALUE_TYPE_TEXT) {
|
|
|
|
rv = stmt->GetString(0, mKey.ToString());
|
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("Bad SQLite type!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2011-01-07 09:21:36 +03:00
|
|
|
GetKeyHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-01-07 09:21:36 +03:00
|
|
|
NS_ASSERTION(!mKey.IsUnset(), "Badness!");
|
|
|
|
return IDBObjectStore::GetJSValFromKey(mKey, aCx, aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-12-16 00:21:02 +03:00
|
|
|
GetHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
2010-10-19 21:58:52 +04:00
|
|
|
mTransaction->IndexGetObjectStatement(mIndex->IsUnique(),
|
|
|
|
mIndex->IsAutoIncrement());
|
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);
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"),
|
|
|
|
mIndex->Id());
|
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_NAMED_LITERAL_CSTRING(value, "value");
|
|
|
|
|
|
|
|
if (mKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(value, mKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(value, mKey.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad 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
|
|
|
|
|
|
|
mKey = Key::UNSETKEY;
|
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
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 (hasResult) {
|
2010-12-21 19:02:04 +03:00
|
|
|
rv = IDBObjectStore::GetStructuredCloneDataFromStatement(stmt, 0,
|
|
|
|
mCloneBuffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
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
|
2011-01-07 09:21:36 +03:00
|
|
|
GetHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-01-27 04:53:02 +03:00
|
|
|
nsresult rv = ConvertCloneBufferToJSVal(aCx, mCloneBuffer, aVal);
|
|
|
|
|
|
|
|
mCloneBuffer.clear(aCx);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-12-16 00:21:02 +03:00
|
|
|
GetAllKeysHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
if (!mKeys.SetCapacity(50)) {
|
|
|
|
NS_ERROR("Out of memory!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCString keyColumn;
|
|
|
|
nsCString tableName;
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsAutoIncrement()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyColumn.AssignLiteral("ai_object_data_id");
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsUnique()) {
|
2010-08-05 01:44:30 +04:00
|
|
|
tableName.AssignLiteral("ai_unique_index_data");
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
tableName.AssignLiteral("ai_index_data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyColumn.AssignLiteral("object_data_key");
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsUnique()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
tableName.AssignLiteral("unique_index_data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tableName.AssignLiteral("index_data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(indexId, "index_id");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(value, "value");
|
|
|
|
|
|
|
|
nsCString keyClause;
|
|
|
|
if (!mKey.IsUnset()) {
|
|
|
|
keyClause = NS_LITERAL_CSTRING(" AND ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" = :") + value;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString limitClause;
|
|
|
|
if (mLimit != PR_UINT32_MAX) {
|
|
|
|
limitClause = NS_LITERAL_CSTRING(" LIMIT ");
|
|
|
|
limitClause.AppendInt(mLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(" FROM ") + tableName +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE ") + indexId +
|
|
|
|
NS_LITERAL_CSTRING(" = :") + indexId + keyClause +
|
|
|
|
limitClause;
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
|
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);
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsresult rv = stmt->BindInt64ByName(indexId, mIndex->Id());
|
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.IsUnset()) {
|
|
|
|
if (mKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(value, mKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(value, mKey.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad 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
|
|
|
}
|
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
while(NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
|
|
|
|
if (mKeys.Capacity() == mKeys.Length()) {
|
|
|
|
if (!mKeys.SetCapacity(mKeys.Capacity() * 2)) {
|
|
|
|
NS_ERROR("Out of memory!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Key* key = mKeys.AppendElement();
|
|
|
|
NS_ASSERTION(key, "This shouldn't fail!");
|
|
|
|
|
|
|
|
PRInt32 keyType;
|
|
|
|
rv = stmt->GetTypeOfIndex(0, &keyType);
|
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(keyType == mozIStorageStatement::VALUE_TYPE_INTEGER ||
|
|
|
|
keyType == mozIStorageStatement::VALUE_TYPE_TEXT,
|
|
|
|
"Bad key type!");
|
|
|
|
|
|
|
|
if (keyType == mozIStorageStatement::VALUE_TYPE_INTEGER) {
|
|
|
|
*key = stmt->AsInt64(0);
|
|
|
|
}
|
|
|
|
else if (keyType == mozIStorageStatement::VALUE_TYPE_TEXT) {
|
|
|
|
rv = stmt->GetString(0, key->ToString());
|
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("Bad SQLite 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
|
|
|
|
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
|
2011-01-07 09:21:36 +03:00
|
|
|
GetAllKeysHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-01-07 09:21:36 +03:00
|
|
|
NS_ASSERTION(mKeys.Length() <= mLimit, "Too many results!");
|
|
|
|
|
|
|
|
nsTArray<Key> keys;
|
|
|
|
if (!mKeys.SwapElements(keys)) {
|
|
|
|
NS_ERROR("Failed to swap elements!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-01-07 09:21:36 +03:00
|
|
|
JSAutoRequest ar(aCx);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-01-07 09:21:36 +03:00
|
|
|
JSObject* array = JS_NewArrayObject(aCx, 0, NULL);
|
|
|
|
if (!array) {
|
|
|
|
NS_WARNING("Failed to make array!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-01-07 09:21:36 +03:00
|
|
|
if (!keys.IsEmpty()) {
|
|
|
|
if (!JS_SetArrayLength(aCx, array, jsuint(keys.Length()))) {
|
|
|
|
NS_WARNING("Failed to set array length!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsint count = jsint(keys.Length());
|
|
|
|
for (jsint index = 0; index < count; index++) {
|
|
|
|
const Key& key = keys[index];
|
|
|
|
NS_ASSERTION(!key.IsUnset(), "Bad key!");
|
|
|
|
|
|
|
|
jsval value;
|
|
|
|
nsresult rv = IDBObjectStore::GetJSValFromKey(key, aCx, &value);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to get jsval for key!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_SetElement(aCx, array, index, &value)) {
|
|
|
|
NS_WARNING("Failed to set array element!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aVal = OBJECT_TO_JSVAL(array);
|
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-12-16 00:21:02 +03:00
|
|
|
GetAllHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aConnection, "Passed a null connection!");
|
|
|
|
|
2010-12-21 19:02:04 +03:00
|
|
|
if (!mCloneBuffers.SetCapacity(50)) {
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ERROR("Out of memory!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCString dataTableName;
|
|
|
|
nsCString objectDataId;
|
|
|
|
nsCString indexTableName;
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsAutoIncrement()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
dataTableName.AssignLiteral("ai_object_data");
|
|
|
|
objectDataId.AssignLiteral("ai_object_data_id");
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsUnique()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
indexTableName.AssignLiteral("ai_unique_index_data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
indexTableName.AssignLiteral("ai_index_data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dataTableName.AssignLiteral("object_data");
|
|
|
|
objectDataId.AssignLiteral("object_data_id");
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsUnique()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
indexTableName.AssignLiteral("unique_index_data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
indexTableName.AssignLiteral("index_data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(indexId, "index_id");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(value, "value");
|
|
|
|
|
|
|
|
nsCString keyClause;
|
|
|
|
if (!mKey.IsUnset()) {
|
|
|
|
keyClause = NS_LITERAL_CSTRING(" AND ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" = :") + value;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString limitClause;
|
|
|
|
if (mLimit != PR_UINT32_MAX) {
|
|
|
|
limitClause = NS_LITERAL_CSTRING(" LIMIT ");
|
|
|
|
limitClause.AppendInt(mLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT data FROM ") + dataTableName +
|
|
|
|
NS_LITERAL_CSTRING(" INNER JOIN ") + indexTableName +
|
|
|
|
NS_LITERAL_CSTRING(" ON ") + dataTableName +
|
|
|
|
NS_LITERAL_CSTRING(".id = ") + indexTableName +
|
|
|
|
NS_LITERAL_CSTRING(".") + objectDataId +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE ") + indexId +
|
|
|
|
NS_LITERAL_CSTRING(" = :") + indexId + keyClause +
|
|
|
|
limitClause;
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
|
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);
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsresult rv = stmt->BindInt64ByName(indexId, mIndex->Id());
|
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.IsUnset()) {
|
|
|
|
if (mKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(value, mKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(value, mKey.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad 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
|
|
|
}
|
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
while(NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
|
2010-12-21 19:02:04 +03:00
|
|
|
if (mCloneBuffers.Capacity() == mCloneBuffers.Length()) {
|
|
|
|
if (!mCloneBuffers.SetCapacity(mCloneBuffers.Capacity() * 2)) {
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ERROR("Out of memory!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-21 19:02:04 +03:00
|
|
|
JSAutoStructuredCloneBuffer* buffer = mCloneBuffers.AppendElement();
|
|
|
|
NS_ASSERTION(buffer, "This shouldn't fail!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-21 19:02:04 +03:00
|
|
|
rv = IDBObjectStore::GetStructuredCloneDataFromStatement(stmt, 0, *buffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
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
|
2011-01-07 09:21:36 +03:00
|
|
|
GetAllHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-12-21 19:02:04 +03:00
|
|
|
NS_ASSERTION(mCloneBuffers.Length() <= mLimit, "Too many results!");
|
2011-01-27 04:53:02 +03:00
|
|
|
|
|
|
|
nsresult rv = ConvertCloneBuffersToArray(aCx, mCloneBuffers, aVal);
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < mCloneBuffers.Length(); index++) {
|
|
|
|
mCloneBuffers[index].clear(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-12-16 00:21:02 +03:00
|
|
|
OpenKeyCursorHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsCString table;
|
|
|
|
nsCString keyColumn;
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsAutoIncrement()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyColumn.AssignLiteral("ai_object_data_id");
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsUnique()) {
|
2010-08-05 01:44:30 +04:00
|
|
|
table.AssignLiteral("ai_unique_index_data");
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("ai_index_data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyColumn.AssignLiteral("object_data_key");
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsUnique()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
table.AssignLiteral("unique_index_data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("index_data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
NS_NAMED_LITERAL_CSTRING(id, "id");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(lowerKeyName, "lower_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(upperKeyName, "upper_key");
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(value, "value");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsCAutoString keyRangeClause;
|
2010-11-11 02:26:00 +03:00
|
|
|
if (!mLowerKey.IsUnset()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
AppendConditionClause(value, lowerKeyName, false, !mLowerOpen,
|
|
|
|
keyRangeClause);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
if (!mUpperKey.IsUnset()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
AppendConditionClause(value, upperKeyName, true, !mUpperOpen,
|
|
|
|
keyRangeClause);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
nsCAutoString directionClause = NS_LITERAL_CSTRING(" ORDER BY ") + value;
|
2010-06-23 23:46:08 +04:00
|
|
|
switch (mDirection) {
|
|
|
|
case nsIIDBCursor::NEXT:
|
|
|
|
case nsIIDBCursor::NEXT_NO_DUPLICATE:
|
2010-12-10 05:15:00 +03:00
|
|
|
directionClause.AppendLiteral(" ASC");
|
2010-06-23 23:46:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV:
|
|
|
|
case nsIIDBCursor::PREV_NO_DUPLICATE:
|
2010-12-10 05:15:00 +03:00
|
|
|
directionClause.AppendLiteral(" DESC");
|
2010-06-23 23:46:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown direction!");
|
|
|
|
}
|
2010-12-10 05:15:00 +03:00
|
|
|
directionClause += NS_LITERAL_CSTRING(", ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(" ASC");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
nsCString firstQuery = NS_LITERAL_CSTRING("SELECT value, ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(" FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE index_id = :") + id +
|
|
|
|
keyRangeClause + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(firstQuery);
|
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);
|
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
nsresult rv = stmt->BindInt64ByName(id, mIndex->Id());
|
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:26:00 +03:00
|
|
|
if (!mLowerKey.IsUnset()) {
|
|
|
|
if (mLowerKey.IsString()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->BindStringByName(lowerKeyName, mLowerKey.StringValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
else if (mLowerKey.IsInt()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->BindInt64ByName(lowerKeyName, mLowerKey.IntValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad key!");
|
|
|
|
}
|
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:26:00 +03:00
|
|
|
if (!mUpperKey.IsUnset()) {
|
|
|
|
if (mUpperKey.IsString()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->BindStringByName(upperKeyName, mUpperKey.StringValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
else if (mUpperKey.IsInt()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->BindInt64ByName(upperKeyName, mUpperKey.IntValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad key!");
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
PRBool hasResult;
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
if (!hasResult) {
|
|
|
|
mKey = Key::UNSETKEY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
PRInt32 keyType;
|
|
|
|
rv = stmt->GetTypeOfIndex(0, &keyType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
NS_ASSERTION(keyType == mozIStorageStatement::VALUE_TYPE_INTEGER ||
|
|
|
|
keyType == mozIStorageStatement::VALUE_TYPE_TEXT,
|
|
|
|
"Bad key type!");
|
|
|
|
|
|
|
|
if (keyType == mozIStorageStatement::VALUE_TYPE_INTEGER) {
|
|
|
|
mKey = stmt->AsInt64(0);
|
|
|
|
}
|
|
|
|
else if (keyType == mozIStorageStatement::VALUE_TYPE_TEXT) {
|
|
|
|
rv = stmt->GetString(0, mKey.ToString());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-12-10 05:15:00 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad SQLite type!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->GetTypeOfIndex(1, &keyType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
NS_ASSERTION(keyType == mozIStorageStatement::VALUE_TYPE_INTEGER ||
|
|
|
|
keyType == mozIStorageStatement::VALUE_TYPE_TEXT,
|
|
|
|
"Bad key type!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
if (keyType == mozIStorageStatement::VALUE_TYPE_INTEGER) {
|
|
|
|
mObjectKey = stmt->AsInt64(1);
|
|
|
|
}
|
|
|
|
else if (keyType == mozIStorageStatement::VALUE_TYPE_TEXT) {
|
|
|
|
rv = stmt->GetString(1, mObjectKey.ToString());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-12-10 05:15:00 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad SQLite type!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
// Now we need to make the query to get the next match.
|
|
|
|
nsCAutoString queryStart = NS_LITERAL_CSTRING("SELECT value, ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(" FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE index_id = :") + id;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
NS_NAMED_LITERAL_CSTRING(currentKey, "current_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(rangeKey, "range_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(objectKey, "object_key");
|
|
|
|
|
|
|
|
switch (mDirection) {
|
|
|
|
case nsIIDBCursor::NEXT:
|
|
|
|
if (!mUpperKey.IsUnset()) {
|
|
|
|
AppendConditionClause(value, rangeKey, true, !mUpperOpen, queryStart);
|
|
|
|
mRangeKey = mUpperKey;
|
|
|
|
}
|
|
|
|
mContinueQuery = queryStart + NS_LITERAL_CSTRING(" AND ( ( value = :") +
|
|
|
|
currentKey + NS_LITERAL_CSTRING(" AND ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(" > :") + objectKey +
|
|
|
|
NS_LITERAL_CSTRING(" ) OR ( value > :") + currentKey +
|
|
|
|
NS_LITERAL_CSTRING(" ) )") + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
mContinueToQuery = queryStart + NS_LITERAL_CSTRING(" AND value >= :") +
|
|
|
|
currentKey + NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::NEXT_NO_DUPLICATE:
|
|
|
|
if (!mUpperKey.IsUnset()) {
|
|
|
|
AppendConditionClause(value, rangeKey, true, !mUpperOpen, queryStart);
|
|
|
|
mRangeKey = mUpperKey;
|
|
|
|
}
|
|
|
|
mContinueQuery = queryStart + NS_LITERAL_CSTRING(" AND value > :") +
|
|
|
|
currentKey + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
mContinueToQuery = queryStart + NS_LITERAL_CSTRING(" AND value >= :") +
|
|
|
|
currentKey + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV:
|
|
|
|
if (!mLowerKey.IsUnset()) {
|
|
|
|
AppendConditionClause(value, rangeKey, false, !mLowerOpen, queryStart);
|
|
|
|
mRangeKey = mLowerKey;
|
|
|
|
}
|
|
|
|
mContinueQuery = queryStart + NS_LITERAL_CSTRING(" AND ( ( value = :") +
|
|
|
|
currentKey + NS_LITERAL_CSTRING(" AND ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(" < :") + objectKey +
|
|
|
|
NS_LITERAL_CSTRING(" ) OR ( value < :") + currentKey +
|
|
|
|
NS_LITERAL_CSTRING(" ) )") + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
mContinueToQuery = queryStart + NS_LITERAL_CSTRING(" AND value <= :") +
|
|
|
|
currentKey + NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV_NO_DUPLICATE:
|
|
|
|
if (!mLowerKey.IsUnset()) {
|
|
|
|
AppendConditionClause(value, rangeKey, false, !mLowerOpen, queryStart);
|
|
|
|
mRangeKey = mLowerKey;
|
|
|
|
}
|
|
|
|
mContinueQuery = queryStart + NS_LITERAL_CSTRING(" AND value < :") +
|
|
|
|
currentKey + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
mContinueToQuery = queryStart + NS_LITERAL_CSTRING(" AND value <= :") +
|
|
|
|
currentKey + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown direction type!");
|
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
|
2011-01-07 09:21:36 +03:00
|
|
|
OpenKeyCursorHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-12-10 05:15:00 +03:00
|
|
|
if (mKey.IsUnset()) {
|
2011-01-07 09:21:36 +03:00
|
|
|
*aVal = JSVAL_VOID;
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBCursor> cursor =
|
2010-12-10 05:15:00 +03:00
|
|
|
IDBCursor::Create(mRequest, mTransaction, mIndex, mDirection, mRangeKey,
|
|
|
|
mContinueQuery, mContinueToQuery, mKey, mObjectKey);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(cursor, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-01-07 09:21:36 +03:00
|
|
|
return WrapNative(aCx, cursor, aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-12-16 00:21:02 +03:00
|
|
|
OpenCursorHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsCString indexTable;
|
|
|
|
nsCString objectTable;
|
2010-12-10 05:15:00 +03:00
|
|
|
nsCString objectDataIdColumn;
|
|
|
|
nsCString keyValueColumn;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsAutoIncrement()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
objectTable.AssignLiteral("ai_object_data");
|
2010-12-10 05:15:00 +03:00
|
|
|
objectDataIdColumn.AssignLiteral("ai_object_data_id");
|
|
|
|
keyValueColumn.AssignLiteral("id");
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsUnique()) {
|
2010-08-05 01:44:30 +04:00
|
|
|
indexTable.AssignLiteral("ai_unique_index_data");
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
indexTable.AssignLiteral("ai_index_data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
objectTable.AssignLiteral("object_data");
|
2010-12-10 05:15:00 +03:00
|
|
|
objectDataIdColumn.AssignLiteral("object_data_id");
|
|
|
|
keyValueColumn.AssignLiteral("key_value");
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mIndex->IsUnique()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
indexTable.AssignLiteral("unique_index_data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
indexTable.AssignLiteral("index_data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
NS_NAMED_LITERAL_CSTRING(id, "id");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(lowerKeyName, "lower_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(upperKeyName, "upper_key");
|
|
|
|
|
|
|
|
nsCString value = indexTable + NS_LITERAL_CSTRING(".value");
|
|
|
|
nsCString data = objectTable + NS_LITERAL_CSTRING(".data");
|
|
|
|
nsCString keyValue = objectTable + NS_LITERAL_CSTRING(".") + keyValueColumn;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsCAutoString keyRangeClause;
|
2010-11-11 02:26:00 +03:00
|
|
|
if (!mLowerKey.IsUnset()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
AppendConditionClause(value, lowerKeyName, false, !mLowerOpen,
|
|
|
|
keyRangeClause);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
if (!mUpperKey.IsUnset()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
AppendConditionClause(value, upperKeyName, true, !mUpperOpen,
|
|
|
|
keyRangeClause);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
nsCAutoString directionClause = NS_LITERAL_CSTRING(" ORDER BY ") + value;
|
2010-06-23 23:46:08 +04:00
|
|
|
switch (mDirection) {
|
|
|
|
case nsIIDBCursor::NEXT:
|
|
|
|
case nsIIDBCursor::NEXT_NO_DUPLICATE:
|
2010-12-10 05:15:00 +03:00
|
|
|
directionClause.AppendLiteral(" ASC");
|
2010-06-23 23:46:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV:
|
|
|
|
case nsIIDBCursor::PREV_NO_DUPLICATE:
|
2010-12-10 05:15:00 +03:00
|
|
|
directionClause.AppendLiteral(" DESC");
|
2010-06-23 23:46:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown direction!");
|
|
|
|
}
|
2010-12-10 05:15:00 +03:00
|
|
|
directionClause += NS_LITERAL_CSTRING(", ") + keyValue +
|
|
|
|
NS_LITERAL_CSTRING(" ASC");
|
|
|
|
|
|
|
|
nsCString firstQuery = NS_LITERAL_CSTRING("SELECT ") + value +
|
|
|
|
NS_LITERAL_CSTRING(", ") + keyValue +
|
|
|
|
NS_LITERAL_CSTRING(", ") + data +
|
|
|
|
NS_LITERAL_CSTRING(" FROM ") + objectTable +
|
|
|
|
NS_LITERAL_CSTRING(" INNER JOIN ") + indexTable +
|
|
|
|
NS_LITERAL_CSTRING(" ON ") + indexTable +
|
|
|
|
NS_LITERAL_CSTRING(".") + objectDataIdColumn +
|
|
|
|
NS_LITERAL_CSTRING(" = ") + objectTable +
|
|
|
|
NS_LITERAL_CSTRING(".id WHERE ") + indexTable +
|
|
|
|
NS_LITERAL_CSTRING(".index_id = :id") +
|
|
|
|
keyRangeClause + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(firstQuery);
|
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);
|
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
nsresult rv = stmt->BindInt64ByName(id, mIndex->Id());
|
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:26:00 +03:00
|
|
|
if (!mLowerKey.IsUnset()) {
|
|
|
|
if (mLowerKey.IsString()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->BindStringByName(lowerKeyName, mLowerKey.StringValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
else if (mLowerKey.IsInt()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->BindInt64ByName(lowerKeyName, mLowerKey.IntValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad key!");
|
|
|
|
}
|
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:26:00 +03:00
|
|
|
if (!mUpperKey.IsUnset()) {
|
|
|
|
if (mUpperKey.IsString()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->BindStringByName(upperKeyName, mUpperKey.StringValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
else if (mUpperKey.IsInt()) {
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->BindInt64ByName(upperKeyName, mUpperKey.IntValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad key!");
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
PRBool hasResult;
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
if (!hasResult) {
|
|
|
|
mKey = Key::UNSETKEY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
PRInt32 keyType;
|
|
|
|
rv = stmt->GetTypeOfIndex(0, &keyType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
NS_ASSERTION(keyType == mozIStorageStatement::VALUE_TYPE_INTEGER ||
|
|
|
|
keyType == mozIStorageStatement::VALUE_TYPE_TEXT,
|
|
|
|
"Bad key type!");
|
|
|
|
|
|
|
|
if (keyType == mozIStorageStatement::VALUE_TYPE_INTEGER) {
|
|
|
|
mKey = stmt->AsInt64(0);
|
|
|
|
}
|
|
|
|
else if (keyType == mozIStorageStatement::VALUE_TYPE_TEXT) {
|
|
|
|
rv = stmt->GetString(0, mKey.ToString());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-12-10 05:15:00 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad SQLite type!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->GetTypeOfIndex(1, &keyType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
NS_ASSERTION(keyType == mozIStorageStatement::VALUE_TYPE_INTEGER ||
|
|
|
|
keyType == mozIStorageStatement::VALUE_TYPE_TEXT,
|
|
|
|
"Bad key type!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
if (keyType == mozIStorageStatement::VALUE_TYPE_INTEGER) {
|
|
|
|
mObjectKey = stmt->AsInt64(1);
|
|
|
|
}
|
|
|
|
else if (keyType == mozIStorageStatement::VALUE_TYPE_TEXT) {
|
|
|
|
rv = stmt->GetString(1, mObjectKey.ToString());
|
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-12-10 05:15:00 +03:00
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Bad SQLite type!");
|
|
|
|
}
|
|
|
|
|
2010-12-21 19:02:04 +03:00
|
|
|
rv = IDBObjectStore::GetStructuredCloneDataFromStatement(stmt, 2,
|
|
|
|
mCloneBuffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
// Now we need to make the query to get the next match.
|
|
|
|
nsCAutoString queryStart = NS_LITERAL_CSTRING("SELECT ") + value +
|
|
|
|
NS_LITERAL_CSTRING(", ") + keyValue +
|
|
|
|
NS_LITERAL_CSTRING(", ") + data +
|
|
|
|
NS_LITERAL_CSTRING(" FROM ") + objectTable +
|
|
|
|
NS_LITERAL_CSTRING(" INNER JOIN ") + indexTable +
|
|
|
|
NS_LITERAL_CSTRING(" ON ") + indexTable +
|
|
|
|
NS_LITERAL_CSTRING(".") + objectDataIdColumn +
|
|
|
|
NS_LITERAL_CSTRING(" = ") + objectTable +
|
|
|
|
NS_LITERAL_CSTRING(".id WHERE ") + indexTable +
|
|
|
|
NS_LITERAL_CSTRING(".index_id = :id");
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(currentKey, "current_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(rangeKey, "range_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(objectKey, "object_key");
|
|
|
|
|
|
|
|
switch (mDirection) {
|
|
|
|
case nsIIDBCursor::NEXT:
|
|
|
|
if (!mUpperKey.IsUnset()) {
|
|
|
|
AppendConditionClause(value, rangeKey, true, !mUpperOpen, queryStart);
|
|
|
|
mRangeKey = mUpperKey;
|
|
|
|
}
|
|
|
|
mContinueQuery = queryStart + NS_LITERAL_CSTRING(" AND ( ( ") +
|
|
|
|
value + NS_LITERAL_CSTRING(" = :") + currentKey +
|
|
|
|
NS_LITERAL_CSTRING(" AND ") + keyValue +
|
|
|
|
NS_LITERAL_CSTRING(" > :") + objectKey +
|
|
|
|
NS_LITERAL_CSTRING(" ) OR ( ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" > :") + currentKey +
|
|
|
|
NS_LITERAL_CSTRING(" ) )") + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
mContinueToQuery = queryStart + NS_LITERAL_CSTRING(" AND ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" >= :") +
|
|
|
|
currentKey + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::NEXT_NO_DUPLICATE:
|
|
|
|
if (!mUpperKey.IsUnset()) {
|
|
|
|
AppendConditionClause(value, rangeKey, true, !mUpperOpen, queryStart);
|
|
|
|
mRangeKey = mUpperKey;
|
|
|
|
}
|
|
|
|
mContinueQuery = queryStart + NS_LITERAL_CSTRING(" AND ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" > :") + currentKey +
|
|
|
|
directionClause + NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
mContinueToQuery = queryStart + NS_LITERAL_CSTRING(" AND ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" >= :") + currentKey +
|
|
|
|
directionClause + NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV:
|
|
|
|
if (!mLowerKey.IsUnset()) {
|
|
|
|
AppendConditionClause(value, rangeKey, false, !mLowerOpen, queryStart);
|
|
|
|
mRangeKey = mLowerKey;
|
|
|
|
}
|
|
|
|
mContinueQuery = queryStart + NS_LITERAL_CSTRING(" AND ( ( ") +
|
|
|
|
value + NS_LITERAL_CSTRING(" = :") + currentKey +
|
|
|
|
NS_LITERAL_CSTRING(" AND ") + keyValue +
|
|
|
|
NS_LITERAL_CSTRING(" < :") + objectKey +
|
|
|
|
NS_LITERAL_CSTRING(" ) OR ( ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" < :") + currentKey +
|
|
|
|
NS_LITERAL_CSTRING(" ) )") + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
mContinueToQuery = queryStart + NS_LITERAL_CSTRING(" AND ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" <= :") +
|
|
|
|
currentKey + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV_NO_DUPLICATE:
|
|
|
|
if (!mLowerKey.IsUnset()) {
|
|
|
|
AppendConditionClause(value, rangeKey, false, !mLowerOpen, queryStart);
|
|
|
|
mRangeKey = mLowerKey;
|
|
|
|
}
|
|
|
|
mContinueQuery = queryStart + NS_LITERAL_CSTRING(" AND ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" < :") + currentKey +
|
|
|
|
directionClause + NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
mContinueToQuery = queryStart + NS_LITERAL_CSTRING(" AND ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" <= :") + currentKey +
|
|
|
|
directionClause + NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown direction type!");
|
|
|
|
}
|
|
|
|
|
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
|
2011-01-07 09:21:36 +03:00
|
|
|
OpenCursorHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-12-10 05:15:00 +03:00
|
|
|
if (mKey.IsUnset()) {
|
2011-01-07 09:21:36 +03:00
|
|
|
*aVal = JSVAL_VOID;
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBCursor> cursor =
|
2010-12-10 05:15:00 +03:00
|
|
|
IDBCursor::Create(mRequest, mTransaction, mIndex, mDirection, mRangeKey,
|
|
|
|
mContinueQuery, mContinueToQuery, mKey, mObjectKey,
|
2010-12-21 19:02:04 +03:00
|
|
|
mCloneBuffer);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(cursor, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
NS_ASSERTION(!mCloneBuffer.data(), "Should have swapped!");
|
|
|
|
|
2011-01-07 09:21:36 +03:00
|
|
|
return WrapNative(aCx, cursor, aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|