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
16namespace openm
17{
19 class DbExecSqlite : public DbExecBase, public IDbExec
20 {
21 public:
23 DbExecSqlite(const string & i_connectionStr);
24
26 ~DbExecSqlite(void) noexcept;
27
29 string provider(void) const override { return SQLITE_DB_PROVIDER; }
30
32 int selectToInt(const string & i_sql, int i_default) override;
33
35 long long selectToLong(const string & i_sql, long long i_default) override;
36
38 string selectToStr(const string & i_sql) override;
39
41 bool selectToBool(const string & i_sql) override;
42
44 double selectToDouble(const string & i_sql, double i_default) override;
45
47 vector<string> selectRowStr(const string & i_sql) override;
48
50 IRowBaseVec selectRowVector(const string & i_sql, const IRowAdapter & i_adapter) override;
51
53 IRowBaseList selectRowList(const string & i_sql, const IRowAdapter & i_adapter) override;
54
56 void selectToRowProcessor(const string & i_sql, const IRowAdapter & i_adapter, IRowProcessor & i_processor) override;
57
59 size_t selectColumn(
60 const string & i_sql, int i_column, const type_info & i_type, size_t i_size, void * io_valueArr
61 ) override;
62
64 size_t update(const string & i_sql) override;
65
67 void beginTransaction(void) override;
68
70 unique_lock<recursive_mutex> beginTransactionThreaded(void) override;
71
73 void commit(void) override;
74
76 void rollback(void) override;
77
79 bool isTransaction(void) override { return DbExecBase::isTransaction(); }
80
82 void createStatement(const string & i_sql, int i_paramCount, const type_info ** i_typeArr) override;
83
85 void releaseStatement(void) noexcept override;
86
88 void executeStatement(int i_paramCount, const DbValue * i_valueArr) override;
89
91 void runSqlScript(const string & i_sqlScript) override { DbExecBase::runSqlScript(this, i_sqlScript); };
92
93 private:
94 sqlite3 * theDb; // sqlite db
95 sqlite3_stmt * theStmt; // sqlite statement
96
97 // method to bind parameter value
98 typedef void(DbExecSqlite::*BindHandler)(int, const DbValue &);
99
100 vector<BindHandler> bindFncVec; // methods to bind parameter values to the statement
101
103 void cleanup(void) noexcept override;
104
106 void validateConnectionProps(void) override;
107
108 // select value of first (row,column) or default if no records or value is NULL
109 template <typename TCvt>
110 TCvt selectTo(const string & i_sql, const TCvt & i_default, TCvt (DbExecSqlite::*ToRetType)(int));
111
112 // retrieve single column field values into io_valueArray[i_size] buffer and return row count
113 template <typename TCol>
114 size_t retrieveColumnTo(int i_column, size_t i_size, void* io_valueArr, TCol (DbExecSqlite::*ToRetType)(int));
115
116 // bind integer sql parameter at specified position
117 void bindLong(int i_position, const DbValue & i_value);
118
119 // bind double sql parameter at specified position, use NULL if double value not is finite
120 template<typename TDbl>
121 void bindDbl(int i_position, const DbValue & i_value);
122
123 // bind bool sql parameter at specified position, use integer one if value is true else use zero
124 void bindBool(int i_position, const DbValue & i_value);
125
126 // bind string sql parameter at specified position, length of source string expected to be less than OM_STRLEN_MAX
127 void bindStr(int i_position, const DbValue & i_value);
128
129 // convert field values of current row
130 template<typename TInt> TInt fieldToInt(int i_column);
131 template<typename TDbl> TDbl fieldToDbl(int i_column);
132 string fieldToStr(int i_column);
133 int64_t fieldToInt64(int i_column);
134 double fieldToDouble(int i_column);
135 bool fieldToBool(int i_column);
136
137 // set integer field value by using row adapter
138 template<typename TInt>
139 void setFieldInt(IRowBase * i_row, int i_column, const IRowAdapter & i_adapter);
140
141 // set double field value by using row adapter
142 template<typename TDbl>
143 void setFieldDbl(IRowBase * i_row, int i_column, const IRowAdapter & i_adapter);
144
145 // set boolean field value by using row adapter
146 void setFieldBool(IRowBase * i_row, int i_column, const IRowAdapter & i_adapter);
147
148 // set string field value by using row adapter
149 void setFieldStr(IRowBase * i_row, int i_column, const IRowAdapter & i_adapter);
150
151 private:
152 DbExecSqlite(const DbExecSqlite & i_dbExec) = delete;
153 DbExecSqlite & operator=(const DbExecSqlite & i_dbExec) = delete;
154 };
155
156 // setter to pass field value into row adapter
157 typedef void (DbExecSqlite::*SetFieldHandler)(IRowBase *, int, const IRowAdapter &);
158}
159
160#endif // DB_EXEC_SQLITE_H
base class for database connection wrapper
Definition: dbExecBase.h:20
static void runSqlScript(IDbExec *i_dbExec, const string &i_sqlScript)
parse and execute list of sql statements.
Definition: dbExecBase.cpp:199
bool isTransaction(void)
return true in transaction scope.
Definition: dbExecBase.cpp:33
db connection wrapper for SQLite.
Definition: dbExecSqlite.h:20
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 runSqlScript(const string &i_sqlScript) override
parse and execute list of sql statements.
Definition: dbExecSqlite.h:91
~DbExecSqlite(void) noexcept
close db-connection and cleanup connection resources.
Definition: dbExecSqlite.cpp:77
void beginTransaction(void) override
begin transaction, throw exception if transaction already active or statement is active.
Definition: dbExecSqlite.cpp:642
bool isTransaction(void) override
return true in transaction scope.
Definition: dbExecSqlite.h:79
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
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
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
DbExecSqlite(const string &i_connectionStr)
create new db-connection.
Definition: dbExecSqlite.cpp:27
void executeStatement(int i_paramCount, const DbValue *i_valueArr) override
execute statement with parameters.
Definition: dbExecSqlite.cpp:860
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
void releaseStatement(void) noexcept override
release statement resources.
Definition: dbExecSqlite.cpp:116
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
void rollback(void) override
rollback transaction, does nothing if no active transaction, throw exception if statement is active.
Definition: dbExecSqlite.cpp:719
void commit(void) override
commit transaction, does nothing if no active transaction, throw exception if statement is active.
Definition: dbExecSqlite.cpp:688
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
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
string provider(void) const override
return sql provider name, e.g.
Definition: dbExecSqlite.h:29
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
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
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
size_t update(const string &i_sql) override
execute sql statement (update, insert, delete, create, etc).
Definition: dbExecSqlite.cpp:616
database connection wrapper to execute sql commands.
Definition: dbExec.h:21
row factory and setter interface to select row from database
Definition: dbCommon.h:45
public interafce for row processing during select, ie: select and append to row list
Definition: dbCommon.h:181
OpenM++ data library: public interface.
OpenM++ data library: common classes for db connection wrapper.
openM++ namespace
Definition: log.h:32
const char * SQLITE_DB_PROVIDER
SQLite db-provider name.
Definition: dbExec.cpp:14
std::list< IRowBaseUptr > IRowBaseList
db rows: list of unique pointers to db row
Definition: omHelper.h:242
std::vector< IRowBaseUptr > IRowBaseVec
db rows: vector of unique pointers to db row
Definition: omHelper.h:239
db-row abstract base
Definition: omHelper.h:231
union to pass value to database methods
Definition: dbCommon.h:73