Kompex SQLite Wrapper  1.11.14
KompexSQLiteStatement.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 KompexSQLiteStatement_H
20 #define KompexSQLiteStatement_H
21 
22 #include <map>
23 #include <string>
24 
25 #include "sqlite3.h"
26 
28 
29 namespace Kompex
30 {
31  class SQLiteDatabase;
32 
33  enum ENCODING
34  {
35  UTF8 = 1,
36  UTF16LE = 2,
37  UTF16BE = 3,
38  UTF16 = 4,
40  };
41 
44  {
45  public:
50  virtual ~SQLiteStatement();
51 
57  inline void SqlStatement(const std::string &sqlStatement) {SqlStatement(sqlStatement.c_str());}
63  void SqlStatement(const char *sqlStatement);
69  inline void SqlStatement(const std::wstring &sqlStatement) {SqlStatement(sqlStatement.c_str());}
75  void SqlStatement(const wchar_t *sqlStatement);
76 
80  inline void Sql(const std::string &sql) {Prepare(sql.c_str());}
84  inline void Sql(const char *sql) {Prepare(sql);}
88  inline void Sql(const std::wstring &sql) {Prepare(sql.c_str());}
92  inline void Sql(const wchar_t *sql) {Prepare(sql);}
93 
96  bool FetchRow() const;
98  void FreeQuery();
99 
104  float SqlAggregateFuncResult(const std::string &countSql);
109  float SqlAggregateFuncResult(wchar_t *countSql);
114  float SqlAggregateFuncResult(const char *countSql);
115 
119  const char *GetColumnName(int column) const;
123  const char *GetColumnName(const std::string &column) const;
124 
128  wchar_t *GetColumnName16(int column) const;
132  wchar_t *GetColumnName16(const std::string &column) const;
133 
143  int GetColumnType(int column) const;
153  int GetColumnType(const std::string &column) const;
154 
159  const unsigned char *GetColumnCString(int column) const;
164  const unsigned char *GetColumnCString(const std::string &column) const;
165 
170  std::string GetColumnString(int column) const;
175  std::string GetColumnString(const std::string &column) const;
176 
181  wchar_t *GetColumnString16(int column) const;
186  wchar_t *GetColumnString16(const std::string &column) const;
187 
191  double GetColumnDouble(int column) const;
195  double GetColumnDouble(const std::string &column) const;
196 
200  int GetColumnInt(int column) const;
204  int GetColumnInt(const std::string &column) const;
205 
212  bool GetColumnBool(int column) const;
219  bool GetColumnBool(const std::string &column) const;
220 
224  int64 GetColumnInt64(int column) const;
228  int64 GetColumnInt64(const std::string &column) const;
229 
233  const void *GetColumnBlob(int column) const;
237  const void *GetColumnBlob(const std::string &column) const;
238 
242  int GetColumnBytes(int column) const;
246  int GetColumnBytes(const std::string &column) const;
250  int GetColumnBytes16(int column) const;
254  int GetColumnBytes16(const std::string &column) const;
255 
259  const char *GetColumnDatabaseName(int column) const;
263  const char *GetColumnDatabaseName(const std::string &column) const;
267  wchar_t *GetColumnDatabaseName16(int column) const;
271  wchar_t *GetColumnDatabaseName16(const std::string &column) const;
272 
276  const char *GetColumnTableName(int column) const;
280  const char *GetColumnTableName(const std::string &column) const;
284  wchar_t *GetColumnTableName16(int column) const;
288  wchar_t *GetColumnTableName16(const std::string &column) const;
289 
293  const char *GetColumnOriginName(int column) const;
297  const char *GetColumnOriginName(const std::string &column) const;
301  wchar_t *GetColumnOriginName16(int column) const;
305  wchar_t *GetColumnOriginName16(const std::string &column) const;
306 
310  const char *GetColumnDeclaredDatatype(int column) const;
314  const char *GetColumnDeclaredDatatype(const std::string &column) const;
318  wchar_t *GetColumnDeclaredDatatype16(int column) const;
322  wchar_t *GetColumnDeclaredDatatype16(const std::string &column) const;
323 
326  int GetColumnCount() const;
329  inline int GetDataCount() const {return sqlite3_data_count(mStatement);}
330 
335  void BindInt(int column, int value) const;
340  void BindBool(int column, bool value) const;
345  void BindString(int column, const std::string &string) const;
350  void BindString16(int column, const wchar_t *string) const;
357  void BindString64(int column, const char *string, uint64 byteLength, ENCODING encoding) const;
362  void BindDouble(int column, double value) const;
367  void BindInt64(int column, int64 value) const;
371  void BindNull(int column) const;
381  void BindBlob(int column, const void* data, int numberOfBytes = -1) const;
392  void BindBlob64(int column, const void* data, uint64 numberOfBytes = -1) const;
397  void BindZeroBlob(int column, int length) const;
403  void BindZeroBlob64(int column, uint64 length) const;
404 
410  void Execute() const;
413  void ExecuteAndFree();
414 
420  void GetTable(const std::string &sql, unsigned short consoleOutputColumnWidth = 20) const;
421 
427  void GetTableColumnMetadata(const std::string &tableName, const std::string &columnName) const;
428 
431  void ClearBindings() const;
432 
436  void Reset() const;
437 
439  void BeginTransaction();
442  void CommitTransaction();
444  inline void RollbackTransaction()
445  {
446  FreeQuery();
447  SqlStatement("ROLLBACK;");
448  }
449 
456  inline void Transaction(const char *sql) {mTransactionSQL[mTransactionID++] = std::make_pair(sql, false);}
463  inline void Transaction(const std::string &sql) {mTransactionSQL[mTransactionID++] = std::make_pair(sql.c_str(), false);}
470  inline void Transaction(const wchar_t *sql) {mTransactionSQL16[mTransactionID++] = std::make_pair(sql, false);}
471 
477  void SecureTransaction(const char *sql);
483  void SecureTransaction(const std::string sql);
489  void SecureTransaction(const wchar_t *sql);
490 
494  std::string GetSqlResultString(const std::string &sql, const std::string &defaultReturnValue = "");
498  std::string GetSqlResultString(const char *sql, const std::string &defaultReturnValue = "");
502  std::string GetSqlResultString(const wchar_t *sql, const std::string &defaultReturnValue = "");
503 
507  int GetSqlResultInt(const std::string &sql, int defaultReturnValue = -1);
511  int GetSqlResultInt(const char *sql, int defaultReturnValue = -1);
515  int GetSqlResultInt(const wchar_t *sql, int defaultReturnValue = -1);
516 
520  int64 GetSqlResultInt64(const std::string &sql, int64 defaultReturnValue = -1);
524  int64 GetSqlResultInt64(const char *sql, int64 defaultReturnValue = -1);
528  int64 GetSqlResultInt64(const wchar_t *sql, int64 defaultReturnValue = -1);
529 
533  double GetSqlResultDouble(const std::string &sql, double defaultReturnValue = -1);
537  double GetSqlResultDouble(const char *sql, double defaultReturnValue = -1);
541  double GetSqlResultDouble(const wchar_t *sql, double defaultReturnValue = -1);
542 
551  const unsigned char *GetSqlResultCString(const std::string &sql, const unsigned char *defaultReturnValue = 0);
560  const unsigned char *GetSqlResultCString(const char *sql, const unsigned char *defaultReturnValue = 0);
569  const unsigned char *GetSqlResultCString(const wchar_t *sql, const unsigned char *defaultReturnValue = 0);
570 
579  wchar_t *GetSqlResultString16(const std::string &sql, wchar_t *defaultReturnValue = 0);
588  wchar_t *GetSqlResultString16(const char *sql, wchar_t *defaultReturnValue = 0);
597  wchar_t *GetSqlResultString16(const wchar_t *sql, wchar_t *defaultReturnValue = 0);
598 
607  const void *GetSqlResultBlob(const std::string &sql, const void *defaultReturnValue = 0);
616  const void *GetSqlResultBlob(const char *sql, const void *defaultReturnValue = 0);
625  const void *GetSqlResultBlob(const wchar_t *sql, const void *defaultReturnValue = 0);
626 
631  unsigned int GetNumberOfRows();
632 
649  static std::string Mprintf(const char *sql, ...);
666  static std::string Vmprintf(const char *sql, va_list args);
667 
668  protected:
671  void Prepare(const char *sqlStatement);
674  void Prepare(const wchar_t *sqlStatement);
676  bool Step() const;
678  void CheckStatement() const;
680  void CheckDatabase() const;
681 
683  sqlite3_stmt *GetStatementHandle() const {return mStatement;}
684 
686  const unsigned char *SqlResultCString(const unsigned char *defaultReturnValue);
688  wchar_t *SqlResultString16(wchar_t *defaultReturnValue);
690  const void *SqlResultBlob(const void *defaultReturnValue);
691 
695  void CheckColumnNumber(int columnNumber, const std::string &functionName = "") const;
696 
698  void CleanUpTransaction();
699 
703  template<class T>
704  inline void DeleteTransactionSqlStr(bool isMemAllocated, T *str)
705  {
706  if(isMemAllocated)
707  delete[] str;
708  }
709 
714  template<class S, class T>
715  T GetColumnValue(S sql, T(Kompex::SQLiteStatement::*getColumnFunc)(int columnNumber)const, T defaultReturnValue);
716 
717  private:
719  void AssignColumnNumberToColumnName() const;
721  int GetAssignedColumnNumber(const std::string &columnName) const;
722 
724  struct sqlite3_stmt *mStatement;
727 
729  typedef std::map<unsigned short /* transaction id */, std::pair<const char* /* sql */, bool /* memory allocated */> > TTransactionSQL;
731  typedef std::map<unsigned short /* transaction id */, std::pair<const wchar_t* /* sql */, bool /* is memory allocated */> > TTransactionSQL16;
732 
734  TTransactionSQL mTransactionSQL;
736  TTransactionSQL16 mTransactionSQL16;
737 
739  unsigned short mTransactionID;
740 
742  mutable std::map<std::string /* column name */, int /* column number */> mColumnNumberToColumnNameAssignment;
745 
746  };
747 };
748 
749 #endif // KompexSQLiteStatement_H
Execution of SQL statements and result processing.
Definition: KompexSQLiteStatement.h:43
void Sql(const std::wstring &sql)
Definition: KompexSQLiteStatement.h:88
TTransactionSQL16 mTransactionSQL16
Stores UTF-16 transaction statements.
Definition: KompexSQLiteStatement.h:736
struct sqlite3_stmt * mStatement
SQL statement.
Definition: KompexSQLiteStatement.h:724
Definition: KompexSQLiteStatement.h:36
Definition: KompexSQLiteStatement.h:35
int GetDataCount() const
Definition: KompexSQLiteStatement.h:329
TTransactionSQL mTransactionSQL
Stores UTF-8 transaction statements.
Definition: KompexSQLiteStatement.h:734
Definition: KompexSQLiteStatement.h:39
std::map< unsigned short, std::pair< const char *, bool > > TTransactionSQL
typedef for UTF-8 transaction statements
Definition: KompexSQLiteStatement.h:729
void Sql(const wchar_t *sql)
Definition: KompexSQLiteStatement.h:92
Definition: KompexSQLiteStatement.h:37
sqlite3_stmt * GetStatementHandle() const
Returns the SQLite statement handle.
Definition: KompexSQLiteStatement.h:683
ENCODING
Definition: KompexSQLiteStatement.h:33
bool mIsColumnNumberAssignedToColumnName
Saves whether the assignments for every column number and the corresponding column name was already d...
Definition: KompexSQLiteStatement.h:744
unsigned short mTransactionID
ID for transactions.
Definition: KompexSQLiteStatement.h:739
Administration of the database and all concerning settings.
Definition: KompexSQLiteDatabase.h:31
unsigned long long int uint64
Definition: KompexSQLitePrerequisites.h:42
void Transaction(const char *sql)
Definition: KompexSQLiteStatement.h:456
void Sql(const char *sql)
Definition: KompexSQLiteStatement.h:84
long long int int64
Definition: KompexSQLitePrerequisites.h:41
SQLiteDatabase * mDatabase
Database pointer.
Definition: KompexSQLiteStatement.h:726
#define _SQLiteWrapperExport
Definition: KompexSQLitePrerequisites.h:32
Definition: KompexSQLiteStatement.h:38
void DeleteTransactionSqlStr(bool isMemAllocated, T *str)
Definition: KompexSQLiteStatement.h:704
void SqlStatement(const std::wstring &sqlStatement)
Definition: KompexSQLiteStatement.h:69
void Transaction(const std::string &sql)
Definition: KompexSQLiteStatement.h:463
void SqlStatement(const std::string &sqlStatement)
Definition: KompexSQLiteStatement.h:57
void RollbackTransaction()
Rollback a transaction.
Definition: KompexSQLiteStatement.h:444
void Transaction(const wchar_t *sql)
Definition: KompexSQLiteStatement.h:470
std::map< std::string, int > mColumnNumberToColumnNameAssignment
Container which stores the assignments for every column number and the corresponding column name (cac...
Definition: KompexSQLiteStatement.h:742
void Sql(const std::string &sql)
Definition: KompexSQLiteStatement.h:80
std::map< unsigned short, std::pair< const wchar_t *, bool > > TTransactionSQL16
typedef for UTF-16 transaction statements
Definition: KompexSQLiteStatement.h:731
Definition: KompexSQLiteBlob.h:26