2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #if ENABLE(INDEXED_DATABASE)
31 #include "IDBCursorBackendInterface.h"
32 #include "IDBDatabaseException.h"
33 #include "IDBIndexBackendInterface.h"
35 #include "IDBKeyRange.h"
36 #include "IDBObjectStore.h"
37 #include "IDBRequest.h"
38 #include "IDBTracing.h"
39 #include "IDBTransaction.h"
43 static const unsigned short defaultDirection = IDBCursor::NEXT;
45 IDBIndex::IDBIndex(const IDBIndexMetadata& metadata, PassRefPtr<IDBIndexBackendInterface> backend, IDBObjectStore* objectStore, IDBTransaction* transaction)
46 : m_metadata(metadata)
48 , m_objectStore(objectStore)
49 , m_transaction(transaction)
53 ASSERT(m_objectStore);
54 ASSERT(m_transaction);
61 PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, const String& directionString, ExceptionCode& ec)
63 IDB_TRACE("IDBIndex::openCursor");
65 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
68 if (!m_transaction->isActive()) {
69 ec = IDBDatabaseException::TRANSACTION_INACTIVE_ERR;
72 unsigned short direction = IDBCursor::stringToDirection(directionString, ec);
76 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
77 request->setCursorType(IDBCursorBackendInterface::IndexCursor);
78 m_backend->openCursor(keyRange, direction, request, m_transaction->backend(), ec);
80 request->markEarlyDeath();
86 PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, ExceptionCode& ec)
88 IDB_TRACE("IDBIndex::openCursor");
89 DEFINE_STATIC_LOCAL(String, consoleMessage, ("Numeric direction values are deprecated in IDBIndex.openCursor. Use \"next\", \"nextunique\", \"prev\", or \"prevunique\"."));
90 context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, consoleMessage);
91 const String& directionString = IDBCursor::directionToString(direction, ec);
94 return openCursor(context, keyRange, directionString, ec);
97 PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, const String& direction, ExceptionCode& ec)
99 IDB_TRACE("IDBIndex::openCursor");
100 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
103 return openCursor(context, keyRange.release(), ec);
106 PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, unsigned short direction, ExceptionCode& ec)
108 IDB_TRACE("IDBIndex::openCursor");
109 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
112 return openCursor(context, keyRange.release(), ec);
115 PassRefPtr<IDBRequest> IDBIndex::count(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, ExceptionCode& ec)
117 IDB_TRACE("IDBIndex::count");
119 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
122 if (!m_transaction->isActive()) {
123 ec = IDBDatabaseException::TRANSACTION_INACTIVE_ERR;
126 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
127 m_backend->count(keyRange, request, m_transaction->backend(), ec);
129 request->markEarlyDeath();
135 PassRefPtr<IDBRequest> IDBIndex::count(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
137 IDB_TRACE("IDBIndex::count");
138 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
141 return count(context, keyRange.release(), ec);
144 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, const String& directionString, ExceptionCode& ec)
146 IDB_TRACE("IDBIndex::openKeyCursor");
148 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
151 if (!m_transaction->isActive()) {
152 ec = IDBDatabaseException::TRANSACTION_INACTIVE_ERR;
156 unsigned short direction = IDBCursor::stringToDirection(directionString, ec);
160 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
161 request->setCursorType(IDBCursorBackendInterface::IndexKeyCursor);
162 m_backend->openKeyCursor(keyRange, direction, request, m_transaction->backend(), ec);
164 request->markEarlyDeath();
170 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, ExceptionCode& ec)
172 IDB_TRACE("IDBIndex::openKeyCursor");
173 DEFINE_STATIC_LOCAL(String, consoleMessage, ("Numeric direction values are deprecated in IDBIndex.openKeyCursor. Use \"next\", \"nextunique\", \"prev\", or \"prevunique\"."));
174 context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, consoleMessage);
175 const String& directionString = IDBCursor::directionToString(direction, ec);
178 return openKeyCursor(context, keyRange, directionString, ec);
181 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, const String& direction, ExceptionCode& ec)
183 IDB_TRACE("IDBIndex::openKeyCursor");
184 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
187 return openKeyCursor(context, keyRange.release(), ec);
190 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, unsigned short direction, ExceptionCode& ec)
192 IDB_TRACE("IDBIndex::openKeyCursor");
193 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
196 return openKeyCursor(context, keyRange.release(), ec);
199 PassRefPtr<IDBRequest> IDBIndex::get(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
201 IDB_TRACE("IDBIndex::get");
202 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
205 return get(context, keyRange.release(), ec);
208 PassRefPtr<IDBRequest> IDBIndex::get(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, ExceptionCode& ec)
210 IDB_TRACE("IDBIndex::get");
212 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
215 if (!m_transaction->isActive()) {
216 ec = IDBDatabaseException::TRANSACTION_INACTIVE_ERR;
220 ec = IDBDatabaseException::DATA_ERR;
224 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
225 m_backend->get(keyRange, request, m_transaction->backend(), ec);
227 request->markEarlyDeath();
233 PassRefPtr<IDBRequest> IDBIndex::getKey(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
235 IDB_TRACE("IDBIndex::getKey");
236 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
240 return getKey(context, keyRange.release(), ec);
243 PassRefPtr<IDBRequest> IDBIndex::getKey(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, ExceptionCode& ec)
245 IDB_TRACE("IDBIndex::getKey");
247 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
250 if (!m_transaction->isActive()) {
251 ec = IDBDatabaseException::TRANSACTION_INACTIVE_ERR;
255 ec = IDBDatabaseException::DATA_ERR;
259 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
260 m_backend->getKey(keyRange, request, m_transaction->backend(), ec);
262 request->markEarlyDeath();
268 } // namespace WebCore
270 #endif // ENABLE(INDEXED_DATABASE)