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 "IDBObjectStore.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#include "nsIJSContextStack.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
|
|
|
#include "nsIVariant.h"
|
|
|
|
|
2010-06-29 02:22:41 +04:00
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "nsContentUtils.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-29 02:22:41 +04:00
|
|
|
#include "nsJSON.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "AsyncConnectionHelper.h"
|
2010-06-28 22:51:06 +04:00
|
|
|
#include "IDBCursor.h"
|
2010-06-29 02:22:41 +04:00
|
|
|
#include "IDBEvents.h"
|
|
|
|
#include "IDBIndex.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "IDBKeyRange.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 {
|
|
|
|
|
|
|
|
class AddHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 20:46:21 +04:00
|
|
|
AddHelper(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
IDBRequest* aRequest,
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBObjectStore* aObjectStore,
|
2010-06-23 23:46:08 +04:00
|
|
|
const nsAString& aValue,
|
|
|
|
const Key& aKey,
|
|
|
|
bool aOverwrite,
|
|
|
|
nsTArray<IndexUpdateInfo>& aIndexUpdateInfo)
|
2010-10-19 21:58:52 +04:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
|
|
|
mValue(aValue), mKey(aKey), mOverwrite(aOverwrite)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsresult ModifyValueForNewKey();
|
|
|
|
nsresult UpdateIndexes(mozIStorageConnection* aConnection,
|
|
|
|
PRInt64 aObjectDataId);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// In-params.
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
// These may change in the autoincrement case.
|
|
|
|
nsString mValue;
|
|
|
|
Key mKey;
|
|
|
|
const bool mOverwrite;
|
|
|
|
nsTArray<IndexUpdateInfo> mIndexUpdateInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
class GetHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 20:46:21 +04:00
|
|
|
GetHelper(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
IDBRequest* aRequest,
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBObjectStore* aObjectStore,
|
|
|
|
const Key& aKey)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
|
|
|
mKey(aKey)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult OnSuccess(nsIDOMEventTarget* aTarget);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
protected:
|
|
|
|
// In-params.
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2010-06-23 23:46:08 +04:00
|
|
|
const Key mKey;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Out-params.
|
|
|
|
nsString mValue;
|
|
|
|
};
|
|
|
|
|
2010-11-11 02:25:57 +03:00
|
|
|
class DeleteHelper : public GetHelper
|
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,
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBObjectStore* aObjectStore,
|
|
|
|
const Key& aKey)
|
|
|
|
: GetHelper(aTransaction, aRequest, aObjectStore, aKey)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult OnSuccess(nsIDOMEventTarget* aTarget);
|
|
|
|
nsresult GetSuccessResult(nsIWritableVariant* aResult);
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2010-08-27 00:57:30 +04:00
|
|
|
class ClearHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ClearHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBRequest* aRequest,
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBObjectStore* aObjectStore)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore)
|
2010-08-27 00:57:30 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2010-08-27 00:57:30 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-08-27 00:57:30 +04:00
|
|
|
protected:
|
|
|
|
// In-params.
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2010-08-27 00:57:30 +04:00
|
|
|
};
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
class OpenCursorHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 20:46:21 +04:00
|
|
|
OpenCursorHelper(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
IDBRequest* aRequest,
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore* aObjectStore,
|
2010-11-11 02:26:00 +03:00
|
|
|
const Key& aLowerKey,
|
|
|
|
const Key& aUpperKey,
|
|
|
|
PRBool aLowerOpen,
|
|
|
|
PRBool aUpperOpen,
|
2010-11-11 02:25:53 +03:00
|
|
|
PRUint16 aDirection)
|
2010-06-23 23:46:08 +04:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
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);
|
|
|
|
nsresult GetSuccessResult(nsIWritableVariant* aResult);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
private:
|
|
|
|
// In-params.
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
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.
|
|
|
|
nsTArray<KeyValuePair> mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CreateIndexHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 20:46:21 +04:00
|
|
|
CreateIndexHelper(IDBTransaction* aTransaction,
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBIndex* aIndex)
|
|
|
|
: AsyncConnectionHelper(aTransaction, nsnull), mIndex(aIndex)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult OnSuccess(nsIDOMEventTarget* aTarget);
|
|
|
|
void OnError(nsIDOMEventTarget* aTarget, nsresult aErrorCode);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
2010-10-19 21:58:52 +04:00
|
|
|
mIndex = nsnull;
|
2010-09-10 08:54:25 +04:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
private:
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult InsertDataFromObjectStore(mozIStorageConnection* aConnection);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
// In-params.
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBIndex> mIndex;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
// Out-params.
|
|
|
|
PRInt64 mId;
|
|
|
|
};
|
|
|
|
|
2010-11-11 02:25:57 +03:00
|
|
|
class DeleteIndexHelper : public AsyncConnectionHelper
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
public:
|
2010-11-11 02:25:57 +03:00
|
|
|
DeleteIndexHelper(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
const nsAString& aName,
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore* aObjectStore)
|
2010-10-19 21:58:52 +04:00
|
|
|
: AsyncConnectionHelper(aTransaction, nsnull), mName(aName),
|
2010-06-23 23:46:08 +04:00
|
|
|
mObjectStore(aObjectStore)
|
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult OnSuccess(nsIDOMEventTarget* aTarget);
|
|
|
|
void OnError(nsIDOMEventTarget* aTarget, nsresult aErrorCode);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
private:
|
|
|
|
// In-params
|
|
|
|
nsString mName;
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class GetAllHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 20:46:21 +04:00
|
|
|
GetAllHelper(IDBTransaction* aTransaction,
|
2010-06-23 23:46:08 +04:00
|
|
|
IDBRequest* aRequest,
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBObjectStore* aObjectStore,
|
2010-11-11 02:26:00 +03:00
|
|
|
const Key& aLowerKey,
|
|
|
|
const Key& aUpperKey,
|
|
|
|
const PRBool aLowerOpen,
|
|
|
|
const PRBool aUpperOpen,
|
2010-10-19 21:58:52 +04:00
|
|
|
const PRUint32 aLimit)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
2010-11-11 02:26:00 +03:00
|
|
|
mLowerKey(aLowerKey), mUpperKey(aUpperKey), mLowerOpen(aLowerOpen),
|
|
|
|
mUpperOpen(aUpperOpen), mLimit(aLimit)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult OnSuccess(nsIDOMEventTarget* aTarget);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
protected:
|
|
|
|
// In-params.
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
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 PRUint32 mLimit;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Out-params.
|
|
|
|
nsTArray<nsString> mValues;
|
|
|
|
};
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_STACK_CLASS
|
|
|
|
class AutoRemoveIndex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoRemoveIndex(PRUint32 aDatabaseId,
|
|
|
|
const nsAString& aObjectStoreName,
|
|
|
|
const nsAString& aIndexName)
|
|
|
|
: mDatabaseId(aDatabaseId), mObjectStoreName(aObjectStoreName),
|
|
|
|
mIndexName(aIndexName)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~AutoRemoveIndex()
|
|
|
|
{
|
|
|
|
if (mDatabaseId) {
|
|
|
|
ObjectStoreInfo* info;
|
|
|
|
if (ObjectStoreInfo::Get(mDatabaseId, mObjectStoreName, &info)) {
|
|
|
|
for (PRUint32 index = 0; index < info->indexes.Length(); index++) {
|
|
|
|
if (info->indexes[index].name == mIndexName) {
|
|
|
|
info->indexes.RemoveElementAt(index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void forget()
|
|
|
|
{
|
|
|
|
mDatabaseId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRUint32 mDatabaseId;
|
|
|
|
nsString mObjectStoreName;
|
|
|
|
nsString mIndexName;
|
|
|
|
};
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
inline
|
|
|
|
nsresult
|
|
|
|
GetKeyFromObject(JSContext* aCx,
|
|
|
|
JSObject* aObj,
|
|
|
|
const nsString& aKeyPath,
|
|
|
|
Key& aKey)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aCx && aObj, "Null pointers!");
|
|
|
|
NS_ASSERTION(!aKeyPath.IsVoid(), "This will explode!");
|
|
|
|
|
|
|
|
const jschar* keyPathChars = reinterpret_cast<const jschar*>(aKeyPath.get());
|
|
|
|
const size_t keyPathLen = aKeyPath.Length();
|
|
|
|
|
|
|
|
jsval key;
|
|
|
|
JSBool ok = JS_GetUCProperty(aCx, aObj, keyPathChars, keyPathLen, &key);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
|
|
|
|
|
2010-06-29 02:22:41 +04:00
|
|
|
nsresult rv = IDBObjectStore::GetKeyFromJSVal(key, aKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-06-29 02:22:41 +04:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
inline
|
|
|
|
already_AddRefed<IDBRequest>
|
|
|
|
GenerateRequest(IDBObjectStore* aObjectStore)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
IDBDatabase* database = aObjectStore->Transaction()->Database();
|
2010-11-11 02:26:03 +03:00
|
|
|
return IDBRequest::Create(aObjectStore, database->ScriptContext(),
|
|
|
|
database->Owner(), aObjectStore->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<IDBObjectStore>
|
2010-09-10 02:15:38 +04:00
|
|
|
IDBObjectStore::Create(IDBTransaction* aTransaction,
|
2010-10-19 21:58:52 +04:00
|
|
|
const ObjectStoreInfo* aStoreInfo)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBObjectStore> objectStore = new IDBObjectStore();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
objectStore->mScriptContext = aTransaction->Database()->ScriptContext();
|
|
|
|
objectStore->mOwner = aTransaction->Database()->Owner();
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
objectStore->mTransaction = aTransaction;
|
|
|
|
objectStore->mName = aStoreInfo->name;
|
|
|
|
objectStore->mId = aStoreInfo->id;
|
|
|
|
objectStore->mKeyPath = aStoreInfo->keyPath;
|
|
|
|
objectStore->mAutoIncrement = aStoreInfo->autoIncrement;
|
|
|
|
objectStore->mDatabaseId = aStoreInfo->databaseId;
|
|
|
|
|
|
|
|
return objectStore.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::GetKeyFromVariant(nsIVariant* aKeyVariant,
|
|
|
|
Key& aKey)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
if (!aKeyVariant) {
|
|
|
|
aKey = Key::UNSETKEY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint16 type;
|
|
|
|
nsresult rv = aKeyVariant->GetDataType(&type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// See xpcvariant.cpp, these are the only types we should expect.
|
|
|
|
switch (type) {
|
|
|
|
case nsIDataType::VTYPE_VOID:
|
|
|
|
aKey = Key::UNSETKEY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIDataType::VTYPE_EMPTY:
|
|
|
|
aKey = Key::NULLKEY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIDataType::VTYPE_WSTRING_SIZE_IS:
|
|
|
|
rv = aKeyVariant->GetAsAString(aKey.ToString());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIDataType::VTYPE_INT32:
|
|
|
|
case nsIDataType::VTYPE_DOUBLE:
|
|
|
|
rv = aKeyVariant->GetAsInt64(aKey.ToIntPtr());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-29 02:22:41 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
IDBObjectStore::GetKeyFromJSVal(jsval aKeyVal,
|
|
|
|
Key& aKey)
|
|
|
|
{
|
|
|
|
if (JSVAL_IS_VOID(aKeyVal)) {
|
|
|
|
aKey = Key::UNSETKEY;
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_NULL(aKeyVal)) {
|
|
|
|
aKey = Key::NULLKEY;
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_STRING(aKeyVal)) {
|
|
|
|
aKey = nsDependentJSString(aKeyVal);
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_INT(aKeyVal)) {
|
|
|
|
aKey = JSVAL_TO_INT(aKeyVal);
|
|
|
|
}
|
|
|
|
else if (JSVAL_IS_DOUBLE(aKeyVal)) {
|
2010-07-15 10:19:36 +04:00
|
|
|
aKey = JSVAL_TO_DOUBLE(aKeyVal);
|
2010-06-29 02:22:41 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
IDBObjectStore::GetJSValFromKey(const Key& aKey,
|
|
|
|
JSContext* aCx,
|
|
|
|
jsval* aKeyVal)
|
|
|
|
{
|
|
|
|
if (aKey.IsUnset()) {
|
|
|
|
*aKeyVal = JSVAL_VOID;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aKey.IsNull()) {
|
|
|
|
*aKeyVal = JSVAL_NULL;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aKey.IsInt()) {
|
|
|
|
JSBool ok = JS_NewNumberValue(aCx, aKey.IntValue(), aKeyVal);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aKey.IsString()) {
|
|
|
|
const nsString& keyString = aKey.StringValue();
|
|
|
|
JSString* str =
|
|
|
|
JS_NewUCStringCopyN(aCx,
|
|
|
|
reinterpret_cast<const jschar*>(keyString.get()),
|
|
|
|
keyString.Length());
|
|
|
|
NS_ENSURE_TRUE(str, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
*aKeyVal = STRING_TO_JSVAL(str);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Unknown key type!");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::GetJSONFromArg0(/* jsval arg0, */
|
|
|
|
nsAString& aJSON)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
NS_ENSURE_TRUE(xpc, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsAXPCNativeCallContext* cc;
|
|
|
|
nsresult rv = xpc->GetCurrentNativeCallContext(&cc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(cc, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
PRUint32 argc;
|
|
|
|
rv = cc->GetArgc(&argc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
return NS_ERROR_XPC_NOT_ENOUGH_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval* argv;
|
|
|
|
rv = cc->GetArgvPtr(&argv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
JSContext* cx;
|
|
|
|
rv = cc->GetJSContext(&cx);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIJSON> json(new nsJSON());
|
|
|
|
|
2010-10-11 02:35:42 +04:00
|
|
|
rv = json->EncodeFromJSVal(&argv[0], cx, aJSON);
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::GetKeyPathValueFromJSON(const nsAString& aJSON,
|
|
|
|
const nsAString& aKeyPath,
|
|
|
|
JSContext** aCx,
|
|
|
|
Key& aValue)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aJSON.IsEmpty(), "Empty JSON!");
|
|
|
|
NS_ASSERTION(!aKeyPath.IsEmpty(), "Empty keyPath!");
|
|
|
|
NS_ASSERTION(aCx, "Null pointer!");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!*aCx) {
|
|
|
|
rv = nsContentUtils::ThreadJSContextStack()->GetSafeJSContext(aCx);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoRequest ar(*aCx);
|
|
|
|
|
|
|
|
js::AutoValueRooter clone(*aCx);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIJSON> json(new nsJSON());
|
2010-07-15 10:19:36 +04:00
|
|
|
rv = json->DecodeToJSVal(aJSON, *aCx, clone.jsval_addr());
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
if (JSVAL_IS_PRIMITIVE(clone.jsval_value())) {
|
2010-06-23 23:46:08 +04:00
|
|
|
// This isn't an object, so just leave the key unset.
|
|
|
|
aValue = Key::UNSETKEY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
JSObject* obj = JSVAL_TO_OBJECT(clone.jsval_value());
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
const jschar* keyPathChars =
|
|
|
|
reinterpret_cast<const jschar*>(aKeyPath.BeginReading());
|
|
|
|
const size_t keyPathLen = aKeyPath.Length();
|
|
|
|
|
|
|
|
js::AutoValueRooter value(*aCx);
|
|
|
|
JSBool ok = JS_GetUCProperty(*aCx, obj, keyPathChars, keyPathLen,
|
2010-07-15 10:19:36 +04:00
|
|
|
value.jsval_addr());
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
rv = GetKeyFromJSVal(value.jsval_value(), aValue);
|
2010-06-29 02:22:41 +04:00
|
|
|
if (NS_FAILED(rv) || aValue.IsNull()) {
|
|
|
|
// If the object doesn't have a value that we can use for our index then we
|
|
|
|
// leave it unset.
|
2010-06-23 23:46:08 +04:00
|
|
|
aValue = Key::UNSETKEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::GetIndexUpdateInfo(ObjectStoreInfo* aObjectStoreInfo,
|
|
|
|
JSContext* aCx,
|
|
|
|
jsval aObject,
|
|
|
|
nsTArray<IndexUpdateInfo>& aUpdateInfoArray)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
JSObject* cloneObj = nsnull;
|
|
|
|
|
|
|
|
PRUint32 count = aObjectStoreInfo->indexes.Length();
|
|
|
|
if (count && !JSVAL_IS_PRIMITIVE(aObject)) {
|
|
|
|
if (!aUpdateInfoArray.SetCapacity(count)) {
|
|
|
|
NS_ERROR("Out of memory!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
cloneObj = JSVAL_TO_OBJECT(aObject);
|
|
|
|
|
|
|
|
for (PRUint32 indexesIndex = 0; indexesIndex < count; indexesIndex++) {
|
|
|
|
const IndexInfo& indexInfo = aObjectStoreInfo->indexes[indexesIndex];
|
|
|
|
|
|
|
|
const jschar* keyPathChars =
|
|
|
|
reinterpret_cast<const jschar*>(indexInfo.keyPath.BeginReading());
|
|
|
|
const size_t keyPathLen = indexInfo.keyPath.Length();
|
|
|
|
|
|
|
|
jsval keyPathValue;
|
|
|
|
JSBool ok = JS_GetUCProperty(aCx, cloneObj, keyPathChars, keyPathLen,
|
|
|
|
&keyPathValue);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
Key value;
|
2010-06-29 02:22:41 +04:00
|
|
|
nsresult rv = GetKeyFromJSVal(keyPathValue, value);
|
|
|
|
if (NS_FAILED(rv) || value.IsUnset() || value.IsNull()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
// Not a value we can do anything with, ignore it.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexUpdateInfo* updateInfo = aUpdateInfoArray.AppendElement();
|
|
|
|
updateInfo->info = indexInfo;
|
|
|
|
updateInfo->value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aUpdateInfoArray.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::UpdateIndexes(IDBTransaction* aTransaction,
|
|
|
|
PRInt64 aObjectStoreId,
|
|
|
|
const Key& aObjectStoreKey,
|
|
|
|
bool aAutoIncrement,
|
|
|
|
bool aOverwrite,
|
|
|
|
PRInt64 aObjectDataId,
|
|
|
|
const nsTArray<IndexUpdateInfo>& aUpdateInfoArray)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (aAutoIncrement) {
|
|
|
|
NS_ASSERTION(aObjectDataId != LL_MININT, "Bad objectData id!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(aObjectDataId == LL_MININT, "Bad objectData id!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PRUint32 indexCount = aUpdateInfoArray.Length();
|
|
|
|
NS_ASSERTION(indexCount, "Don't call me!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!aAutoIncrement) {
|
|
|
|
stmt = aTransaction->GetCachedStatement(
|
|
|
|
"SELECT id "
|
|
|
|
"FROM object_data "
|
|
|
|
"WHERE object_store_id = :osid "
|
|
|
|
"AND key_value = :key_value"
|
|
|
|
);
|
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), aObjectStoreId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(!aObjectStoreKey.IsUnset(), "This shouldn't happen!");
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(keyValue, "key_value");
|
|
|
|
|
|
|
|
if (aObjectStoreKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(keyValue, aObjectStoreKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (aObjectStoreKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(keyValue, aObjectStoreKey.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown key type!");
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!hasResult) {
|
|
|
|
NS_ERROR("This is bad, we just added this value! Where'd it go?!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
aObjectDataId = stmt->AsInt64(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(aObjectDataId != LL_MININT, "Bad objectData id!");
|
|
|
|
|
|
|
|
for (PRUint32 indexIndex = 0; indexIndex < indexCount; indexIndex++) {
|
|
|
|
const IndexUpdateInfo& updateInfo = aUpdateInfoArray[indexIndex];
|
|
|
|
|
|
|
|
stmt = aTransaction->IndexUpdateStatement(updateInfo.info.autoIncrement,
|
|
|
|
updateInfo.info.unique,
|
|
|
|
aOverwrite);
|
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
mozStorageStatementScoper scoper2(stmt);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"),
|
|
|
|
updateInfo.info.id);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("object_data_id"),
|
|
|
|
aObjectDataId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(objectDataKey, "object_data_key");
|
|
|
|
|
|
|
|
if (aObjectStoreKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(objectDataKey, aObjectStoreKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (aObjectStoreKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(objectDataKey, aObjectStoreKey.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown key type!");
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(value, "value");
|
|
|
|
|
|
|
|
if (updateInfo.value.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(value, updateInfo.value.IntValue());
|
|
|
|
}
|
|
|
|
else if (updateInfo.value.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(value, updateInfo.value.StringValue());
|
|
|
|
}
|
|
|
|
else if (updateInfo.value.IsUnset()) {
|
|
|
|
rv = stmt->BindStringByName(value, updateInfo.value.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unknown key type!");
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::IDBObjectStore()
|
2010-06-23 23:46:08 +04:00
|
|
|
: mId(LL_MININT),
|
2010-10-19 21:58:52 +04:00
|
|
|
mAutoIncrement(PR_FALSE)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::~IDBObjectStore()
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-06-29 02:22:41 +04:00
|
|
|
IDBObjectStore::GetAddInfo(JSContext* aCx,
|
|
|
|
jsval aValue,
|
|
|
|
jsval aKeyVal,
|
2010-06-28 22:51:06 +04:00
|
|
|
nsString& aJSON,
|
|
|
|
Key& aKey,
|
|
|
|
nsTArray<IndexUpdateInfo>& aUpdateInfoArray)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-10-11 02:35:42 +04:00
|
|
|
nsresult rv;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-11 02:35:42 +04:00
|
|
|
JSAutoRequest ar(aCx);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
if (mKeyPath.IsEmpty()) {
|
2010-06-29 02:22:41 +04:00
|
|
|
rv = GetKeyFromJSVal(aKeyVal, aKey);
|
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
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Inline keys live on the object. Make sure it is an object.
|
2010-10-11 02:35:42 +04:00
|
|
|
if (JSVAL_IS_PRIMITIVE(aValue)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-10-11 02:35:42 +04:00
|
|
|
rv = GetKeyFromObject(aCx, JSVAL_TO_OBJECT(aValue), mKeyPath, aKey);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
// Except if null was passed, in which case we're supposed to generate the
|
|
|
|
// key.
|
2010-06-29 02:22:41 +04:00
|
|
|
if (aKey.IsUnset() && JSVAL_IS_NULL(aKeyVal)) {
|
2010-06-23 23:46:08 +04:00
|
|
|
aKey = Key::NULLKEY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aKey.IsUnset() && !mAutoIncrement) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out indexes and the index values to update here.
|
2010-11-11 02:25:44 +03:00
|
|
|
ObjectStoreInfo* info;
|
|
|
|
if (!ObjectStoreInfo::Get(mTransaction->Database()->Id(), mName, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
rv = GetIndexUpdateInfo(info, aCx, aValue, aUpdateInfoArray);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIJSON> json(new nsJSON());
|
2010-10-11 02:35:42 +04:00
|
|
|
rv = json->EncodeFromJSVal(&aValue, aCx, aJSON);
|
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
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBObjectStore)
|
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBObjectStore)
|
2010-09-10 02:15:38 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mTransaction,
|
|
|
|
nsPIDOMEventTarget)
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mScriptContext)
|
2010-11-11 02:25:40 +03:00
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < tmp->mCreatedIndexes.Length(); i++) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mCreatedIndexes[i]");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIIDBIndex*>(tmp->mCreatedIndexes[i].get()));
|
|
|
|
}
|
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(IDBObjectStore)
|
2010-09-10 02:15:38 +04:00
|
|
|
// Don't unlink mTransaction!
|
2010-11-11 02:26:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mScriptContext)
|
2010-11-11 02:25:40 +03:00
|
|
|
|
|
|
|
tmp->mCreatedIndexes.Clear();
|
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(IDBObjectStore)
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBObjectStore)
|
2010-06-28 22:51:06 +04:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBObjectStore)
|
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(IDBObjectStore)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBObjectStore)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
DOMCI_DATA(IDBObjectStore, IDBObjectStore)
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::GetName(nsAString& aName)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
aName.Assign(mName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::GetKeyPath(nsAString& aKeyPath)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
aKeyPath.Assign(mKeyPath);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::GetIndexNames(nsIDOMDOMStringList** aIndexNames)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
ObjectStoreInfo* info;
|
|
|
|
if (!ObjectStoreInfo::Get(mTransaction->Database()->Id(), mName, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsRefPtr<nsDOMStringList> list(new nsDOMStringList());
|
|
|
|
|
|
|
|
PRUint32 count = info->indexes.Length();
|
|
|
|
for (PRUint32 index = 0; index < count; index++) {
|
|
|
|
NS_ENSURE_TRUE(list->Add(info->indexes[index].name),
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
list.forget(aIndexNames);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::Get(nsIVariant* aKey,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (!mTransaction->TransactionIsOpen()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Key key;
|
|
|
|
nsresult rv = GetKeyFromVariant(aKey, 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
|
|
|
|
|
|
|
if (key.IsUnset()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_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-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<GetHelper> helper(new GetHelper(mTransaction, 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
|
|
|
IDBObjectStore::GetAll(nsIIDBKeyRange* aKeyRange,
|
|
|
|
PRUint32 aLimit,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (!mTransaction->TransactionIsOpen()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aOptionalArgCount < 2) {
|
|
|
|
aLimit = PR_UINT32_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
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-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->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
|
|
|
}
|
|
|
|
|
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-11-11 02:26:00 +03:00
|
|
|
new GetAllHelper(mTransaction, request, this, lowerKey, upperKey, lowerOpen,
|
|
|
|
upperOpen, 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-07-15 10:19:36 +04:00
|
|
|
IDBObjectStore::Add(const jsval &aValue,
|
|
|
|
const jsval &aKey,
|
2010-06-29 02:22:41 +04:00
|
|
|
JSContext* aCx,
|
|
|
|
PRUint8 aOptionalArgCount,
|
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() || !IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-08-27 00:57:30 +04:00
|
|
|
jsval keyval;
|
|
|
|
if (aOptionalArgCount >= 1) {
|
|
|
|
keyval = aKey;
|
|
|
|
if (mAutoIncrement && JSVAL_IS_NULL(keyval)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2010-08-27 00:57:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyval = JSVAL_VOID;
|
|
|
|
}
|
2010-06-29 02:22:41 +04:00
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsString jsonValue;
|
|
|
|
Key key;
|
|
|
|
nsTArray<IndexUpdateInfo> updateInfo;
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
nsresult rv = GetAddInfo(aCx, aValue, keyval, jsonValue, key, updateInfo);
|
2010-06-23 23:46:08 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.IsUnset() && !mAutoIncrement) {
|
2010-11-11 02:25:44 +03:00
|
|
|
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
|
|
|
|
|
|
|
nsRefPtr<AddHelper> helper =
|
2010-10-19 21:58:52 +04:00
|
|
|
new AddHelper(mTransaction, request, this, jsonValue, key, false,
|
|
|
|
updateInfo);
|
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-08-27 00:57:28 +04:00
|
|
|
IDBObjectStore::Put(const jsval &aValue,
|
|
|
|
const jsval &aKey,
|
|
|
|
JSContext* aCx,
|
|
|
|
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
|
|
|
if (!mTransaction->TransactionIsOpen() || !IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
jsval keyval = (aOptionalArgCount >= 1) ? aKey : JSVAL_VOID;
|
2010-06-29 02:22:41 +04:00
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsString jsonValue;
|
|
|
|
Key key;
|
|
|
|
nsTArray<IndexUpdateInfo> updateInfo;
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
nsresult rv = GetAddInfo(aCx, aValue, keyval, jsonValue, key, updateInfo);
|
2010-06-23 23:46:08 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.IsUnset() || key.IsNull()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
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
|
|
|
|
|
|
|
nsRefPtr<AddHelper> helper =
|
2010-10-19 21:58:52 +04:00
|
|
|
new AddHelper(mTransaction, request, this, jsonValue, key, true,
|
|
|
|
updateInfo);
|
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
|
|
|
IDBObjectStore::Delete(nsIVariant* aKey,
|
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() || !IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Key key;
|
|
|
|
nsresult rv = GetKeyFromVariant(aKey, key);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.IsUnset() || key.IsNull()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
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-11-11 02:25:57 +03:00
|
|
|
nsRefPtr<DeleteHelper> helper =
|
|
|
|
new DeleteHelper(mTransaction, 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;
|
|
|
|
}
|
|
|
|
|
2010-08-27 00:57:30 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBObjectStore::Clear(nsIIDBRequest** _retval)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
if (!mTransaction->TransactionIsOpen() || !IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-08-27 00:57:30 +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-08-27 00:57:30 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<ClearHelper> helper(new ClearHelper(mTransaction, request, this));
|
2010-11-11 02:25:44 +03:00
|
|
|
|
2010-08-27 00:57:30 +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-08-27 00:57:30 +04:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::OpenCursor(nsIIDBKeyRange* aKeyRange,
|
|
|
|
PRUint16 aDirection,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (!mTransaction->TransactionIsOpen()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_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);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
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-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->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(mTransaction, 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-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::CreateIndex(const nsAString& aName,
|
|
|
|
const nsAString& aKeyPath,
|
|
|
|
PRBool aUnique,
|
2010-10-19 21:58:52 +04:00
|
|
|
nsIIDBIndex** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (aName.IsEmpty() || aKeyPath.IsEmpty()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
IDBTransaction* transaction = AsyncConnectionHelper::GetCurrentTransaction();
|
|
|
|
|
|
|
|
if (!transaction ||
|
|
|
|
transaction != mTransaction ||
|
|
|
|
mTransaction->Mode() != nsIIDBTransaction::VERSION_CHANGE) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectStoreInfo* info;
|
|
|
|
if (!ObjectStoreInfo::Get(mTransaction->Database()->Id(), mName, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
PRUint32 indexCount = info->indexes.Length();
|
|
|
|
for (PRUint32 index = 0; index < indexCount; index++) {
|
|
|
|
if (info->indexes[index].name == aName) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ASSERTION(mTransaction->TransactionIsOpen(), "Impossible!");
|
|
|
|
|
|
|
|
DatabaseInfo* databaseInfo;
|
|
|
|
if (!DatabaseInfo::Get(mTransaction->Database()->Id(), &databaseInfo)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
IndexInfo* indexInfo = info->indexes.AppendElement();
|
|
|
|
if (!indexInfo) {
|
|
|
|
NS_WARNING("Out of memory!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
indexInfo->id = databaseInfo->nextIndexId++;
|
|
|
|
indexInfo->name = aName;
|
|
|
|
indexInfo->keyPath = aKeyPath;
|
|
|
|
indexInfo->unique = aUnique;
|
|
|
|
indexInfo->autoIncrement = mAutoIncrement;
|
|
|
|
|
|
|
|
// Don't leave this in the list if we fail below!
|
|
|
|
AutoRemoveIndex autoRemove(databaseInfo->id, mName, aName);
|
|
|
|
|
2010-11-11 02:25:40 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
for (PRUint32 index = 0; index < mCreatedIndexes.Length(); index++) {
|
|
|
|
if (mCreatedIndexes[index]->Name() == aName) {
|
|
|
|
NS_ERROR("Already created this one!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBIndex> index(IDBIndex::Create(this, indexInfo));
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:40 +03:00
|
|
|
if (!mCreatedIndexes.AppendElement(index)) {
|
|
|
|
NS_WARNING("Out of memory!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-11-11 02:25:40 +03:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsRefPtr<CreateIndexHelper> helper =
|
2010-10-19 21:58:52 +04:00
|
|
|
new CreateIndexHelper(mTransaction, index);
|
|
|
|
|
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
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
autoRemove.forget();
|
|
|
|
|
|
|
|
index.forget(_retval);
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::Index(const nsAString& aName,
|
2010-06-28 22:51:06 +04:00
|
|
|
nsIIDBIndex** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (!mTransaction->TransactionIsOpen()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aName.IsEmpty()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_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(), mName, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
IndexInfo* indexInfo = nsnull;
|
|
|
|
PRUint32 indexCount = info->indexes.Length();
|
|
|
|
for (PRUint32 index = 0; index < indexCount; index++) {
|
|
|
|
if (info->indexes[index].name == aName) {
|
|
|
|
indexInfo = &(info->indexes[index]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!indexInfo) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:40 +03:00
|
|
|
nsRefPtr<IDBIndex> retval;
|
|
|
|
for (PRUint32 i = 0; i < mCreatedIndexes.Length(); i++) {
|
|
|
|
nsRefPtr<IDBIndex>& index = mCreatedIndexes[i];
|
|
|
|
if (index->Name() == aName) {
|
|
|
|
retval = index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:40 +03:00
|
|
|
if (!retval) {
|
|
|
|
retval = IDBIndex::Create(this, indexInfo);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(retval, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-11-11 02:25:40 +03:00
|
|
|
|
|
|
|
if (!mCreatedIndexes.AppendElement(retval)) {
|
|
|
|
NS_WARNING("Out of memory!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-11-11 02:25:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval.forget(_retval);
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-11-11 02:25:57 +03:00
|
|
|
IDBObjectStore::DeleteIndex(const nsAString& aName)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
if (aName.IsEmpty()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
IDBTransaction* transaction = AsyncConnectionHelper::GetCurrentTransaction();
|
|
|
|
|
|
|
|
if (!transaction ||
|
|
|
|
transaction != mTransaction ||
|
|
|
|
mTransaction->Mode() != nsIIDBTransaction::VERSION_CHANGE) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mTransaction->TransactionIsOpen(), "Impossible!");
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
ObjectStoreInfo* info;
|
|
|
|
if (!ObjectStoreInfo::Get(mTransaction->Database()->Id(), mName, &info)) {
|
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
PRUint32 index = 0;
|
|
|
|
for (; index < info->indexes.Length(); index++) {
|
2010-06-23 23:46:08 +04:00
|
|
|
if (info->indexes[index].name == aName) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (index == info->indexes.Length()) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:57 +03:00
|
|
|
nsRefPtr<DeleteIndexHelper> helper =
|
|
|
|
new DeleteIndexHelper(mTransaction, aName, this);
|
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
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
info->indexes.RemoveElementAt(index);
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-06-23 23:46:08 +04:00
|
|
|
AddHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (mKey.IsNull()) {
|
|
|
|
NS_WARNING("Using a UUID for null keys, probably can do something faster!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidGen =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1", &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
|
|
|
|
|
|
|
nsID id;
|
|
|
|
rv = uuidGen->GenerateUUIDInPlace(&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
|
|
|
|
|
|
|
char idString[NSID_LENGTH] = { 0 };
|
|
|
|
id.ToProvidedString(idString);
|
|
|
|
|
|
|
|
mKey = NS_ConvertASCIItoUTF16(idString);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mayOverwrite = mOverwrite;
|
|
|
|
bool unsetKey = mKey.IsUnset();
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
bool autoIncrement = mObjectStore->IsAutoIncrement();
|
|
|
|
PRInt64 osid = mObjectStore->Id();
|
|
|
|
const nsString& keyPath = mObjectStore->KeyPath();
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
if (unsetKey) {
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ASSERTION(autoIncrement, "Must have a key for non-autoIncrement!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
// Will need to add first and then set the key later.
|
|
|
|
mayOverwrite = false;
|
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (autoIncrement && !unsetKey) {
|
2010-06-23 23:46:08 +04:00
|
|
|
mayOverwrite = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
if (!mOverwrite && !unsetKey) {
|
|
|
|
// Make sure the key doesn't exist already
|
2010-10-19 21:58:52 +04:00
|
|
|
stmt = mTransaction->GetStatement(autoIncrement);
|
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
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), osid);
|
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(id, "id");
|
|
|
|
|
|
|
|
if (mKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(id, mKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(id, 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
|
|
|
|
|
|
|
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-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we add it to the database (or update, depending on our variables).
|
2010-10-19 21:58:52 +04:00
|
|
|
stmt = mTransaction->AddStatement(true, mayOverwrite, autoIncrement);
|
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");
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), osid);
|
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-19 21:58:52 +04:00
|
|
|
if (!autoIncrement || mayOverwrite) {
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ASSERTION(!mKey.IsUnset(), "This shouldn't happen!");
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-08-05 01:44:26 +04:00
|
|
|
rv = stmt->Execute();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-08-27 00:57:28 +04:00
|
|
|
if (mayOverwrite && rv == NS_ERROR_STORAGE_CONSTRAINT) {
|
2010-08-05 01:44:26 +04:00
|
|
|
scoper.Abandon();
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
stmt = mTransaction->AddStatement(false, true, autoIncrement);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-05 01:44:26 +04:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper2(stmt);
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), osid);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-05 01:44:26 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (!autoIncrement) {
|
2010-08-05 01:44:26 +04:00
|
|
|
NS_ASSERTION(!mKey.IsUnset(), "This shouldn't happen!");
|
|
|
|
|
|
|
|
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-08-05 01:44:26 +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-08-05 01:44:26 +04:00
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-08-05 01:44:26 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are supposed to generate a key, get the new id.
|
2010-10-19 21:58:52 +04:00
|
|
|
if (autoIncrement && !mOverwrite) {
|
2010-06-23 23:46:08 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
PRInt64 oldKey = unsetKey ? 0 : mKey.IntValue();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rv = aConnection->GetLastInsertRowID(mKey.ToIntPtr());
|
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
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(mKey.IsInt(), "Bad key value!");
|
|
|
|
if (!unsetKey) {
|
|
|
|
NS_ASSERTION(mKey.IntValue() == oldKey, "Something went haywire!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (!keyPath.IsEmpty() && unsetKey) {
|
2010-06-23 23:46:08 +04:00
|
|
|
// Special case where someone put an object into an autoIncrement'ing
|
|
|
|
// objectStore with no key in its keyPath set. We needed to figure out
|
|
|
|
// which row id we would get above before we could set that properly.
|
|
|
|
rv = ModifyValueForNewKey();
|
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
|
|
|
|
|
|
|
scoper.Abandon();
|
|
|
|
rv = stmt->Reset();
|
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
|
|
|
|
|
|
|
stmt = mTransaction->AddStatement(false, true, true);
|
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 scoper2(stmt);
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), osid);
|
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->BindInt64ByName(keyValue, mKey.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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update our indexes if needed.
|
|
|
|
if (!mIndexUpdateInfo.IsEmpty()) {
|
2010-10-19 21:58:52 +04:00
|
|
|
PRInt64 objectDataId = autoIncrement ? mKey.IntValue() : LL_MININT;
|
|
|
|
rv = IDBObjectStore::UpdateIndexes(mTransaction, osid, mKey,
|
|
|
|
autoIncrement, mOverwrite,
|
2010-06-28 22:51:06 +04:00
|
|
|
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
|
|
|
AddHelper::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("Unknown key type!");
|
|
|
|
}
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AddHelper::ModifyValueForNewKey()
|
|
|
|
{
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ASSERTION(mObjectStore->IsAutoIncrement() &&
|
|
|
|
!mObjectStore->KeyPath().IsEmpty() &&
|
|
|
|
mKey.IsInt(),
|
2010-06-23 23:46:08 +04:00
|
|
|
"Don't call me!");
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
const nsString& keyPath = mObjectStore->KeyPath();
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
JSContext* cx;
|
|
|
|
nsresult rv = nsContentUtils::ThreadJSContextStack()->GetSafeJSContext(&cx);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
js::AutoValueRooter clone(cx);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIJSON> json(new nsJSON());
|
2010-07-15 10:19:36 +04:00
|
|
|
rv = json->DecodeToJSVal(mValue, cx, clone.jsval_addr());
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
JSObject* obj = JSVAL_TO_OBJECT(clone.jsval_value());
|
2010-06-23 23:46:08 +04:00
|
|
|
JSBool ok;
|
|
|
|
js::AutoValueRooter key(cx);
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
const jschar* keyPathChars = reinterpret_cast<const jschar*>(keyPath.get());
|
|
|
|
const size_t keyPathLen = keyPath.Length();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2010-07-15 10:19:36 +04:00
|
|
|
ok = JS_GetUCProperty(cx, obj, keyPathChars, keyPathLen, key.jsval_addr());
|
|
|
|
NS_ASSERTION(ok && JSVAL_IS_VOID(key.jsval_value()), "Already has a key prop!");
|
2010-06-23 23:46:08 +04:00
|
|
|
#endif
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
ok = JS_NewNumberValue(cx, mKey.IntValue(), key.jsval_addr());
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
ok = JS_DefineUCProperty(cx, obj, keyPathChars, keyPathLen, key.jsval_value(),
|
2010-06-23 23:46:08 +04:00
|
|
|
nsnull, nsnull, JSPROP_ENUMERATE);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
rv = json->EncodeFromJSVal(clone.jsval_addr(), cx, mValue);
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-06-23 23:46:08 +04:00
|
|
|
GetHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
2010-10-19 21:58:52 +04:00
|
|
|
mTransaction->GetStatement(mObjectStore->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("osid"),
|
|
|
|
mObjectStore->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_ASSERTION(!mKey.IsUnset() && !mKey.IsNull(), "Must have a key here!");
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(id, "id");
|
|
|
|
|
|
|
|
if (mKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(id, mKey.IntValue());
|
|
|
|
}
|
|
|
|
else if (mKey.IsString()) {
|
|
|
|
rv = stmt->BindStringByName(id, 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!
|
|
|
|
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-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the value based on results.
|
|
|
|
(void)stmt->GetString(0, mValue);
|
|
|
|
|
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
|
|
|
GetHelper::OnSuccess(nsIDOMEventTarget* aTarget)
|
|
|
|
{
|
|
|
|
nsRefPtr<GetSuccessEvent> event(new GetSuccessEvent(mValue));
|
|
|
|
nsresult rv = event->Init(mRequest, mTransaction);
|
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 dummy;
|
|
|
|
aTarget->DispatchEvent(static_cast<nsDOMEvent*>(event), &dummy);
|
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(mObjectStore->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("osid"),
|
|
|
|
mObjectStore->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_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::OnSuccess(nsIDOMEventTarget* aTarget)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
return AsyncConnectionHelper::OnSuccess(aTarget);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-08-27 00:57:30 +04:00
|
|
|
ClearHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsCString table;
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
2010-08-27 00:57:30 +04:00
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("DELETE FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :osid");
|
|
|
|
|
|
|
|
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-08-27 00:57:30 +04:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"),
|
|
|
|
mObjectStore->Id());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-27 00:57:30 +04:00
|
|
|
|
|
|
|
rv = stmt->Execute();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-27 00:57:30 +04:00
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-08-27 00:57:30 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-06-23 23:46:08 +04:00
|
|
|
OpenCursorHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsCString table;
|
|
|
|
nsCString keyColumn;
|
|
|
|
|
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
keyColumn.AssignLiteral("id");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
keyColumn.AssignLiteral("key_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(osid, "osid");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(leftKeyName, "left_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(rightKeyName, "right_key");
|
|
|
|
|
|
|
|
nsCAutoString keyRangeClause;
|
2010-11-11 02:26:00 +03:00
|
|
|
if (!mLowerKey.IsUnset()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyRangeClause = NS_LITERAL_CSTRING(" AND ") + keyColumn;
|
2010-11-11 02:26:00 +03:00
|
|
|
if (mLowerOpen) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyRangeClause.AppendLiteral(" > :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" >= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(leftKeyName);
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:26:00 +03:00
|
|
|
if (!mUpperKey.IsUnset()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyRangeClause += NS_LITERAL_CSTRING(" AND ") + keyColumn;
|
2010-11-11 02:26:00 +03:00
|
|
|
if (mUpperOpen) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyRangeClause.AppendLiteral(" < :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" <= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(rightKeyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString directionClause;
|
|
|
|
switch (mDirection) {
|
|
|
|
case nsIIDBCursor::NEXT:
|
|
|
|
case nsIIDBCursor::NEXT_NO_DUPLICATE:
|
|
|
|
directionClause = NS_LITERAL_CSTRING(" DESC");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV:
|
|
|
|
case nsIIDBCursor::PREV_NO_DUPLICATE:
|
|
|
|
directionClause = NS_LITERAL_CSTRING(" ASC");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown direction type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(", data FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :") + osid +
|
|
|
|
keyRangeClause + NS_LITERAL_CSTRING(" ORDER BY ") +
|
|
|
|
keyColumn + directionClause;
|
|
|
|
|
|
|
|
if (!mData.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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindInt64ByName(osid, mObjectStore->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()) {
|
|
|
|
rv = stmt->BindStringByName(leftKeyName, mLowerKey.StringValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
else if (mLowerKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(leftKeyName, 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()) {
|
|
|
|
rv = stmt->BindStringByName(rightKeyName, mUpperKey.StringValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
else if (mUpperKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(rightKeyName, 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
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("Copying all results for cursor snapshot, do something smarter!");
|
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
while (NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
|
|
|
|
if (mData.Capacity() == mData.Length()) {
|
|
|
|
if (!mData.SetCapacity(mData.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyValuePair* pair = mData.AppendElement();
|
|
|
|
NS_ASSERTION(pair, "Shouldn't fail if SetCapacity succeeded!");
|
|
|
|
|
|
|
|
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) {
|
|
|
|
pair->key = stmt->AsInt64(0);
|
|
|
|
}
|
|
|
|
else if (keyType == mozIStorageStatement::VALUE_TYPE_TEXT) {
|
|
|
|
rv = stmt->GetString(0, pair->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!");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
PRInt32 valueType;
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(stmt->GetTypeOfIndex(1, &valueType)) &&
|
|
|
|
valueType == mozIStorageStatement::VALUE_TYPE_TEXT,
|
|
|
|
"Bad value type!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rv = stmt->GetString(1, pair->value);
|
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
|
|
|
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
|
|
|
OpenCursorHelper::GetSuccessResult(nsIWritableVariant* aResult)
|
|
|
|
{
|
|
|
|
if (mData.IsEmpty()) {
|
|
|
|
aResult->SetAsEmpty();
|
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 =
|
|
|
|
IDBCursor::Create(mRequest, mTransaction, mObjectStore, mDirection, mData);
|
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
|
|
|
|
2010-11-11 02:26:03 +03:00
|
|
|
aResult->SetAsISupports(cursor);
|
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
|
|
|
CreateIndexHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
// Insert the data into the database.
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(
|
2010-10-19 21:58:52 +04:00
|
|
|
"INSERT INTO object_store_index (id, name, key_path, unique_index, "
|
2010-06-23 23:46:08 +04:00
|
|
|
"object_store_id, object_store_autoincrement) "
|
2010-10-19 21:58:52 +04:00
|
|
|
"VALUES (:id, :name, :key_path, :unique, :osid, :os_auto_increment)"
|
2010-06-23 23:46:08 +04:00
|
|
|
);
|
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("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-10-19 21:58:52 +04:00
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("name"), mIndex->Name());
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("key_path"),
|
|
|
|
mIndex->KeyPath());
|
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->BindInt32ByName(NS_LITERAL_CSTRING("unique"),
|
2010-10-19 21:58:52 +04:00
|
|
|
mIndex->IsUnique() ? 1 : 0);
|
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-19 21:58:52 +04:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"),
|
|
|
|
mIndex->ObjectStore()->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
|
|
|
|
|
|
|
rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("os_auto_increment"),
|
2010-10-19 21:58:52 +04:00
|
|
|
mIndex->IsAutoIncrement() ? 1 : 0);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Get the id of this object store, and store it for future use.
|
|
|
|
(void)aConnection->GetLastInsertRowID(&mId);
|
|
|
|
|
|
|
|
// Now we need to populate the index with data from the object store.
|
2010-11-11 02:25:44 +03:00
|
|
|
rv = InsertDataFromObjectStore(aConnection);
|
|
|
|
NS_ENSURE_TRUE(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
|
2010-06-23 23:46:08 +04:00
|
|
|
CreateIndexHelper::InsertDataFromObjectStore(mozIStorageConnection* aConnection)
|
|
|
|
{
|
2010-10-19 21:58:52 +04:00
|
|
|
bool autoIncrement = mIndex->IsAutoIncrement();
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsCAutoString table;
|
|
|
|
nsCAutoString columns;
|
2010-10-19 21:58:52 +04:00
|
|
|
if (autoIncrement) {
|
2010-06-23 23:46:08 +04:00
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
columns.AssignLiteral("id, data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
columns.AssignLiteral("id, data, key_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT ") + columns +
|
|
|
|
NS_LITERAL_CSTRING(" FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :osid");
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"),
|
2010-10-19 21:58:52 +04:00
|
|
|
mIndex->ObjectStore()->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
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
while (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
|
|
|
|
nsCOMPtr<mozIStorageStatement> insertStmt =
|
2010-10-19 21:58:52 +04:00
|
|
|
mTransaction->IndexUpdateStatement(autoIncrement, mIndex->IsUnique(),
|
|
|
|
false);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(insertStmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper2(insertStmt);
|
|
|
|
|
|
|
|
rv = insertStmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"), mId);
|
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 = insertStmt->BindInt64ByName(NS_LITERAL_CSTRING("object_data_id"),
|
|
|
|
stmt->AsInt64(0));
|
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-19 21:58:52 +04:00
|
|
|
if (!autoIncrement) {
|
2010-06-23 23:46:08 +04:00
|
|
|
// XXX does this cause problems with the affinity?
|
|
|
|
nsString key;
|
|
|
|
rv = stmt->GetString(2, 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
|
|
|
|
|
|
|
rv = insertStmt->BindStringByName(NS_LITERAL_CSTRING("object_data_key"),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
nsString json;
|
|
|
|
rv = stmt->GetString(1, json);
|
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
|
|
|
|
|
|
|
Key key;
|
|
|
|
JSContext* cx = nsnull;
|
2010-10-19 21:58:52 +04:00
|
|
|
rv = IDBObjectStore::GetKeyPathValueFromJSON(json, mIndex->KeyPath(), &cx,
|
|
|
|
key);
|
2010-06-23 23:46:08 +04:00
|
|
|
// XXX this should be a constraint error maybe?
|
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 (key.IsUnset()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.IsInt()) {
|
|
|
|
rv = insertStmt->BindInt64ByName(value, key.IntValue());
|
|
|
|
}
|
|
|
|
else if (key.IsString()) {
|
|
|
|
rv = insertStmt->BindStringByName(value, key.StringValue());
|
|
|
|
}
|
|
|
|
else {
|
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
|
|
|
|
|
|
|
rv = insertStmt->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-10-19 21:58:52 +04:00
|
|
|
CreateIndexHelper::OnSuccess(nsIDOMEventTarget* aTarget)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ASSERTION(!aTarget, "Huh?!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void
|
|
|
|
CreateIndexHelper::OnError(nsIDOMEventTarget* aTarget,
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult aErrorCode)
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aTarget, "Huh?!");
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-11-11 02:25:57 +03:00
|
|
|
DeleteIndexHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(
|
|
|
|
"DELETE FROM object_store_index "
|
|
|
|
"WHERE name = :name "
|
|
|
|
);
|
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->BindStringByName(NS_LITERAL_CSTRING("name"), mName);
|
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_NOT_FOUND_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
|
|
|
DeleteIndexHelper::OnSuccess(nsIDOMEventTarget* aTarget)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_ASSERTION(!aTarget, "Huh?!");
|
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-10-19 21:58:52 +04:00
|
|
|
void
|
2010-11-11 02:25:57 +03:00
|
|
|
DeleteIndexHelper::OnError(nsIDOMEventTarget* aTarget,
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult aErrorCode)
|
2010-10-19 21:58:52 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("Removing an index should never fail here!");
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2010-06-23 23:46:08 +04:00
|
|
|
GetAllHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsCString table;
|
|
|
|
nsCString keyColumn;
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
keyColumn.AssignLiteral("id");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
keyColumn.AssignLiteral("key_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(osid, "osid");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(leftKeyName, "left_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(rightKeyName, "right_key");
|
|
|
|
|
|
|
|
nsCAutoString keyRangeClause;
|
2010-11-11 02:26:00 +03:00
|
|
|
if (!mLowerKey.IsUnset()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyRangeClause = NS_LITERAL_CSTRING(" AND ") + keyColumn;
|
2010-11-11 02:26:00 +03:00
|
|
|
if (mLowerOpen) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyRangeClause.AppendLiteral(" > :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" >= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(leftKeyName);
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:26:00 +03:00
|
|
|
if (!mUpperKey.IsUnset()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyRangeClause += NS_LITERAL_CSTRING(" AND ") + keyColumn;
|
2010-11-11 02:26:00 +03:00
|
|
|
if (mUpperOpen) {
|
2010-06-23 23:46:08 +04:00
|
|
|
keyRangeClause.AppendLiteral(" < :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" <= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(rightKeyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString limitClause;
|
|
|
|
if (mLimit != PR_UINT32_MAX) {
|
|
|
|
limitClause.AssignLiteral(" LIMIT ");
|
|
|
|
limitClause.AppendInt(mLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT data FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :") + osid +
|
|
|
|
keyRangeClause + NS_LITERAL_CSTRING(" ORDER BY ") +
|
|
|
|
keyColumn + NS_LITERAL_CSTRING(" ASC") + limitClause;
|
|
|
|
|
|
|
|
if (!mValues.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
|
|
|
}
|
|
|
|
|
|
|
|
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(osid, mObjectStore->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()) {
|
|
|
|
rv = stmt->BindStringByName(leftKeyName, mLowerKey.StringValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
else if (mLowerKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(leftKeyName, 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()) {
|
|
|
|
rv = stmt->BindStringByName(rightKeyName, mUpperKey.StringValue());
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
else if (mUpperKey.IsInt()) {
|
|
|
|
rv = stmt->BindInt64ByName(rightKeyName, 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;
|
|
|
|
while (NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
|
|
|
|
if (mValues.Capacity() == mValues.Length()) {
|
|
|
|
if (!mValues.SetCapacity(mValues.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString* value = mValues.AppendElement();
|
|
|
|
NS_ASSERTION(value, "Shouldn't fail if SetCapacity succeeded!");
|
|
|
|
|
|
|
|
rv = stmt->GetString(0, *value);
|
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
|
|
|
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
|
|
|
GetAllHelper::OnSuccess(nsIDOMEventTarget* aTarget)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mValues.Length() <= mLimit, "Too many results!");
|
|
|
|
|
|
|
|
nsRefPtr<GetAllSuccessEvent> event(new GetAllSuccessEvent(mValues));
|
|
|
|
|
|
|
|
NS_ASSERTION(mValues.IsEmpty(), "Should have swapped!");
|
|
|
|
|
|
|
|
nsresult rv = event->Init(mRequest, mTransaction);
|
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 dummy;
|
|
|
|
aTarget->DispatchEvent(static_cast<nsDOMEvent*>(event), &dummy);
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|