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