OpenM++ runtime library (libopenm)
dbExecSqlite.h
Go to the documentation of this file.
1 
5 // Copyright (c) 2013-2015 OpenM++
6 // This code is licensed under the MIT license (see LICENSE.txt for details)
7 
8 #ifndef DB_EXEC_SQLITE_H
9 #define DB_EXEC_SQLITE_H
10 
11 #include "dbExec.h"
12 #include "dbExecBase.h"
13 
14 #include "sqlite3.h"
15 #include "extension_functions.h"
16 
17 namespace openm
18 {
20  class DbExecSqlite : public DbExecBase, public IDbExec
21  {
22  public:
24  DbExecSqlite(const string & i_connectionStr);
25 
27  ~DbExecSqlite(void) noexcept;
28 
30  int selectToInt(const string & i_sql, int i_default) override;
31 
33  long long selectToLong(const string & i_sql, long long i_default) override;
34 
36  string selectToStr(const string & i_sql) override;
37 
39  bool selectToBool(const string & i_sql) override;
40 
42  double selectToDouble(const string & i_sql, double i_default) override;
43 
45  vector<string> selectRowStr(const string & i_sql) override;
46 
48  IRowBaseVec selectRowVector(const string & i_sql, const IRowAdapter & i_adapter) override;
49 
51  IRowBaseList selectRowList(const string & i_sql, const IRowAdapter & i_adapter) override;
52 
54  void selectToRowProcessor(const string & i_sql, const IRowAdapter & i_adapter, IRowProcessor & i_processor) override;
55 
57  size_t selectColumn(
58  const string & i_sql, int i_column, const type_info & i_type, size_t i_size, void * io_valueArr
59  ) override;
60 
62  size_t update(const string & i_sql) override;
63 
65  void beginTransaction(void) override;
66 
68  unique_lock<recursive_mutex> beginTransactionThreaded(void) override;
69 
71  void commit(void) override;
72 
74  void rollback(void) override;
75 
77  bool isTransaction(void) override { return DbExecBase::isTransaction(); }
78 
80  void createStatement(const string & i_sql, int i_paramCount, const type_info ** i_typeArr) override;
81 
83  void releaseStatement(void) noexcept override;
84 
86  void executeStatement(int i_paramCount, const DbValue * i_valueArr) override;
87 
89  void runSqlScript(const string & i_sqlScript) override { DbExecBase::runSqlScript(this, i_sqlScript); };
90 
91  private:
92  sqlite3 * theDb; // sqlite db
93  sqlite3_stmt * theStmt; // sqlite statement
94 
95  // method to bind parameter value
96  typedef void(DbExecSqlite::*BindHandler)(int, const DbValue &);
97 
98  vector<BindHandler> bindFncVec; // methods to bind parameter values to the statement
99 
101  void cleanup(void) noexcept override;
102 
104  void validateConnectionProps(void) override;
105 
106  // select value of first (row,column) or default if no records or value is NULL
107  template <typename TCvt>
108  TCvt selectTo(const string & i_sql, const TCvt & i_default, TCvt (DbExecSqlite::*ToRetType)(int));
109 
110  // retrieve single column field values into io_valueArray[i_size] buffer and return row count
111  template <typename TCol>
112  size_t retrieveColumnTo(int i_column, size_t i_size, void* io_valueArr, TCol (DbExecSqlite::*ToRetType)(int));
113 
114  // bind integer sql parameter at specified position
115  void bindLong(int i_position, const DbValue & i_value);
116 
117  // bind double sql parameter at specified position, use NULL if double value not is finite
118  template<typename TDbl>
119  void bindDbl(int i_position, const DbValue & i_value);
120 
121  // bind bool sql parameter at specified position, use integer one if value is true else use zero
122  void bindBool(int i_position, const DbValue & i_value);
123 
124  // bind string sql parameter at specified position, length of source string expected to be less than OM_STRLEN_MAX
125  void bindStr(int i_position, const DbValue & i_value);
126 
127  // convert field values of current row
128  template<typename TInt> TInt fieldToInt(int i_column);
129  template<typename TDbl> TDbl fieldToDbl(int i_column);
130  string fieldToStr(int i_column);
131  int64_t fieldToInt64(int i_column);
132  double fieldToDouble(int i_column);
133  bool fieldToBool(int i_column);
134 
135  // set integer field value by using row adapter
136  template<typename TInt>
137  void setFieldInt(IRowBase * i_row, int i_column, const IRowAdapter & i_adapter);
138 
139  // set double field value by using row adapter
140  template<typename TDbl>
141  void setFieldDbl(IRowBase * i_row, int i_column, const IRowAdapter & i_adapter);
142 
143  // set boolean field value by using row adapter
144  void setFieldBool(IRowBase * i_row, int i_column, const IRowAdapter & i_adapter);
145 
146  // set string field value by using row adapter
147  void setFieldStr(IRowBase * i_row, int i_column, const IRowAdapter & i_adapter);
148 
149  private:
150  DbExecSqlite(const DbExecSqlite & i_dbExec) = delete;
151  DbExecSqlite & operator=(const DbExecSqlite & i_dbExec) = delete;
152  };
153 
154  // setter to pass field value into row adapter
155  typedef void (DbExecSqlite::*SetFieldHandler)(IRowBase *, int, const IRowAdapter &);
156 }
157 
158 #endif // DB_EXEC_SQLITE_H
public interafce for row processing during select, ie: select and append to row list ...
Definition: dbCommon.h:180
DbExecSqlite(const string &i_connectionStr)
create new db-connection.
Definition: dbExecSqlite.cpp:27
db-row abstract base
Definition: omHelper.h:181
db connection wrapper for SQLite.
Definition: dbExecSqlite.h:20
int selectToInt(const string &i_sql, int i_default) override
select integer value of first (row,column) or default if no rows or value IS NULL.
Definition: dbExecSqlite.cpp:191
IRowBaseList selectRowList(const string &i_sql, const IRowAdapter &i_adapter) override
select list of rows, each row created and field values set by row adapter.
Definition: dbExecSqlite.cpp:358
double selectToDouble(const string &i_sql, double i_default) override
select double value of first (row,column) or default if no rows or value IS NULL. ...
Definition: dbExecSqlite.cpp:241
void createStatement(const string &i_sql, int i_paramCount, const type_info **i_typeArr) override
create new statement with specified parameters.
Definition: dbExecSqlite.cpp:768
void selectToRowProcessor(const string &i_sql, const IRowAdapter &i_adapter, IRowProcessor &i_processor) override
select and process rows: each row created by row adapter and passed to processor. ...
Definition: dbExecSqlite.cpp:366
vector< string > selectRowStr(const string &i_sql) override
select string vector of first row or empty vector if no rows, empty "" string used for NULLs...
Definition: dbExecSqlite.cpp:292
IRowBaseVec selectRowVector(const string &i_sql, const IRowAdapter &i_adapter) override
select vector of rows, each row created and field values set by row adapter.
Definition: dbExecSqlite.cpp:343
union to pass value to database methods
Definition: dbCommon.h:72
OpenM++ data library: common classes for db connection wrapper.
list< IRowBaseUptr > IRowBaseList
db rows: list of unique pointers to db row
Definition: omHelper.h:193
openM++ namespace
Definition: argReader.cpp:16
bool selectToBool(const string &i_sql) override
select boolean value of first (row,column) or false if no rows or value IS NULL.
Definition: dbExecSqlite.cpp:228
bool isTransaction(void)
return true in transaction scope.
Definition: dbExecBase.cpp:33
void executeStatement(int i_paramCount, const DbValue *i_valueArr) override
execute statement with parameters.
Definition: dbExecSqlite.cpp:860
unique_lock< recursive_mutex > beginTransactionThreaded(void) override
begin transaction in multi-threaded environment, throw exception if transaction already active or sta...
Definition: dbExecSqlite.cpp:671
database connection wrapper to execute sql commands.
Definition: dbExec.h:20
size_t update(const string &i_sql) override
execute sql statement (update, insert, delete, create, etc).
Definition: dbExecSqlite.cpp:616
void commit(void) override
commit transaction, does nothing if no active transaction, throw exception if statement is active...
Definition: dbExecSqlite.cpp:688
vector< IRowBaseUptr > IRowBaseVec
db rows: vector of unique pointers to db row
Definition: omHelper.h:190
row factory and setter interface to select row from database
Definition: dbCommon.h:44
~DbExecSqlite(void) noexcept
close db-connection and cleanup connection resources.
Definition: dbExecSqlite.cpp:77
static void runSqlScript(IDbExec *i_dbExec, const string &i_sqlScript)
parse and execute list of sql statements.
Definition: dbExecBase.cpp:199
OpenM++ data library: public interface.
long long selectToLong(const string &i_sql, long long i_default) override
select long value of first (row,column) or default if no rows or value IS NULL.
Definition: dbExecSqlite.cpp:204
bool isTransaction(void) override
return true in transaction scope.
Definition: dbExecSqlite.h:77
void beginTransaction(void) override
begin transaction, throw exception if transaction already active or statement is active.
Definition: dbExecSqlite.cpp:642
void rollback(void) override
rollback transaction, does nothing if no active transaction, throw exception if statement is active...
Definition: dbExecSqlite.cpp:719
base class for database connection wrapper
Definition: dbExecBase.h:19
size_t selectColumn(const string &i_sql, int i_column, const type_info &i_type, size_t i_size, void *io_valueArr) override
select column into io_valueArray[i_size] buffer and return row count.
Definition: dbExecSqlite.cpp:522
void runSqlScript(const string &i_sqlScript) override
parse and execute list of sql statements.
Definition: dbExecSqlite.h:89
void releaseStatement(void) noexcept override
release statement resources.
Definition: dbExecSqlite.cpp:116
string selectToStr(const string &i_sql) override
select string value of first (row,column) or empty "" string if no rows or value IS NULL...
Definition: dbExecSqlite.cpp:216