1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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.
13 *
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.
24 */
25
26 #include "config.h"
27 #include "IDBIndex.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "IDBCursorBackendInterface.h"
32 #include "IDBDatabaseException.h"
33 #include "IDBIndexBackendInterface.h"
34 #include "IDBKey.h"
35 #include "IDBKeyRange.h"
36 #include "IDBObjectStore.h"
37 #include "IDBRequest.h"
38 #include "IDBTransaction.h"
39
40 namespace WebCore {
41
42 static const unsigned short defaultDirection = IDBCursor::NEXT;
43
IDBIndex(PassRefPtr<IDBIndexBackendInterface> backend,IDBObjectStore * objectStore,IDBTransaction * transaction)44 IDBIndex::IDBIndex(PassRefPtr<IDBIndexBackendInterface> backend, IDBObjectStore* objectStore, IDBTransaction* transaction)
45 : m_backend(backend)
46 , m_objectStore(objectStore)
47 , m_transaction(transaction)
48 {
49 ASSERT(m_backend);
50 ASSERT(m_objectStore);
51 ASSERT(m_transaction);
52 }
53
~IDBIndex()54 IDBIndex::~IDBIndex()
55 {
56 }
57
openCursor(ScriptExecutionContext * context,PassRefPtr<IDBKeyRange> keyRange,unsigned short direction,ExceptionCode & ec)58 PassRefPtr<IDBRequest> IDBIndex::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, ExceptionCode& ec)
59 {
60 if (direction != IDBCursor::NEXT && direction != IDBCursor::NEXT_NO_DUPLICATE && direction != IDBCursor::PREV && direction != IDBCursor::PREV_NO_DUPLICATE) {
61 // FIXME: May need to change when specced: http://www.w3.org/Bugs/Public/show_bug.cgi?id=11406
62 ec = IDBDatabaseException::CONSTRAINT_ERR;
63 return 0;
64 }
65
66 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
67 request->setCursorType(IDBCursorBackendInterface::IndexCursor);
68 m_backend->openCursor(keyRange, direction, request, m_transaction->backend(), ec);
69 if (ec) {
70 request->markEarlyDeath();
71 return 0;
72 }
73 return request;
74 }
75
openKeyCursor(ScriptExecutionContext * context,PassRefPtr<IDBKeyRange> keyRange,unsigned short direction,ExceptionCode & ec)76 PassRefPtr<IDBRequest> IDBIndex::openKeyCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> keyRange, unsigned short direction, ExceptionCode& ec)
77 {
78 if (direction != IDBCursor::NEXT && direction != IDBCursor::NEXT_NO_DUPLICATE && direction != IDBCursor::PREV && direction != IDBCursor::PREV_NO_DUPLICATE) {
79 // FIXME: May need to change when specced: http://www.w3.org/Bugs/Public/show_bug.cgi?id=11406
80 ec = IDBDatabaseException::CONSTRAINT_ERR;
81 return 0;
82 }
83
84 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
85 request->setCursorType(IDBCursorBackendInterface::IndexKeyCursor);
86 m_backend->openKeyCursor(keyRange, direction, request, m_transaction->backend(), ec);
87 if (ec) {
88 request->markEarlyDeath();
89 return 0;
90 }
91 return request;
92 }
93
get(ScriptExecutionContext * context,PassRefPtr<IDBKey> key,ExceptionCode & ec)94 PassRefPtr<IDBRequest> IDBIndex::get(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
95 {
96 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
97 m_backend->get(key, request, m_transaction->backend(), ec);
98 if (ec) {
99 request->markEarlyDeath();
100 return 0;
101 }
102 return request;
103 }
104
getKey(ScriptExecutionContext * context,PassRefPtr<IDBKey> key,ExceptionCode & ec)105 PassRefPtr<IDBRequest> IDBIndex::getKey(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
106 {
107 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
108 m_backend->getKey(key, request, m_transaction->backend(), ec);
109 if (ec) {
110 request->markEarlyDeath();
111 return 0;
112 }
113 return request;
114 }
115
116 } // namespace WebCore
117
118 #endif // ENABLE(INDEXED_DATABASE)
119