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;
79 IDBObjectStoreInfo* infoForObjectStore(uint64_t objectStoreIdentifier) final;
80 void deleteBackingStore() final;
82 bool supportsSimultaneousTransactions() final { return false; }
83 bool isEphemeral() final { return false; }
85 void unregisterCursor(SQLiteIDBCursor&);
87 String fullDatabaseDirectory() const;
89 IDBBackingStoreTemporaryFileHandler& temporaryFileHandler() const { return m_temporaryFileHandler; }
91 IDBError getBlobRecordsForObjectStoreRecord(int64_t objectStoreRecord, Vector<String>& blobURLs, Vector<String>& blobFilePaths);
93 static String databaseNameFromEncodedFilename(const String&);
96 String filenameForDatabaseName() const;
97 String fullDatabasePath() const;
99 bool ensureValidRecordsTable();
100 bool ensureValidIndexRecordsTable();
101 bool ensureValidIndexRecordsIndex();
102 bool ensureValidBlobTables();
103 std::unique_ptr<IDBDatabaseInfo> createAndPopulateInitialDatabaseInfo();
104 std::unique_ptr<IDBDatabaseInfo> extractExistingDatabaseInfo();
106 IDBError deleteRecord(SQLiteIDBTransaction&, int64_t objectStoreID, const IDBKeyData&);
107 IDBError uncheckedGetKeyGeneratorValue(int64_t objectStoreID, uint64_t& outValue);
108 IDBError uncheckedSetKeyGeneratorValue(int64_t objectStoreID, uint64_t value);
110 IDBError updateAllIndexesForAddRecord(const IDBObjectStoreInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
111 IDBError updateOneIndexForAddRecord(const IDBIndexInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
112 IDBError uncheckedPutIndexKey(const IDBIndexInfo&, const IDBKeyData& keyValue, const IndexKey&, int64_t recordID);
113 IDBError uncheckedPutIndexRecord(int64_t objectStoreID, int64_t indexID, const IDBKeyData& keyValue, const IDBKeyData& indexKey, int64_t recordID);
114 IDBError uncheckedHasIndexRecord(const IDBIndexInfo&, const IDBKeyData&, bool& hasRecord);
115 IDBError uncheckedGetIndexRecordForOneKey(int64_t indexeID, int64_t objectStoreID, IndexedDB::IndexRecordType, const IDBKeyData&, IDBGetResult&);
117 IDBError deleteUnusedBlobFileRecords(SQLiteIDBTransaction&);
119 IDBError getAllObjectStoreRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
120 IDBError getAllIndexRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
122 void closeSQLiteDB();
124 enum class SQL : size_t {
125 CreateObjectStoreInfo,
126 CreateObjectStoreKeyGenerator,
127 DeleteObjectStoreInfo,
128 DeleteObjectStoreKeyGenerator,
129 DeleteObjectStoreRecords,
130 DeleteObjectStoreIndexInfo,
131 DeleteObjectStoreIndexRecords,
132 DeleteObjectStoreBlobRecords,
134 ClearObjectStoreRecords,
135 ClearObjectStoreIndexRecords,
140 GetIndexRecordForOneKey,
143 KeyExistsInObjectStore,
144 GetUnusedBlobFilenames,
146 GetObjectStoreRecordID,
148 DeleteObjectStoreRecord,
149 DeleteObjectStoreIndexRecord,
150 AddObjectStoreRecord,
152 BlobFilenameForBlobURL,
155 GetKeyGeneratorValue,
156 SetKeyGeneratorValue,
157 GetAllKeyRecordsLowerOpenUpperOpen,
158 GetAllKeyRecordsLowerOpenUpperClosed,
159 GetAllKeyRecordsLowerClosedUpperOpen,
160 GetAllKeyRecordsLowerClosedUpperClosed,
161 GetValueRecordsLowerOpenUpperOpen,
162 GetValueRecordsLowerOpenUpperClosed,
163 GetValueRecordsLowerClosedUpperOpen,
164 GetValueRecordsLowerClosedUpperClosed,
165 GetKeyRecordsLowerOpenUpperOpen,
166 GetKeyRecordsLowerOpenUpperClosed,
167 GetKeyRecordsLowerClosedUpperOpen,
168 GetKeyRecordsLowerClosedUpperClosed,
172 SQLiteStatement* cachedStatement(SQL, const char*);
173 SQLiteStatement* cachedStatementForGetAllObjectStoreRecords(const IDBGetAllRecordsData&);
175 std::unique_ptr<SQLiteStatement> m_cachedStatements[static_cast<int>(SQL::Count)];
178 JSC::JSGlobalObject& globalObject();
181 IDBDatabaseIdentifier m_identifier;
182 std::unique_ptr<IDBDatabaseInfo> m_databaseInfo;
183 std::unique_ptr<IDBDatabaseInfo> m_originalDatabaseInfoBeforeVersionChange;
185 std::unique_ptr<SQLiteDatabase> m_sqliteDB;
187 HashMap<IDBResourceIdentifier, std::unique_ptr<SQLiteIDBTransaction>> m_transactions;
188 HashMap<IDBResourceIdentifier, SQLiteIDBCursor*> m_cursors;
190 String m_absoluteDatabaseDirectory;
192 RefPtr<JSC::VM> m_vm;
193 JSC::Strong<JSC::JSGlobalObject> m_globalObject;
195 IDBBackingStoreTemporaryFileHandler& m_temporaryFileHandler;
198 } // namespace IDBServer
199 } // namespace WebCore
201 #endif // ENABLE(INDEXED_DATABASE)