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 COMPUTER, 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 COMPUTER, 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 "platform/heap/Handle.h" 31 #include "wtf/Threading.h" 32 #include "wtf/ThreadingPrimitives.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 extern const int SQLResultDone; 49 extern const int SQLResultOk; 50 extern const int SQLResultRow; 51 extern const int SQLResultFull; 52 extern const int SQLResultInterrupt; 53 extern const int SQLResultConstraint; 54 55 class SQLiteDatabase { 56 DISALLOW_ALLOCATION(); 57 WTF_MAKE_NONCOPYABLE(SQLiteDatabase); 58 friend class SQLiteTransaction; 59 public: 60 SQLiteDatabase(); 61 ~SQLiteDatabase(); 62 63 bool open(const String& filename, bool forWebSQLDatabase = false); isOpen()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 73 bool tableExists(const String&); 74 int runVacuumCommand(); 75 int runIncrementalVacuumCommand(); 76 transactionInProgress()77 bool transactionInProgress() const { return m_transactionInProgress; } 78 79 int64_t lastInsertRowID(); 80 int lastChanges(); 81 82 void setBusyTimeout(int ms); 83 84 // 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 void setMaximumSize(int64_t); 89 90 // Gets the number of unused bytes in the database file. 91 int64_t freeSpaceSize(); 92 int64_t totalSize(); 93 94 int lastError(); 95 const char* lastErrorMsg(); 96 sqlite3Handle()97 sqlite3* sqlite3Handle() const { 98 ASSERT(m_sharable || currentThread() == m_openingThread || !m_db); 99 return m_db; 100 } 101 102 void setAuthorizer(DatabaseAuthorizer*); 103 databaseMutex()104 Mutex& databaseMutex() { return m_lockingMutex; } 105 bool isAutoCommitOn() const; 106 107 // The SQLite AUTO_VACUUM pragma can be either NONE, FULL, or INCREMENTAL. 108 // NONE - SQLite does not do any vacuuming 109 // FULL - SQLite moves all empty pages to the end of the DB file and truncates 110 // the file to remove those pages after every transaction. This option 111 // requires SQLite to store additional information about each page in 112 // the database file. 113 // INCREMENTAL - SQLite stores extra information for each page in the database 114 // file, but removes the empty pages only when PRAGMA INCREMANTAL_VACUUM 115 // is called. 116 enum AutoVacuumPragma { AutoVacuumNone = 0, AutoVacuumFull = 1, AutoVacuumIncremental = 2 }; 117 bool turnOnIncrementalAutoVacuum(); 118 119 void trace(Visitor*); 120 121 private: 122 static int authorizerFunction(void*, int, const char*, const char*, const char*, const char*); 123 124 void enableAuthorizer(bool enable); 125 126 int pageSize(); 127 128 sqlite3* m_db; 129 int m_pageSize; 130 131 bool m_transactionInProgress; 132 bool m_sharable; 133 134 Mutex m_authorizerLock; 135 RefPtrWillBeMember<DatabaseAuthorizer> m_authorizer; 136 137 Mutex m_lockingMutex; 138 ThreadIdentifier m_openingThread; 139 140 Mutex m_databaseClosingMutex; 141 bool m_interrupted; 142 143 int m_openError; 144 CString m_openErrorMessage; 145 146 int m_lastChangesCount; 147 }; 148 149 } // namespace WebCore 150 151 #endif 152