Kompex SQLite Wrapper  1.11.14
KompexSQLiteDatabase.h
Go to the documentation of this file.
1 /*
2  This file is part of Kompex SQLite Wrapper.
3  Copyright (c) 2008-2015 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 
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 
247  int GetNumberOfCheckedOutLookasideMemorySlots() const;
249  int GetHeapMemoryUsedByPagerCaches() const;
254  int GetHeapMemoryUsedToStoreSchemas() const;
257  int GetHeapAndLookasideMemoryUsedByPreparedStatements() const;
259  int GetPagerCacheHitCount() const;
261  int GetPagerCacheMissCount() const;
268  int GetNumberOfDirtyCacheEntries() const;
270  int GetNumberOfUnresolvedForeignKeys() const;
271 
274  int GetHighestNumberOfCheckedOutLookasideMemorySlots(bool resetValue = false);
277  int GetLookasideMemoryHitCount(bool resetValue = false);
281  int GetLookasideMemoryMissCountDueToSmallSlotSize(bool resetValue = false);
285  int GetLookasideMemoryMissCountDueToFullMemory(bool resetValue = false);
286 
287  protected:
289  static void TraceOutput(void *ptr, const char *sql);
291  static void ProfileOutput(void* ptr, const char* sql, sqlite3_uint64 time);
293  static int ProcessDDLRow(void *db, int nColumns, char **values, char **columns);
295  static int ProcessDMLRow(void *db, int nColumns, char **values, char **columns);
297  void TakeSnapshot(sqlite3 *destinationDatabase);
299  int GetRuntimeStatusInformation(int operation, bool highwaterValue = false, bool resetFlag = false) const;
300 
301  private:
303  struct sqlite3 *mDatabaseHandle;
310 
312  void CleanUpFailedMemoryDatabase(sqlite3 *memoryDatabase, sqlite3 *rollbackDatabase, bool isDetachNecessary, bool isRollbackNecessary, sqlite3_stmt *stmt, const std::string &errMsg, int internalSqliteErrCode);
313 
314  };
315 
316 };
317 
318 #endif // KompexSQLiteDatabase_H
void ActivateTracing() const
Definition: KompexSQLiteDatabase.h:163
Definition: KompexSQLiteStatement.h:35
struct sqlite3 * mDatabaseHandle
SQLite db handle.
Definition: KompexSQLiteDatabase.h:303
void SetSoftHeapLimit(int heapLimit) const
Definition: KompexSQLiteDatabase.h:179
long long GetLastInsertRowId() const
Definition: KompexSQLiteDatabase.h:221
void InterruptDatabaseOperation() const
Definition: KompexSQLiteDatabase.h:154
static void ProfileOutput(void *ptr, const char *sql, sqlite3_uint64 time)
Callback function for ActivateProfiling() [sqlite3_profile].
Definition: KompexSQLiteDatabase.cpp:136
std::string mDatabaseFilenameUtf8
Database filename UTF-8.
Definition: KompexSQLiteDatabase.h:305
Administration of the database and all concerning settings.
Definition: KompexSQLiteDatabase.h:31
static void TraceOutput(void *ptr, const char *sql)
Callback function for ActivateTracing() [sqlite3_trace].
Definition: KompexSQLiteDatabase.cpp:131
long long GetMemoryUsage() const
GetMemoryUsed() returns the number of bytes of memory currently outstanding (malloced but not freed)...
Definition: KompexSQLiteDatabase.h:189
long long GetMemoryHighwaterMark(bool resetFlag=false) const
Definition: KompexSQLiteDatabase.h:194
int GetTotalDatabaseChanges() const
Definition: KompexSQLiteDatabase.h:150
int GetLibVersionNumber() const
Returns the version number of sqlite.
Definition: KompexSQLiteDatabase.h:141
#define _SQLiteWrapperExport
Definition: KompexSQLitePrerequisites.h:32
Definition: KompexSQLiteStatement.h:38
int GetDatabaseChanges() const
Definition: KompexSQLiteDatabase.h:144
int GetAutoCommit() const
Definition: KompexSQLiteDatabase.h:158
std::wstring mDatabaseFilenameUtf16
Database filename UTF-16.
Definition: KompexSQLiteDatabase.h:307
sqlite3 * GetDatabaseHandle() const
Returns the SQLite db handle.
Definition: KompexSQLiteDatabase.h:139
int ReleaseMemory(int bytesOfMemory) const
Definition: KompexSQLiteDatabase.h:186
void ActivateProfiling() const
Definition: KompexSQLiteDatabase.h:169
UtfEncoding
Provided encodings for MoveDatabaseToMemory().
Definition: KompexSQLiteDatabase.h:197
void ReleaseMemory()
This function attempts to free as much heap memory as possible from the database connection.
Definition: KompexSQLiteDatabase.h:228
Definition: KompexSQLiteBlob.h:26
bool mIsMemoryDatabaseActive
Is the database currently stored in memory?
Definition: KompexSQLiteDatabase.h:309