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#ifndef SQLiteDatabase_h 28#define SQLiteDatabase_h 29 30#include <functional> 31#include <wtf/Threading.h> 32#include <wtf/text/CString.h> 33#include <wtf/text/WTFString.h> 34 35#if COMPILER(MSVC) 36#pragma warning(disable: 4800) 37#endif 38 39struct sqlite3; 40 41namespace WebCore { 42 43class DatabaseAuthorizer; 44class SQLiteStatement; 45class SQLiteTransaction; 46 47extern const int SQLResultDone; 48extern const int SQLResultError; 49extern const int SQLResultOk; 50extern const int SQLResultRow; 51extern const int SQLResultSchema; 52extern const int SQLResultFull; 53extern const int SQLResultInterrupt; 54extern const int SQLResultConstraint; 55 56class SQLiteDatabase { 57 WTF_MAKE_NONCOPYABLE(SQLiteDatabase); 58 friend class SQLiteTransaction; 59public: 60 SQLiteDatabase(); 61 ~SQLiteDatabase(); 62 63 bool open(const String& filename, bool forWebSQLDatabase = false); 64 bool isOpen() const { return m_db; } 65 void close(); 66 void interrupt(); 67 bool isInterrupted(); 68 69 void updateLastChangesCount(); 70 71 bool executeCommand(const String&); 72 bool returnsAtLeastOneResult(const String&); 73 74 bool tableExists(const String&); 75 void clearAllTables(); 76 int runVacuumCommand(); 77 int runIncrementalVacuumCommand(); 78 79 bool transactionInProgress() const { return m_transactionInProgress; } 80 81 int64_t lastInsertRowID(); 82 int lastChanges(); 83 84 void setBusyTimeout(int ms); 85 void setBusyHandler(int(*)(void*, int)); 86 87 void setFullsync(bool); 88 89 // Gets/sets the maximum size in bytes 90 // 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 91 // These chunks will never be anything other than 512, 1024, 2048, 4096, 8192, 16384, or 32768 bytes in size. 92 // setMaximumSize() will round the size down to the next smallest chunk if the passed size doesn't align. 93 int64_t maximumSize(); 94 void setMaximumSize(int64_t); 95 96 // Gets the number of unused bytes in the database file. 97 int64_t freeSpaceSize(); 98 int64_t totalSize(); 99 100 // The SQLite SYNCHRONOUS pragma can be either FULL, NORMAL, or OFF 101 // FULL - Any writing calls to the DB block until the data is actually on the disk surface 102 // NORMAL - SQLite pauses at some critical moments when writing, but much less than FULL 103 // OFF - Calls return immediately after the data has been passed to disk 104 enum SynchronousPragma { SyncOff = 0, SyncNormal = 1, SyncFull = 2 }; 105 void setSynchronous(SynchronousPragma); 106 107 int lastError(); 108 const char* lastErrorMsg(); 109 110 sqlite3* sqlite3Handle() const { 111#if !PLATFORM(IOS) 112 ASSERT(m_sharable || currentThread() == m_openingThread || !m_db); 113#endif 114 return m_db; 115 } 116 117 void setAuthorizer(PassRefPtr<DatabaseAuthorizer>); 118 119 Mutex& databaseMutex() { return m_lockingMutex; } 120 bool isAutoCommitOn() const; 121 122 // The SQLite AUTO_VACUUM pragma can be either NONE, FULL, or INCREMENTAL. 123 // NONE - SQLite does not do any vacuuming 124 // FULL - SQLite moves all empty pages to the end of the DB file and truncates 125 // the file to remove those pages after every transaction. This option 126 // requires SQLite to store additional information about each page in 127 // the database file. 128 // INCREMENTAL - SQLite stores extra information for each page in the database 129 // file, but removes the empty pages only when PRAGMA INCREMANTAL_VACUUM 130 // is called. 131 enum AutoVacuumPragma { AutoVacuumNone = 0, AutoVacuumFull = 1, AutoVacuumIncremental = 2 }; 132 bool turnOnIncrementalAutoVacuum(); 133 134 void setCollationFunction(const String& collationName, std::function<int(int, const void*, int, const void*)>); 135 void removeCollationFunction(const String& collationName); 136 137 // Set this flag to allow access from multiple threads. Not all multi-threaded accesses are safe! 138 // See http://www.sqlite.org/cvstrac/wiki?p=MultiThreading for more info. 139#ifndef NDEBUG 140 void disableThreadingChecks(); 141#else 142 void disableThreadingChecks() {} 143#endif 144 145private: 146 static int authorizerFunction(void*, int, const char*, const char*, const char*, const char*); 147 148 void enableAuthorizer(bool enable); 149 150 int pageSize(); 151 152 sqlite3* m_db; 153 int m_pageSize; 154 155 bool m_transactionInProgress; 156 bool m_sharable; 157 158 Mutex m_authorizerLock; 159 RefPtr<DatabaseAuthorizer> m_authorizer; 160 161 Mutex m_lockingMutex; 162 ThreadIdentifier m_openingThread; 163 164 Mutex m_databaseClosingMutex; 165 bool m_interrupted; 166 167 int m_openError; 168 CString m_openErrorMessage; 169 170 int m_lastChangesCount; 171}; 172 173} // namespace WebCore 174 175#endif 176