OpenM++ runtime library (libopenm)
dbExec.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_H
9#define DB_EXEC_H
10
11#include <mutex>
12using namespace std;
13
14#include "libopenm/omLog.h"
16
17namespace openm
18{
20 class IDbExec
21 {
22 public:
24 virtual ~IDbExec(void) noexcept = 0;
25
31 static IDbExec * create(const string & i_sqlProvider, const string & i_connectionStr);
32
34 virtual string provider(void) const = 0;
35
44 virtual int selectToInt(const string & i_sql, int i_default) = 0;
45
54 virtual long long selectToLong(const string & i_sql, long long i_default) = 0;
55
63 virtual string selectToStr(const string & i_sql) = 0;
64
72 virtual bool selectToBool(const string & i_sql) = 0;
73
82 virtual double selectToDouble(const string & i_sql, double i_default) = 0;
83
91 virtual vector<string> selectRowStr(const string & i_sql) = 0;
92
101 virtual IRowBaseVec selectRowVector(const string & i_sql, const IRowAdapter & i_adapter) = 0;
102
111 virtual IRowBaseList selectRowList(const string & i_sql, const IRowAdapter & i_adapter) = 0;
112
120 virtual void selectToRowProcessor(const string & i_sql, const IRowAdapter & i_adapter, IRowProcessor & i_processor) = 0;
121
138 virtual size_t selectColumn(
139 const string & i_sql, int i_column, const type_info & i_type, size_t i_size, void * io_valueArr
140 ) = 0;
141
149 virtual size_t update(const string & i_sql) = 0;
150
152 virtual void beginTransaction(void) = 0;
153
155 virtual unique_lock<recursive_mutex> beginTransactionThreaded(void) = 0;
156
158 virtual void commit(void) = 0;
159
161 virtual void rollback(void) = 0;
162
164 virtual bool isTransaction(void) = 0;
165
185 virtual void createStatement(const string & i_sql, int i_paramCount, const type_info ** i_typeArr) = 0;
186
202 virtual void releaseStatement(void) noexcept = 0;
203
223 virtual void executeStatement(int i_paramCount, const DbValue * i_valueArr) = 0;
224
226 static list<string> parseListOfProviderNames(const string & i_sqlProviderNames);
227
229 static bool isValidProviderName(const char * i_sqlProvider);
230
232 static bool isSqlKeyword(const char * i_keyword, size_t i_length = 0);
233
237 static const int maxDbTableNameSize = 63;
238
240 static const string makeDbNamePrefix(int i_id, const string & i_src);
241
243 static const string makeDbNameSuffix(int i_id, const string & i_src, const string i_digest);
244
246 static string bigIntTypeName(const string & i_sqlProvider);
247
249 static string floatTypeName(const string & i_sqlProvider);
250
252 static string textTypeName(const string & i_sqlProvider, int i_size);
253
255 static string valueDbType(const string & i_sqlProvider, const string & i_typeName, int i_typeId);
256
258 static string makeSqlBeginTransaction(const string & i_sqlProvider);
259
261 static string makeSqlCommitTransaction(const string & i_sqlProvider);
262
277 static string makeSqlCreateTableIfNotExist(
278 const string & i_sqlProvider, const string & i_tableName, const string & i_tableBodySql
279 );
280
297 static string makeSqlCreateViewReplace(
298 const string & i_sqlProvider, const string & i_viewName, const string & i_viewBodySql
299 );
300
326 virtual void runSqlScript(const string & i_sqlScript) = 0;
327
328 private:
329 // if max size of db table name is too short then use crc32(md5) digest
330 // table name is: paramNameAsPrefix + _p + md5Suffix, for example: ageSex_p12345678
331 // prefix based on parameter name or output table name
332 // suffix is 32 chars of md5 or 8 chars of crc32
333 // there is extra 2 chars: _p, _w, _v, _a in table name between prefix and suffix
334 //
335 // 2016-08-17: always use short crc32 name suffix
336 // isCrc32Name = minSize < 50;
337
338 static const bool isCrc32Name = true; // if true then use crc32 as db table name suffix
339 static const int dbSuffixSize = isCrc32Name ? 8 : 32; // db table suffix name size
340 static const int dbPrefixSize = maxDbTableNameSize - (2 + dbSuffixSize); // db table prefix name size
341 };
342}
343
344#endif // DB_EXEC_H
database connection wrapper to execute sql commands.
Definition: dbExec.h:21
virtual void releaseStatement(void) noexcept=0
release statement resources.
static string valueDbType(const string &i_sqlProvider, const string &i_typeName, int i_typeId)
return db type name by model type for specific db provider
Definition: dbExec.cpp:173
static string makeSqlCreateViewReplace(const string &i_sqlProvider, const string &i_viewName, const string &i_viewBodySql)
make sql statement to create or replace view.
Definition: dbExec.cpp:288
virtual unique_lock< recursive_mutex > beginTransactionThreaded(void)=0
begin transaction in multi-threaded environment.
virtual bool selectToBool(const string &i_sql)=0
select boolean value of first (row,column) or false if no rows or value IS NULL.
static list< string > parseListOfProviderNames(const string &i_sqlProviderNames)
return list of provider names from supplied comma or semicolon separated string or exception on inval...
Definition: dbExec.cpp:44
static const int maxDbTableNameSize
max length of db table or view name.
Definition: dbExec.h:237
static bool isValidProviderName(const char *i_sqlProvider)
check if provider name is valid.
Definition: dbExec.cpp:81
virtual vector< string > selectRowStr(const string &i_sql)=0
select string vector of first row or empty vector if no rows, empty "" string used for NULLs.
virtual ~IDbExec(void) noexcept=0
close db-connection and cleanup connection resources.
Definition: dbExec.cpp:41
static string makeSqlCreateTableIfNotExist(const string &i_sqlProvider, const string &i_tableName, const string &i_tableBodySql)
make sql statement to create table if not exists.
Definition: dbExec.cpp:255
virtual void createStatement(const string &i_sql, int i_paramCount, const type_info **i_typeArr)=0
create new statement with specified parameters.
static string makeSqlCommitTransaction(const string &i_sqlProvider)
return sql statement to commit transaction (db-provider specific).
Definition: dbExec.cpp:249
virtual void commit(void)=0
commit transaction, does nothing if no active transaction.
static string makeSqlBeginTransaction(const string &i_sqlProvider)
return sql statement to begin transaction (db-provider specific).
Definition: dbExec.cpp:221
virtual double selectToDouble(const string &i_sql, double i_default)=0
select double value of first (row,column) or default if no rows or value IS NULL.
virtual bool isTransaction(void)=0
return true in transaction scope.
virtual string selectToStr(const string &i_sql)=0
select string value of first (row,column) or empty "" string if no rows or value IS NULL.
virtual size_t selectColumn(const string &i_sql, int i_column, const type_info &i_type, size_t i_size, void *io_valueArr)=0
select column into io_valueArray[i_size] buffer and return row count.
virtual long long selectToLong(const string &i_sql, long long i_default)=0
select long value of first (row,column) or default if no rows or value IS NULL.
static string floatTypeName(const string &i_sqlProvider)
return type name for FLOAT standard sql type
Definition: dbExec.cpp:136
virtual void selectToRowProcessor(const string &i_sql, const IRowAdapter &i_adapter, IRowProcessor &i_processor)=0
select and process rows: each row created by row adapter and passed to processor.
virtual int selectToInt(const string &i_sql, int i_default)=0
select integer value of first (row,column) or default if no rows or value IS NULL.
virtual IRowBaseList selectRowList(const string &i_sql, const IRowAdapter &i_adapter)=0
select list of rows, each row created and field values set by row adapter.
virtual string provider(void) const =0
return sql provider name, e.g.
static const string makeDbNameSuffix(int i_id, const string &i_src, const string i_digest)
make unique part of db table name by using digest or crc32(digest)
Definition: dbExec.cpp:111
virtual void executeStatement(int i_paramCount, const DbValue *i_valueArr)=0
execute statement with parameters.
static string textTypeName(const string &i_sqlProvider, int i_size)
return column type DDL for long VARCHAR columns, use it for len > 255.
Definition: dbExec.cpp:143
virtual IRowBaseVec selectRowVector(const string &i_sql, const IRowAdapter &i_adapter)=0
select vector of rows, each row created and field values set by row adapter.
static string bigIntTypeName(const string &i_sqlProvider)
return type name for BIGINT sql type
Definition: dbExec.cpp:129
virtual void rollback(void)=0
rollback transaction.
virtual size_t update(const string &i_sql)=0
execute sql statement (update, insert, delete, create, etc).
static const string makeDbNamePrefix(int i_id, const string &i_src)
make prefix part of db table name by shorten source name, ie: ageSexProvince = > ageSexPr
Definition: dbExec.cpp:91
static bool isSqlKeyword(const char *i_keyword, size_t i_length=0)
return true if i_keword matched one of SQL reserved keywords, comparison is case neutral and limited ...
Definition: dbExec.cpp:502
virtual void runSqlScript(const string &i_sqlScript)=0
parse and execute list of sql statements.
virtual void beginTransaction(void)=0
begin transaction.
static IDbExec * create(const string &i_sqlProvider, const string &i_connectionStr)
db-connection factory: create new db-connection.
Definition: dbExecProvider.cpp:19
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 for db common structures.
openM++ namespace
Definition: log.h:32
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
OpenM++: public interface for log and trace support.
union to pass value to database methods
Definition: dbCommon.h:73