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.
27 #include "IDBObjectStore.h"
29 #if ENABLE(INDEXED_DATABASE)
31 #include "DOMStringList.h"
33 #include "IDBDatabaseException.h"
36 #include "IDBKeyPath.h"
37 #include "IDBKeyRange.h"
38 #include "IDBTransaction.h"
39 #include "SerializedScriptValue.h"
40 #include <wtf/UnusedParam.h>
44 static const unsigned short defaultDirection = IDBCursor::NEXT;
46 IDBObjectStore::IDBObjectStore(PassRefPtr<IDBObjectStoreBackendInterface> idbObjectStore, IDBTransaction* transaction)
47 : m_objectStore(idbObjectStore)
48 , m_transaction(transaction)
50 ASSERT(m_objectStore);
51 ASSERT(m_transaction);
52 // We pass a reference to this object before it can be adopted.
53 relaxAdoptionRequirement();
56 String IDBObjectStore::name() const
58 return m_objectStore->name();
61 String IDBObjectStore::keyPath() const
63 return m_objectStore->keyPath();
66 PassRefPtr<DOMStringList> IDBObjectStore::indexNames() const
68 return m_objectStore->indexNames();
71 PassRefPtr<IDBRequest> IDBObjectStore::get(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
73 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
74 m_objectStore->get(key, request, m_transaction->backend(), ec);
76 request->markEarlyDeath();
79 return request.release();
82 PassRefPtr<IDBRequest> IDBObjectStore::add(ScriptExecutionContext* context, PassRefPtr<SerializedScriptValue> value, PassRefPtr<IDBKey> key, ExceptionCode& ec)
84 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
85 m_objectStore->put(value, key, IDBObjectStoreBackendInterface::AddOnly, request, m_transaction->backend(), ec);
87 request->markEarlyDeath();
90 return request.release();
93 PassRefPtr<IDBRequest> IDBObjectStore::put(ScriptExecutionContext* context, PassRefPtr<SerializedScriptValue> value, PassRefPtr<IDBKey> key, ExceptionCode& ec)
95 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
96 m_objectStore->put(value, key, IDBObjectStoreBackendInterface::AddOrUpdate, request, m_transaction->backend(), ec);
98 request->markEarlyDeath();
101 return request.release();
104 PassRefPtr<IDBRequest> IDBObjectStore::deleteFunction(ScriptExecutionContext* context, PassRefPtr<IDBKey> key, ExceptionCode& ec)
106 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
107 m_objectStore->deleteFunction(key, request, m_transaction->backend(), ec);
109 request->markEarlyDeath();
112 return request.release();
115 PassRefPtr<IDBRequest> IDBObjectStore::clear(ScriptExecutionContext* context, ExceptionCode& ec)
117 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
118 m_objectStore->clear(request, m_transaction->backend(), ec);
120 request->markEarlyDeath();
123 return request.release();
126 PassRefPtr<IDBIndex> IDBObjectStore::createIndex(const String& name, const String& keyPath, const OptionsObject& options, ExceptionCode& ec)
128 if (!IDBIsValidKeyPath(keyPath)) {
129 ec = IDBDatabaseException::NON_TRANSIENT_ERR;
134 options.getKeyBool("unique", unique);
136 RefPtr<IDBIndexBackendInterface> index = m_objectStore->createIndex(name, keyPath, unique, m_transaction->backend(), ec);
137 ASSERT(!index != !ec); // If we didn't get an index, we should have gotten an exception code. And vice versa.
140 return IDBIndex::create(index.release(), this, m_transaction.get());
143 PassRefPtr<IDBIndex> IDBObjectStore::index(const String& name, ExceptionCode& ec)
145 RefPtr<IDBIndexBackendInterface> index = m_objectStore->index(name, ec);
146 ASSERT(!index != !ec); // If we didn't get an index, we should have gotten an exception code. And vice versa.
149 return IDBIndex::create(index.release(), this, m_transaction.get());
152 void IDBObjectStore::deleteIndex(const String& name, ExceptionCode& ec)
154 m_objectStore->deleteIndex(name, m_transaction->backend(), ec);
157 PassRefPtr<IDBRequest> IDBObjectStore::openCursor(ScriptExecutionContext* context, PassRefPtr<IDBKeyRange> range, unsigned short direction, ExceptionCode& ec)
159 if (direction != IDBCursor::NEXT && direction != IDBCursor::NEXT_NO_DUPLICATE && direction != IDBCursor::PREV && direction != IDBCursor::PREV_NO_DUPLICATE) {
160 // FIXME: May need to change when specced: http://www.w3.org/Bugs/Public/show_bug.cgi?id=11406
161 ec = IDBDatabaseException::CONSTRAINT_ERR;
165 RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), m_transaction.get());
166 request->setCursorType(IDBCursorBackendInterface::ObjectStoreCursor);
167 m_objectStore->openCursor(range, direction, request, m_transaction->backend(), ec);
169 request->markEarlyDeath();
172 return request.release();
175 } // namespace WebCore
177 #endif // ENABLE(INDEXED_DATABASE)