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 unsigned short direction = IDBCursor::stringToDirection(directionString, ec);
72 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
73 request->setCursorType(IDBCursorBackendInterface::IndexCursor);
74 m_backend->openCursor(keyRange, direction, request, m_transaction->backend(), ec);
76 request->markEarlyDeath();
82 PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, ExceptionCode& ec)
84 IDB_TRACE("IDBIndex::openCursor");
85 DEFINE_STATIC_LOCAL(String, consoleMessage, ("Numeric direction values are deprecated in IDBIndex.openCursor. Use \"next\", \"nextunique\", \"prev\", or \"prevunique\"."));
86 context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, consoleMessage);
87 const String& directionString = IDBCursor::directionToString(direction, ec);
90 return openCursor(context, keyRange, directionString, ec);
93 PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, const String& direction, ExceptionCode& ec)
95 IDB_TRACE("IDBIndex::openCursor");
96 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
99 return openCursor(context, keyRange.release(), ec);
102 PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, unsigned short direction, ExceptionCode& ec)
104 IDB_TRACE("IDBIndex::openCursor");
105 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
108 return openCursor(context, keyRange.release(), ec);
111 PassRefPtr<IDBRequest> IDBIndex::count(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, ExceptionCode& ec)
113 IDB_TRACE("IDBIndex::count");
115 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
118 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
119 m_backend->count(keyRange, request, m_transaction->backend(), ec);
121 request->markEarlyDeath();
127 PassRefPtr<IDBRequest> IDBIndex::count(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
129 IDB_TRACE("IDBIndex::count");
130 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
133 return count(context, keyRange.release(), ec);
136 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, const String& directionString, ExceptionCode& ec)
138 IDB_TRACE("IDBIndex::openKeyCursor");
140 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
144 unsigned short direction = IDBCursor::stringToDirection(directionString, ec);
148 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
149 request->setCursorType(IDBCursorBackendInterface::IndexKeyCursor);
150 m_backend->openKeyCursor(keyRange, direction, request, m_transaction->backend(), ec);
152 request->markEarlyDeath();
158 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, ExceptionCode& ec)
160 IDB_TRACE("IDBIndex::openKeyCursor");
161 DEFINE_STATIC_LOCAL(String, consoleMessage, ("Numeric direction values are deprecated in IDBIndex.openKeyCursor. Use \"next\", \"nextunique\", \"prev\", or \"prevunique\"."));
162 context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, consoleMessage);
163 const String& directionString = IDBCursor::directionToString(direction, ec);
166 return openKeyCursor(context, keyRange, directionString, ec);
169 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, const String& direction, ExceptionCode& ec)
171 IDB_TRACE("IDBIndex::openKeyCursor");
172 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
175 return openKeyCursor(context, keyRange.release(), ec);
178 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, unsigned short direction, ExceptionCode& ec)
180 IDB_TRACE("IDBIndex::openKeyCursor");
181 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
184 return openKeyCursor(context, keyRange.release(), ec);
187 PassRefPtr<IDBRequest> IDBIndex::get(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
189 IDB_TRACE("IDBIndex::get");
190 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
193 return get(context, keyRange.release(), ec);
196 PassRefPtr<IDBRequest> IDBIndex::get(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, ExceptionCode& ec)
198 IDB_TRACE("IDBIndex::get");
200 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
204 ec = IDBDatabaseException::DATA_ERR;
208 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
209 m_backend->get(keyRange, request, m_transaction->backend(), ec);
211 request->markEarlyDeath();
217 PassRefPtr<IDBRequest> IDBIndex::getKey(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
219 IDB_TRACE("IDBIndex::getKey");
220 RefPtr<IDBKeyRange> keyRange = IDBKeyRange::only(key, ec);
224 return getKey(context, keyRange.release(), ec);
227 PassRefPtr<IDBRequest> IDBIndex::getKey(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, ExceptionCode& ec)
229 IDB_TRACE("IDBIndex::getKey");
231 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
235 ec = IDBDatabaseException::DATA_ERR;
239 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
240 m_backend->getKey(keyRange, request, m_transaction->backend(), ec);
242 request->markEarlyDeath();
248 } // namespace WebCore
250 #endif // ENABLE(INDEXED_DATABASE)