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#ifndef IDBObjectStore_h
27#define IDBObjectStore_h
28
29#include "Dictionary.h"
30#include "IDBCursor.h"
31#include "IDBDatabaseMetadata.h"
32#include "IDBIndex.h"
33#include "IDBKey.h"
34#include "IDBKeyRange.h"
35#include "IDBRequest.h"
36#include "IDBTransaction.h"
37#include "ScriptWrappable.h"
38#include "SerializedScriptValue.h"
39#include <wtf/PassRefPtr.h>
40#include <wtf/RefCounted.h>
41#include <wtf/RefPtr.h>
42#include <wtf/text/WTFString.h>
43
44#if ENABLE(INDEXED_DATABASE)
45
46namespace WebCore {
47
48class DOMStringList;
49class IDBAny;
50
51class IDBObjectStore : public ScriptWrappable, public RefCounted<IDBObjectStore> {
52public:
53    static PassRefPtr<IDBObjectStore> create(const IDBObjectStoreMetadata& metadata, IDBTransaction* transaction)
54    {
55        return adoptRef(new IDBObjectStore(metadata, transaction));
56    }
57    ~IDBObjectStore() { }
58
59    // Implement the IDBObjectStore IDL
60    int64_t id() const { return m_metadata.id; }
61    const String name() const { return m_metadata.name; }
62    PassRefPtr<IDBAny> keyPathAny() const { return IDBAny::create(m_metadata.keyPath); }
63    const IDBKeyPath keyPath() const { return m_metadata.keyPath; }
64    PassRefPtr<DOMStringList> indexNames() const;
65    PassRefPtr<IDBTransaction> transaction() const { return m_transaction; }
66    bool autoIncrement() const { return m_metadata.autoIncrement; }
67
68    PassRefPtr<IDBRequest> add(JSC::ExecState*, Deprecated::ScriptValue&, ExceptionCode&);
69    PassRefPtr<IDBRequest> put(JSC::ExecState*, Deprecated::ScriptValue&, ExceptionCode&);
70    PassRefPtr<IDBRequest> openCursor(ScriptExecutionContext*, ExceptionCode&);
71    PassRefPtr<IDBRequest> openCursor(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, ExceptionCode&);
72    PassRefPtr<IDBRequest> openCursor(ScriptExecutionContext*, const Deprecated::ScriptValue& key, ExceptionCode&);
73    PassRefPtr<IDBRequest> openCursor(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, const String& direction, ExceptionCode&);
74    PassRefPtr<IDBRequest> openCursor(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, const String& direction, IDBDatabaseBackend::TaskType, ExceptionCode&);
75    PassRefPtr<IDBRequest> openCursor(ScriptExecutionContext*, const Deprecated::ScriptValue& key, const String& direction, ExceptionCode&);
76
77    PassRefPtr<IDBRequest> get(ScriptExecutionContext*, const Deprecated::ScriptValue& key, ExceptionCode&);
78    PassRefPtr<IDBRequest> get(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, ExceptionCode&);
79    PassRefPtr<IDBRequest> add(JSC::ExecState*, Deprecated::ScriptValue&, const Deprecated::ScriptValue& key, ExceptionCode&);
80    PassRefPtr<IDBRequest> put(JSC::ExecState*, Deprecated::ScriptValue&, const Deprecated::ScriptValue& key, ExceptionCode&);
81    PassRefPtr<IDBRequest> deleteFunction(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, ExceptionCode&);
82    PassRefPtr<IDBRequest> deleteFunction(ScriptExecutionContext*, const Deprecated::ScriptValue& key, ExceptionCode&);
83    PassRefPtr<IDBRequest> clear(ScriptExecutionContext*, ExceptionCode&);
84
85    PassRefPtr<IDBIndex> createIndex(ScriptExecutionContext* context, const String& name, const String& keyPath, const Dictionary& options, ExceptionCode& ec) { return createIndex(context, name, IDBKeyPath(keyPath), options, ec); }
86    PassRefPtr<IDBIndex> createIndex(ScriptExecutionContext* context, const String& name, const Vector<String>& keyPath, const Dictionary& options, ExceptionCode& ec) { return createIndex(context, name, IDBKeyPath(keyPath), options, ec); }
87    PassRefPtr<IDBIndex> createIndex(ScriptExecutionContext*, const String& name, const IDBKeyPath&, const Dictionary&, ExceptionCode&);
88    PassRefPtr<IDBIndex> createIndex(ScriptExecutionContext*, const String& name, const IDBKeyPath&, bool unique, bool multiEntry, ExceptionCode&);
89
90    PassRefPtr<IDBIndex> index(const String& name, ExceptionCode&);
91    void deleteIndex(const String& name, ExceptionCode&);
92
93    PassRefPtr<IDBRequest> count(ScriptExecutionContext* context, ExceptionCode& ec) { return count(context, static_cast<IDBKeyRange*>(0), ec); }
94    PassRefPtr<IDBRequest> count(ScriptExecutionContext*, PassRefPtr<IDBKeyRange>, ExceptionCode&);
95    PassRefPtr<IDBRequest> count(ScriptExecutionContext*, const Deprecated::ScriptValue& key, ExceptionCode&);
96
97    PassRefPtr<IDBRequest> put(IDBDatabaseBackend::PutMode, PassRefPtr<IDBAny> source, JSC::ExecState*, Deprecated::ScriptValue&, const Deprecated::ScriptValue& key, ExceptionCode&);
98    PassRefPtr<IDBRequest> put(IDBDatabaseBackend::PutMode, PassRefPtr<IDBAny> source, JSC::ExecState*, Deprecated::ScriptValue&, PassRefPtr<IDBKey>, ExceptionCode&);
99    void markDeleted() { m_deleted = true; }
100    void transactionFinished();
101
102    IDBObjectStoreMetadata metadata() const { return m_metadata; }
103    void setMetadata(const IDBObjectStoreMetadata& metadata) { m_metadata = metadata; }
104
105    typedef Vector<RefPtr<IDBKey>> IndexKeys;
106    typedef HashMap<String, IndexKeys> IndexKeyMap;
107
108    IDBDatabaseBackend* backendDB() const;
109
110private:
111    IDBObjectStore(const IDBObjectStoreMetadata&, IDBTransaction*);
112
113    int64_t findIndexId(const String& name) const;
114    bool containsIndex(const String& name) const
115    {
116        return findIndexId(name) != IDBIndexMetadata::InvalidId;
117    }
118
119    IDBObjectStoreMetadata m_metadata;
120    RefPtr<IDBTransaction> m_transaction;
121    bool m_deleted;
122
123    typedef HashMap<String, RefPtr<IDBIndex>> IDBIndexMap;
124    IDBIndexMap m_indexMap;
125};
126
127} // namespace WebCore
128
129#endif
130
131#endif // IDBObjectStore_h
132