2010-06-23 12:46:08 -07: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 11:51:06 -07:00
|
|
|
#include "IDBObjectStore.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
#include "nsIJSContextStack.h"
|
|
|
|
|
2011-06-29 12:09:20 -07:00
|
|
|
#include "jsclone.h"
|
2010-06-28 15:22:41 -07:00
|
|
|
#include "mozilla/storage.h"
|
2011-11-21 06:56:24 -08:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2010-06-28 15:22:41 -07:00
|
|
|
#include "nsContentUtils.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "nsDOMClassInfo.h"
|
2010-11-10 15:26:03 -08:00
|
|
|
#include "nsEventDispatcher.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "AsyncConnectionHelper.h"
|
2010-06-28 11:51:06 -07:00
|
|
|
#include "IDBCursor.h"
|
2010-06-28 15:22:41 -07:00
|
|
|
#include "IDBEvents.h"
|
|
|
|
#include "IDBIndex.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "IDBKeyRange.h"
|
2010-06-28 09:46:21 -07:00
|
|
|
#include "IDBTransaction.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "DatabaseInfo.h"
|
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AddHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 09:46:21 -07:00
|
|
|
AddHelper(IDBTransaction* aTransaction,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest,
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBObjectStore* aObjectStore,
|
2010-12-21 08:02:04 -08:00
|
|
|
JSAutoStructuredCloneBuffer& aCloneBuffer,
|
2010-06-23 12:46:08 -07:00
|
|
|
const Key& aKey,
|
|
|
|
bool aOverwrite,
|
2011-06-29 12:09:20 -07:00
|
|
|
nsTArray<IndexUpdateInfo>& aIndexUpdateInfo,
|
|
|
|
PRUint64 aOffsetToKeyProp)
|
2010-10-19 10:58:52 -07:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
2011-06-29 12:09:20 -07:00
|
|
|
mKey(aKey), mOverwrite(aOverwrite), mOffsetToKeyProp(aOffsetToKeyProp)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-12-21 08:02:04 -08:00
|
|
|
mCloneBuffer.swap(aCloneBuffer);
|
2010-06-23 12:46:08 -07:00
|
|
|
mIndexUpdateInfo.SwapElements(aIndexUpdateInfo);
|
|
|
|
}
|
|
|
|
|
2011-01-26 17:53:02 -08:00
|
|
|
~AddHelper()
|
|
|
|
{
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffer);
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-06 22:21:36 -08:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
2010-12-21 08:02:04 -08:00
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffer);
|
2010-10-19 10:58:52 -07:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
private:
|
|
|
|
// In-params.
|
2010-10-19 10:58:52 -07:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
// These may change in the autoincrement case.
|
2010-12-21 08:02:04 -08:00
|
|
|
JSAutoStructuredCloneBuffer mCloneBuffer;
|
2010-06-23 12:46:08 -07:00
|
|
|
Key mKey;
|
|
|
|
const bool mOverwrite;
|
|
|
|
nsTArray<IndexUpdateInfo> mIndexUpdateInfo;
|
2011-06-29 12:09:20 -07:00
|
|
|
PRUint64 mOffsetToKeyProp;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class GetHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 09:46:21 -07:00
|
|
|
GetHelper(IDBTransaction* aTransaction,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest,
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBObjectStore* aObjectStore,
|
2011-11-03 08:59:17 -07:00
|
|
|
IDBKeyRange* aKeyRange)
|
2010-10-19 10:58:52 -07:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
2011-11-03 08:59:17 -07:00
|
|
|
mKeyRange(aKeyRange)
|
2010-06-23 12:46:08 -07:00
|
|
|
{ }
|
|
|
|
|
2011-01-26 17:53:02 -08:00
|
|
|
~GetHelper()
|
|
|
|
{
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffer);
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-06 22:21:36 -08:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
2011-11-03 08:59:17 -07:00
|
|
|
mKeyRange = nsnull;
|
2010-12-21 08:02:04 -08:00
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffer);
|
2010-10-19 10:58:52 -07:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
protected:
|
|
|
|
// In-params.
|
2010-10-19 10:58:52 -07:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2011-11-03 08:59:17 -07:00
|
|
|
nsRefPtr<IDBKeyRange> mKeyRange;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Out-params.
|
2010-12-21 08:02:04 -08:00
|
|
|
JSAutoStructuredCloneBuffer mCloneBuffer;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
2010-11-10 15:25:57 -08:00
|
|
|
class DeleteHelper : public GetHelper
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-10 15:25:57 -08:00
|
|
|
DeleteHelper(IDBTransaction* aTransaction,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest,
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBObjectStore* aObjectStore,
|
2011-11-03 08:59:17 -07:00
|
|
|
IDBKeyRange* aKeyRange)
|
|
|
|
: GetHelper(aTransaction, aRequest, aObjectStore, aKeyRange)
|
2010-06-23 12:46:08 -07:00
|
|
|
{ }
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-06 22:21:36 -08:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
2010-08-26 13:57:30 -07:00
|
|
|
class ClearHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ClearHelper(IDBTransaction* aTransaction,
|
|
|
|
IDBRequest* aRequest,
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBObjectStore* aObjectStore)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore)
|
2010-08-26 13:57:30 -07:00
|
|
|
{ }
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2010-08-26 13:57:30 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-08-26 13:57:30 -07:00
|
|
|
protected:
|
|
|
|
// In-params.
|
2010-10-19 10:58:52 -07:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2010-08-26 13:57:30 -07:00
|
|
|
};
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
class OpenCursorHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 09:46:21 -07:00
|
|
|
OpenCursorHelper(IDBTransaction* aTransaction,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest,
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore* aObjectStore,
|
2011-11-03 08:57:30 -07:00
|
|
|
IDBKeyRange* aKeyRange,
|
2010-11-10 15:25:53 -08:00
|
|
|
PRUint16 aDirection)
|
2010-06-23 12:46:08 -07:00
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
2011-11-03 08:57:30 -07:00
|
|
|
mKeyRange(aKeyRange), mDirection(aDirection)
|
2010-06-23 12:46:08 -07:00
|
|
|
{ }
|
|
|
|
|
2011-01-26 17:53:02 -08:00
|
|
|
~OpenCursorHelper()
|
|
|
|
{
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffer);
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-06 22:21:36 -08:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-09-09 21:54:25 -07:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
2011-11-03 08:57:30 -07:00
|
|
|
mKeyRange = nsnull;
|
2010-12-21 08:02:04 -08:00
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffer);
|
2010-09-09 21:54:25 -07:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
private:
|
|
|
|
// In-params.
|
2010-06-28 11:51:06 -07:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2011-11-03 08:57:30 -07:00
|
|
|
nsRefPtr<IDBKeyRange> mKeyRange;
|
2010-06-23 12:46:08 -07:00
|
|
|
const PRUint16 mDirection;
|
|
|
|
|
|
|
|
// Out-params.
|
2010-12-09 18:15:00 -08:00
|
|
|
Key mKey;
|
2010-12-21 08:02:04 -08:00
|
|
|
JSAutoStructuredCloneBuffer mCloneBuffer;
|
2010-12-09 18:15:00 -08:00
|
|
|
nsCString mContinueQuery;
|
|
|
|
nsCString mContinueToQuery;
|
|
|
|
Key mRangeKey;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class CreateIndexHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2011-07-25 15:41:50 -07:00
|
|
|
CreateIndexHelper(IDBTransaction* aTransaction, IDBIndex* aIndex);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-06 22:21:36 -08:00
|
|
|
|
|
|
|
nsresult OnSuccess()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnError()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTransaction->IsAborted(), "How else can this fail?!");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-09-09 21:54:25 -07:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
2010-10-19 10:58:52 -07:00
|
|
|
mIndex = nsnull;
|
2010-09-09 21:54:25 -07:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
private:
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult InsertDataFromObjectStore(mozIStorageConnection* aConnection);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-07-25 15:41:50 -07:00
|
|
|
static void DestroyTLSEntry(void* aPtr);
|
|
|
|
|
|
|
|
static PRUintn sTLSIndex;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
// In-params.
|
2010-10-19 10:58:52 -07:00
|
|
|
nsRefPtr<IDBIndex> mIndex;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
PRUintn CreateIndexHelper::sTLSIndex = PRUintn(BAD_TLS_INDEX);
|
2011-07-25 15:41:50 -07:00
|
|
|
|
2010-11-10 15:25:57 -08:00
|
|
|
class DeleteIndexHelper : public AsyncConnectionHelper
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-10 15:25:57 -08:00
|
|
|
DeleteIndexHelper(IDBTransaction* aTransaction,
|
2010-06-23 12:46:08 -07:00
|
|
|
const nsAString& aName,
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore* aObjectStore)
|
2010-10-19 10:58:52 -07:00
|
|
|
: AsyncConnectionHelper(aTransaction, nsnull), mName(aName),
|
2010-06-23 12:46:08 -07:00
|
|
|
mObjectStore(aObjectStore)
|
|
|
|
{ }
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-06 22:21:36 -08:00
|
|
|
|
|
|
|
nsresult OnSuccess()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnError()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mTransaction->IsAborted(), "How else can this fail?!");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-09-09 21:54:25 -07:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
private:
|
|
|
|
// In-params
|
|
|
|
nsString mName;
|
2010-06-28 11:51:06 -07:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class GetAllHelper : public AsyncConnectionHelper
|
|
|
|
{
|
|
|
|
public:
|
2010-06-28 09:46:21 -07:00
|
|
|
GetAllHelper(IDBTransaction* aTransaction,
|
2010-06-23 12:46:08 -07:00
|
|
|
IDBRequest* aRequest,
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBObjectStore* aObjectStore,
|
2011-11-03 08:57:30 -07:00
|
|
|
IDBKeyRange* aKeyRange,
|
2010-10-19 10:58:52 -07:00
|
|
|
const PRUint32 aLimit)
|
|
|
|
: AsyncConnectionHelper(aTransaction, aRequest), mObjectStore(aObjectStore),
|
2011-11-03 08:57:30 -07:00
|
|
|
mKeyRange(aKeyRange), mLimit(aLimit)
|
2010-06-23 12:46:08 -07:00
|
|
|
{ }
|
|
|
|
|
2011-01-26 17:53:02 -08:00
|
|
|
~GetAllHelper()
|
|
|
|
{
|
|
|
|
for (PRUint32 index = 0; index < mCloneBuffers.Length(); index++) {
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffers[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult DoDatabaseWork(mozIStorageConnection* aConnection);
|
2011-01-06 22:21:36 -08:00
|
|
|
nsresult GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
void ReleaseMainThreadObjects()
|
|
|
|
{
|
|
|
|
mObjectStore = nsnull;
|
2011-11-03 08:57:30 -07:00
|
|
|
mKeyRange = nsnull;
|
2010-12-21 08:02:04 -08:00
|
|
|
for (PRUint32 index = 0; index < mCloneBuffers.Length(); index++) {
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(mCloneBuffers[index]);
|
|
|
|
}
|
2010-10-19 10:58:52 -07:00
|
|
|
AsyncConnectionHelper::ReleaseMainThreadObjects();
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
protected:
|
|
|
|
// In-params.
|
2010-10-19 10:58:52 -07:00
|
|
|
nsRefPtr<IDBObjectStore> mObjectStore;
|
2011-11-03 08:57:30 -07:00
|
|
|
nsRefPtr<IDBKeyRange> mKeyRange;
|
2010-06-23 12:46:08 -07:00
|
|
|
const PRUint32 mLimit;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Out-params.
|
2010-12-21 08:02:04 -08:00
|
|
|
nsTArray<JSAutoStructuredCloneBuffer> mCloneBuffers;
|
2010-06-23 12:46:08 -07:00
|
|
|
};
|
|
|
|
|
2011-11-03 08:57:42 -07: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 10:58:52 -07:00
|
|
|
NS_STACK_CLASS
|
|
|
|
class AutoRemoveIndex
|
|
|
|
{
|
|
|
|
public:
|
2011-11-07 16:15:43 -08:00
|
|
|
AutoRemoveIndex(IDBDatabase* aDatabase,
|
2010-10-19 10:58:52 -07:00
|
|
|
const nsAString& aObjectStoreName,
|
|
|
|
const nsAString& aIndexName)
|
2011-11-07 16:15:43 -08:00
|
|
|
: mDatabase(aDatabase), mObjectStoreName(aObjectStoreName),
|
2010-10-19 10:58:52 -07:00
|
|
|
mIndexName(aIndexName)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~AutoRemoveIndex()
|
|
|
|
{
|
2011-11-07 16:15:43 -08:00
|
|
|
if (mDatabase) {
|
2010-10-19 10:58:52 -07:00
|
|
|
ObjectStoreInfo* info;
|
2011-11-07 16:15:43 -08:00
|
|
|
if (mDatabase->Info()->GetObjectStore(mObjectStoreName, &info)) {
|
2010-10-19 10:58:52 -07:00
|
|
|
for (PRUint32 index = 0; index < info->indexes.Length(); index++) {
|
|
|
|
if (info->indexes[index].name == mIndexName) {
|
|
|
|
info->indexes.RemoveElementAt(index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void forget()
|
|
|
|
{
|
2011-11-07 16:15:43 -08:00
|
|
|
mDatabase = nsnull;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-11-07 16:15:43 -08:00
|
|
|
IDBDatabase* mDatabase;
|
2010-10-19 10:58:52 -07:00
|
|
|
nsString mObjectStoreName;
|
|
|
|
nsString mIndexName;
|
|
|
|
};
|
|
|
|
|
2011-11-21 06:56:24 -08:00
|
|
|
inline
|
|
|
|
bool
|
|
|
|
IgnoreWhitespace(PRUnichar c)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef nsCharSeparatedTokenizerTemplate<IgnoreWhitespace> KeyPathTokenizer;
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
inline
|
|
|
|
nsresult
|
2011-12-04 09:39:01 -08:00
|
|
|
GetJSValFromKeyPath(JSContext* aCx,
|
|
|
|
jsval aVal,
|
|
|
|
const nsAString& aKeyPath,
|
|
|
|
jsval& aKey)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2011-11-21 06:56:24 -08:00
|
|
|
NS_ASSERTION(aCx, "Null pointer!");
|
2011-11-23 06:15:15 -08:00
|
|
|
// aVal can be primitive iff the key path is empty.
|
2011-11-21 06:56:24 -08:00
|
|
|
NS_ASSERTION(IDBObjectStore::IsValidKeyPath(aCx, aKeyPath),
|
|
|
|
"This will explode!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-21 06:56:24 -08:00
|
|
|
KeyPathTokenizer tokenizer(aKeyPath, '.');
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-21 06:56:24 -08:00
|
|
|
jsval intermediate = aVal;
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
2011-12-04 09:39:01 -08:00
|
|
|
const nsDependentSubstring& token = tokenizer.nextToken();
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
NS_ASSERTION(!token.IsEmpty(), "Should be a valid keypath");
|
2011-11-21 06:56:24 -08:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
const jschar* keyPathChars = token.BeginReading();
|
2011-11-21 06:56:24 -08:00
|
|
|
const size_t keyPathLen = token.Length();
|
|
|
|
|
|
|
|
if (JSVAL_IS_PRIMITIVE(intermediate)) {
|
2011-12-04 09:39:01 -08:00
|
|
|
intermediate = JSVAL_VOID;
|
|
|
|
break;
|
2011-11-21 06:56:24 -08: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 09:39:01 -08: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 06:56:24 -08:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
if (NS_FAILED(aKey.SetFromJSVal(aCx, key))) {
|
2011-11-21 06:56:24 -08:00
|
|
|
aKey.Unset();
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-06-28 15:22:41 -07:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
inline
|
|
|
|
already_AddRefed<IDBRequest>
|
|
|
|
GenerateRequest(IDBObjectStore* aObjectStore)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
IDBDatabase* database = aObjectStore->Transaction()->Database();
|
2010-11-10 15:26:03 -08:00
|
|
|
return IDBRequest::Create(aObjectStore, database->ScriptContext(),
|
|
|
|
database->Owner(), aObjectStore->Transaction());
|
2010-09-09 15:15:38 -07:00
|
|
|
}
|
|
|
|
|
2011-06-29 12:09:20 -07: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
|
|
|
|
};
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
StructuredCloneWriteDummyProp(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JSObject* aObj,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
if (JS_GET_CLASS(aCx, aObj) == &gDummyPropClass) {
|
|
|
|
PRUint64* closure = reinterpret_cast<PRUint64*>(aClosure);
|
|
|
|
|
|
|
|
NS_ASSERTION(*closure == 0, "We should not have been here before!");
|
|
|
|
*closure = js_GetSCOffset(aWriter);
|
|
|
|
|
|
|
|
PRUint64 value = 0;
|
|
|
|
return JS_WriteBytes(aWriter, &value, sizeof(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
// try using the runtime callbacks
|
|
|
|
const JSStructuredCloneCallbacks* runtimeCallbacks =
|
|
|
|
aCx->runtime->structuredCloneCallbacks;
|
|
|
|
if (runtimeCallbacks) {
|
|
|
|
return runtimeCallbacks->write(aCx, aWriter, aObj, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// static
|
2010-06-28 11:51:06 -07:00
|
|
|
already_AddRefed<IDBObjectStore>
|
2010-09-09 15:15:38 -07:00
|
|
|
IDBObjectStore::Create(IDBTransaction* aTransaction,
|
2010-10-19 10:58:52 -07:00
|
|
|
const ObjectStoreInfo* aStoreInfo)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-06-28 11:51:06 -07:00
|
|
|
nsRefPtr<IDBObjectStore> objectStore = new IDBObjectStore();
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
objectStore->mScriptContext = aTransaction->Database()->ScriptContext();
|
|
|
|
objectStore->mOwner = aTransaction->Database()->Owner();
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
objectStore->mTransaction = aTransaction;
|
|
|
|
objectStore->mName = aStoreInfo->name;
|
|
|
|
objectStore->mId = aStoreInfo->id;
|
|
|
|
objectStore->mKeyPath = aStoreInfo->keyPath;
|
|
|
|
objectStore->mAutoIncrement = aStoreInfo->autoIncrement;
|
|
|
|
objectStore->mDatabaseId = aStoreInfo->databaseId;
|
|
|
|
|
|
|
|
return objectStore.forget();
|
|
|
|
}
|
|
|
|
|
2011-11-21 06:56:24 -08: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;
|
|
|
|
if (!xpc_qsStringToJsval(aCx, token, &stringVal)) {
|
|
|
|
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 12:46:08 -07:00
|
|
|
// static
|
|
|
|
nsresult
|
2011-12-04 09:39:01 -08:00
|
|
|
IDBObjectStore::AppendIndexUpdateInfo(PRInt64 aIndexID,
|
|
|
|
const nsAString& aKeyPath,
|
|
|
|
bool aUnique,
|
|
|
|
bool aMultiEntry,
|
|
|
|
JSContext* aCx,
|
|
|
|
jsval aObject,
|
|
|
|
nsTArray<IndexUpdateInfo>& aUpdateInfoArray)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2011-12-04 09:39:01 -08:00
|
|
|
jsval key;
|
|
|
|
nsresult rv = GetJSValFromKeyPath(aCx, aObject, aKeyPath, key);
|
2011-11-21 06:56:24 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08: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 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-12-04 09:39:01 -08: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-02 19:46:25 -08:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
Key value;
|
2011-12-04 09:39:01 -08:00
|
|
|
if (NS_FAILED(value.SetFromJSVal(aCx, arrayItem)) ||
|
|
|
|
value.IsUnset()) {
|
2010-06-23 12:46:08 -07:00
|
|
|
// Not a value we can do anything with, ignore it.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexUpdateInfo* updateInfo = aUpdateInfoArray.AppendElement();
|
2011-12-04 09:39:01 -08:00
|
|
|
updateInfo->indexId = aIndexID;
|
|
|
|
updateInfo->indexUnique = aUnique;
|
2010-06-23 12:46:08 -07:00
|
|
|
updateInfo->value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-12-04 09:39:01 -08: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 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
// static
|
2010-06-23 12:46:08 -07:00
|
|
|
nsresult
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore::UpdateIndexes(IDBTransaction* aTransaction,
|
|
|
|
PRInt64 aObjectStoreId,
|
|
|
|
const Key& aObjectStoreKey,
|
|
|
|
bool aAutoIncrement,
|
|
|
|
bool aOverwrite,
|
|
|
|
PRInt64 aObjectDataId,
|
|
|
|
const nsTArray<IndexUpdateInfo>& aUpdateInfoArray)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2011-12-04 09:39:01 -08:00
|
|
|
NS_ASSERTION(!aAutoIncrement || aObjectDataId != LL_MININT,
|
|
|
|
"Bad objectData id!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
if (aObjectDataId == LL_MININT) {
|
2010-06-23 12:46:08 -07:00
|
|
|
stmt = aTransaction->GetCachedStatement(
|
|
|
|
"SELECT id "
|
|
|
|
"FROM object_data "
|
|
|
|
"WHERE object_store_id = :osid "
|
|
|
|
"AND key_value = :key_value"
|
|
|
|
);
|
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), aObjectStoreId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(!aObjectStoreKey.IsUnset(), "This shouldn't happen!");
|
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
rv = aObjectStoreKey.BindToStatement(stmt, NS_LITERAL_CSTRING("key_value"));
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasResult;
|
2010-06-23 12:46:08 -07:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!hasResult) {
|
|
|
|
NS_ERROR("This is bad, we just added this value! Where'd it go?!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
aObjectDataId = stmt->AsInt64(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(aObjectDataId != LL_MININT, "Bad objectData id!");
|
|
|
|
|
2011-11-21 20:18:19 -08:00
|
|
|
NS_NAMED_LITERAL_CSTRING(indexId, "index_id");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(objectDataId, "object_data_id");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(objectDataKey, "object_data_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(value, "value");
|
|
|
|
|
|
|
|
if (aOverwrite) {
|
|
|
|
stmt = aTransaction->IndexDataDeleteStatement(aAutoIncrement, false);
|
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
mozStorageStatementScoper scoper2(stmt);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(objectDataId, aObjectDataId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
stmt = aTransaction->IndexDataDeleteStatement(aAutoIncrement, true);
|
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
mozStorageStatementScoper scoper3(stmt);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64ByName(objectDataId, aObjectDataId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
PRUint32 infoCount = aUpdateInfoArray.Length();
|
|
|
|
for (PRUint32 i = 0; i < infoCount; i++) {
|
|
|
|
const IndexUpdateInfo& updateInfo = aUpdateInfoArray[i];
|
2011-11-21 20:18:19 -08:00
|
|
|
|
|
|
|
// Insert new values.
|
|
|
|
stmt = aTransaction->IndexDataInsertStatement(aAutoIncrement,
|
2011-12-04 09:39:01 -08:00
|
|
|
updateInfo.indexUnique);
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_FAILURE);
|
|
|
|
|
2011-11-21 20:18:19 -08:00
|
|
|
mozStorageStatementScoper scoper4(stmt);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
rv = stmt->BindInt64ByName(indexId, updateInfo.indexId);
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-21 20:18:19 -08:00
|
|
|
rv = stmt->BindInt64ByName(objectDataId, aObjectDataId);
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
if (!aAutoIncrement) {
|
2011-11-21 20:18:19 -08:00
|
|
|
rv = aObjectStoreKey.BindToStatement(stmt, objectDataKey);
|
2011-04-13 00:26:20 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-11-21 20:18:19 -08:00
|
|
|
rv = updateInfo.value.BindToStatement(stmt, value);
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
2011-12-04 09:39:01 -08: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 12:46:08 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-21 08:02:04 -08:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
IDBObjectStore::GetStructuredCloneDataFromStatement(
|
|
|
|
mozIStorageStatement* aStatement,
|
|
|
|
PRUint32 aIndex,
|
|
|
|
JSAutoStructuredCloneBuffer& aBuffer)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
PRInt32 valueType;
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(aStatement->GetTypeOfIndex(aIndex, &valueType)) &&
|
|
|
|
valueType == mozIStorageStatement::VALUE_TYPE_BLOB,
|
|
|
|
"Bad value type!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-22 03:54:50 -08:00
|
|
|
const PRUint8* data;
|
|
|
|
PRUint32 dataLength;
|
|
|
|
nsresult rv = aStatement->GetSharedBlob(aIndex, &dataLength, &data);
|
2010-12-21 08:02:04 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-11-22 03:54:50 -08:00
|
|
|
return aBuffer.copy(reinterpret_cast<const uint64_t *>(data), dataLength) ?
|
2011-07-12 10:42:24 -07:00
|
|
|
NS_OK :
|
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-12-21 08:02:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
IDBObjectStore::ClearStructuredCloneBuffer(JSAutoStructuredCloneBuffer& aBuffer)
|
|
|
|
{
|
|
|
|
if (aBuffer.data()) {
|
2011-07-12 10:42:24 -07:00
|
|
|
aBuffer.clear();
|
2010-12-21 08:02:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-08 19:21:53 -07:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IDBObjectStore::DeserializeValue(JSContext* aCx,
|
|
|
|
JSAutoStructuredCloneBuffer& aBuffer,
|
2011-06-29 12:09:20 -07:00
|
|
|
jsval* aValue,
|
|
|
|
JSStructuredCloneCallbacks* aCallbacks,
|
|
|
|
void* aClosure)
|
2011-06-08 19:21:53 -07:00
|
|
|
{
|
2011-06-24 07:21:21 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
|
|
|
"Should only be deserializing on the main thread!");
|
2011-06-08 19:21:53 -07:00
|
|
|
NS_ASSERTION(aCx, "A JSContext is required!");
|
|
|
|
|
|
|
|
if (!aBuffer.data()) {
|
|
|
|
*aValue = JSVAL_VOID;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoRequest ar(aCx);
|
|
|
|
|
2011-07-12 10:42:24 -07:00
|
|
|
return aBuffer.read(aCx, aValue, aCallbacks, aClosure);
|
2011-06-08 19:21:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
|
|
|
IDBObjectStore::SerializeValue(JSContext* aCx,
|
|
|
|
JSAutoStructuredCloneBuffer& aBuffer,
|
2011-06-29 12:09:20 -07:00
|
|
|
jsval aValue,
|
|
|
|
JSStructuredCloneCallbacks* aCallbacks,
|
|
|
|
void* aClosure)
|
2011-06-08 19:21:53 -07:00
|
|
|
{
|
2011-06-24 07:21:21 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
|
|
|
"Should only be serializing on the main thread!");
|
2011-06-08 19:21:53 -07:00
|
|
|
NS_ASSERTION(aCx, "A JSContext is required!");
|
|
|
|
|
|
|
|
JSAutoRequest ar(aCx);
|
|
|
|
|
2011-06-29 12:09:20 -07:00
|
|
|
return aBuffer.write(aCx, aValue, aCallbacks, aClosure);
|
2011-06-08 19:21:53 -07:00
|
|
|
}
|
|
|
|
|
2011-06-24 07:21:21 -07:00
|
|
|
static inline jsdouble
|
|
|
|
SwapBytes(PRUint64 u)
|
|
|
|
{
|
|
|
|
#ifdef IS_BIG_ENDIAN
|
|
|
|
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);
|
|
|
|
#else
|
2011-11-03 08:57:30 -07:00
|
|
|
return jsdouble(u);
|
2011-06-24 07:21:21 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
IDBObjectStore::ModifyValueForNewKey(JSAutoStructuredCloneBuffer& aBuffer,
|
2011-06-29 12:09:20 -07:00
|
|
|
Key& aKey,
|
|
|
|
PRUint64 aOffsetToKeyProp)
|
2011-06-24 07:21:21 -07:00
|
|
|
{
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_ASSERTION(IsAutoIncrement() && aKey.IsInteger(), "Don't call me!");
|
2011-06-24 07:21:21 -07:00
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Wrong thread");
|
|
|
|
|
|
|
|
// This is a duplicate of the js engine's byte munging here
|
|
|
|
union {
|
|
|
|
jsdouble d;
|
|
|
|
PRUint64 u;
|
|
|
|
} pun;
|
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
pun.d = SwapBytes(aKey.ToInteger());
|
2011-06-24 07:21:21 -07:00
|
|
|
|
2011-06-29 12:09:20 -07:00
|
|
|
memcpy((char*)aBuffer.data() + aOffsetToKeyProp, &pun.u, sizeof(PRUint64));
|
2011-06-24 07:21:21 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore::IDBObjectStore()
|
2010-06-23 12:46:08 -07:00
|
|
|
: mId(LL_MININT),
|
2011-10-17 07:59:28 -07:00
|
|
|
mAutoIncrement(false)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore::~IDBObjectStore()
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-06-28 15:22:41 -07:00
|
|
|
IDBObjectStore::GetAddInfo(JSContext* aCx,
|
|
|
|
jsval aValue,
|
|
|
|
jsval aKeyVal,
|
2010-12-21 08:02:04 -08:00
|
|
|
JSAutoStructuredCloneBuffer& aCloneBuffer,
|
2010-06-28 11:51:06 -07:00
|
|
|
Key& aKey,
|
2011-06-29 12:09:20 -07:00
|
|
|
nsTArray<IndexUpdateInfo>& aUpdateInfoArray,
|
|
|
|
PRUint64* aOffsetToKeyProp)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-10-10 15:35:42 -07:00
|
|
|
nsresult rv;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-12-15 13:21:11 -08:00
|
|
|
// Return DATA_ERR if a key was passed in and this objectStore uses inline
|
|
|
|
// keys.
|
2011-12-04 09:39:01 -08:00
|
|
|
if (!JSVAL_IS_VOID(aKeyVal) && HasKeyPath()) {
|
2010-12-15 13:21:11 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
|
2010-10-10 15:35:42 -07:00
|
|
|
JSAutoRequest ar(aCx);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
if (!HasKeyPath()) {
|
2010-12-15 13:21:11 -08:00
|
|
|
// Out-of-line keys must be passed in.
|
2011-11-03 08:57:30 -07:00
|
|
|
rv = aKey.SetFromJSVal(aCx, aKeyVal);
|
2010-12-15 13:21:07 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
2011-12-02 19:46:25 -08:00
|
|
|
else {
|
2010-12-15 13:21:11 -08:00
|
|
|
// Inline keys live on the object. Make sure that the value passed in is an
|
|
|
|
// object.
|
2011-11-21 06:56:24 -08:00
|
|
|
rv = GetKeyFromValue(aCx, aValue, mKeyPath, aKey);
|
2010-12-15 13:21:07 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-12-15 13:21:07 -08:00
|
|
|
// Return DATA_ERR if no key was specified this isn't an autoIncrement
|
|
|
|
// objectStore.
|
2010-06-23 12:46:08 -07:00
|
|
|
if (aKey.IsUnset() && !mAutoIncrement) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out indexes and the index values to update here.
|
2010-11-10 15:25:44 -08:00
|
|
|
ObjectStoreInfo* info;
|
2011-11-07 16:15:43 -08:00
|
|
|
if (!mTransaction->Database()->Info()->GetObjectStore(mName, &info)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
PRUint32 count = info->indexes.Length();
|
|
|
|
aUpdateInfoArray.SetCapacity(count); // Pretty good estimate
|
|
|
|
for (PRUint32 indexesIndex = 0; indexesIndex < count; indexesIndex++) {
|
|
|
|
const IndexInfo& indexInfo = info->indexes[indexesIndex];
|
|
|
|
|
|
|
|
rv = AppendIndexUpdateInfo(indexInfo.id, indexInfo.keyPath,
|
|
|
|
indexInfo.unique, indexInfo.multiEntry,
|
|
|
|
aCx, aValue, aUpdateInfoArray);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-02 19:46:25 -08:00
|
|
|
const jschar* keyPathChars =
|
|
|
|
reinterpret_cast<const jschar*>(mKeyPath.get());
|
|
|
|
const size_t keyPathLen = mKeyPath.Length();
|
|
|
|
JSBool ok = JS_FALSE;
|
2011-12-02 18:32:46 -08:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
if (HasKeyPath() && aKey.IsUnset()) {
|
2011-12-02 19:46:25 -08:00
|
|
|
NS_ASSERTION(mAutoIncrement, "Should have bailed earlier!");
|
2011-12-04 09:39:01 -08:00
|
|
|
|
|
|
|
if (JSVAL_IS_PRIMITIVE(aValue)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
2011-06-24 07:21:21 -07:00
|
|
|
|
2011-12-02 19:46:25 -08:00
|
|
|
JSObject* obj = JS_NewObject(aCx, &gDummyPropClass, nsnull, nsnull);
|
|
|
|
NS_ENSURE_TRUE(obj, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-06-24 07:21:21 -07:00
|
|
|
|
2011-12-02 19:46:25 -08:00
|
|
|
jsval key = OBJECT_TO_JSVAL(obj);
|
|
|
|
|
|
|
|
ok = JS_DefineUCProperty(aCx, JSVAL_TO_OBJECT(aValue), keyPathChars,
|
|
|
|
keyPathLen, key, nsnull, nsnull,
|
|
|
|
JSPROP_ENUMERATE);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-06-24 07:21:21 -07:00
|
|
|
|
2011-12-02 19:46:25 -08:00
|
|
|
// From this point on we have to try to remove the property.
|
2011-06-24 07:21:21 -07:00
|
|
|
}
|
|
|
|
|
2011-06-29 12:09:20 -07:00
|
|
|
JSStructuredCloneCallbacks callbacks = {
|
|
|
|
nsnull,
|
|
|
|
StructuredCloneWriteDummyProp,
|
|
|
|
nsnull
|
|
|
|
};
|
|
|
|
*aOffsetToKeyProp = 0;
|
|
|
|
|
2011-06-24 07:21:21 -07: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-06-29 12:09:20 -07:00
|
|
|
!IDBObjectStore::SerializeValue(aCx, aCloneBuffer, aValue, &callbacks,
|
|
|
|
aOffsetToKeyProp)) {
|
2011-06-24 07:21:21 -07:00
|
|
|
rv = NS_ERROR_DOM_DATA_CLONE_ERR;
|
|
|
|
}
|
|
|
|
|
2011-12-02 19:46:25 -08:00
|
|
|
if (ok) {
|
2011-06-24 07:21:21 -07:00
|
|
|
// If this fails, we lose, and the web page sees a magical property
|
|
|
|
// appear on the object :-(
|
|
|
|
jsval succeeded;
|
2011-12-02 19:46:25 -08:00
|
|
|
ok = JS_DeleteUCProperty2(aCx, JSVAL_TO_OBJECT(aValue), keyPathChars,
|
|
|
|
keyPathLen, &succeeded);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-06-24 07:21:21 -07: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 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-12-15 13:21:07 -08: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-15 13:21:17 -08:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-15 13:21:14 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_READ_ONLY_ERR;
|
2010-12-15 13:21:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
jsval keyval = (aOptionalArgCount >= 1) ? aKey : JSVAL_VOID;
|
|
|
|
|
2010-12-21 08:02:04 -08:00
|
|
|
JSAutoStructuredCloneBuffer cloneBuffer;
|
2010-12-15 13:21:07 -08:00
|
|
|
Key key;
|
|
|
|
nsTArray<IndexUpdateInfo> updateInfo;
|
2011-06-29 12:09:20 -07:00
|
|
|
PRUint64 offset;
|
2010-12-15 13:21:07 -08:00
|
|
|
|
2011-06-29 12:09:20 -07:00
|
|
|
nsresult rv =
|
|
|
|
GetAddInfo(aCx, aValue, keyval, cloneBuffer, key, updateInfo, &offset);
|
2010-12-15 13:21:07 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put requires a key.
|
|
|
|
if (aOverwrite && key.IsUnset()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
nsRefPtr<AddHelper> helper =
|
2010-12-21 08:02:04 -08:00
|
|
|
new AddHelper(mTransaction, request, this, cloneBuffer, key, aOverwrite,
|
2011-06-29 12:09:20 -07:00
|
|
|
updateInfo, offset);
|
2010-12-15 13:21:07 -08:00
|
|
|
|
|
|
|
rv = helper->DispatchToTransactionPool();
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBObjectStore)
|
|
|
|
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBObjectStore)
|
2010-09-09 15:15:38 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mTransaction,
|
2011-06-23 19:18:00 -07:00
|
|
|
nsIDOMEventTarget)
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mScriptContext)
|
2010-11-10 15:25:40 -08: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-09 15:15:38 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IDBObjectStore)
|
2010-09-09 15:15:38 -07:00
|
|
|
// Don't unlink mTransaction!
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mScriptContext)
|
2010-11-10 15:25:40 -08:00
|
|
|
|
|
|
|
tmp->mCreatedIndexes.Clear();
|
2010-09-09 15:15:38 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBObjectStore)
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIDBObjectStore)
|
2010-06-28 11:51:06 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(IDBObjectStore)
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
2010-09-09 15:15:38 -07:00
|
|
|
|
2010-11-10 15:26:03 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBObjectStore)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBObjectStore)
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-06-28 11:51:06 -07:00
|
|
|
DOMCI_DATA(IDBObjectStore, IDBObjectStore)
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore::GetName(nsAString& aName)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
aName.Assign(mName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore::GetKeyPath(nsAString& aKeyPath)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
aKeyPath.Assign(mKeyPath);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-09 18:14:09 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBObjectStore::GetTransaction(nsIIDBTransaction** aTransaction)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIDBTransaction> transaction(mTransaction);
|
|
|
|
transaction.forget(aTransaction);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_IMETHODIMP
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore::GetIndexNames(nsIDOMDOMStringList** aIndexNames)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
ObjectStoreInfo* info;
|
2011-11-07 16:15:43 -08:00
|
|
|
if (!mTransaction->Database()->Info()->GetObjectStore(mName, &info)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsDOMStringList> list(new nsDOMStringList());
|
|
|
|
|
|
|
|
PRUint32 count = info->indexes.Length();
|
|
|
|
for (PRUint32 index = 0; index < count; index++) {
|
|
|
|
NS_ENSURE_TRUE(list->Add(info->indexes[index].name),
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
list.forget(aIndexNames);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-11-03 08:57:30 -07:00
|
|
|
IDBObjectStore::Get(const jsval& aKey,
|
|
|
|
JSContext* aCx,
|
2010-06-28 11:51:06 -07:00
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-15 13:21:14 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
nsresult rv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
if (!keyRange) {
|
|
|
|
// Must specify a key or keyRange for get().
|
2010-12-15 13:21:07 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
nsRefPtr<GetHelper> helper =
|
|
|
|
new GetHelper(mTransaction, request, this, keyRange);
|
2010-11-10 15:25:44 -08:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
rv = helper->DispatchToTransactionPool();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-11-03 08:57:30 -07:00
|
|
|
IDBObjectStore::GetAll(const jsval& aKey,
|
2010-06-28 11:51:06 -07:00
|
|
|
PRUint32 aLimit,
|
2011-11-03 08:57:30 -07:00
|
|
|
JSContext* aCx,
|
2010-06-28 11:51:06 -07:00
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-15 13:21:14 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
if (aOptionalArgCount) {
|
|
|
|
rv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2011-11-02 18:57:48 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
if (aOptionalArgCount < 2) {
|
|
|
|
aLimit = PR_UINT32_MAX;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
nsRefPtr<GetAllHelper> helper =
|
2011-11-03 08:57:30 -07:00
|
|
|
new GetAllHelper(mTransaction, request, this, keyRange, aLimit);
|
2010-11-10 15:25:44 -08:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
rv = helper->DispatchToTransactionPool();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-12-15 13:21:07 -08:00
|
|
|
IDBObjectStore::Add(const jsval& aValue,
|
|
|
|
const jsval& aKey,
|
2010-06-28 15:22:41 -07:00
|
|
|
JSContext* aCx,
|
|
|
|
PRUint8 aOptionalArgCount,
|
2010-06-28 11:51:06 -07:00
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-15 13:21:07 -08:00
|
|
|
return AddOrPut(aValue, aKey, aCx, aOptionalArgCount, _retval, false);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-12-15 13:21:07 -08:00
|
|
|
IDBObjectStore::Put(const jsval& aValue,
|
|
|
|
const jsval& aKey,
|
2010-08-26 13:57:28 -07:00
|
|
|
JSContext* aCx,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-15 13:21:07 -08:00
|
|
|
return AddOrPut(aValue, aKey, aCx, aOptionalArgCount, _retval, true);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-12-15 13:21:11 -08:00
|
|
|
IDBObjectStore::Delete(const jsval& aKey,
|
|
|
|
JSContext* aCx,
|
2010-06-28 11:51:06 -07:00
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-15 13:21:14 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_READ_ONLY_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
nsresult rv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
if (!keyRange) {
|
|
|
|
// Must specify a key or keyRange for delete().
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:57 -08:00
|
|
|
nsRefPtr<DeleteHelper> helper =
|
2011-11-03 08:59:17 -07:00
|
|
|
new DeleteHelper(mTransaction, request, this, keyRange);
|
2010-11-10 15:25:44 -08:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
rv = helper->DispatchToTransactionPool();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-26 13:57:30 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
IDBObjectStore::Clear(nsIIDBRequest** _retval)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-15 13:21:14 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsWriteAllowed()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_READ_ONLY_ERR;
|
2010-08-26 13:57:30 -07:00
|
|
|
}
|
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-26 13:57:30 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsRefPtr<ClearHelper> helper(new ClearHelper(mTransaction, request, this));
|
2010-11-10 15:25:44 -08:00
|
|
|
|
2010-08-26 13:57:30 -07:00
|
|
|
nsresult rv = helper->DispatchToTransactionPool();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-26 13:57:30 -07:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_IMETHODIMP
|
2011-11-03 08:57:30 -07:00
|
|
|
IDBObjectStore::OpenCursor(const jsval& aKey,
|
2010-06-28 11:51:06 -07:00
|
|
|
PRUint16 aDirection,
|
2011-11-03 08:57:30 -07:00
|
|
|
JSContext* aCx,
|
2010-06-28 11:51:06 -07:00
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-15 13:21:14 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
if (aOptionalArgCount) {
|
|
|
|
rv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-02 18:57:48 -07:00
|
|
|
|
2011-11-03 08:57:30 -07: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 12:46:08 -07:00
|
|
|
}
|
2011-11-03 08:57:30 -07:00
|
|
|
else {
|
|
|
|
aDirection = nsIIDBCursor::NEXT;
|
2011-11-02 18:57:48 -07:00
|
|
|
}
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-09-09 15:15:38 -07:00
|
|
|
nsRefPtr<IDBRequest> request = GenerateRequest(this);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
nsRefPtr<OpenCursorHelper> helper =
|
2011-11-03 08:57:30 -07:00
|
|
|
new OpenCursorHelper(mTransaction, request, this, keyRange, aDirection);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
rv = helper->DispatchToTransactionPool();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
request.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore::CreateIndex(const nsAString& aName,
|
|
|
|
const nsAString& aKeyPath,
|
2010-12-21 08:02:01 -08:00
|
|
|
const jsval& aOptions,
|
|
|
|
JSContext* aCx,
|
2010-10-19 10:58:52 -07:00
|
|
|
nsIIDBIndex** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2011-11-21 06:56:24 -08:00
|
|
|
if (!IsValidKeyPath(aCx, aKeyPath)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
IDBTransaction* transaction = AsyncConnectionHelper::GetCurrentTransaction();
|
|
|
|
|
|
|
|
if (!transaction ||
|
|
|
|
transaction != mTransaction ||
|
|
|
|
mTransaction->Mode() != nsIIDBTransaction::VERSION_CHANGE) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectStoreInfo* info;
|
2011-11-07 16:15:43 -08:00
|
|
|
if (!mTransaction->Database()->Info()->GetObjectStore(mName, &info)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
PRUint32 indexCount = info->indexes.Length();
|
|
|
|
for (PRUint32 index = 0; index < indexCount; index++) {
|
|
|
|
if (info->indexes[index].name == aName) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
NS_ASSERTION(mTransaction->IsOpen(), "Impossible!");
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2010-12-21 08:02:01 -08:00
|
|
|
bool unique = false;
|
2011-12-04 09:39:01 -08:00
|
|
|
bool multiEntry = false;
|
2010-12-21 08:02:01 -08:00
|
|
|
|
|
|
|
// Get optional arguments.
|
|
|
|
if (!JSVAL_IS_VOID(aOptions) && !JSVAL_IS_NULL(aOptions)) {
|
|
|
|
if (JSVAL_IS_PRIMITIVE(aOptions)) {
|
2011-11-07 22:25:51 -08:00
|
|
|
// XXX Update spec for a real code here
|
|
|
|
return NS_ERROR_DOM_TYPE_ERR;
|
2010-12-21 08:02:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(JSVAL_IS_OBJECT(aOptions), "Huh?!");
|
|
|
|
JSObject* options = JSVAL_TO_OBJECT(aOptions);
|
|
|
|
|
2011-11-07 22:25:51 -08:00
|
|
|
jsval val;
|
|
|
|
if (!JS_GetPropertyById(aCx, options, nsDOMClassInfo::sUnique_id, &val)) {
|
|
|
|
NS_WARNING("JS_GetPropertyById failed!");
|
2010-12-21 08:02:01 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2011-11-07 22:25:51 -08:00
|
|
|
JSBool boolVal;
|
|
|
|
if (!JS_ValueToBoolean(aCx, val, &boolVal)) {
|
|
|
|
NS_WARNING("JS_ValueToBoolean failed!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-12-21 08:02:01 -08:00
|
|
|
}
|
2011-11-07 22:25:51 -08:00
|
|
|
unique = !!boolVal;
|
2011-12-04 09:39:01 -08:00
|
|
|
|
|
|
|
if (!JS_GetPropertyById(aCx, options, nsDOMClassInfo::sMultiEntry_id, &val)) {
|
|
|
|
NS_WARNING("JS_GetPropertyById failed!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_ValueToBoolean(aCx, val, &boolVal)) {
|
|
|
|
NS_WARNING("JS_ValueToBoolean failed!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
multiEntry = !!boolVal;
|
2010-12-21 08:02:01 -08:00
|
|
|
}
|
|
|
|
|
2011-11-07 16:06:13 -08:00
|
|
|
DatabaseInfo* databaseInfo = mTransaction->Database()->Info();
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
IndexInfo* indexInfo = info->indexes.AppendElement();
|
|
|
|
if (!indexInfo) {
|
|
|
|
NS_WARNING("Out of memory!");
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
indexInfo->id = databaseInfo->nextIndexId++;
|
|
|
|
indexInfo->name = aName;
|
|
|
|
indexInfo->keyPath = aKeyPath;
|
2010-12-21 08:02:01 -08:00
|
|
|
indexInfo->unique = unique;
|
2011-12-04 09:39:01 -08:00
|
|
|
indexInfo->multiEntry = multiEntry;
|
2010-10-19 10:58:52 -07:00
|
|
|
indexInfo->autoIncrement = mAutoIncrement;
|
|
|
|
|
|
|
|
// Don't leave this in the list if we fail below!
|
2011-11-07 16:15:43 -08:00
|
|
|
AutoRemoveIndex autoRemove(mTransaction->Database(), mName, aName);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2010-11-10 15:25:40 -08: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 10:58:52 -07:00
|
|
|
nsRefPtr<IDBIndex> index(IDBIndex::Create(this, indexInfo));
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:40 -08:00
|
|
|
if (!mCreatedIndexes.AppendElement(index)) {
|
|
|
|
NS_WARNING("Out of memory!");
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-11-10 15:25:40 -08:00
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
nsRefPtr<CreateIndexHelper> helper =
|
2010-10-19 10:58:52 -07:00
|
|
|
new CreateIndexHelper(mTransaction, index);
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
nsresult rv = helper->DispatchToTransactionPool();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
autoRemove.forget();
|
|
|
|
|
|
|
|
index.forget(_retval);
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-06-28 11:51:06 -07:00
|
|
|
IDBObjectStore::Index(const nsAString& aName,
|
2010-06-28 11:51:06 -07:00
|
|
|
nsIIDBIndex** _retval)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
if (!mTransaction->IsOpen()) {
|
2010-12-15 13:21:14 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
ObjectStoreInfo* info;
|
2011-11-07 16:15:43 -08:00
|
|
|
if (!mTransaction->Database()->Info()->GetObjectStore(mName, &info)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
IndexInfo* indexInfo = nsnull;
|
|
|
|
PRUint32 indexCount = info->indexes.Length();
|
|
|
|
for (PRUint32 index = 0; index < indexCount; index++) {
|
|
|
|
if (info->indexes[index].name == aName) {
|
|
|
|
indexInfo = &(info->indexes[index]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!indexInfo) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:40 -08: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 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:40 -08:00
|
|
|
if (!retval) {
|
|
|
|
retval = IDBIndex::Create(this, indexInfo);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(retval, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-11-10 15:25:40 -08:00
|
|
|
|
|
|
|
if (!mCreatedIndexes.AppendElement(retval)) {
|
|
|
|
NS_WARNING("Out of memory!");
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-11-10 15:25:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval.forget(_retval);
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-11-10 15:25:57 -08:00
|
|
|
IDBObjectStore::DeleteIndex(const nsAString& aName)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
IDBTransaction* transaction = AsyncConnectionHelper::GetCurrentTransaction();
|
|
|
|
|
|
|
|
if (!transaction ||
|
|
|
|
transaction != mTransaction ||
|
|
|
|
mTransaction->Mode() != nsIIDBTransaction::VERSION_CHANGE) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
|
2010-10-19 10:58:52 -07:00
|
|
|
}
|
|
|
|
|
2010-12-15 13:21:17 -08:00
|
|
|
NS_ASSERTION(mTransaction->IsOpen(), "Impossible!");
|
2010-10-19 10:58:52 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
ObjectStoreInfo* info;
|
2011-11-07 16:15:43 -08:00
|
|
|
if (!mTransaction->Database()->Info()->GetObjectStore(mName, &info)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ERROR("This should never fail!");
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
PRUint32 index = 0;
|
|
|
|
for (; index < info->indexes.Length(); index++) {
|
2010-06-23 12:46:08 -07:00
|
|
|
if (info->indexes[index].name == aName) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (index == info->indexes.Length()) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:57 -08:00
|
|
|
nsRefPtr<DeleteIndexHelper> helper =
|
|
|
|
new DeleteIndexHelper(mTransaction, aName, this);
|
2010-11-10 15:25:44 -08:00
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
nsresult rv = helper->DispatchToTransactionPool();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
info->indexes.RemoveElementAt(index);
|
2010-06-23 12:46:08 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_IMETHODIMP
|
2011-11-03 08:57:42 -07:00
|
|
|
IDBObjectStore::Count(const jsval& aKey,
|
2011-11-03 08:57:30 -07:00
|
|
|
JSContext* aCx,
|
|
|
|
PRUint8 aOptionalArgCount,
|
|
|
|
nsIIDBRequest** _retval)
|
|
|
|
{
|
|
|
|
if (!mTransaction->IsOpen()) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
|
|
|
|
}
|
|
|
|
|
2011-11-03 08:57:42 -07: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 08:57:30 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-06-23 12:46:08 -07:00
|
|
|
AddHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
bool mayOverwrite = mOverwrite;
|
|
|
|
bool unsetKey = mKey.IsUnset();
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
bool autoIncrement = mObjectStore->IsAutoIncrement();
|
|
|
|
PRInt64 osid = mObjectStore->Id();
|
|
|
|
const nsString& keyPath = mObjectStore->KeyPath();
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
if (unsetKey) {
|
2010-10-19 10:58:52 -07:00
|
|
|
NS_ASSERTION(autoIncrement, "Must have a key for non-autoIncrement!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
// Will need to add first and then set the key later.
|
|
|
|
mayOverwrite = false;
|
|
|
|
}
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (autoIncrement && !unsetKey) {
|
2010-06-23 12:46:08 -07:00
|
|
|
mayOverwrite = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
if (!mOverwrite && !unsetKey) {
|
|
|
|
// Make sure the key doesn't exist already
|
2011-11-03 08:59:17 -07:00
|
|
|
NS_NAMED_LITERAL_CSTRING(id, "id");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(osidStr, "osid");
|
|
|
|
|
|
|
|
nsCString table;
|
|
|
|
nsCString value;
|
|
|
|
|
|
|
|
if (autoIncrement) {
|
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
value = id;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
value.AssignLiteral("key_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT data FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE ") + value +
|
|
|
|
NS_LITERAL_CSTRING(" = :") + id +
|
|
|
|
NS_LITERAL_CSTRING(" AND object_store_id = :") + osidStr;
|
|
|
|
|
|
|
|
stmt = mTransaction->GetCachedStatement(query);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
rv = stmt->BindInt64ByName(osidStr, osid);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
rv = mKey.BindToStatement(stmt, NS_LITERAL_CSTRING("id"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasResult;
|
2010-06-23 12:46:08 -07:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
if (hasResult) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we add it to the database (or update, depending on our variables).
|
2010-10-19 10:58:52 -07:00
|
|
|
stmt = mTransaction->AddStatement(true, mayOverwrite, autoIncrement);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(keyValue, "key_value");
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), osid);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (!autoIncrement || mayOverwrite) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ASSERTION(!mKey.IsUnset(), "This shouldn't happen!");
|
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
rv = mKey.BindToStatement(stmt, keyValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-11-22 03:54:50 -08:00
|
|
|
const PRUint8* buffer = reinterpret_cast<const PRUint8*>(mCloneBuffer.data());
|
|
|
|
size_t bufferLength = mCloneBuffer.nbytes();
|
2011-11-21 20:18:29 -08:00
|
|
|
|
2011-11-22 03:54:50 -08:00
|
|
|
rv = stmt->BindBlobByName(NS_LITERAL_CSTRING("data"), buffer, bufferLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-08-04 14:44:26 -07:00
|
|
|
rv = stmt->Execute();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-08-26 13:57:28 -07:00
|
|
|
if (mayOverwrite && rv == NS_ERROR_STORAGE_CONSTRAINT) {
|
2010-12-21 08:02:04 -08:00
|
|
|
scoper.Abandon();
|
|
|
|
|
|
|
|
rv = stmt->Reset();
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
stmt = mTransaction->AddStatement(false, true, autoIncrement);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-04 14:44:26 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper2(stmt);
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), osid);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-04 14:44:26 -07:00
|
|
|
|
2010-12-15 13:21:11 -08:00
|
|
|
NS_ASSERTION(!mKey.IsUnset(), "This shouldn't happen!");
|
2010-08-04 14:44:26 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
rv = mKey.BindToStatement(stmt, keyValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-08-04 14:44:26 -07:00
|
|
|
|
2011-11-22 03:54:50 -08:00
|
|
|
rv = stmt->BindBlobByName(NS_LITERAL_CSTRING("data"), buffer,
|
|
|
|
bufferLength);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-04 14:44:26 -07:00
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-08-04 14:44:26 -07:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are supposed to generate a key, get the new id.
|
2010-10-19 10:58:52 -07:00
|
|
|
if (autoIncrement && !mOverwrite) {
|
2010-06-23 12:46:08 -07:00
|
|
|
#ifdef DEBUG
|
2011-11-03 08:57:30 -07:00
|
|
|
PRInt64 oldKey = unsetKey ? 0 : mKey.ToInteger();
|
2010-06-23 12:46:08 -07:00
|
|
|
#endif
|
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
PRInt64 newIntKey;
|
|
|
|
rv = aConnection->GetLastInsertRowID(&newIntKey);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
rv = mKey.SetFromInteger(newIntKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
#ifdef DEBUG
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_ASSERTION(mKey.IsInteger(), "Bad key value!");
|
2010-06-23 12:46:08 -07:00
|
|
|
if (!unsetKey) {
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_ASSERTION(mKey.ToInteger() == oldKey, "Something went haywire!");
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (!keyPath.IsEmpty() && unsetKey) {
|
2010-06-23 12:46:08 -07:00
|
|
|
// Special case where someone put an object into an autoIncrement'ing
|
|
|
|
// objectStore with no key in its keyPath set. We needed to figure out
|
|
|
|
// which row id we would get above before we could set that properly.
|
2011-06-29 12:09:20 -07:00
|
|
|
rv = mObjectStore->ModifyValueForNewKey(mCloneBuffer, mKey,
|
|
|
|
mOffsetToKeyProp);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
scoper.Abandon();
|
|
|
|
rv = stmt->Reset();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
stmt = mTransaction->AddStatement(false, true, true);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper2(stmt);
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"), osid);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
rv = mKey.BindToStatement(stmt, keyValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-22 03:54:50 -08:00
|
|
|
buffer = reinterpret_cast<const PRUint8*>(mCloneBuffer.data());
|
|
|
|
bufferLength = mCloneBuffer.nbytes();
|
2010-12-21 08:02:04 -08:00
|
|
|
|
2011-11-22 03:54:50 -08:00
|
|
|
rv = stmt->BindBlobByName(NS_LITERAL_CSTRING("data"), buffer,
|
|
|
|
bufferLength);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
rv = stmt->Execute();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update our indexes if needed.
|
2011-11-21 20:18:19 -08:00
|
|
|
if (mOverwrite || !mIndexUpdateInfo.IsEmpty()) {
|
2011-11-03 08:57:30 -07:00
|
|
|
PRInt64 objectDataId = autoIncrement ? mKey.ToInteger() : LL_MININT;
|
2010-10-19 10:58:52 -07:00
|
|
|
rv = IDBObjectStore::UpdateIndexes(mTransaction, osid, mKey,
|
|
|
|
autoIncrement, mOverwrite,
|
2010-06-28 11:51:06 -07:00
|
|
|
objectDataId, mIndexUpdateInfo);
|
2010-06-23 12:46:08 -07:00
|
|
|
if (rv == NS_ERROR_STORAGE_CONSTRAINT) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2011-01-06 22:21:36 -08:00
|
|
|
AddHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-12-15 13:21:07 -08:00
|
|
|
NS_ASSERTION(!mKey.IsUnset(), "Badness!");
|
2011-01-26 17:53:02 -08:00
|
|
|
|
2011-07-12 10:42:24 -07:00
|
|
|
mCloneBuffer.clear();
|
2011-01-26 17:53:02 -08:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
return mKey.ToJSVal(aCx, aVal);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2011-11-03 08:59:17 -07:00
|
|
|
GetHelper::DoDatabaseWork(mozIStorageConnection* /* aConnection */)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2011-11-03 08:59:17 -07:00
|
|
|
NS_ASSERTION(mKeyRange, "Must have a key range here!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
nsCString table;
|
|
|
|
nsCString value;
|
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
value.AssignLiteral("id");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
value.AssignLiteral("key_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString keyRangeClause;
|
|
|
|
mKeyRange->GetBindingClause(value, keyRangeClause);
|
|
|
|
|
|
|
|
NS_ASSERTION(!keyRangeClause.IsEmpty(), "Huh?!");
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(osid, "osid");
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT data FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :") + osid +
|
|
|
|
keyRangeClause + NS_LITERAL_CSTRING(" LIMIT 1");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
nsresult rv = stmt->BindInt64ByName(osid, mObjectStore->Id());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
rv = mKeyRange->BindToStatement(stmt);
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasResult;
|
2010-06-23 12:46:08 -07:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-12-15 13:21:09 -08:00
|
|
|
if (hasResult) {
|
2010-12-21 08:02:04 -08:00
|
|
|
rv = IDBObjectStore::GetStructuredCloneDataFromStatement(stmt, 0,
|
|
|
|
mCloneBuffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2011-01-06 22:21:36 -08:00
|
|
|
GetHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2011-06-08 19:21:53 -07:00
|
|
|
bool result = IDBObjectStore::DeserializeValue(aCx, mCloneBuffer, aVal);
|
2011-01-26 17:53:02 -08:00
|
|
|
|
2011-07-12 10:42:24 -07:00
|
|
|
mCloneBuffer.clear();
|
2011-01-26 17:53:02 -08:00
|
|
|
|
2011-06-08 19:21:53 -07:00
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
|
2011-01-26 17:53:02 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2011-11-03 08:59:17 -07:00
|
|
|
DeleteHelper::DoDatabaseWork(mozIStorageConnection* /*aConnection */)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2011-11-03 08:59:17 -07:00
|
|
|
NS_ASSERTION(mKeyRange, "Must have a key range here!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
nsCString table;
|
|
|
|
nsCString value;
|
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
value.AssignLiteral("id");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
value.AssignLiteral("key_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString keyRangeClause;
|
|
|
|
mKeyRange->GetBindingClause(value, keyRangeClause);
|
|
|
|
|
|
|
|
NS_ASSERTION(!keyRangeClause.IsEmpty(), "Huh?!");
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(osid, "osid");
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("DELETE FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :") + osid +
|
|
|
|
keyRangeClause;
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
nsresult rv = stmt->BindInt64ByName(osid, mObjectStore->Id());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
rv = mKeyRange->BindToStatement(stmt);
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
rv = stmt->Execute();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2011-01-06 22:21:36 -08:00
|
|
|
DeleteHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2011-11-07 18:08:02 -08:00
|
|
|
*aVal = JSVAL_VOID;
|
2011-11-03 08:59:17 -07:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-08-26 13:57:30 -07:00
|
|
|
ClearHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aConnection, "Passed a null connection!");
|
|
|
|
|
|
|
|
nsCString table;
|
2010-10-19 10:58:52 -07:00
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
2010-08-26 13:57:30 -07:00
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("DELETE FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :osid");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-26 13:57:30 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"),
|
|
|
|
mObjectStore->Id());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-26 13:57:30 -07:00
|
|
|
|
|
|
|
rv = stmt->Execute();
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-08-26 13:57:30 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-08-26 13:57:30 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-06-23 12:46:08 -07:00
|
|
|
OpenCursorHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsCString table;
|
|
|
|
nsCString keyColumn;
|
|
|
|
|
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
keyColumn.AssignLiteral("id");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
keyColumn.AssignLiteral("key_value");
|
|
|
|
}
|
|
|
|
|
2010-12-09 18:15:00 -08:00
|
|
|
NS_NAMED_LITERAL_CSTRING(id, "id");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:59:17 -07:00
|
|
|
nsCString keyRangeClause;
|
2011-11-03 08:57:30 -07:00
|
|
|
if (mKeyRange) {
|
2011-11-03 08:59:17 -07:00
|
|
|
mKeyRange->GetBindingClause(keyColumn, keyRangeClause);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-12-09 18:15:00 -08:00
|
|
|
nsCAutoString directionClause = NS_LITERAL_CSTRING(" ORDER BY ") + keyColumn;
|
2010-06-23 12:46:08 -07:00
|
|
|
switch (mDirection) {
|
|
|
|
case nsIIDBCursor::NEXT:
|
|
|
|
case nsIIDBCursor::NEXT_NO_DUPLICATE:
|
2010-12-09 18:15:00 -08:00
|
|
|
directionClause += NS_LITERAL_CSTRING(" ASC");
|
2010-06-23 12:46:08 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV:
|
|
|
|
case nsIIDBCursor::PREV_NO_DUPLICATE:
|
2010-12-09 18:15:00 -08:00
|
|
|
directionClause += NS_LITERAL_CSTRING(" DESC");
|
2010-06-23 12:46:08 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown direction type!");
|
|
|
|
}
|
|
|
|
|
2010-12-09 18:15:00 -08:00
|
|
|
nsCString firstQuery = NS_LITERAL_CSTRING("SELECT ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(", data FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :") +
|
2011-11-21 20:18:25 -08:00
|
|
|
id + keyRangeClause + directionClause +
|
|
|
|
NS_LITERAL_CSTRING(" LIMIT 1");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-12-09 18:15:00 -08:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(firstQuery);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2010-12-09 18:15:00 -08:00
|
|
|
nsresult rv = stmt->BindInt64ByName(id, mObjectStore->Id());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
if (mKeyRange) {
|
2011-11-03 08:59:17 -07:00
|
|
|
rv = mKeyRange->BindToStatement(stmt);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasResult;
|
2010-12-09 18:15:00 -08:00
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-12-09 18:15:00 -08:00
|
|
|
if (!hasResult) {
|
2011-11-03 08:57:30 -07:00
|
|
|
mKey.Unset();
|
2010-12-09 18:15:00 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
rv = mKey.SetFromStatement(stmt, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-12-21 08:02:04 -08:00
|
|
|
rv = IDBObjectStore::GetStructuredCloneDataFromStatement(stmt, 1,
|
|
|
|
mCloneBuffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-12-09 18:15:00 -08: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:
|
|
|
|
AppendConditionClause(keyColumn, currentKey, false, false,
|
|
|
|
keyRangeClause);
|
|
|
|
AppendConditionClause(keyColumn, currentKey, false, true,
|
|
|
|
continueToKeyRangeClause);
|
2011-11-03 08:57:30 -07:00
|
|
|
if (mKeyRange && !mKeyRange->Upper().IsUnset()) {
|
|
|
|
AppendConditionClause(keyColumn, rangeKey, true,
|
|
|
|
!mKeyRange->IsUpperOpen(), keyRangeClause);
|
|
|
|
AppendConditionClause(keyColumn, rangeKey, true,
|
|
|
|
!mKeyRange->IsUpperOpen(),
|
2010-12-09 18:15:00 -08:00
|
|
|
continueToKeyRangeClause);
|
2011-11-03 08:57:30 -07:00
|
|
|
mRangeKey = mKeyRange->Upper();
|
2010-12-09 18:15:00 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIIDBCursor::PREV:
|
|
|
|
case nsIIDBCursor::PREV_NO_DUPLICATE:
|
|
|
|
AppendConditionClause(keyColumn, currentKey, true, false, keyRangeClause);
|
|
|
|
AppendConditionClause(keyColumn, currentKey, true, true,
|
|
|
|
continueToKeyRangeClause);
|
2011-11-03 08:57:30 -07:00
|
|
|
if (mKeyRange && !mKeyRange->Lower().IsUnset()) {
|
|
|
|
AppendConditionClause(keyColumn, rangeKey, false,
|
|
|
|
!mKeyRange->IsLowerOpen(), keyRangeClause);
|
|
|
|
AppendConditionClause(keyColumn, rangeKey, false,
|
|
|
|
!mKeyRange->IsLowerOpen(),
|
2010-12-09 18:15:00 -08:00
|
|
|
continueToKeyRangeClause);
|
2011-11-03 08:57:30 -07:00
|
|
|
mRangeKey = mKeyRange->Lower();
|
2010-12-09 18:15:00 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown direction type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
mContinueQuery = NS_LITERAL_CSTRING("SELECT ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(", data FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :") + id +
|
|
|
|
keyRangeClause + directionClause +
|
2011-11-21 20:18:25 -08:00
|
|
|
NS_LITERAL_CSTRING(" LIMIT ");
|
2010-12-09 18:15:00 -08:00
|
|
|
|
|
|
|
mContinueToQuery = NS_LITERAL_CSTRING("SELECT ") + keyColumn +
|
|
|
|
NS_LITERAL_CSTRING(", data FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :") + id +
|
|
|
|
continueToKeyRangeClause + directionClause +
|
2011-11-07 15:37:19 -08:00
|
|
|
NS_LITERAL_CSTRING(" LIMIT ");
|
2010-12-09 18:15:00 -08:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2011-01-06 22:21:36 -08:00
|
|
|
OpenCursorHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-12-09 18:15:00 -08:00
|
|
|
if (mKey.IsUnset()) {
|
2011-01-06 22:21:36 -08:00
|
|
|
*aVal = JSVAL_VOID;
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-06-28 11:51:06 -07:00
|
|
|
nsRefPtr<IDBCursor> cursor =
|
2010-12-09 18:15:00 -08:00
|
|
|
IDBCursor::Create(mRequest, mTransaction, mObjectStore, mDirection,
|
|
|
|
mRangeKey, mContinueQuery, mContinueToQuery, mKey,
|
2010-12-21 08:02:04 -08:00
|
|
|
mCloneBuffer);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(cursor, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-01-26 17:53:02 -08:00
|
|
|
NS_ASSERTION(!mCloneBuffer.data(), "Should have swapped!");
|
|
|
|
|
2011-01-06 22:21:36 -08:00
|
|
|
return WrapNative(aCx, cursor, aVal);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-07-25 15:41:50 -07:00
|
|
|
class ThreadLocalJSRuntime
|
|
|
|
{
|
|
|
|
JSRuntime* mRuntime;
|
|
|
|
JSContext* mContext;
|
|
|
|
JSObject* mGlobal;
|
|
|
|
|
|
|
|
static JSClass sGlobalClass;
|
2011-09-20 14:47:14 -07:00
|
|
|
static const unsigned sRuntimeHeapSize = 256 * 1024; // should be enough for anyone
|
2011-07-25 15:41:50 -07: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-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-06-23 12:46:08 -07:00
|
|
|
CreateIndexHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
// Insert the data into the database.
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(
|
2010-10-19 10:58:52 -07:00
|
|
|
"INSERT INTO object_store_index (id, name, key_path, unique_index, "
|
2011-12-04 09:39:01 -08:00
|
|
|
"multientry, object_store_id, object_store_autoincrement) "
|
|
|
|
"VALUES (:id, :name, :key_path, :unique, :multientry, :osid, "
|
|
|
|
":os_auto_increment)"
|
2010-06-23 12:46:08 -07:00
|
|
|
);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("id"),
|
|
|
|
mIndex->Id());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("name"), mIndex->Name());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-10-19 10:58:52 -07:00
|
|
|
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("key_path"),
|
|
|
|
mIndex->KeyPath());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("unique"),
|
2010-10-19 10:58:52 -07:00
|
|
|
mIndex->IsUnique() ? 1 : 0);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08: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 10:58:52 -07:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"),
|
|
|
|
mIndex->ObjectStore()->Id());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("os_auto_increment"),
|
2010-10-19 10:58:52 -07:00
|
|
|
mIndex->IsAutoIncrement() ? 1 : 0);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(stmt->Execute())) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-01-23 10:16:00 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
PRInt64 id;
|
|
|
|
aConnection->GetLastInsertRowID(&id);
|
|
|
|
NS_ASSERTION(mIndex->Id() == id, "Bad index id!");
|
|
|
|
}
|
|
|
|
#endif
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
// Now we need to populate the index with data from the object store.
|
2010-11-10 15:25:44 -08:00
|
|
|
rv = InsertDataFromObjectStore(aConnection);
|
2010-12-15 13:21:11 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-06-23 12:46:08 -07:00
|
|
|
CreateIndexHelper::InsertDataFromObjectStore(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsCAutoString table;
|
|
|
|
nsCAutoString columns;
|
2011-01-23 10:16:00 -08:00
|
|
|
if (mIndex->IsAutoIncrement()) {
|
2010-06-23 12:46:08 -07:00
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
columns.AssignLiteral("id, data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
columns.AssignLiteral("id, data, key_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT ") + columns +
|
|
|
|
NS_LITERAL_CSTRING(" FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :osid");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("osid"),
|
2010-10-19 10:58:52 -07:00
|
|
|
mIndex->ObjectStore()->Id());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
NS_ENSURE_TRUE(sTLSIndex != BAD_TLS_INDEX, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08: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 10:16:00 -08:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
ThreadLocalJSRuntime* tlsEntry =
|
|
|
|
reinterpret_cast<ThreadLocalJSRuntime*>(PR_GetThreadPrivate(sTLSIndex));
|
2011-12-02 19:46:25 -08:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
if (!tlsEntry) {
|
|
|
|
tlsEntry = ThreadLocalJSRuntime::Create();
|
|
|
|
NS_ENSURE_TRUE(tlsEntry, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2011-11-02 18:57:48 -07:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
PR_SetThreadPrivate(sTLSIndex, tlsEntry);
|
|
|
|
}
|
2011-12-02 19:46:25 -08:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
JSContext* cx = tlsEntry->Context();
|
|
|
|
JSAutoRequest ar(cx);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
do {
|
2011-11-22 03:54:50 -08:00
|
|
|
const PRUint8* data;
|
|
|
|
PRUint32 dataLength;
|
|
|
|
rv = stmt->GetSharedBlob(1, &dataLength, &data);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
jsval clone;
|
|
|
|
if (!JS_ReadStructuredClone(cx, reinterpret_cast<const uint64*>(data),
|
|
|
|
dataLength, JS_STRUCTURED_CLONE_VERSION,
|
|
|
|
&clone, NULL, NULL)) {
|
|
|
|
return NS_ERROR_DOM_DATA_CLONE_ERR;
|
2011-12-02 18:32:46 -08:00
|
|
|
}
|
2011-12-02 19:46:25 -08:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
nsTArray<IndexUpdateInfo> updateInfo;
|
|
|
|
rv = IDBObjectStore::AppendIndexUpdateInfo(mIndex->Id(),
|
|
|
|
mIndex->KeyPath(),
|
|
|
|
mIndex->IsUnique(),
|
|
|
|
mIndex->IsMultiEntry(),
|
|
|
|
tlsEntry->Context(),
|
|
|
|
clone, updateInfo);
|
2011-12-02 19:46:25 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
PRInt64 objectDataID = stmt->AsInt64(0);
|
|
|
|
|
|
|
|
Key key;
|
|
|
|
if (!mIndex->IsAutoIncrement()) {
|
|
|
|
rv = key.SetFromStatement(stmt, 2);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
key.SetFromInteger(objectDataID);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
rv = IDBObjectStore::UpdateIndexes(mTransaction, mIndex->Id(),
|
|
|
|
key, mIndex->IsAutoIncrement(),
|
|
|
|
false, objectDataID, updateInfo);
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-12-04 09:39:01 -08:00
|
|
|
} while (NS_SUCCEEDED(rv = stmt->ExecuteStep(&hasResult)) && hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-11-10 15:25:57 -08:00
|
|
|
DeleteIndexHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mTransaction->GetCachedStatement(
|
|
|
|
"DELETE FROM object_store_index "
|
|
|
|
"WHERE name = :name "
|
|
|
|
);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindStringByName(NS_LITERAL_CSTRING("name"), mName);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(stmt->Execute())) {
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2010-06-23 12:46:08 -07:00
|
|
|
GetAllHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsCString table;
|
|
|
|
nsCString keyColumn;
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
2010-06-23 12:46:08 -07:00
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
keyColumn.AssignLiteral("id");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
keyColumn.AssignLiteral("key_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(osid, "osid");
|
2010-12-09 18:15:00 -08:00
|
|
|
NS_NAMED_LITERAL_CSTRING(lowerKeyName, "lower_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(upperKeyName, "upper_key");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
nsCAutoString keyRangeClause;
|
2011-11-03 08:57:30 -07:00
|
|
|
if (mKeyRange) {
|
|
|
|
if (!mKeyRange->Lower().IsUnset()) {
|
|
|
|
keyRangeClause = NS_LITERAL_CSTRING(" AND ") + keyColumn;
|
|
|
|
if (mKeyRange->IsLowerOpen()) {
|
|
|
|
keyRangeClause.AppendLiteral(" > :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" >= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(lowerKeyName);
|
2011-11-02 18:57:48 -07:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
if (!mKeyRange->Upper().IsUnset()) {
|
|
|
|
keyRangeClause += NS_LITERAL_CSTRING(" AND ") + keyColumn;
|
|
|
|
if (mKeyRange->IsUpperOpen()) {
|
|
|
|
keyRangeClause.AppendLiteral(" < :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" <= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(upperKeyName);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString limitClause;
|
|
|
|
if (mLimit != PR_UINT32_MAX) {
|
|
|
|
limitClause.AssignLiteral(" LIMIT ");
|
|
|
|
limitClause.AppendInt(mLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT data FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :") + osid +
|
|
|
|
keyRangeClause + NS_LITERAL_CSTRING(" ORDER BY ") +
|
|
|
|
keyColumn + NS_LITERAL_CSTRING(" ASC") + limitClause;
|
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
mCloneBuffers.SetCapacity(50);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
2010-10-19 10:58:52 -07:00
|
|
|
nsresult rv = stmt->BindInt64ByName(osid, mObjectStore->Id());
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
if (mKeyRange) {
|
|
|
|
if (!mKeyRange->Lower().IsUnset()) {
|
|
|
|
rv = mKeyRange->Lower().BindToStatement(stmt, lowerKeyName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-02 18:57:48 -07:00
|
|
|
}
|
2011-11-03 08:57:30 -07:00
|
|
|
if (!mKeyRange->Upper().IsUnset()) {
|
|
|
|
rv = mKeyRange->Upper().BindToStatement(stmt, upperKeyName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-02 18:57:48 -07:00
|
|
|
}
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasResult;
|
2010-06-23 12:46:08 -07:00
|
|
|
while (NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
|
2010-12-21 08:02:04 -08:00
|
|
|
if (mCloneBuffers.Capacity() == mCloneBuffers.Length()) {
|
|
|
|
if (!mCloneBuffers.SetCapacity(mCloneBuffers.Capacity() * 2)) {
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_ERROR("Out of memory!");
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-21 08:02:04 -08:00
|
|
|
JSAutoStructuredCloneBuffer* buffer = mCloneBuffers.AppendElement();
|
|
|
|
NS_ASSERTION(buffer, "Shouldn't fail if SetCapacity succeeded!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-12-21 08:02:04 -08:00
|
|
|
rv = IDBObjectStore::GetStructuredCloneDataFromStatement(stmt, 0, *buffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
2010-11-10 15:25:44 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 15:25:44 -08:00
|
|
|
nsresult
|
2011-01-06 22:21:36 -08:00
|
|
|
GetAllHelper::GetSuccessResult(JSContext* aCx,
|
|
|
|
jsval* aVal)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2010-12-21 08:02:04 -08:00
|
|
|
NS_ASSERTION(mCloneBuffers.Length() <= mLimit, "Too many results!");
|
2011-01-26 17:53:02 -08:00
|
|
|
|
|
|
|
nsresult rv = ConvertCloneBuffersToArray(aCx, mCloneBuffers, aVal);
|
|
|
|
|
|
|
|
for (PRUint32 index = 0; index < mCloneBuffers.Length(); index++) {
|
2011-07-12 10:42:24 -07:00
|
|
|
mCloneBuffers[index].clear();
|
2011-01-26 17:53:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
2011-11-03 08:57:42 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
CountHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
|
|
|
|
{
|
|
|
|
nsCString table;
|
|
|
|
nsCString keyColumn;
|
|
|
|
|
|
|
|
if (mObjectStore->IsAutoIncrement()) {
|
|
|
|
table.AssignLiteral("ai_object_data");
|
|
|
|
keyColumn.AssignLiteral("id");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
table.AssignLiteral("object_data");
|
|
|
|
keyColumn.AssignLiteral("key_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(osid, "osid");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(lowerKeyName, "lower_key");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(upperKeyName, "upper_key");
|
|
|
|
|
|
|
|
nsCAutoString keyRangeClause;
|
|
|
|
if (mKeyRange) {
|
|
|
|
if (!mKeyRange->Lower().IsUnset()) {
|
|
|
|
keyRangeClause = NS_LITERAL_CSTRING(" AND ") + keyColumn;
|
|
|
|
if (mKeyRange->IsLowerOpen()) {
|
|
|
|
keyRangeClause.AppendLiteral(" > :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" >= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(lowerKeyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mKeyRange->Upper().IsUnset()) {
|
|
|
|
keyRangeClause += NS_LITERAL_CSTRING(" AND ") + keyColumn;
|
|
|
|
if (mKeyRange->IsUpperOpen()) {
|
|
|
|
keyRangeClause.AppendLiteral(" < :");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keyRangeClause.AppendLiteral(" <= :");
|
|
|
|
}
|
|
|
|
keyRangeClause.Append(upperKeyName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString query = NS_LITERAL_CSTRING("SELECT count(*) FROM ") + table +
|
|
|
|
NS_LITERAL_CSTRING(" WHERE object_store_id = :") + osid +
|
|
|
|
keyRangeClause;
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
|
|
|
|
NS_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
nsresult rv = stmt->BindInt64ByName(osid, mObjectStore->Id());
|
|
|
|
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);
|
|
|
|
}
|