2 * Copyright (C) 2016 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
28 #if ENABLE(INDEXED_DATABASE)
30 #include "IDBBackingStore.h"
31 #include "IDBDatabaseIdentifier.h"
32 #include "IDBDatabaseInfo.h"
33 #include "IDBResourceIdentifier.h"
34 #include "SQLiteIDBTransaction.h"
35 #include <JavaScriptCore/Strong.h>
36 #include <wtf/HashMap.h>
42 class SQLiteStatement;
46 class SQLiteIDBCursor;
48 class SQLiteIDBBackingStore : public IDBBackingStore {
50 SQLiteIDBBackingStore(const IDBDatabaseIdentifier&, const String& databaseRootDirectory, IDBBackingStoreTemporaryFileHandler&);
52 ~SQLiteIDBBackingStore() final;
54 IDBError getOrEstablishDatabaseInfo(IDBDatabaseInfo&) final;
56 IDBError beginTransaction(const IDBTransactionInfo&) final;
57 IDBError abortTransaction(const IDBResourceIdentifier& transactionIdentifier) final;
58 IDBError commitTransaction(const IDBResourceIdentifier& transactionIdentifier) final;
59 IDBError createObjectStore(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&) final;
60 IDBError deleteObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier) final;
61 IDBError renameObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const String& newName) final;
62 IDBError clearObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier) final;
63 IDBError createIndex(const IDBResourceIdentifier& transactionIdentifier, const IDBIndexInfo&) final;
64 IDBError deleteIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier) final;
65 IDBError renameIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName) final;
66 IDBError keyExistsInObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyData&, bool& keyExists) final;
67 IDBError deleteRange(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&) final;
68 IDBError addRecord(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&, const IDBKeyData&, const IDBValue&) final;
69 IDBError getRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&, IDBGetRecordDataType, IDBGetResult& outValue) final;
70 IDBError getAllRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue) final;
71 IDBError getIndexRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType, const IDBKeyRangeData&, IDBGetResult& outValue) final;
72 IDBError getCount(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData&, uint64_t& outCount) final;
73 IDBError generateKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t& keyNumber) final;
74 IDBError revertGeneratedKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t keyNumber) final;
75 IDBError maybeUpdateKeyGeneratorNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, double newKeyNumber) final;
76 IDBError openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo&, IDBGetResult& outResult) final;
77 IDBError iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBIterateCursorData&, IDBGetResult& outResult) final;
78 bool prefetchCursor(const IDBResourceIdentifier&, const IDBResourceIdentifier&) final;
80 IDBObjectStoreInfo* infoForObjectStore(uint64_t objectStoreIdentifier) final;
81 void deleteBackingStore() final;
83 bool supportsSimultaneousTransactions() final { return false; }
84 bool isEphemeral() final { return false; }
86 void unregisterCursor(SQLiteIDBCursor&);
88 String fullDatabaseDirectory() const;
90 IDBBackingStoreTemporaryFileHandler& temporaryFileHandler() const { return m_temporaryFileHandler; }
92 IDBError getBlobRecordsForObjectStoreRecord(int64_t objectStoreRecord, Vector<String>& blobURLs, Vector<String>& blobFilePaths);
94 static String databaseNameFromEncodedFilename(const String&);
97 String filenameForDatabaseName() const;
98 String fullDatabasePath() const;
100 bool ensureValidRecordsTable();
101 bool ensureValidIndexRecordsTable();
102 bool ensureValidIndexRecordsIndex();
103 bool ensureValidBlobTables();
104 std::unique_ptr<IDBDatabaseInfo> createAndPopulateInitialDatabaseInfo();
105 std::unique_ptr<IDBDatabaseInfo> extractExistingDatabaseInfo();
107 IDBError deleteRecord(SQLiteIDBTransaction&, int64_t objectStoreID, const IDBKeyData&);
108 IDBError uncheckedGetKeyGeneratorValue(int64_t objectStoreID, uint64_t& outValue);
109 IDBError uncheckedSetKeyGeneratorValue(int64_t objectStoreID, uint64_t value);
111 IDBError updateAllIndexesForAddRecord(const IDBObjectStoreInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
112 IDBError updateOneIndexForAddRecord(const IDBIndexInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
113 IDBError uncheckedPutIndexKey(const IDBIndexInfo&, const IDBKeyData& keyValue, const IndexKey&, int64_t recordID);
114 IDBError uncheckedPutIndexRecord(int64_t objectStoreID, int64_t indexID, const IDBKeyData& keyValue, const IDBKeyData& indexKey, int64_t recordID);
115 IDBError uncheckedHasIndexRecord(const IDBIndexInfo&, const IDBKeyData&, bool& hasRecord);
116 IDBError uncheckedGetIndexRecordForOneKey(int64_t indexeID, int64_t objectStoreID, IndexedDB::IndexRecordType, const IDBKeyData&, IDBGetResult&);
118 IDBError deleteUnusedBlobFileRecords(SQLiteIDBTransaction&);
120 IDBError getAllObjectStoreRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
121 IDBError getAllIndexRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
123 void closeSQLiteDB();
125 enum class SQL : size_t {
126 CreateObjectStoreInfo,
127 CreateObjectStoreKeyGenerator,
128 DeleteObjectStoreInfo,
129 DeleteObjectStoreKeyGenerator,
130 DeleteObjectStoreRecords,
131 DeleteObjectStoreIndexInfo,
132 DeleteObjectStoreIndexRecords,
133 DeleteObjectStoreBlobRecords,
135 ClearObjectStoreRecords,
136 ClearObjectStoreIndexRecords,
141 GetIndexRecordForOneKey,
144 KeyExistsInObjectStore,
145 GetUnusedBlobFilenames,
147 GetObjectStoreRecordID,
149 DeleteObjectStoreRecord,
150 DeleteObjectStoreIndexRecord,
151 AddObjectStoreRecord,
153 BlobFilenameForBlobURL,
156 GetKeyGeneratorValue,
157 SetKeyGeneratorValue,
158 GetAllKeyRecordsLowerOpenUpperOpen,
159 GetAllKeyRecordsLowerOpenUpperClosed,
160 GetAllKeyRecordsLowerClosedUpperOpen,
161 GetAllKeyRecordsLowerClosedUpperClosed,
162 GetValueRecordsLowerOpenUpperOpen,
163 GetValueRecordsLowerOpenUpperClosed,
164 GetValueRecordsLowerClosedUpperOpen,
165 GetValueRecordsLowerClosedUpperClosed,
166 GetKeyRecordsLowerOpenUpperOpen,
167 GetKeyRecordsLowerOpenUpperClosed,
168 GetKeyRecordsLowerClosedUpperOpen,
169 GetKeyRecordsLowerClosedUpperClosed,
173 SQLiteStatement* cachedStatement(SQL, const char*);
174 SQLiteStatement* cachedStatementForGetAllObjectStoreRecords(const IDBGetAllRecordsData&);
176 std::unique_ptr<SQLiteStatement> m_cachedStatements[static_cast<int>(SQL::Count)];
179 JSC::JSGlobalObject& globalObject();
182 IDBDatabaseIdentifier m_identifier;
183 std::unique_ptr<IDBDatabaseInfo> m_databaseInfo;
184 std::unique_ptr<IDBDatabaseInfo> m_originalDatabaseInfoBeforeVersionChange;
186 std::unique_ptr<SQLiteDatabase> m_sqliteDB;
188 HashMap<IDBResourceIdentifier, std::unique_ptr<SQLiteIDBTransaction>> m_transactions;
189 HashMap<IDBResourceIdentifier, SQLiteIDBCursor*> m_cursors;
191 String m_absoluteDatabaseDirectory;
193 RefPtr<JSC::VM> m_vm;
194 JSC::Strong<JSC::JSGlobalObject> m_globalObject;
196 IDBBackingStoreTemporaryFileHandler& m_temporaryFileHandler;
199 } // namespace IDBServer
200 } // namespace WebCore
202 #endif // ENABLE(INDEXED_DATABASE)