| 1 | /* |
| 2 | * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 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 INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include <functional> |
| 30 | #include <sqlite3.h> |
| 31 | #include <wtf/Lock.h> |
| 32 | #include <wtf/Threading.h> |
| 33 | #include <wtf/text/CString.h> |
| 34 | #include <wtf/text/WTFString.h> |
| 35 | |
| 36 | #if COMPILER(MSVC) |
| 37 | #pragma warning(disable: 4800) |
| 38 | #endif |
| 39 | |
| 40 | struct sqlite3; |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
| 44 | class DatabaseAuthorizer; |
| 45 | class SQLiteStatement; |
| 46 | class SQLiteTransaction; |
| 47 | |
| 48 | class SQLiteDatabase { |
| 49 | WTF_MAKE_FAST_ALLOCATED; |
| 50 | WTF_MAKE_NONCOPYABLE(SQLiteDatabase); |
| 51 | friend class SQLiteTransaction; |
| 52 | public: |
| 53 | WEBCORE_EXPORT SQLiteDatabase(); |
| 54 | WEBCORE_EXPORT ~SQLiteDatabase(); |
| 55 | |
| 56 | enum class OpenMode { ReadOnly, ReadWrite, ReadWriteCreate }; |
| 57 | WEBCORE_EXPORT bool open(const String& filename, OpenMode = OpenMode::ReadWriteCreate); |
| 58 | bool isOpen() const { return m_db; } |
| 59 | WEBCORE_EXPORT void close(); |
| 60 | |
| 61 | void updateLastChangesCount(); |
| 62 | |
| 63 | WEBCORE_EXPORT bool executeCommand(const String&); |
| 64 | bool returnsAtLeastOneResult(const String&); |
| 65 | |
| 66 | WEBCORE_EXPORT bool tableExists(const String&); |
| 67 | void clearAllTables(); |
| 68 | WEBCORE_EXPORT int runVacuumCommand(); |
| 69 | int runIncrementalVacuumCommand(); |
| 70 | |
| 71 | bool transactionInProgress() const { return m_transactionInProgress; } |
| 72 | |
| 73 | // Aborts the current database operation. This is thread safe. |
| 74 | void interrupt(); |
| 75 | |
| 76 | int64_t lastInsertRowID(); |
| 77 | int lastChanges(); |
| 78 | |
| 79 | void setBusyTimeout(int ms); |
| 80 | void setBusyHandler(int(*)(void*, int)); |
| 81 | |
| 82 | void setFullsync(bool); |
| 83 | |
| 84 | // Gets/sets the maximum size in bytes |
| 85 | // Depending on per-database attributes, the size will only be settable in units that are the page size of the database, which is established at creation |
| 86 | // These chunks will never be anything other than 512, 1024, 2048, 4096, 8192, 16384, or 32768 bytes in size. |
| 87 | // setMaximumSize() will round the size down to the next smallest chunk if the passed size doesn't align. |
| 88 | int64_t maximumSize(); |
| 89 | void setMaximumSize(int64_t); |
| 90 | |
| 91 | // Gets the number of unused bytes in the database file. |
| 92 | int64_t freeSpaceSize(); |
| 93 | int64_t totalSize(); |
| 94 | |
| 95 | // The SQLite SYNCHRONOUS pragma can be either FULL, NORMAL, or OFF |
| 96 | // FULL - Any writing calls to the DB block until the data is actually on the disk surface |
| 97 | // NORMAL - SQLite pauses at some critical moments when writing, but much less than FULL |
| 98 | // OFF - Calls return immediately after the data has been passed to disk |
| 99 | enum SynchronousPragma { SyncOff = 0, SyncNormal = 1, SyncFull = 2 }; |
| 100 | void setSynchronous(SynchronousPragma); |
| 101 | |
| 102 | WEBCORE_EXPORT int lastError(); |
| 103 | WEBCORE_EXPORT const char* lastErrorMsg(); |
| 104 | |
| 105 | sqlite3* sqlite3Handle() const |
| 106 | { |
| 107 | #if !PLATFORM(IOS_FAMILY) |
| 108 | ASSERT(m_sharable || m_openingThread == &Thread::current() || !m_db); |
| 109 | #endif |
| 110 | return m_db; |
| 111 | } |
| 112 | |
| 113 | void setAuthorizer(DatabaseAuthorizer&); |
| 114 | |
| 115 | Lock& databaseMutex() { return m_lockingMutex; } |
| 116 | bool isAutoCommitOn() const; |
| 117 | |
| 118 | // The SQLite AUTO_VACUUM pragma can be either NONE, FULL, or INCREMENTAL. |
| 119 | // NONE - SQLite does not do any vacuuming |
| 120 | // FULL - SQLite moves all empty pages to the end of the DB file and truncates |
| 121 | // the file to remove those pages after every transaction. This option |
| 122 | // requires SQLite to store additional information about each page in |
| 123 | // the database file. |
| 124 | // INCREMENTAL - SQLite stores extra information for each page in the database |
| 125 | // file, but removes the empty pages only when PRAGMA INCREMANTAL_VACUUM |
| 126 | // is called. |
| 127 | enum AutoVacuumPragma { AutoVacuumNone = 0, AutoVacuumFull = 1, AutoVacuumIncremental = 2 }; |
| 128 | bool turnOnIncrementalAutoVacuum(); |
| 129 | |
| 130 | WEBCORE_EXPORT void setCollationFunction(const String& collationName, WTF::Function<int(int, const void*, int, const void*)>&&); |
| 131 | void removeCollationFunction(const String& collationName); |
| 132 | |
| 133 | // Set this flag to allow access from multiple threads. Not all multi-threaded accesses are safe! |
| 134 | // See http://www.sqlite.org/cvstrac/wiki?p=MultiThreading for more info. |
| 135 | #ifndef NDEBUG |
| 136 | WEBCORE_EXPORT void disableThreadingChecks(); |
| 137 | #else |
| 138 | WEBCORE_EXPORT void disableThreadingChecks() {} |
| 139 | #endif |
| 140 | |
| 141 | WEBCORE_EXPORT static void setIsDatabaseOpeningForbidden(bool); |
| 142 | |
| 143 | private: |
| 144 | static int authorizerFunction(void*, int, const char*, const char*, const char*, const char*); |
| 145 | |
| 146 | void enableAuthorizer(bool enable); |
| 147 | void useWALJournalMode(); |
| 148 | |
| 149 | int pageSize(); |
| 150 | |
| 151 | void overrideUnauthorizedFunctions(); |
| 152 | |
| 153 | sqlite3* m_db { nullptr }; |
| 154 | int m_pageSize { -1 }; |
| 155 | |
| 156 | bool m_transactionInProgress { false }; |
| 157 | #ifndef NDEBUG |
| 158 | bool m_sharable { false }; |
| 159 | #endif |
| 160 | |
| 161 | bool m_useWAL { false }; |
| 162 | |
| 163 | Lock m_authorizerLock; |
| 164 | RefPtr<DatabaseAuthorizer> m_authorizer; |
| 165 | |
| 166 | Lock m_lockingMutex; |
| 167 | RefPtr<Thread> m_openingThread { nullptr }; |
| 168 | |
| 169 | Lock m_databaseClosingMutex; |
| 170 | |
| 171 | int m_openError { SQLITE_ERROR }; |
| 172 | CString m_openErrorMessage; |
| 173 | |
| 174 | int m_lastChangesCount { 0 }; |
| 175 | }; |
| 176 | |
| 177 | } // namespace WebCore |
| 178 | |