Kompex SQLite Wrapper  1.9.11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
KompexSQLiteDatabase.h
Go to the documentation of this file.
1 /*
2  This file is part of Kompex SQLite Wrapper.
3  Copyright (c) 2008-2013 Sven Broeske
4 
5  Kompex SQLite Wrapper is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Kompex SQLite Wrapper is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Kompex SQLite Wrapper. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef KompexSQLiteDatabase_H
20 #define KompexSQLiteDatabase_H
21 
22 #include <string>
23 
24 #include "sqlite3.h"
25 
27 
28 namespace Kompex
29 {
32  {
33  public:
37 
57  SQLiteDatabase(const char *filename, int flags, const char *zVfs);
58 
78  SQLiteDatabase(const std::string &filename, int flags, const char *zVfs);
79 
83  SQLiteDatabase(const wchar_t *filename);
84 
86  virtual ~SQLiteDatabase();
87 
107  void Open(const char *filename, int flags, const char *zVfs);
108 
128  void Open(const std::string &filename, int flags, const char *zVfs);
129 
133  void Open(const wchar_t *filename);
134 
136  void Close();
137 
139  sqlite3 *GetDatabaseHandle() const {return mDatabaseHandle;}
141  inline int GetLibVersionNumber() const {return sqlite3_libversion_number();}
144  int GetDatabaseChanges() const {return sqlite3_changes(mDatabaseHandle);}
150  int GetTotalDatabaseChanges() const {return sqlite3_total_changes(mDatabaseHandle);}
154  void InterruptDatabaseOperation() const {sqlite3_interrupt(mDatabaseHandle);}
158  int GetAutoCommit() const {return sqlite3_get_autocommit(mDatabaseHandle);}
159 
163  inline void ActivateTracing() const {sqlite3_trace(mDatabaseHandle, &Kompex::SQLiteDatabase::TraceOutput, 0);}
169  inline void ActivateProfiling() const {sqlite3_profile(mDatabaseHandle, &Kompex::SQLiteDatabase::ProfileOutput, 0);}
170 
179  inline void SetSoftHeapLimit(int heapLimit) const {sqlite3_soft_heap_limit(heapLimit);}
180 
186  inline int ReleaseMemory(int bytesOfMemory) const {return sqlite3_release_memory(bytesOfMemory);}
187 
189  long long GetMemoryUsage() const {return sqlite3_memory_used();}
190 
194  long long GetMemoryHighwaterMark(bool resetFlag = false) const {return sqlite3_memory_highwater(resetFlag);}
195 
197  enum UtfEncoding {UTF8, UTF16};
198 
204  void MoveDatabaseToMemory(UtfEncoding encoding = UTF8);
205 
209  void SaveDatabaseFromMemoryToFile(const std::string &filename = "");
212  void SaveDatabaseFromMemoryToFile(const wchar_t *filename);
213 
221  long long GetLastInsertRowId() const {return sqlite3_last_insert_rowid(mDatabaseHandle);}
222 
225  bool IsDatabaseReadOnly();
226 
228  inline void ReleaseMemory() {sqlite3_db_release_memory(mDatabaseHandle);}
229 
244  void CreateModule(const std::string &moduleName, const sqlite3_module *module, void *clientData, void(*xDestroy)(void*));
245 
246  protected:
248  static void TraceOutput(void *ptr, const char *sql);
250  static void ProfileOutput(void* ptr, const char* sql, sqlite3_uint64 time);
252  static int ProcessDDLRow(void *db, int nColumns, char **values, char **columns);
254  static int ProcessDMLRow(void *db, int nColumns, char **values, char **columns);
256  void TakeSnapshot(sqlite3 *destinationDatabase);
257 
258  private:
260  struct sqlite3 *mDatabaseHandle;
267 
269  void CleanUpFailedMemoryDatabase(sqlite3 *memoryDatabase, sqlite3 *rollbackDatabase, bool isDetachNecessary, bool isRollbackNecessary, sqlite3_stmt *stmt, const std::string &errMsg);
270 
271  };
272 
273 };
274 
275 #endif // KompexSQLiteDatabase_H