OpenM++ runtime library (libopenm)
dbExecBase.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_BASE_H
9#define DB_EXEC_BASE_H
10
11using namespace std;
12
13#include "helper.h"
14#include "dbExec.h"
15
16namespace openm
17{
20 {
21 public:
23 DbExecBase(const string & i_connectionStr);
24
26 ~DbExecBase(void) noexcept { }
27
29 bool isTransaction(void);
30
31 protected:
33 thread::id trxThreadId;
34
37
38 protected:
40 bool isTransactionNonOwn(void);
41
43 void setTransactionActive(void);
44
46 void releaseTransaction(void);
47
49 string strConnProperty(const string & i_key) const;
50
52 bool boolConnProperty(const string & i_key) const;
53
55 long long longConnProperty(const string & i_key, long i_default) const;
56
58 static void runSqlScript(IDbExec * i_dbExec, const string & i_sqlScript);
59
60 private:
62 NoCaseMap parseConnectionStr(const string & i_connectionStr);
63
64 // cleanup connection resources
65 virtual void cleanup(void) noexcept = 0;
66
67 // validate connection properties
68 virtual void validateConnectionProps(void) = 0;
69
70 private:
71 DbExecBase(const DbExecBase & i_dbExec) = delete;
72 DbExecBase & operator=(const DbExecBase & i_dbExec) = delete;
73 };
74
76 template<typename TContainer>
78 {
79 public:
80
82 void processRow(IRowBaseUptr & i_row) override { rowContainer.push_back(std::move(i_row)); };
83
85 TContainer rowContainer;
86 };
87
89 extern recursive_mutex dbMutex;
90}
91
92#endif // DB_EXEC_BASE_H
base class for database connection wrapper
Definition: dbExecBase.h:20
~DbExecBase(void) noexcept
cleanup connection resources.
Definition: dbExecBase.h:26
static void runSqlScript(IDbExec *i_dbExec, const string &i_sqlScript)
parse and execute list of sql statements.
Definition: dbExecBase.cpp:199
bool isTransactionNonOwn(void)
return true if other thread have active transaction.
Definition: dbExecBase.cpp:51
long long longConnProperty(const string &i_key, long i_default) const
return long value of connection property or default value if key not found (case neutral).
Definition: dbExecBase.cpp:91
string strConnProperty(const string &i_key) const
return string value of connection property by key (case neutral) or empty "" string if key not found.
Definition: dbExecBase.cpp:77
NoCaseMap connProps
connection properties as key+value string pairs (case neutral).
Definition: dbExecBase.h:36
void setTransactionActive(void)
set transaction ownership status.
Definition: dbExecBase.cpp:59
bool isTransaction(void)
return true in transaction scope.
Definition: dbExecBase.cpp:33
bool boolConnProperty(const string &i_key) const
return true if value of connection property is 'true', 'yes', '1' (case neutral).
Definition: dbExecBase.cpp:83
thread::id trxThreadId
if true then transaction is active
Definition: dbExecBase.h:33
void releaseTransaction(void)
release transaction: clean active transaction status.
Definition: dbExecBase.cpp:68
DbExecBase(const string &i_connectionStr)
prepare to open new db-connection.
Definition: dbExecBase.cpp:16
database connection wrapper to execute sql commands.
Definition: dbExec.h:21
public interafce for row processing during select, ie: select and append to row list
Definition: dbCommon.h:181
row processor to append rows to sequence container, ie: to list or vector
Definition: dbExecBase.h:78
void processRow(IRowBaseUptr &i_row) override
append row to sequence container, ie: to list or vector
Definition: dbExecBase.h:82
TContainer rowContainer
row container
Definition: dbExecBase.h:85
OpenM++ data library: public interface.
OpenM++ common helper utilities.
openM++ namespace
Definition: log.h:32
std::unique_ptr< IRowBase > IRowBaseUptr
unique pointer to db row
Definition: omHelper.h:236
std::map< std::string, std::string, LessNoCase > NoCaseMap
map of key-value strings with case neutral key search
Definition: omHelper.h:224
recursive_mutex dbMutex
mutex to lock database operations
Definition: dbExecBase.cpp:13