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 SQLDatabase_h 28 #define SQLDatabase_h 29 30 #include "PlatformString.h" 31 #include <wtf/Threading.h> 32 33 #if COMPILER(MSVC) 34 #pragma warning(disable: 4800) 35 #endif 36 37 struct sqlite3; 38 39 namespace WebCore { 40 41 class DatabaseAuthorizer; 42 class SQLiteStatement; 43 class SQLiteTransaction; 44 45 extern const int SQLResultDone; 46 extern const int SQLResultError; 47 extern const int SQLResultOk; 48 extern const int SQLResultRow; 49 extern const int SQLResultSchema; 50 extern const int SQLResultFull; 51 52 class SQLiteDatabase : public Noncopyable { 53 friend class SQLiteTransaction; 54 public: 55 SQLiteDatabase(); 56 ~SQLiteDatabase(); 57 58 bool open(const String& filename); isOpen()59 bool isOpen() const { return m_db; } 60 void close(); 61 62 bool executeCommand(const String&); 63 bool returnsAtLeastOneResult(const String&); 64 65 bool tableExists(const String&); 66 void clearAllTables(); 67 void runVacuumCommand(); 68 transactionInProgress()69 bool transactionInProgress() const { return m_transactionInProgress; } 70 71 int64_t lastInsertRowID(); 72 int lastChanges(); 73 74 void setBusyTimeout(int ms); 75 void setBusyHandler(int(*)(void*, int)); 76 77 void setFullsync(bool); 78 79 // Gets/sets the maximum size in bytes 80 // 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 81 // These chunks will never be anything other than 512, 1024, 2048, 4096, 8192, 16384, or 32768 bytes in size. 82 // setMaximumSize() will round the size down to the next smallest chunk if the passed size doesn't align. 83 int64_t maximumSize(); 84 void setMaximumSize(int64_t); 85 86 // The SQLite SYNCHRONOUS pragma can be either FULL, NORMAL, or OFF 87 // FULL - Any writing calls to the DB block until the data is actually on the disk surface 88 // NORMAL - SQLite pauses at some critical moments when writing, but much less than FULL 89 // OFF - Calls return immediately after the data has been passed to disk 90 enum SynchronousPragma { SyncOff = 0, SyncNormal = 1, SyncFull = 2 }; 91 void setSynchronous(SynchronousPragma); 92 93 int lastError(); 94 const char* lastErrorMsg(); 95 sqlite3Handle()96 sqlite3* sqlite3Handle() const { 97 ASSERT(currentThread() == m_openingThread); 98 return m_db; 99 } 100 101 void setAuthorizer(PassRefPtr<DatabaseAuthorizer>); 102 103 // (un)locks the database like a mutex 104 void lock(); 105 void unlock(); 106 107 private: 108 static int authorizerFunction(void*, int, const char*, const char*, const char*, const char*); 109 110 void enableAuthorizer(bool enable); 111 112 int pageSize(); 113 114 sqlite3* m_db; 115 int m_lastError; 116 int m_pageSize; 117 118 bool m_transactionInProgress; 119 120 Mutex m_authorizerLock; 121 RefPtr<DatabaseAuthorizer> m_authorizer; 122 123 Mutex m_lockingMutex; 124 ThreadIdentifier m_openingThread; 125 126 }; // class SQLiteDatabase 127 128 } // namespace WebCore 129 130 #endif 131