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"
|
|
|
|
|
2012-01-15 12:13:08 +04:00
|
|
|
#include "jsfriendapi.h"
|
2011-12-16 11:34:24 +04:00
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
2010-06-29 02:22:41 +04:00
|
|
|
#include "mozilla/storage.h"
|
2011-11-21 18:56:24 +04:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2010-06-29 02:22:41 +04:00
|
|
|
#include "nsContentUtils.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsDOMClassInfo.h"
|
2011-12-16 11:34:24 +04:00
|
|
|
#include "nsDOMFile.h"
|
|
|
|
#include "nsDOMLists.h"
|
2010-11-11 02:26:03 +03:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2011-12-08 03:24:20 +04:00
|
|
|
#include "snappy/snappy.h"
|
2011-12-16 11:34:24 +04:00
|
|
|
#include "test_quota.h"
|
2011-12-24 12:27:04 +04:00
|
|
|
#include "xpcpublic.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
#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"
|
2011-12-27 22:01:28 +04:00
|
|
|
#include "DictionaryHelpers.h"
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
#define FILE_COPY_BUFFER_SIZE 32768
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
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,
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneWriteInfo& aCloneWriteInfo,
|
2010-06-23 23:46:08 +04:00
|
|
|
const Key& aKey,
|
|
|
|
bool aOverwrite,
|
2011-12-16 11:34:24 +04:00
|
|
|
nsTArray<IndexUpdateInfo>& aIndexUpdateInfo)
|
2010-10-19 21:58:52 +04:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
2011-12-16 11:34:24 +04:00
|
|
|
mKey(aKey), mOverwrite(aOverwrite)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-12-16 11:34:24 +04:00
|
|
|
mCloneWriteInfo.Swap(aCloneWriteInfo);
|
2010-06-23 23:46:08 +04:00
|
|
|
mIndexUpdateInfo.SwapElements(aIndexUpdateInfo);
|
|
|
|
}
|
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
~AddHelper()
|
|
|
|
{
|
2011-12-16 11:34:24 +04:00
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneWriteInfo.mCloneBuffer);
|
2011-01-27 04:53:02 +03:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
2011-12-16 11:34:24 +04:00
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneWriteInfo.mCloneBuffer);
|
2010-10-19 21:58:52 +04:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
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.
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneWriteInfo mCloneWriteInfo;
|
2010-06-23 23:46:08 +04:00
|
|
|
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,
|
2011-11-03 19:59:17 +04:00
|
|
|
IDBKeyRange* aKeyRange)
|
2010-10-19 21:58:52 +04:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
2011-11-03 19:59:17 +04:00
|
|
|
mKeyRange(aKeyRange)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
~GetHelper()
|
|
|
|
{
|
2011-12-16 11:34:24 +04:00
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneReadInfo.mCloneBuffer);
|
2011-01-27 04:53:02 +03:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
2011-11-03 19:59:17 +04:00
|
|
|
mKeyRange = nsnull;
|
2011-12-16 11:34:24 +04:00
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneReadInfo.mCloneBuffer);
|
2010-10-19 21:58:52 +04:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
protected:
|
|
|
|
// In-params.
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2011-11-03 19:59:17 +04:00
|
|
|
nsRefPtr<IDBKeyRange> mKeyRange;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Out-params.
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneReadInfo mCloneReadInfo;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
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,
|
2011-11-03 19:59:17 +04:00
|
|
|
IDBKeyRange* aKeyRange)
|
|
|
|
: GetHelper(aTransaction, aRequest, aObjectStore, aKeyRange)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2010-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,
|
2011-11-03 19:57:30 +04:00
|
|
|
IDBKeyRange* aKeyRange,
|
2010-11-11 02:25:53 +03:00
|
|
|
PRUint16 aDirection)
|
2010-06-23 23:46:08 +04:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
2011-11-03 19:57:30 +04:00
|
|
|
mKeyRange(aKeyRange), mDirection(aDirection)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
~OpenCursorHelper()
|
|
|
|
{
|
2011-12-16 11:34:24 +04:00
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneReadInfo.mCloneBuffer);
|
2011-01-27 04:53:02 +03:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-09-10 08:54:25 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
2011-11-03 19:57:30 +04:00
|
|
|
mKeyRange = nsnull;
|
2011-12-16 11:34:24 +04:00
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneReadInfo.mCloneBuffer);
|
2010-09-10 08:54:25 +04:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
private:
|
|
|
|
// In-params.
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2011-11-03 19:57:30 +04:00
|
|
|
nsRefPtr<IDBKeyRange> mKeyRange;
|
2010-06-23 23:46:08 +04:00
|
|
|
const PRUint16 mDirection;
|
|
|
|
|
|
|
|
// Out-params.
|
2010-12-10 05:15:00 +03:00
|
|
|
Key mKey;
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneReadInfo mCloneReadInfo;
|
2010-12-10 05:15:00 +03:00
|
|
|
nsCString mContinueQuery;
|
|
|
|
nsCString mContinueToQuery;
|
|
|
|
Key mRangeKey;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class CreateIndexHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2011-07-26 02:41:50 +04:00
|
|
|
CreateIndexHelper(IDBTransaction* aTransaction, IDBIndex* aIndex);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
|
|
|
|
nsresult OnSuccess()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnError()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTransaction->IsAborted(), "How else can this fail?!");
|
|
|
|
}
|
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
|
|
|
|
2011-07-26 02:41:50 +04:00
|
|
|
static void DestroyTLSEntry(void* aPtr);
|
|
|
|
|
|
|
|
static PRUintn sTLSIndex;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
PRUintn CreateIndexHelper::sTLSIndex = PRUintn(BAD_TLS_INDEX);
|
2011-07-26 02:41:50 +04:00
|
|
|
|
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);
|
2011-01-07 09:21:36 +03:00
|
|
|
|
|
|
|
nsresult OnSuccess()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnError()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTransaction->IsAborted(), "How else can this fail?!");
|
|
|
|
}
|
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,
|
2011-11-03 19:57:30 +04:00
|
|
|
IDBKeyRange* aKeyRange,
|
2010-10-19 21:58:52 +04:00
|
|
|
const PRUint32 aLimit)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
2011-11-03 19:57:30 +04:00
|
|
|
mKeyRange(aKeyRange), mLimit(aLimit)
|
2010-06-23 23:46:08 +04:00
|
|
|
{ }
|
|
|
|
|
2011-01-27 04:53:02 +03:00
|
|
|
~GetAllHelper()
|
|
|
|
{
|
2011-12-16 11:34:24 +04:00
|
|
|
for (PRUint32 index = 0; index < mCloneReadInfos.Length(); index++) {
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(
|
|
|
|
mCloneReadInfos[index].mCloneBuffer);
|
2011-01-27 04:53:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-07 09:21:36 +03:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
2011-11-03 19:57:30 +04:00
|
|
|
mKeyRange = nsnull;
|
2011-12-16 11:34:24 +04:00
|
|
|
for (PRUint32 index = 0; index < mCloneReadInfos.Length(); index++) {
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(
|
|
|
|
mCloneReadInfos[index].mCloneBuffer);
|
2010-12-21 19:02:04 +03:00
|
|
|
}
|
2010-10-19 21:58:52 +04:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
protected:
|
|
|
|
// In-params.
|
2010-10-19 21:58:52 +04:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2011-11-03 19:57:30 +04:00
|
|
|
nsRefPtr<IDBKeyRange> mKeyRange;
|
2010-06-23 23:46:08 +04:00
|
|
|
const PRUint32 mLimit;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Out-params.
|
2011-12-16 11:34:24 +04:00
|
|
|
nsTArray<StructuredCloneReadInfo> mCloneReadInfos;
|
2010-06-23 23:46:08 +04:00
|
|
|
};
|
|
|
|
|
2011-11-03 19:57:42 +04:00
|
|
|
class CountHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CountHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBRequest* aRequest,
|
|
|
|
IDBObjectStore* aObjectStore,
|
|
|
|
IDBKeyRange* aKeyRange)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
|
|
|
mKeyRange(aKeyRange), mCount(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
|
|
|
|
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
mKeyRange = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
|
|
|
nsRefPtr<IDBKeyRange> mKeyRange;
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRUint64 mCount;
|
|
|
|
};
|
|
|
|
|
2010-10-19 21:58:52 +04:00
|
|
|
NS_STACK_CLASS
|
|
|
|
class AutoRemoveIndex
|
|
|
|
{
|
|
|
|
public:
|
2011-12-17 04:40:47 +04:00
|
|
|
AutoRemoveIndex(ObjectStoreInfo* aObjectStoreInfo,
|
2010-10-19 21:58:52 +04:00
|
|
|
const nsAString& aIndexName)
|
2011-12-17 04:40:47 +04:00
|
|
|
: mObjectStoreInfo(aObjectStoreInfo), mIndexName(aIndexName)
|
2010-10-19 21:58:52 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
~AutoRemoveIndex()
|
|
|
|
{
|
2011-12-17 04:40:47 +04:00
|
|
|
if (mObjectStoreInfo) {
|
|
|
|
for (PRUint32 i = 0; i < mObjectStoreInfo->indexes.Length(); i++) {
|
|
|
|
if (mObjectStoreInfo->indexes[i].name == mIndexName) {
|
|
|
|
mObjectStoreInfo->indexes.RemoveElementAt(i);
|
|
|
|
break;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void forget()
|
|
|
|
{
|
2011-12-17 04:40:47 +04:00
|
|
|
mObjectStoreInfo = nsnull;
|
2010-10-19 21:58:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-12-17 04:40:47 +04:00
|
|
|
ObjectStoreInfo* mObjectStoreInfo;
|
2010-10-19 21:58:52 +04:00
|
|
|
nsString mIndexName;
|
|
|
|
};
|
|
|
|
|
2011-11-21 18:56:24 +04:00
|
|
|
inline
|
|
|
|
bool
|
|
|
|
IgnoreWhitespace(PRUnichar c)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef nsCharSeparatedTokenizerTemplate<IgnoreWhitespace> KeyPathTokenizer;
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
inline
|
|
|
|
nsresult
|
2011-12-04 21:39:01 +04:00
|
|
|
GetJSValFromKeyPath(JSContext* aCx,
|
|
|
|
jsval aVal,
|
|
|
|
const nsAString& aKeyPath,
|
|
|
|
jsval& aKey)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-11-21 18:56:24 +04:00
|
|
|
NS_ASSERTION(aCx, "Null pointer!");
|
2011-11-23 18:15:15 +04:00
|
|
|
// aVal can be primitive iff the key path is empty.
|
2011-11-21 18:56:24 +04:00
|
|
|
NS_ASSERTION(IDBObjectStore::IsValidKeyPath(aCx, aKeyPath),
|
|
|
|
"This will explode!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-21 18:56:24 +04:00
|
|
|
KeyPathTokenizer tokenizer(aKeyPath, '.');
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-21 18:56:24 +04:00
|
|
|
jsval intermediate = aVal;
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
2011-12-04 21:39:01 +04:00
|
|
|
const nsDependentSubstring& token = tokenizer.nextToken();
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
NS_ASSERTION(!token.IsEmpty(), "Should be a valid keypath");
|
2011-11-21 18:56:24 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
const jschar* keyPathChars = token.BeginReading();
|
2011-11-21 18:56:24 +04:00
|
|
|
const size_t keyPathLen = token.Length();
|
|
|
|
|
|
|
|
if (JSVAL_IS_PRIMITIVE(intermediate)) {
|
2011-12-04 21:39:01 +04:00
|
|
|
intermediate = JSVAL_VOID;
|
|
|
|
break;
|
2011-11-21 18:56:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool ok = JS_GetUCProperty(aCx, JSVAL_TO_OBJECT(intermediate),
|
|
|
|
keyPathChars, keyPathLen, &intermediate);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
}
|
2011-12-04 21:39:01 +04:00
|
|
|
|
|
|
|
aKey = intermediate;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
nsresult
|
|
|
|
GetKeyFromValue(JSContext* aCx,
|
|
|
|
jsval aVal,
|
|
|
|
const nsAString& aKeyPath,
|
|
|
|
Key& aKey)
|
|
|
|
{
|
|
|
|
jsval key;
|
|
|
|
nsresult rv = GetJSValFromKeyPath(aCx, aVal, aKeyPath, key);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-21 18:56:24 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
if (NS_FAILED(aKey.SetFromJSVal(aCx, key))) {
|
2011-11-21 18:56:24 +04:00
|
|
|
aKey.Unset();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-12-20 14:59:07 +04:00
|
|
|
inline
|
|
|
|
nsresult
|
|
|
|
GetKeyFromValue(JSContext* aCx,
|
|
|
|
jsval aVal,
|
|
|
|
const nsTArray<nsString>& aKeyPathArray,
|
|
|
|
Key& aKey)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!aKeyPathArray.IsEmpty(),
|
|
|
|
"Should not use empty keyPath array");
|
|
|
|
for (PRUint32 i = 0; i < aKeyPathArray.Length(); ++i) {
|
|
|
|
jsval key;
|
|
|
|
nsresult rv = GetJSValFromKeyPath(aCx, aVal, aKeyPathArray[i], key);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (NS_FAILED(aKey.AppendArrayItem(aCx, i == 0, key))) {
|
|
|
|
NS_ASSERTION(aKey.IsUnset(), "Encoding error should unset");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aKey.FinishArray();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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();
|
2012-01-24 14:03:37 +04:00
|
|
|
return IDBRequest::Create(aObjectStore, database,
|
|
|
|
aObjectStore->Transaction());
|
2010-09-10 02:15:38 +04:00
|
|
|
}
|
|
|
|
|
2011-06-29 23:09:20 +04:00
|
|
|
JSClass gDummyPropClass = {
|
|
|
|
"dummy", 0,
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_PropertyStub, JS_StrictPropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub,
|
|
|
|
JS_ConvertStub, JS_FinalizeStub,
|
|
|
|
JSCLASS_NO_OPTIONAL_MEMBERS
|
|
|
|
};
|
|
|
|
|
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,
|
2011-12-17 04:40:47 +04:00
|
|
|
ObjectStoreInfo* aStoreInfo,
|
|
|
|
nsIAtom* aDatabaseId)
|
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
|
|
|
|
|
|
|
objectStore->mTransaction = aTransaction;
|
|
|
|
objectStore->mName = aStoreInfo->name;
|
|
|
|
objectStore->mId = aStoreInfo->id;
|
|
|
|
objectStore->mKeyPath = aStoreInfo->keyPath;
|
2011-12-20 14:59:07 +04:00
|
|
|
objectStore->mKeyPathArray = aStoreInfo->keyPathArray;
|
2011-12-17 04:40:47 +04:00
|
|
|
objectStore->mAutoIncrement = !!aStoreInfo->nextAutoIncrementId;
|
|
|
|
objectStore->mDatabaseId = aDatabaseId;
|
|
|
|
objectStore->mInfo = aStoreInfo;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
return objectStore.forget();
|
|
|
|
}
|
|
|
|
|
2011-11-21 18:56:24 +04:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IDBObjectStore::IsValidKeyPath(JSContext* aCx,
|
|
|
|
const nsAString& aKeyPath)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!aKeyPath.IsVoid(), "What?");
|
|
|
|
|
|
|
|
KeyPathTokenizer tokenizer(aKeyPath, '.');
|
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
nsString token(tokenizer.nextToken());
|
|
|
|
|
|
|
|
if (!token.Length()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval stringVal;
|
2011-12-24 12:27:04 +04:00
|
|
|
if (!xpc::StringToJsval(aCx, token, &stringVal)) {
|
2011-11-21 18:56:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(JSVAL_IS_STRING(stringVal), "This should never happen");
|
|
|
|
JSString* str = JSVAL_TO_STRING(stringVal);
|
|
|
|
|
|
|
|
JSBool isIdentifier = JS_FALSE;
|
|
|
|
if (!JS_IsIdentifier(aCx, str, &isIdentifier) || !isIdentifier) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the very last character was a '.', the tokenizer won't give us an empty
|
|
|
|
// token, but the keyPath is still invalid.
|
|
|
|
if (!aKeyPath.IsEmpty() &&
|
|
|
|
aKeyPath.CharAt(aKeyPath.Length() - 1) == '.') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
2011-12-04 21:39:01 +04:00
|
|
|
IDBObjectStore::AppendIndexUpdateInfo(PRInt64 aIndexID,
|
|
|
|
const nsAString& aKeyPath,
|
2011-12-20 14:59:07 +04:00
|
|
|
const nsTArray<nsString>& aKeyPathArray,
|
2011-12-04 21:39:01 +04:00
|
|
|
bool aUnique,
|
|
|
|
bool aMultiEntry,
|
|
|
|
JSContext* aCx,
|
2011-12-20 14:59:07 +04:00
|
|
|
jsval aVal,
|
2011-12-04 21:39:01 +04:00
|
|
|
nsTArray<IndexUpdateInfo>& aUpdateInfoArray)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-12-20 14:59:07 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (!aKeyPathArray.IsEmpty()) {
|
|
|
|
Key arrayKey;
|
|
|
|
rv = GetKeyFromValue(aCx, aVal, aKeyPathArray, arrayKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!arrayKey.IsUnset()) {
|
|
|
|
IndexUpdateInfo* updateInfo = aUpdateInfoArray.AppendElement();
|
|
|
|
updateInfo->indexId = aIndexID;
|
|
|
|
updateInfo->indexUnique = aUnique;
|
|
|
|
updateInfo->value = arrayKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
jsval key;
|
2011-12-20 14:59:07 +04:00
|
|
|
rv = GetJSValFromKeyPath(aCx, aVal, aKeyPath, key);
|
2011-11-21 18:56:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
if (aMultiEntry && !JSVAL_IS_PRIMITIVE(key) &&
|
|
|
|
JS_IsArrayObject(aCx, JSVAL_TO_OBJECT(key))) {
|
|
|
|
JSObject* array = JSVAL_TO_OBJECT(key);
|
|
|
|
jsuint arrayLength;
|
|
|
|
if (!JS_GetArrayLength(aCx, array, &arrayLength)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
for (jsuint arrayIndex = 0; arrayIndex < arrayLength; arrayIndex++) {
|
|
|
|
jsval arrayItem;
|
|
|
|
if (!JS_GetElement(aCx, array, arrayIndex, &arrayItem)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2011-12-03 07:46:25 +04:00
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
Key value;
|
2011-12-04 21:39:01 +04:00
|
|
|
if (NS_FAILED(value.SetFromJSVal(aCx, arrayItem)) ||
|
|
|
|
value.IsUnset()) {
|
2010-06-23 23:46:08 +04:00
|
|
|
// Not a value we can do anything with, ignore it.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexUpdateInfo* updateInfo = aUpdateInfoArray.AppendElement();
|
2011-12-04 21:39:01 +04:00
|
|
|
updateInfo->indexId = aIndexID;
|
|
|
|
updateInfo->indexUnique = aUnique;
|
2010-06-23 23:46:08 +04:00
|
|
|
updateInfo->value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-12-04 21:39:01 +04:00
|
|
|
Key value;
|
|
|
|
if (NS_FAILED(value.SetFromJSVal(aCx, key)) ||
|
|
|
|
value.IsUnset()) {
|
|
|
|
// Not a value we can do anything with, ignore it.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexUpdateInfo* updateInfo = aUpdateInfoArray.AppendElement();
|
|
|
|
updateInfo->indexId = aIndexID;
|
|
|
|
updateInfo->indexUnique = aUnique;
|
|
|
|
updateInfo->value = value;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
// static
|
2010-06-23 23:46:08 +04:00
|
|
|
nsresult
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::UpdateIndexes(IDBTransaction* aTransaction,
|
|
|
|
PRInt64 aObjectStoreId,
|
|
|
|
const Key& aObjectStoreKey,
|
|
|
|
bool aOverwrite,
|
|
|
|
PRInt64 aObjectDataId,
|
|
|
|
const nsTArray<IndexUpdateInfo>& aUpdateInfoArray)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_ASSERTION(aObjectDataId != LL_MININT, "Bad objectData id!");
|
|
|
|
|
2011-11-22 08:18:19 +04:00
|
|
|
NS_NAMED_LITERAL_CSTRING(objectDataId, "object_data_id");
|
|
|
|
|
|
|
|
if (aOverwrite) {
|
2011-12-17 04:40:47 +04:00
|
|
|
stmt = aTransaction->GetCachedStatement(
|
|
|
|
"DELETE FROM unique_index_data "
|
|
|
|
"WHERE object_data_id = :object_data_id; "
|
|
|
|
"DELETE FROM index_data "
|
|
|
|
"WHERE object_data_id = :object_data_id");
|
2011-12-17 01:09:42 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
2011-11-22 08:18:19 +04:00
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(objectDataId, aObjectDataId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
PRUint32 infoCount = aUpdateInfoArray.Length();
|
|
|
|
for (PRUint32 i = 0; i < infoCount; i++) {
|
|
|
|
const IndexUpdateInfo& updateInfo = aUpdateInfoArray[i];
|
2011-11-22 08:18:19 +04:00
|
|
|
|
|
|
|
// Insert new values.
|
2011-12-17 04:40:47 +04:00
|
|
|
|
|
|
|
stmt = updateInfo.indexUnique ?
|
|
|
|
aTransaction->GetCachedStatement(
|
|
|
|
"INSERT INTO unique_index_data "
|
|
|
|
"(index_id, object_data_id, object_data_key, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :object_data_key, :value)") :
|
|
|
|
aTransaction->GetCachedStatement(
|
|
|
|
"INSERT OR IGNORE INTO index_data ("
|
|
|
|
"index_id, object_data_id, object_data_key, value) "
|
|
|
|
"VALUES (:index_id, :object_data_id, :object_data_key, :value)");
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_FAILURE);
|
|
|
|
|
2011-11-22 08:18:19 +04:00
|
|
|
mozStorageStatementScoper scoper4(stmt);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"),
|
|
|
|
updateInfo.indexId);
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-22 08:18:19 +04:00
|
|
|
rv = stmt->BindInt64ByName(objectDataId, aObjectDataId);
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
rv = aObjectStoreKey.BindToStatement(stmt,
|
|
|
|
NS_LITERAL_CSTRING("object_data_key"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
rv = updateInfo.value.BindToStatement(stmt, NS_LITERAL_CSTRING("value"));
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
2011-12-04 21:39:01 +04:00
|
|
|
if (rv == NS_ERROR_STORAGE_CONSTRAINT && updateInfo.indexUnique) {
|
|
|
|
// If we're inserting multiple entries for the same unique index, then
|
|
|
|
// we might have failed to insert due to colliding with another entry for
|
|
|
|
// the same index in which case we should ignore it.
|
|
|
|
|
|
|
|
for (PRInt32 j = (PRInt32)i - 1;
|
|
|
|
j >= 0 && aUpdateInfoArray[j].indexId == updateInfo.indexId;
|
|
|
|
--j) {
|
|
|
|
if (updateInfo.value == aUpdateInfoArray[j].value) {
|
|
|
|
// We found a key with the same value for the same index. So we
|
|
|
|
// must have had a collision with a value we just inserted.
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-21 19:02:04 +03:00
|
|
|
// static
|
|
|
|
nsresult
|
2011-12-16 11:34:24 +04:00
|
|
|
IDBObjectStore::GetStructuredCloneReadInfoFromStatement(
|
2010-12-21 19:02:04 +03:00
|
|
|
mozIStorageStatement* aStatement,
|
2011-12-16 11:34:24 +04:00
|
|
|
PRUint32 aDataIndex,
|
|
|
|
PRUint32 aFileIdsIndex,
|
|
|
|
FileManager* aFileManager,
|
|
|
|
StructuredCloneReadInfo& aInfo)
|
2010-12-21 19:02:04 +03:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2011-12-16 11:34:24 +04:00
|
|
|
PRInt32 type;
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(aStatement->GetTypeOfIndex(aDataIndex, &type)) &&
|
|
|
|
type == mozIStorageStatement::VALUE_TYPE_BLOB,
|
2010-12-21 19:02:04 +03:00
|
|
|
"Bad value type!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-08 03:24:20 +04:00
|
|
|
const PRUint8* blobData;
|
|
|
|
PRUint32 blobDataLength;
|
2011-12-16 11:34:24 +04:00
|
|
|
nsresult rv = aStatement->GetSharedBlob(aDataIndex, &blobDataLength,
|
|
|
|
&blobData);
|
2010-12-21 19:02:04 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-12-08 03:24:20 +04:00
|
|
|
const char* compressed = reinterpret_cast<const char*>(blobData);
|
|
|
|
size_t compressedLength = size_t(blobDataLength);
|
|
|
|
|
|
|
|
size_t uncompressedLength;
|
|
|
|
if (!snappy::GetUncompressedLength(compressed, compressedLength,
|
|
|
|
&uncompressedLength)) {
|
|
|
|
NS_WARNING("Snappy can't determine uncompressed length!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoArrayPtr<char> uncompressed(new char[uncompressedLength]);
|
|
|
|
|
|
|
|
if (!snappy::RawUncompress(compressed, compressedLength,
|
|
|
|
uncompressed.get())) {
|
|
|
|
NS_WARNING("Snappy can't determine uncompressed length!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
JSAutoStructuredCloneBuffer& buffer = aInfo.mCloneBuffer;
|
|
|
|
if (!buffer.copy(reinterpret_cast<const uint64_t *>(uncompressed.get()),
|
|
|
|
uncompressedLength)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNull;
|
|
|
|
rv = aStatement->GetIsNull(aFileIdsIndex, &isNull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
if (isNull) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString ids;
|
|
|
|
rv = aStatement->GetString(aFileIdsIndex, ids);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsAutoTArray<PRInt64, 10> array;
|
|
|
|
rv = ConvertFileIdsToArray(ids, array);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < array.Length(); i++) {
|
|
|
|
const PRInt64& id = array.ElementAt(i);
|
|
|
|
|
|
|
|
nsRefPtr<FileInfo> fileInfo = aFileManager->GetFileInfo(id);
|
|
|
|
NS_ASSERTION(fileInfo, "Null file info!");
|
|
|
|
|
|
|
|
aInfo.mFileInfos.AppendElement(fileInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-12-21 19:02:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(JSAutoStructuredCloneBuffer& aBuffer)
|
|
|
|
{
|
|
|
|
if (aBuffer.data()) {
|
2011-07-12 21:42:24 +04:00
|
|
|
aBuffer.clear();
|
2010-12-21 19:02:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-09 06:21:53 +04:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IDBObjectStore::DeserializeValue(JSContext* aCx,
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneReadInfo& aCloneReadInfo,
|
|
|
|
jsval* aValue)
|
2011-06-09 06:21:53 +04:00
|
|
|
{
|
2011-06-24 18:21:21 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
|
|
|
"Should only be deserializing on the main thread!");
|
2011-06-09 06:21:53 +04:00
|
|
|
NS_ASSERTION(aCx, "A JSContext is required!");
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
JSAutoStructuredCloneBuffer& buffer = aCloneReadInfo.mCloneBuffer;
|
|
|
|
|
|
|
|
if (!buffer.data()) {
|
2011-06-09 06:21:53 +04:00
|
|
|
*aValue = JSVAL_VOID;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoRequest ar(aCx);
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
JSStructuredCloneCallbacks callbacks = {
|
|
|
|
IDBObjectStore::StructuredCloneReadCallback,
|
|
|
|
nsnull,
|
|
|
|
nsnull
|
|
|
|
};
|
|
|
|
|
|
|
|
return buffer.read(aCx, aValue, &callbacks, &aCloneReadInfo);
|
2011-06-09 06:21:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IDBObjectStore::SerializeValue(JSContext* aCx,
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneWriteInfo& aCloneWriteInfo,
|
|
|
|
jsval aValue)
|
2011-06-09 06:21:53 +04:00
|
|
|
{
|
2011-06-24 18:21:21 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
|
|
|
"Should only be serializing on the main thread!");
|
2011-06-09 06:21:53 +04:00
|
|
|
NS_ASSERTION(aCx, "A JSContext is required!");
|
|
|
|
|
|
|
|
JSAutoRequest ar(aCx);
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
JSStructuredCloneCallbacks callbacks = {
|
|
|
|
nsnull,
|
|
|
|
StructuredCloneWriteCallback,
|
|
|
|
nsnull
|
|
|
|
};
|
|
|
|
|
|
|
|
JSAutoStructuredCloneBuffer& buffer = aCloneWriteInfo.mCloneBuffer;
|
|
|
|
|
|
|
|
return buffer.write(aCx, aValue, &callbacks, &aCloneWriteInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline PRUint32
|
|
|
|
SwapBytes(PRUint32 u)
|
|
|
|
{
|
|
|
|
#ifdef IS_BIG_ENDIAN
|
|
|
|
return ((u & 0x000000ffU) << 24) |
|
|
|
|
((u & 0x0000ff00U) << 8) |
|
|
|
|
((u & 0x00ff0000U) >> 8) |
|
|
|
|
((u & 0xff000000U) >> 24);
|
|
|
|
#else
|
|
|
|
return u;
|
|
|
|
#endif
|
2011-06-09 06:21:53 +04:00
|
|
|
}
|
|
|
|
|
2011-06-24 18:21:21 +04:00
|
|
|
static inline jsdouble
|
|
|
|
SwapBytes(PRUint64 u)
|
|
|
|
{
|
|
|
|
#ifdef IS_BIG_ENDIAN
|
2011-12-16 11:34:24 +04:00
|
|
|
return ((u & 0x00000000000000ffLLU) << 56) |
|
|
|
|
((u & 0x000000000000ff00LLU) << 40) |
|
|
|
|
((u & 0x0000000000ff0000LLU) << 24) |
|
|
|
|
((u & 0x00000000ff000000LLU) << 8) |
|
|
|
|
((u & 0x000000ff00000000LLU) >> 8) |
|
|
|
|
((u & 0x0000ff0000000000LLU) >> 24) |
|
|
|
|
((u & 0x00ff000000000000LLU) >> 40) |
|
|
|
|
((u & 0xff00000000000000LLU) >> 56);
|
2011-06-24 18:21:21 +04:00
|
|
|
#else
|
2011-12-16 11:34:24 +04:00
|
|
|
return jsdouble(u);
|
2011-06-24 18:21:21 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
static inline bool
|
|
|
|
StructuredCloneReadString(JSStructuredCloneReader* aReader,
|
|
|
|
nsCString& aString)
|
|
|
|
{
|
|
|
|
PRUint32 length;
|
|
|
|
if (!JS_ReadBytes(aReader, &length, sizeof(PRUint32))) {
|
|
|
|
NS_WARNING("Failed to read length!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
length = SwapBytes(length);
|
|
|
|
|
|
|
|
if (!EnsureStringLength(aString, length)) {
|
|
|
|
NS_WARNING("Out of memory?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
char* buffer = aString.BeginWriting();
|
|
|
|
|
|
|
|
if (!JS_ReadBytes(aReader, buffer, length)) {
|
|
|
|
NS_WARNING("Failed to read type!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
IDBObjectStore::StructuredCloneReadCallback(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
2011-12-16 18:42:09 +04:00
|
|
|
uint32_t aTag,
|
|
|
|
uint32_t aData,
|
2011-12-16 11:34:24 +04:00
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
if (aTag == SCTAG_DOM_BLOB || aTag == SCTAG_DOM_FILE) {
|
|
|
|
StructuredCloneReadInfo* cloneReadInfo =
|
|
|
|
reinterpret_cast<StructuredCloneReadInfo*>(aClosure);
|
|
|
|
|
|
|
|
NS_ASSERTION(aData < cloneReadInfo->mFileInfos.Length(),
|
|
|
|
"Bad blob index!");
|
|
|
|
|
|
|
|
nsRefPtr<FileInfo> fileInfo = cloneReadInfo->mFileInfos[aData];
|
|
|
|
nsRefPtr<FileManager> fileManager = fileInfo->Manager();
|
|
|
|
nsCOMPtr<nsIFile> directory = fileManager->GetDirectory();
|
|
|
|
if (!directory) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> nativeFile =
|
|
|
|
fileManager->GetFileForId(directory, fileInfo->Id());
|
|
|
|
if (!nativeFile) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint64 size;
|
|
|
|
if (!JS_ReadBytes(aReader, &size, sizeof(PRUint64))) {
|
|
|
|
NS_WARNING("Failed to read size!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
size = SwapBytes(size);
|
|
|
|
|
|
|
|
nsCString type;
|
|
|
|
if (!StructuredCloneReadString(aReader, type)) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
NS_ConvertUTF8toUTF16 convType(type);
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_BLOB) {
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob = new nsDOMFileFile(convType, size,
|
|
|
|
nativeFile, fileInfo);
|
|
|
|
|
|
|
|
jsval wrappedBlob;
|
|
|
|
nsresult rv =
|
|
|
|
nsContentUtils::WrapNative(aCx, JS_GetGlobalForScopeChain(aCx), blob,
|
|
|
|
&NS_GET_IID(nsIDOMBlob), &wrappedBlob);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to wrap native!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSVAL_TO_OBJECT(wrappedBlob);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString name;
|
|
|
|
if (!StructuredCloneReadString(aReader, name)) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
NS_ConvertUTF8toUTF16 convName(name);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMFile> file = new nsDOMFileFile(convName, convType, size,
|
|
|
|
nativeFile, fileInfo);
|
|
|
|
|
|
|
|
jsval wrappedFile;
|
|
|
|
nsresult rv =
|
|
|
|
nsContentUtils::WrapNative(aCx, JS_GetGlobalForScopeChain(aCx), file,
|
|
|
|
&NS_GET_IID(nsIDOMFile), &wrappedFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to wrap native!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSVAL_TO_OBJECT(wrappedFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
const JSStructuredCloneCallbacks* runtimeCallbacks =
|
2012-01-15 12:13:08 +04:00
|
|
|
js::GetContextStructuredCloneCallbacks(aCx);
|
2011-12-16 11:34:24 +04:00
|
|
|
|
|
|
|
if (runtimeCallbacks) {
|
|
|
|
return runtimeCallbacks->read(aCx, aReader, aTag, aData, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
IDBObjectStore::StructuredCloneWriteCallback(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JSObject* aObj,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
StructuredCloneWriteInfo* cloneWriteInfo =
|
|
|
|
reinterpret_cast<StructuredCloneWriteInfo*>(aClosure);
|
|
|
|
|
2012-02-04 04:54:57 +04:00
|
|
|
if (JS_GetClass(aObj) == &gDummyPropClass) {
|
2011-12-16 11:34:24 +04:00
|
|
|
NS_ASSERTION(cloneWriteInfo->mOffsetToKeyProp == 0,
|
|
|
|
"We should not have been here before!");
|
|
|
|
cloneWriteInfo->mOffsetToKeyProp = js_GetSCOffset(aWriter);
|
|
|
|
|
|
|
|
PRUint64 value = 0;
|
|
|
|
return JS_WriteBytes(aWriter, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrappedNative;
|
|
|
|
nsContentUtils::XPConnect()->
|
|
|
|
GetWrappedNativeOfJSObject(aCx, aObj, getter_AddRefs(wrappedNative));
|
|
|
|
|
|
|
|
if (wrappedNative) {
|
|
|
|
nsISupports* supports = wrappedNative->Native();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob = do_QueryInterface(supports);
|
|
|
|
if (blob) {
|
|
|
|
nsCOMPtr<nsIDOMFile> file = do_QueryInterface(blob);
|
|
|
|
|
|
|
|
PRUint64 size;
|
|
|
|
if (NS_FAILED(blob->GetSize(&size))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size = SwapBytes(size);
|
|
|
|
|
|
|
|
nsString type;
|
|
|
|
if (NS_FAILED(blob->GetType(type))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
NS_ConvertUTF16toUTF8 convType(type);
|
|
|
|
PRUint32 convTypeLength = SwapBytes(convType.Length());
|
|
|
|
|
|
|
|
if (!JS_WriteUint32Pair(aWriter, file ? SCTAG_DOM_FILE : SCTAG_DOM_BLOB,
|
|
|
|
cloneWriteInfo->mBlobs.Length()) ||
|
|
|
|
!JS_WriteBytes(aWriter, &size, sizeof(PRUint64)) ||
|
|
|
|
!JS_WriteBytes(aWriter, &convTypeLength, sizeof(PRUint32)) ||
|
|
|
|
!JS_WriteBytes(aWriter, convType.get(), convType.Length())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
nsString name;
|
|
|
|
if (NS_FAILED(file->GetName(name))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
NS_ConvertUTF16toUTF8 convName(name);
|
|
|
|
PRUint32 convNameLength = SwapBytes(convName.Length());
|
|
|
|
|
|
|
|
if (!JS_WriteBytes(aWriter, &convNameLength, sizeof(PRUint32)) ||
|
|
|
|
!JS_WriteBytes(aWriter, convName.get(), convName.Length())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cloneWriteInfo->mBlobs.AppendElement(blob);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try using the runtime callbacks
|
|
|
|
const JSStructuredCloneCallbacks* runtimeCallbacks =
|
2012-01-15 12:13:08 +04:00
|
|
|
js::GetContextStructuredCloneCallbacks(aCx);
|
2011-12-16 11:34:24 +04:00
|
|
|
if (runtimeCallbacks) {
|
|
|
|
return runtimeCallbacks->write(aCx, aWriter, aObj, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-24 18:21:21 +04:00
|
|
|
nsresult
|
2011-12-16 11:34:24 +04:00
|
|
|
IDBObjectStore::ConvertFileIdsToArray(const nsAString& aFileIds,
|
|
|
|
nsTArray<PRInt64>& aResult)
|
|
|
|
{
|
|
|
|
nsCharSeparatedTokenizerTemplate<IgnoreWhitespace> tokenizer(aFileIds, ' ');
|
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
nsString token(tokenizer.nextToken());
|
|
|
|
|
|
|
|
NS_ASSERTION(!token.IsEmpty(), "Should be a valid id!");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
PRInt32 id = token.ToInteger(&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRInt64* element = aResult.AppendElement();
|
|
|
|
*element = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
IDBObjectStore::IDBObjectStore()
|
2010-06-23 23:46:08 +04:00
|
|
|
: mId(LL_MININT),
|
2011-10-17 18:59:28 +04:00
|
|
|
mAutoIncrement(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,
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneWriteInfo& aCloneWriteInfo,
|
2010-06-28 22:51:06 +04:00
|
|
|
Key& aKey,
|
2011-12-16 11:34:24 +04:00
|
|
|
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-12-16 00:21:11 +03:00
|
|
|
// Return DATA_ERR if a key was passed in and this objectStore uses inline
|
|
|
|
// keys.
|
2011-12-04 21:39:01 +04:00
|
|
|
if (!JSVAL_IS_VOID(aKeyVal) && HasKeyPath()) {
|
2010-12-16 00:21:11 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
|
2010-10-11 02:35:42 +04:00
|
|
|
JSAutoRequest ar(aCx);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
if (!HasKeyPath()) {
|
2010-12-16 00:21:11 +03:00
|
|
|
// Out-of-line keys must be passed in.
|
2011-11-03 19:57:30 +04:00
|
|
|
rv = aKey.SetFromJSVal(aCx, aKeyVal);
|
2010-12-16 00:21:07 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2011-12-04 21:39:02 +04:00
|
|
|
else if (!mAutoIncrement) {
|
2010-12-16 00:21:11 +03:00
|
|
|
// Inline keys live on the object. Make sure that the value passed in is an
|
|
|
|
// object.
|
2011-12-20 14:59:07 +04:00
|
|
|
if (UsesKeyPathArray()) {
|
|
|
|
rv = GetKeyFromValue(aCx, aValue, mKeyPathArray, aKey);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = GetKeyFromValue(aCx, aValue, mKeyPath, aKey);
|
|
|
|
}
|
2010-12-16 00:21:07 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-16 00:21:07 +03:00
|
|
|
// Return DATA_ERR if no key was specified this isn't an autoIncrement
|
|
|
|
// objectStore.
|
2010-06-23 23:46:08 +04:00
|
|
|
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.
|
2011-12-17 04:40:47 +04:00
|
|
|
PRUint32 count = mInfo->indexes.Length();
|
2011-12-04 21:39:01 +04:00
|
|
|
aUpdateInfoArray.SetCapacity(count); // Pretty good estimate
|
|
|
|
for (PRUint32 indexesIndex = 0; indexesIndex < count; indexesIndex++) {
|
2011-12-17 04:40:47 +04:00
|
|
|
const IndexInfo& indexInfo = mInfo->indexes[indexesIndex];
|
2011-12-04 21:39:01 +04:00
|
|
|
|
|
|
|
rv = AppendIndexUpdateInfo(indexInfo.id, indexInfo.keyPath,
|
2011-12-20 14:59:07 +04:00
|
|
|
indexInfo.keyPathArray, indexInfo.unique,
|
|
|
|
indexInfo.multiEntry, aCx, aValue,
|
|
|
|
aUpdateInfoArray);
|
2011-12-04 21:39:01 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-04 21:39:02 +04:00
|
|
|
nsString targetObjectPropName;
|
|
|
|
JSObject* targetObject = nsnull;
|
|
|
|
|
|
|
|
rv = NS_OK;
|
|
|
|
if (mAutoIncrement && HasKeyPath()) {
|
|
|
|
NS_ASSERTION(aKey.IsUnset(), "Shouldn't have gotten the key yet!");
|
2011-12-03 06:32:46 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
if (JSVAL_IS_PRIMITIVE(aValue)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
2011-06-24 18:21:21 +04:00
|
|
|
|
2011-12-04 21:39:02 +04:00
|
|
|
KeyPathTokenizer tokenizer(mKeyPath, '.');
|
|
|
|
NS_ASSERTION(tokenizer.hasMoreTokens(),
|
|
|
|
"Shouldn't have empty keypath and autoincrement");
|
2011-06-24 18:21:21 +04:00
|
|
|
|
2011-12-04 21:39:02 +04:00
|
|
|
JSObject* obj = JSVAL_TO_OBJECT(aValue);
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
const nsDependentSubstring& token = tokenizer.nextToken();
|
|
|
|
|
|
|
|
NS_ASSERTION(!token.IsEmpty(), "Should be a valid keypath");
|
|
|
|
|
|
|
|
const jschar* keyPathChars = token.BeginReading();
|
|
|
|
const size_t keyPathLen = token.Length();
|
|
|
|
|
|
|
|
JSBool hasProp;
|
|
|
|
if (!targetObject) {
|
|
|
|
// We're still walking the chain of existing objects
|
|
|
|
|
|
|
|
JSBool ok = JS_HasUCProperty(aCx, obj, keyPathChars, keyPathLen,
|
|
|
|
&hasProp);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
if (hasProp) {
|
|
|
|
// Get if the property exists...
|
|
|
|
jsval intermediate;
|
|
|
|
JSBool ok = JS_GetUCProperty(aCx, obj, keyPathChars, keyPathLen,
|
|
|
|
&intermediate);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
if (tokenizer.hasMoreTokens()) {
|
|
|
|
// ...and walk to it if there are more steps...
|
|
|
|
if (JSVAL_IS_PRIMITIVE(intermediate)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
obj = JSVAL_TO_OBJECT(intermediate);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// ...otherwise use it as key
|
|
|
|
aKey.SetFromJSVal(aCx, intermediate);
|
|
|
|
if (aKey.IsUnset()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If the property doesn't exist, fall into below path of starting
|
|
|
|
// to define properties
|
|
|
|
targetObject = obj;
|
|
|
|
targetObjectPropName = token;
|
|
|
|
}
|
|
|
|
}
|
2011-06-24 18:21:21 +04:00
|
|
|
|
2011-12-04 21:39:02 +04:00
|
|
|
if (targetObject) {
|
|
|
|
// We have started inserting new objects or are about to just insert
|
|
|
|
// the first one.
|
|
|
|
if (tokenizer.hasMoreTokens()) {
|
|
|
|
// If we're not at the end, we need to add a dummy object to the chain.
|
|
|
|
JSObject* dummy = JS_NewObject(aCx, nsnull, nsnull, nsnull);
|
|
|
|
if (!dummy) {
|
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineUCProperty(aCx, obj, token.BeginReading(),
|
|
|
|
token.Length(),
|
|
|
|
OBJECT_TO_JSVAL(dummy), nsnull, nsnull,
|
|
|
|
JSPROP_ENUMERATE)) {
|
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = dummy;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JSObject* dummy = JS_NewObject(aCx, &gDummyPropClass, nsnull, nsnull);
|
|
|
|
if (!dummy) {
|
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineUCProperty(aCx, obj, token.BeginReading(),
|
|
|
|
token.Length(), OBJECT_TO_JSVAL(dummy),
|
|
|
|
nsnull, nsnull, JSPROP_ENUMERATE)) {
|
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-24 18:21:21 +04:00
|
|
|
}
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
aCloneWriteInfo.mOffsetToKeyProp = 0;
|
2011-06-29 23:09:20 +04:00
|
|
|
|
2011-06-24 18:21:21 +04:00
|
|
|
// We guard on rv being a success because we need to run the property
|
|
|
|
// deletion code below even if we should not be serializing the value
|
|
|
|
if (NS_SUCCEEDED(rv) &&
|
2011-12-16 11:34:24 +04:00
|
|
|
!IDBObjectStore::SerializeValue(aCx, aCloneWriteInfo, aValue)) {
|
2011-06-24 18:21:21 +04:00
|
|
|
rv = NS_ERROR_DOM_DATA_CLONE_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-04 21:39:02 +04:00
|
|
|
if (targetObject) {
|
2011-06-24 18:21:21 +04:00
|
|
|
// If this fails, we lose, and the web page sees a magical property
|
|
|
|
// appear on the object :-(
|
|
|
|
jsval succeeded;
|
2011-12-04 21:39:02 +04:00
|
|
|
if (!JS_DeleteUCProperty2(aCx, targetObject,
|
|
|
|
targetObjectPropName.get(),
|
|
|
|
targetObjectPropName.Length(), &succeeded)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2011-06-24 18:21:21 +04:00
|
|
|
NS_ASSERTION(JSVAL_IS_BOOLEAN(succeeded), "Wtf?");
|
|
|
|
NS_ENSURE_TRUE(JSVAL_TO_BOOLEAN(succeeded),
|
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-12-16 00:21:07 +03:00
|
|
|
nsresult
|
|
|
|
IDBObjectStore::AddOrPut(const jsval& aValue,
|
|
|
|
const jsval& aKey,
|
|
|
|
JSContext* aCx,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval,
|
|
|
|
bool aOverwrite)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_READ_ONLY_ERR;
|
2010-12-16 00:21:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
jsval keyval = (aOptionalArgCount >= 1) ? aKey : JSVAL_VOID;
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneWriteInfo cloneWriteInfo;
|
2010-12-16 00:21:07 +03:00
|
|
|
Key key;
|
|
|
|
nsTArray<IndexUpdateInfo> updateInfo;
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
nsresult rv = GetAddInfo(aCx, aValue, keyval, cloneWriteInfo, key,
|
|
|
|
updateInfo);
|
2010-12-16 00:21:07 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsRefPtr<AddHelper> helper =
|
2011-12-16 11:34:24 +04:00
|
|
|
new AddHelper(mTransaction, request, this, cloneWriteInfo, key, aOverwrite,
|
|
|
|
updateInfo);
|
2010-12-16 00:21:07 +03:00
|
|
|
|
|
|
|
rv = helper->DispatchToTransactionPool();
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
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,
|
2011-06-24 06:18:00 +04:00
|
|
|
nsIDOMEventTarget)
|
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: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
|
2011-12-20 14:59:07 +04:00
|
|
|
IDBObjectStore::GetKeyPath(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-12-20 14:59:07 +04:00
|
|
|
if (UsesKeyPathArray()) {
|
|
|
|
JSObject* array = JS_NewArrayObject(aCx, mKeyPathArray.Length(), nsnull);
|
|
|
|
if (!array) {
|
|
|
|
NS_WARNING("Failed to make array!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < mKeyPathArray.Length(); ++i) {
|
|
|
|
jsval val;
|
|
|
|
nsString tmp(mKeyPathArray[i]);
|
2011-12-24 12:27:04 +04:00
|
|
|
if (!xpc::StringToJsval(aCx, tmp, &val)) {
|
2011-12-20 14:59:07 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_SetElement(aCx, array, i, &val)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aVal = OBJECT_TO_JSVAL(array);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsString tmp(mKeyPath);
|
2011-12-24 12:27:04 +04:00
|
|
|
if (!xpc::StringToJsval(aCx, tmp, aVal)) {
|
2011-12-20 14:59:07 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-10 05:14:09 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBObjectStore::GetTransaction(nsIIDBTransaction** aTransaction)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIDBTransaction> transaction(mTransaction);
|
|
|
|
transaction.forget(aTransaction);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:29:15 +04:00
|
|
|
NS_IMETHODIMP
|
2011-12-17 04:40:47 +04:00
|
|
|
IDBObjectStore::GetAutoIncrement(bool* aAutoIncrement)
|
2011-12-17 00:29:15 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
*aAutoIncrement = mAutoIncrement;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBObjectStore::GetIndexNames(nsIDOMDOMStringList** aIndexNames)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2011-12-17 01:09:42 +04:00
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
nsRefPtr<nsDOMStringList> list(new nsDOMStringList());
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
PRUint32 count = mInfo->indexes.Length();
|
2010-06-23 23:46:08 +04:00
|
|
|
for (PRUint32 index = 0; index < count; index++) {
|
2011-12-17 04:40:47 +04:00
|
|
|
NS_ENSURE_TRUE(list->Add(mInfo->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
|
2011-11-03 19:57:30 +04:00
|
|
|
IDBObjectStore::Get(const jsval& aKey,
|
|
|
|
JSContext* aCx,
|
2010-06-28 22:51:06 +04:00
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
nsresult rv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
if (!keyRange) {
|
|
|
|
// Must specify a key or keyRange for get().
|
2010-12-16 00:21:07 +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
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
nsRefPtr<GetHelper> helper =
|
|
|
|
new GetHelper(mTransaction, request, this, keyRange);
|
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
|
2011-11-03 19:57:30 +04:00
|
|
|
IDBObjectStore::GetAll(const jsval& aKey,
|
2010-06-28 22:51:06 +04:00
|
|
|
PRUint32 aLimit,
|
2011-11-03 19:57:30 +04:00
|
|
|
JSContext* aCx,
|
2010-06-28 22:51:06 +04:00
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2010-11-11 02:26:00 +03:00
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
if (aOptionalArgCount) {
|
|
|
|
rv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2011-11-03 05:57:48 +04:00
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
if (aOptionalArgCount < 2) {
|
|
|
|
aLimit = PR_UINT32_MAX;
|
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 =
|
2011-11-03 19:57:30 +04:00
|
|
|
new GetAllHelper(mTransaction, request, this, keyRange, aLimit);
|
2010-11-11 02:25:44 +03:00
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
rv = helper->DispatchToTransactionPool();
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-12-16 00:21:07 +03: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-12-16 00:21:07 +03:00
|
|
|
return AddOrPut(aValue, aKey, aCx, aOptionalArgCount, _retval, false);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-12-16 00:21:07 +03:00
|
|
|
IDBObjectStore::Put(const jsval& aValue,
|
|
|
|
const jsval& aKey,
|
2010-08-27 00:57:28 +04:00
|
|
|
JSContext* aCx,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-16 00:21:07 +03:00
|
|
|
return AddOrPut(aValue, aKey, aCx, aOptionalArgCount, _retval, true);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-12-16 00:21:11 +03:00
|
|
|
IDBObjectStore::Delete(const jsval& aKey,
|
|
|
|
JSContext* aCx,
|
2010-06-28 22:51:06 +04:00
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_READ_ONLY_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
nsresult rv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
if (!keyRange) {
|
|
|
|
// Must specify a key or keyRange for delete().
|
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 =
|
2011-11-03 19:59:17 +04:00
|
|
|
new DeleteHelper(mTransaction, request, this, keyRange);
|
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-12-16 00:21:17 +03:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_READ_ONLY_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
|
2011-11-03 19:57:30 +04:00
|
|
|
IDBObjectStore::OpenCursor(const jsval& aKey,
|
2010-06-28 22:51:06 +04:00
|
|
|
PRUint16 aDirection,
|
2011-11-03 19:57:30 +04:00
|
|
|
JSContext* aCx,
|
2010-06-28 22:51:06 +04:00
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
if (aOptionalArgCount) {
|
|
|
|
rv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-03 05:57:48 +04:00
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
if (aOptionalArgCount >= 2) {
|
|
|
|
if (aDirection != nsIIDBCursor::NEXT &&
|
|
|
|
aDirection != nsIIDBCursor::NEXT_NO_DUPLICATE &&
|
|
|
|
aDirection != nsIIDBCursor::PREV &&
|
|
|
|
aDirection != nsIIDBCursor::PREV_NO_DUPLICATE) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NON_TRANSIENT_ERR;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2011-11-03 19:57:30 +04:00
|
|
|
else {
|
|
|
|
aDirection = nsIIDBCursor::NEXT;
|
2011-11-03 05:57:48 +04:00
|
|
|
}
|
|
|
|
}
|
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<OpenCursorHelper> helper =
|
2011-11-03 19:57:30 +04:00
|
|
|
new OpenCursorHelper(mTransaction, request, this, keyRange, 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,
|
2011-12-20 14:59:07 +04:00
|
|
|
const jsval& aKeyPath,
|
2010-12-21 19:02:01 +03:00
|
|
|
const jsval& aOptions,
|
|
|
|
JSContext* aCx,
|
2010-10-19 21:58:52 +04:00
|
|
|
nsIIDBIndex** _retval)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-12-20 14:59:07 +04:00
|
|
|
// Get KeyPath
|
|
|
|
nsString keyPath;
|
|
|
|
nsTArray<nsString> keyPathArray;
|
|
|
|
|
|
|
|
// See if this is a JS array.
|
|
|
|
if (!JSVAL_IS_PRIMITIVE(aKeyPath) &&
|
|
|
|
JS_IsArrayObject(aCx, JSVAL_TO_OBJECT(aKeyPath))) {
|
|
|
|
|
|
|
|
JSObject* obj = JSVAL_TO_OBJECT(aKeyPath);
|
|
|
|
|
|
|
|
jsuint length;
|
|
|
|
if (!JS_GetArrayLength(aCx, obj, &length)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!length) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyPathArray.SetCapacity(length);
|
|
|
|
|
|
|
|
for (jsuint index = 0; index < length; index++) {
|
|
|
|
jsval val;
|
|
|
|
JSString* jsstr;
|
|
|
|
nsDependentJSString str;
|
|
|
|
if (!JS_GetElement(aCx, obj, index, &val) ||
|
|
|
|
!(jsstr = JS_ValueToString(aCx, val)) ||
|
|
|
|
!str.init(aCx, jsstr)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsValidKeyPath(aCx, str)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyPathArray.AppendElement(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!keyPathArray.IsEmpty(), "This shouldn't have happened!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JSString* jsstr;
|
|
|
|
nsDependentJSString str;
|
|
|
|
if (!(jsstr = JS_ValueToString(aCx, aKeyPath)) ||
|
|
|
|
!str.init(aCx, jsstr)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsValidKeyPath(aCx, str)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyPath = str;
|
2011-11-21 18:56:24 +04:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:59:07 +04:00
|
|
|
// Check name and current mode
|
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;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
bool found = false;
|
2011-12-17 04:40:47 +04:00
|
|
|
PRUint32 indexCount = mInfo->indexes.Length();
|
2010-06-23 23:46:08 +04:00
|
|
|
for (PRUint32 index = 0; index < indexCount; index++) {
|
2011-12-17 04:40:47 +04:00
|
|
|
if (mInfo->indexes[index].name == aName) {
|
2010-06-23 23:46:08 +04:00
|
|
|
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-12-16 00:21:17 +03:00
|
|
|
NS_ASSERTION(mTransaction->IsOpen(), "Impossible!");
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2011-12-27 22:01:28 +04:00
|
|
|
mozilla::dom::IDBIndexParameters params;
|
2010-12-21 19:02:01 +03:00
|
|
|
|
|
|
|
// Get optional arguments.
|
|
|
|
if (!JSVAL_IS_VOID(aOptions) && !JSVAL_IS_NULL(aOptions)) {
|
2011-12-27 22:01:28 +04:00
|
|
|
nsresult rv = params.Init(aCx, &aOptions);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-12-21 19:02:01 +03:00
|
|
|
}
|
|
|
|
|
2011-12-27 22:01:28 +04:00
|
|
|
if (params.multiEntry && !keyPathArray.IsEmpty()) {
|
2011-12-20 14:59:07 +04:00
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
DatabaseInfo* databaseInfo = mTransaction->DBInfo();
|
2010-10-19 21:58:52 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
IndexInfo* indexInfo = mInfo->indexes.AppendElement();
|
2010-10-19 21:58:52 +04:00
|
|
|
indexInfo->id = databaseInfo->nextIndexId++;
|
|
|
|
indexInfo->name = aName;
|
2011-12-20 14:59:07 +04:00
|
|
|
indexInfo->keyPath = keyPath;
|
|
|
|
indexInfo->keyPathArray.SwapElements(keyPathArray);
|
2011-12-27 22:01:28 +04:00
|
|
|
indexInfo->unique = params.unique;
|
|
|
|
indexInfo->multiEntry = params.multiEntry;
|
2010-10-19 21:58:52 +04:00
|
|
|
|
|
|
|
// Don't leave this in the list if we fail below!
|
2011-12-17 04:40:47 +04:00
|
|
|
AutoRemoveIndex autoRemove(mInfo, aName);
|
2010-10-19 21:58:52 +04:00
|
|
|
|
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!");
|
|
|
|
|
2010-12-16 00:21:17 +03:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-16 00:21:14 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IndexInfo* indexInfo = nsnull;
|
2011-12-17 04:40:47 +04:00
|
|
|
PRUint32 indexCount = mInfo->indexes.Length();
|
2010-06-23 23:46:08 +04:00
|
|
|
for (PRUint32 index = 0; index < indexCount; index++) {
|
2011-12-17 04:40:47 +04:00
|
|
|
if (mInfo->indexes[index].name == aName) {
|
|
|
|
indexInfo = &(mInfo->indexes[index]);
|
2010-06-23 23:46:08 +04:00
|
|
|
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!");
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-12-16 00:21:17 +03:00
|
|
|
NS_ASSERTION(mTransaction->IsOpen(), "Impossible!");
|
2010-10-19 21:58:52 +04:00
|
|
|
|
|
|
|
PRUint32 index = 0;
|
2011-12-17 04:40:47 +04:00
|
|
|
for (; index < mInfo->indexes.Length(); index++) {
|
|
|
|
if (mInfo->indexes[index].name == aName) {
|
2010-06-23 23:46:08 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
if (index == mInfo->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
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
mInfo->indexes.RemoveElementAt(index);
|
2011-12-04 21:39:01 +04:00
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < mCreatedIndexes.Length(); i++) {
|
|
|
|
if (mCreatedIndexes[i]->Name() == aName) {
|
|
|
|
mCreatedIndexes.RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:46:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
NS_IMETHODIMP
|
2011-11-03 19:57:42 +04:00
|
|
|
IDBObjectStore::Count(const jsval& aKey,
|
2011-11-03 19:57:30 +04:00
|
|
|
JSContext* aCx,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
|
|
|
{
|
|
|
|
if (!mTransaction->IsOpen()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
|
|
|
}
|
|
|
|
|
2011-11-03 19:57:42 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
if (aOptionalArgCount) {
|
|
|
|
rv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsRefPtr<CountHelper> helper =
|
|
|
|
new CountHelper(mTransaction, request, this, keyRange);
|
|
|
|
rv = helper->DispatchToTransactionPool();
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
inline nsresult
|
|
|
|
CopyData(nsIInputStream* aStream, quota_FILE* aFile)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
char copyBuffer[FILE_COPY_BUFFER_SIZE];
|
|
|
|
|
|
|
|
PRUint32 numRead;
|
|
|
|
nsresult rv = aStream->Read(copyBuffer, FILE_COPY_BUFFER_SIZE, &numRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (numRead <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t numWrite = sqlite3_quota_fwrite(copyBuffer, 1, numRead, aFile);
|
|
|
|
NS_ENSURE_TRUE(numWrite == numRead, NS_ERROR_FAILURE);
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
// Flush and sync
|
|
|
|
NS_ENSURE_TRUE(sqlite3_quota_fflush(aFile, 1) == 0,
|
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
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;
|
2011-12-17 04:40:47 +04:00
|
|
|
bool keyUnset = mKey.IsUnset();
|
2010-10-19 21:58:52 +04:00
|
|
|
PRInt64 osid = mObjectStore->Id();
|
|
|
|
const nsString& keyPath = mObjectStore->KeyPath();
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
// The "|| keyUnset" here is mostly a debugging tool. If a key isn't
|
|
|
|
// specified we should never have a collision and so it shouldn't matter
|
|
|
|
// if we allow overwrite or not. By not allowing overwrite we raise
|
|
|
|
// detectable errors rather than corrupting data
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = !mOverwrite || keyUnset ?
|
|
|
|
mTransaction->GetCachedStatement(
|
|
|
|
"INSERT INTO object_data (object_store_id, key_value, data, file_ids) "
|
|
|
|
"VALUES (:osid, :key_value, :data, :file_ids)") :
|
|
|
|
mTransaction->GetCachedStatement(
|
|
|
|
"INSERT OR REPLACE INTO object_data (object_store_id, key_value, data, file_ids) "
|
|
|
|
"VALUES (:osid, :key_value, :data, :file_ids)");
|
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), osid);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
NS_ASSERTION(!keyUnset || mObjectStore->IsAutoIncrement(),
|
|
|
|
"Should have key unless autoincrement");
|
2011-11-03 19:59:17 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
PRInt64 autoIncrementNum = 0;
|
2011-11-03 19:59:17 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
|
|
|
if (keyUnset) {
|
|
|
|
autoIncrementNum = mObjectStore->Info()->nextAutoIncrementId;
|
|
|
|
if (autoIncrementNum > (1LL << 53)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
mKey.SetFromInteger(autoIncrementNum);
|
2011-11-03 19:59:17 +04:00
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
else if (mKey.IsFloat() &&
|
|
|
|
mKey.ToFloat() >= mObjectStore->Info()->nextAutoIncrementId) {
|
|
|
|
autoIncrementNum = floor(mKey.ToFloat());
|
2011-11-03 19:59:17 +04:00
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
if (keyUnset && !keyPath.IsEmpty()) {
|
|
|
|
// 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.
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
// This is a duplicate of the js engine's byte munging here
|
|
|
|
union {
|
|
|
|
jsdouble d;
|
|
|
|
PRUint64 u;
|
|
|
|
} pun;
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
pun.d = SwapBytes(static_cast<PRUint64>(autoIncrementNum));
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
JSAutoStructuredCloneBuffer& buffer = mCloneWriteInfo.mCloneBuffer;
|
|
|
|
PRUint64 offsetToKeyProp = mCloneWriteInfo.mOffsetToKeyProp;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
memcpy((char*)buffer.data() + offsetToKeyProp, &pun.u, sizeof(PRUint64));
|
|
|
|
}
|
|
|
|
}
|
2011-12-17 00:29:15 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
mKey.BindToStatement(stmt, NS_LITERAL_CSTRING("key_value"));
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
// Compress the bytes before adding into the database.
|
|
|
|
const char* uncompressed =
|
|
|
|
reinterpret_cast<const char*>(mCloneWriteInfo.mCloneBuffer.data());
|
|
|
|
size_t uncompressedLength = mCloneWriteInfo.mCloneBuffer.nbytes();
|
2011-12-17 00:29:15 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
size_t compressedLength = snappy::MaxCompressedLength(uncompressedLength);
|
|
|
|
// This will hold our compressed data until the end of the method. The
|
|
|
|
// BindBlobByName function will copy it.
|
|
|
|
nsAutoArrayPtr<char> compressed(new char[compressedLength]);
|
|
|
|
|
|
|
|
snappy::RawCompress(uncompressed, uncompressedLength, compressed.get(),
|
|
|
|
&compressedLength);
|
2011-12-17 00:29:15 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
const PRUint8* dataBuffer = reinterpret_cast<const PRUint8*>(compressed.get());
|
|
|
|
size_t dataBufferLength = compressedLength;
|
|
|
|
|
|
|
|
rv = stmt->BindBlobByName(NS_LITERAL_CSTRING("data"), dataBuffer,
|
|
|
|
dataBufferLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
// Handle blobs
|
2011-12-16 11:34:24 +04:00
|
|
|
nsRefPtr<FileManager> fileManager = mDatabase->Manager();
|
|
|
|
nsCOMPtr<nsIFile> directory = fileManager->GetDirectory();
|
|
|
|
NS_ENSURE_TRUE(directory, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsAutoString fileIds;
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < mCloneWriteInfo.mBlobs.Length(); index++) {
|
|
|
|
nsCOMPtr<nsIDOMBlob>& domBlob = mCloneWriteInfo.mBlobs[index];
|
|
|
|
|
|
|
|
PRInt64 id = -1;
|
|
|
|
|
|
|
|
// Check if it is a blob created from this db or the blob was already
|
|
|
|
// stored in this db
|
|
|
|
nsRefPtr<FileInfo> fileInfo = domBlob->GetFileInfo(fileManager);
|
|
|
|
if (fileInfo) {
|
|
|
|
id = fileInfo->Id();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == -1) {
|
|
|
|
fileInfo = fileManager->GetNewFileInfo();
|
|
|
|
NS_ENSURE_TRUE(fileInfo, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
id = fileInfo->Id();
|
|
|
|
|
|
|
|
mTransaction->OnNewFileInfo(fileInfo);
|
|
|
|
|
|
|
|
// Copy it
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
rv = domBlob->GetInternalStream(getter_AddRefs(inputStream));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> nativeFile = fileManager->GetFileForId(directory, id);
|
|
|
|
NS_ENSURE_TRUE(nativeFile, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsString nativeFilePath;
|
|
|
|
rv = nativeFile->GetPath(nativeFilePath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
quota_FILE* file =
|
|
|
|
sqlite3_quota_fopen(NS_ConvertUTF16toUTF8(nativeFilePath).get(), "wb");
|
|
|
|
NS_ENSURE_TRUE(file, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
rv = CopyData(inputStream, file);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(sqlite3_quota_fclose(file) == 0,
|
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
domBlob->AddFileInfo(fileInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index) {
|
|
|
|
fileIds.Append(NS_LITERAL_STRING(" "));
|
|
|
|
}
|
|
|
|
fileIds.AppendInt(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileIds.IsEmpty()) {
|
|
|
|
rv = stmt->BindNullByName(NS_LITERAL_CSTRING("file_ids"));
|
2011-12-20 15:23:27 +04:00
|
|
|
}
|
|
|
|
else {
|
2011-12-16 11:34:24 +04:00
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("file_ids"), fileIds);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2010-08-05 01:44:26 +04:00
|
|
|
rv = stmt->Execute();
|
2011-12-17 04:40:47 +04:00
|
|
|
if (rv == NS_ERROR_STORAGE_CONSTRAINT) {
|
|
|
|
NS_ASSERTION(!keyUnset, "Generated key had a collision!?");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2011-12-17 04:40:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
PRInt64 objectDataId;
|
|
|
|
rv = aConnection->GetLastInsertRowID(&objectDataId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
// Update our indexes if needed.
|
2011-11-22 08:18:19 +04:00
|
|
|
if (mOverwrite || !mIndexUpdateInfo.IsEmpty()) {
|
2011-12-17 04:40:47 +04:00
|
|
|
rv = IDBObjectStore::UpdateIndexes(mTransaction, osid, mKey, 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
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
if (autoIncrementNum) {
|
|
|
|
mObjectStore->Info()->nextAutoIncrementId = autoIncrementNum + 1;
|
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2011-01-07 09:21:36 +03:00
|
|
|
AddHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-12-16 00:21:07 +03:00
|
|
|
NS_ASSERTION(!mKey.IsUnset(), "Badness!");
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
mCloneWriteInfo.mCloneBuffer.clear();
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
return mKey.ToJSVal(aCx, aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2011-11-03 19:59:17 +04:00
|
|
|
GetHelper::DoDatabaseWork(mozIStorageConnection* /* aConnection */)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-11-03 19:59:17 +04:00
|
|
|
NS_ASSERTION(mKeyRange, "Must have a key range here!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
nsCString keyRangeClause;
|
2011-12-17 04:40:47 +04:00
|
|
|
mKeyRange->GetBindingClause(NS_LITERAL_CSTRING("key_value"), keyRangeClause);
|
2011-11-03 19:59:17 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(!keyRangeClause.IsEmpty(), "Huh?!");
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT data, file_ids FROM object_data "
|
|
|
|
"WHERE object_store_id = :osid") +
|
2011-11-03 19:59:17 +04:00
|
|
|
keyRangeClause + NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-12-17 04:40:47 +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
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
rv = mKeyRange->BindToStatement(stmt);
|
2011-11-03 19:57:30 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasResult;
|
2010-06-23 23:46:08 +04:00
|
|
|
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
|
|
|
|
2010-12-16 00:21:09 +03:00
|
|
|
if (hasResult) {
|
2011-12-16 11:34:24 +04:00
|
|
|
rv = IDBObjectStore::GetStructuredCloneReadInfoFromStatement(stmt, 0, 1,
|
|
|
|
mDatabase->Manager(), mCloneReadInfo);
|
2010-12-21 19:02:04 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2011-01-07 09:21:36 +03:00
|
|
|
GetHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-12-16 11:34:24 +04:00
|
|
|
bool result = IDBObjectStore::DeserializeValue(aCx, mCloneReadInfo, aVal);
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
mCloneReadInfo.mCloneBuffer.clear();
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2011-06-09 06:21:53 +04:00
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
|
2011-01-27 04:53:02 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2011-11-03 19:59:17 +04:00
|
|
|
DeleteHelper::DoDatabaseWork(mozIStorageConnection* /*aConnection */)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-11-03 19:59:17 +04:00
|
|
|
NS_ASSERTION(mKeyRange, "Must have a key range here!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
nsCString keyRangeClause;
|
2011-12-17 04:40:47 +04:00
|
|
|
mKeyRange->GetBindingClause(NS_LITERAL_CSTRING("key_value"), keyRangeClause);
|
2011-11-03 19:59:17 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(!keyRangeClause.IsEmpty(), "Huh?!");
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
nsCString query = NS_LITERAL_CSTRING("DELETE FROM object_data "
|
|
|
|
"WHERE object_store_id = :osid") +
|
2011-11-03 19:59:17 +04:00
|
|
|
keyRangeClause;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-12-17 04:40:47 +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
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
rv = mKeyRange->BindToStatement(stmt);
|
2011-11-03 19:57:30 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
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
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2011-01-07 09:21:36 +03:00
|
|
|
DeleteHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-11-08 06:08:02 +04:00
|
|
|
*aVal = JSVAL_VOID;
|
2011-11-03 19:59:17 +04: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!");
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(
|
|
|
|
NS_LITERAL_CSTRING("DELETE FROM object_data "
|
|
|
|
"WHERE object_store_id = :osid"));
|
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)
|
|
|
|
{
|
2011-12-17 04:40:47 +04:00
|
|
|
NS_NAMED_LITERAL_CSTRING(keyValue, "key_value");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-03 19:59:17 +04:00
|
|
|
nsCString keyRangeClause;
|
2011-11-03 19:57:30 +04:00
|
|
|
if (mKeyRange) {
|
2011-12-17 04:40:47 +04:00
|
|
|
mKeyRange->GetBindingClause(keyValue, keyRangeClause);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
nsCAutoString directionClause;
|
2010-06-23 23:46:08 +04:00
|
|
|
switch (mDirection) {
|
|
|
|
case nsIIDBCursor::NEXT:
|
|
|
|
case nsIIDBCursor::NEXT_NO_DUPLICATE:
|
2011-12-17 04:40:47 +04:00
|
|
|
directionClause.AssignLiteral(" ORDER BY key_value ASC");
|
2010-06-23 23:46:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV:
|
|
|
|
case nsIIDBCursor::PREV_NO_DUPLICATE:
|
2011-12-17 04:40:47 +04:00
|
|
|
directionClause.AssignLiteral(" ORDER BY key_value DESC");
|
2010-06-23 23:46:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown direction type!");
|
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
nsCString firstQuery = NS_LITERAL_CSTRING("SELECT key_value, data, file_ids "
|
|
|
|
"FROM object_data "
|
|
|
|
"WHERE object_store_id = :id") +
|
|
|
|
keyRangeClause + directionClause +
|
2011-11-22 08:18:25 +04:00
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(firstQuery);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("id"),
|
|
|
|
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
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
if (mKeyRange) {
|
2011-11-03 19:59:17 +04:00
|
|
|
rv = mKeyRange->BindToStatement(stmt);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasResult;
|
2010-12-10 05:15:00 +03:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
if (!hasResult) {
|
2011-11-03 19:57:30 +04:00
|
|
|
mKey.Unset();
|
2010-12-10 05:15:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
rv = mKey.SetFromStatement(stmt, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
rv = IDBObjectStore::GetStructuredCloneReadInfoFromStatement(stmt, 1, 2,
|
|
|
|
mDatabase->Manager(), mCloneReadInfo);
|
2010-12-21 19:02:04 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
// Now we need to make the query to get the next match.
|
|
|
|
keyRangeClause.Truncate();
|
|
|
|
nsCAutoString continueToKeyRangeClause;
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(currentKey, "current_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(rangeKey, "range_key");
|
|
|
|
|
|
|
|
switch (mDirection) {
|
|
|
|
case nsIIDBCursor::NEXT:
|
|
|
|
case nsIIDBCursor::NEXT_NO_DUPLICATE:
|
2011-12-17 04:40:47 +04:00
|
|
|
AppendConditionClause(keyValue, currentKey, false, false,
|
2010-12-10 05:15:00 +03:00
|
|
|
keyRangeClause);
|
2011-12-17 04:40:47 +04:00
|
|
|
AppendConditionClause(keyValue, currentKey, false, true,
|
2010-12-10 05:15:00 +03:00
|
|
|
continueToKeyRangeClause);
|
2011-11-03 19:57:30 +04:00
|
|
|
if (mKeyRange && !mKeyRange->Upper().IsUnset()) {
|
2011-12-17 04:40:47 +04:00
|
|
|
AppendConditionClause(keyValue, rangeKey, true,
|
2011-11-03 19:57:30 +04:00
|
|
|
!mKeyRange->IsUpperOpen(), keyRangeClause);
|
2011-12-17 04:40:47 +04:00
|
|
|
AppendConditionClause(keyValue, rangeKey, true,
|
2011-11-03 19:57:30 +04:00
|
|
|
!mKeyRange->IsUpperOpen(),
|
2010-12-10 05:15:00 +03:00
|
|
|
continueToKeyRangeClause);
|
2011-11-03 19:57:30 +04:00
|
|
|
mRangeKey = mKeyRange->Upper();
|
2010-12-10 05:15:00 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV:
|
|
|
|
case nsIIDBCursor::PREV_NO_DUPLICATE:
|
2011-12-17 04:40:47 +04:00
|
|
|
AppendConditionClause(keyValue, currentKey, true, false, keyRangeClause);
|
|
|
|
AppendConditionClause(keyValue, currentKey, true, true,
|
2010-12-10 05:15:00 +03:00
|
|
|
continueToKeyRangeClause);
|
2011-11-03 19:57:30 +04:00
|
|
|
if (mKeyRange && !mKeyRange->Lower().IsUnset()) {
|
2011-12-17 04:40:47 +04:00
|
|
|
AppendConditionClause(keyValue, rangeKey, false,
|
2011-11-03 19:57:30 +04:00
|
|
|
!mKeyRange->IsLowerOpen(), keyRangeClause);
|
2011-12-17 04:40:47 +04:00
|
|
|
AppendConditionClause(keyValue, rangeKey, false,
|
2011-11-03 19:57:30 +04:00
|
|
|
!mKeyRange->IsLowerOpen(),
|
2010-12-10 05:15:00 +03:00
|
|
|
continueToKeyRangeClause);
|
2011-11-03 19:57:30 +04:00
|
|
|
mRangeKey = mKeyRange->Lower();
|
2010-12-10 05:15:00 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown direction type!");
|
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
NS_NAMED_LITERAL_CSTRING(queryStart, "SELECT key_value, data, file_ids "
|
|
|
|
"FROM object_data "
|
|
|
|
"WHERE object_store_id = :id");
|
|
|
|
|
|
|
|
mContinueQuery = queryStart + keyRangeClause + directionClause +
|
2011-11-22 08:18:25 +04:00
|
|
|
NS_LITERAL_CSTRING(" LIMIT ");
|
2010-12-10 05:15:00 +03:00
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
mContinueToQuery = queryStart + continueToKeyRangeClause + directionClause +
|
2011-11-08 03:37:19 +04:00
|
|
|
NS_LITERAL_CSTRING(" LIMIT ");
|
2010-12-10 05:15:00 +03:00
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2011-01-07 09:21:36 +03:00
|
|
|
OpenCursorHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2010-12-10 05:15:00 +03:00
|
|
|
if (mKey.IsUnset()) {
|
2011-01-07 09:21:36 +03:00
|
|
|
*aVal = JSVAL_VOID;
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-06-28 22:51:06 +04:00
|
|
|
nsRefPtr<IDBCursor> cursor =
|
2010-12-10 05:15:00 +03:00
|
|
|
IDBCursor::Create(mRequest, mTransaction, mObjectStore, mDirection,
|
|
|
|
mRangeKey, mContinueQuery, mContinueToQuery, mKey,
|
2011-12-16 11:34:24 +04:00
|
|
|
mCloneReadInfo);
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_TRUE(cursor, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
NS_ASSERTION(!mCloneReadInfo.mCloneBuffer.data(), "Should have swapped!");
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2011-01-07 09:21:36 +03:00
|
|
|
return WrapNative(aCx, cursor, aVal);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2011-07-26 02:41:50 +04:00
|
|
|
class ThreadLocalJSRuntime
|
|
|
|
{
|
|
|
|
JSRuntime* mRuntime;
|
|
|
|
JSContext* mContext;
|
|
|
|
JSObject* mGlobal;
|
|
|
|
|
|
|
|
static JSClass sGlobalClass;
|
2011-09-21 01:47:14 +04:00
|
|
|
static const unsigned sRuntimeHeapSize = 256 * 1024; // should be enough for anyone
|
2011-07-26 02:41:50 +04:00
|
|
|
|
|
|
|
ThreadLocalJSRuntime()
|
|
|
|
: mRuntime(NULL), mContext(NULL), mGlobal(NULL)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ThreadLocalJSRuntime);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Init()
|
|
|
|
{
|
|
|
|
mRuntime = JS_NewRuntime(sRuntimeHeapSize);
|
|
|
|
NS_ENSURE_TRUE(mRuntime, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
mContext = JS_NewContext(mRuntime, 0);
|
|
|
|
NS_ENSURE_TRUE(mContext, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
JSAutoRequest ar(mContext);
|
|
|
|
|
|
|
|
mGlobal = JS_NewCompartmentAndGlobalObject(mContext, &sGlobalClass, NULL);
|
|
|
|
NS_ENSURE_TRUE(mGlobal, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
JS_SetGlobalObject(mContext, mGlobal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static ThreadLocalJSRuntime *Create()
|
|
|
|
{
|
|
|
|
ThreadLocalJSRuntime *entry = new ThreadLocalJSRuntime();
|
|
|
|
NS_ENSURE_TRUE(entry, nsnull);
|
|
|
|
|
|
|
|
if (NS_FAILED(entry->Init())) {
|
|
|
|
delete entry;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext *Context() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ThreadLocalJSRuntime()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ThreadLocalJSRuntime);
|
|
|
|
|
|
|
|
if (mContext) {
|
|
|
|
JS_DestroyContext(mContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mRuntime) {
|
|
|
|
JS_DestroyRuntime(mRuntime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSClass ThreadLocalJSRuntime::sGlobalClass = {
|
|
|
|
"IndexedDBTransactionThreadGlobal",
|
|
|
|
JSCLASS_GLOBAL_FLAGS,
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
|
|
|
|
};
|
|
|
|
|
|
|
|
CreateIndexHelper::CreateIndexHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBIndex* aIndex)
|
|
|
|
: AsyncConnectionHelper(aTransaction, nsnull), mIndex(aIndex)
|
|
|
|
{
|
|
|
|
if (sTLSIndex == BAD_TLS_INDEX) {
|
|
|
|
PR_NewThreadPrivateIndex(&sTLSIndex, DestroyTLSEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CreateIndexHelper::DestroyTLSEntry(void* aPtr)
|
|
|
|
{
|
|
|
|
delete reinterpret_cast<ThreadLocalJSRuntime *>(aPtr);
|
|
|
|
}
|
|
|
|
|
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, "
|
2011-12-17 04:40:47 +04:00
|
|
|
"multientry, object_store_id) "
|
|
|
|
"VALUES (:id, :name, :key_path, :unique, :multientry, :osid)"
|
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
|
|
|
|
2011-12-20 14:59:07 +04:00
|
|
|
if (mIndex->UsesKeyPathArray()) {
|
|
|
|
// We use a comma in the beginning to indicate that it's an array of
|
|
|
|
// key paths. This is to be able to tell a string-keypath from an
|
|
|
|
// array-keypath which contains only one item.
|
|
|
|
// It also makes serializing easier :-)
|
|
|
|
nsAutoString keyPath;
|
|
|
|
const nsTArray<nsString>& keyPaths = mIndex->KeyPathArray();
|
|
|
|
for (PRUint32 i = 0; i < keyPaths.Length(); ++i) {
|
|
|
|
keyPath.Append(NS_LITERAL_STRING(",") + keyPaths[i]);
|
|
|
|
}
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("key_path"),
|
|
|
|
keyPath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("key_path"),
|
|
|
|
mIndex->KeyPath());
|
|
|
|
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
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("multientry"),
|
|
|
|
mIndex->IsMultiEntry() ? 1 : 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-01-23 21:16:00 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
PRInt64 id;
|
|
|
|
aConnection->GetLastInsertRowID(&id);
|
|
|
|
NS_ASSERTION(mIndex->Id() == id, "Bad index id!");
|
|
|
|
}
|
|
|
|
#endif
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
// Now we need to populate the index with data from the object store.
|
2010-11-11 02:25:44 +03:00
|
|
|
rv = InsertDataFromObjectStore(aConnection);
|
2010-12-16 00:21:11 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return 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)
|
|
|
|
{
|
2011-12-17 04:40:47 +04:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(
|
|
|
|
NS_LITERAL_CSTRING("SELECT id, data, file_ids, key_value FROM object_data "
|
|
|
|
"WHERE object_store_id = :osid"));
|
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
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
NS_ENSURE_TRUE(sTLSIndex != BAD_TLS_INDEX, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
bool hasResult;
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
if (!hasResult) {
|
|
|
|
// Bail early if we have no data to avoid creating the below runtime
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-01-23 21:16:00 +03:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
ThreadLocalJSRuntime* tlsEntry =
|
|
|
|
reinterpret_cast<ThreadLocalJSRuntime*>(PR_GetThreadPrivate(sTLSIndex));
|
2011-12-03 07:46:25 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
if (!tlsEntry) {
|
|
|
|
tlsEntry = ThreadLocalJSRuntime::Create();
|
|
|
|
NS_ENSURE_TRUE(tlsEntry, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-11-03 05:57:48 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
PR_SetThreadPrivate(sTLSIndex, tlsEntry);
|
|
|
|
}
|
2011-12-03 07:46:25 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
JSContext* cx = tlsEntry->Context();
|
|
|
|
JSAutoRequest ar(cx);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
do {
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneReadInfo cloneReadInfo;
|
|
|
|
rv = IDBObjectStore::GetStructuredCloneReadInfoFromStatement(stmt, 1, 2,
|
|
|
|
mDatabase->Manager(), cloneReadInfo);
|
2011-12-08 03:24:20 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
JSAutoStructuredCloneBuffer& buffer = cloneReadInfo.mCloneBuffer;
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks callbacks = {
|
|
|
|
IDBObjectStore::StructuredCloneReadCallback,
|
|
|
|
nsnull,
|
|
|
|
nsnull
|
|
|
|
};
|
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
jsval clone;
|
2011-12-16 11:34:24 +04:00
|
|
|
if (!buffer.read(cx, &clone, &callbacks, &cloneReadInfo)) {
|
2011-12-08 03:24:20 +04:00
|
|
|
NS_WARNING("Failed to deserialize structured clone data!");
|
2011-12-04 21:39:01 +04:00
|
|
|
return NS_ERROR_DOM_DATA_CLONE_ERR;
|
2011-12-03 06:32:46 +04:00
|
|
|
}
|
2011-12-03 07:46:25 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
nsTArray<IndexUpdateInfo> updateInfo;
|
|
|
|
rv = IDBObjectStore::AppendIndexUpdateInfo(mIndex->Id(),
|
|
|
|
mIndex->KeyPath(),
|
2011-12-20 14:59:07 +04:00
|
|
|
mIndex->KeyPathArray(),
|
2011-12-04 21:39:01 +04:00
|
|
|
mIndex->IsUnique(),
|
|
|
|
mIndex->IsMultiEntry(),
|
|
|
|
tlsEntry->Context(),
|
|
|
|
clone, updateInfo);
|
2011-12-03 07:46:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
PRInt64 objectDataID = stmt->AsInt64(0);
|
|
|
|
|
|
|
|
Key key;
|
2011-12-17 04:40:47 +04:00
|
|
|
rv = key.SetFromStatement(stmt, 3);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
rv = IDBObjectStore::UpdateIndexes(mTransaction, mIndex->Id(),
|
2011-12-17 04:40:47 +04:00
|
|
|
key, false, objectDataID, updateInfo);
|
2011-11-03 19:57:30 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-04 21:39:01 +04:00
|
|
|
} while (NS_SUCCEEDED(rv = stmt->ExecuteStep(&hasResult)) && hasResult);
|
|
|
|
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
|
|
|
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-06-23 23:46:08 +04:00
|
|
|
GetAllHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
2010-12-10 05:15:00 +03:00
|
|
|
NS_NAMED_LITERAL_CSTRING(lowerKeyName, "lower_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(upperKeyName, "upper_key");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
|
|
|
nsCAutoString keyRangeClause;
|
2011-11-03 19:57:30 +04:00
|
|
|
if (mKeyRange) {
|
|
|
|
if (!mKeyRange->Lower().IsUnset()) {
|
2011-12-17 04:40:47 +04:00
|
|
|
keyRangeClause = NS_LITERAL_CSTRING(" AND key_value");
|
2011-11-03 19:57:30 +04:00
|
|
|
if (mKeyRange->IsLowerOpen()) {
|
|
|
|
keyRangeClause.AppendLiteral(" > :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" >= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(lowerKeyName);
|
2011-11-03 05:57:48 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
if (!mKeyRange->Upper().IsUnset()) {
|
2011-12-17 04:40:47 +04:00
|
|
|
keyRangeClause += NS_LITERAL_CSTRING(" AND key_value");
|
2011-11-03 19:57:30 +04:00
|
|
|
if (mKeyRange->IsUpperOpen()) {
|
|
|
|
keyRangeClause.AppendLiteral(" < :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" <= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(upperKeyName);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString limitClause;
|
|
|
|
if (mLimit != PR_UINT32_MAX) {
|
|
|
|
limitClause.AssignLiteral(" LIMIT ");
|
|
|
|
limitClause.AppendInt(mLimit);
|
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT data, file_ids FROM object_data "
|
|
|
|
"WHERE object_store_id = :osid") +
|
|
|
|
keyRangeClause +
|
|
|
|
NS_LITERAL_CSTRING(" ORDER BY key_value ASC") +
|
|
|
|
limitClause;
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
mCloneReadInfos.SetCapacity(50);
|
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);
|
|
|
|
|
2011-12-17 04:40:47 +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
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
if (mKeyRange) {
|
|
|
|
if (!mKeyRange->Lower().IsUnset()) {
|
|
|
|
rv = mKeyRange->Lower().BindToStatement(stmt, lowerKeyName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-03 05:57:48 +04:00
|
|
|
}
|
2011-11-03 19:57:30 +04:00
|
|
|
if (!mKeyRange->Upper().IsUnset()) {
|
|
|
|
rv = mKeyRange->Upper().BindToStatement(stmt, upperKeyName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-03 05:57:48 +04:00
|
|
|
}
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasResult;
|
2010-06-23 23:46:08 +04:00
|
|
|
while (NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
|
2011-12-16 11:34:24 +04:00
|
|
|
if (mCloneReadInfos.Capacity() == mCloneReadInfos.Length()) {
|
|
|
|
if (!mCloneReadInfos.SetCapacity(mCloneReadInfos.Capacity() * 2)) {
|
2010-06-23 23:46:08 +04:00
|
|
|
NS_ERROR("Out of memory!");
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
StructuredCloneReadInfo* readInfo = mCloneReadInfos.AppendElement();
|
|
|
|
NS_ASSERTION(readInfo, "Shouldn't fail if SetCapacity succeeded!");
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
rv = IDBObjectStore::GetStructuredCloneReadInfoFromStatement(stmt, 0, 1,
|
|
|
|
mDatabase->Manager(), *readInfo);
|
2010-12-21 19:02:04 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2010-11-11 02:25:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 23:46:08 +04:00
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
|
|
|
|
2010-11-11 02:25:44 +03:00
|
|
|
nsresult
|
2011-01-07 09:21:36 +03:00
|
|
|
GetAllHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 23:46:08 +04:00
|
|
|
{
|
2011-12-16 11:34:24 +04:00
|
|
|
NS_ASSERTION(mCloneReadInfos.Length() <= mLimit, "Too many results!");
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
nsresult rv = ConvertCloneReadInfosToArray(aCx, mCloneReadInfos, aVal);
|
2011-01-27 04:53:02 +03:00
|
|
|
|
2011-12-16 11:34:24 +04:00
|
|
|
for (PRUint32 index = 0; index < mCloneReadInfos.Length(); index++) {
|
|
|
|
mCloneReadInfos[index].mCloneBuffer.clear();
|
2011-01-27 04:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
2010-06-23 23:46:08 +04:00
|
|
|
}
|
2011-11-03 19:57:42 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
CountHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_NAMED_LITERAL_CSTRING(lowerKeyName, "lower_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(upperKeyName, "upper_key");
|
|
|
|
|
|
|
|
nsCAutoString keyRangeClause;
|
|
|
|
if (mKeyRange) {
|
|
|
|
if (!mKeyRange->Lower().IsUnset()) {
|
2011-12-17 04:40:47 +04:00
|
|
|
keyRangeClause = NS_LITERAL_CSTRING(" AND key_value");
|
2011-11-03 19:57:42 +04:00
|
|
|
if (mKeyRange->IsLowerOpen()) {
|
|
|
|
keyRangeClause.AppendLiteral(" > :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" >= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(lowerKeyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mKeyRange->Upper().IsUnset()) {
|
2011-12-17 04:40:47 +04:00
|
|
|
keyRangeClause += NS_LITERAL_CSTRING(" AND key_value");
|
2011-11-03 19:57:42 +04:00
|
|
|
if (mKeyRange->IsUpperOpen()) {
|
|
|
|
keyRangeClause.AppendLiteral(" < :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" <= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(upperKeyName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT count(*) FROM object_data "
|
|
|
|
"WHERE object_store_id = :osid") +
|
2011-11-03 19:57:42 +04:00
|
|
|
keyRangeClause;
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
|
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2011-12-17 04:40:47 +04:00
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"),
|
|
|
|
mObjectStore->Id());
|
2011-11-03 19:57:42 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
if (mKeyRange) {
|
|
|
|
if (!mKeyRange->Lower().IsUnset()) {
|
|
|
|
rv = mKeyRange->Lower().BindToStatement(stmt, lowerKeyName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
if (!mKeyRange->Upper().IsUnset()) {
|
|
|
|
rv = mKeyRange->Upper().BindToStatement(stmt, upperKeyName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasResult;
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
NS_ENSURE_TRUE(hasResult, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
mCount = stmt->AsInt64(0);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CountHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
|
|
|
{
|
|
|
|
return JS_NewNumberValue(aCx, static_cast<jsdouble>(mCount), aVal);
|
|
|
|
}
|