OpenM++ runtime library (libopenm)
dbCommon.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 OM_DB_COMMON_H
9 #define OM_DB_COMMON_H
10 
11 #include <cmath>
12 
13 using namespace std;
14 
15 #include "libopenm/omError.h"
16 
17 namespace openm
18 {
20  extern const char * SQLITE_DB_PROVIDER;
21 
23  extern const char * MYSQL_DB_PROVIDER;
24 
26  extern const char * PGSQL_DB_PROVIDER;
27 
29  extern const char * MSSQL_DB_PROVIDER;
30 
32  extern const char * ORACLE_DB_PROVIDER;
33 
35  extern const char * DB2_DB_PROVIDER;
36 
38  extern const char dbUnknownErrorMessage[];
39 
42 
45  {
46  public:
47  virtual ~IRowAdapter(void) noexcept { }
48 
50  virtual IRowBase * createRow(void) const = 0;
51 
53  virtual int size(void) const = 0;
54 
56  virtual const type_info * const * columnTypes(void) const = 0;
57 
68  virtual void set(IRowBase * i_row, int i_column, const void * i_value) const = 0;
69  };
70 
72  union DbValue
73  {
74  public:
75 
77  long long llVal;
78 
80  unsigned long long ullVal;
81 
83  bool isVal;
84 
86  float fVal;
87 
89  double dVal;
90 
92  long double dlVal;
93 
95  const char * szVal;
96 
98  DbValue(void) { clear(); }
99 
101  DbValue(int i_value) : DbValue(static_cast<long long>(i_value)) { }
102 
104  DbValue(unsigned int i_value) : DbValue(static_cast<unsigned long long>(i_value)) { }
105 
107  DbValue(long i_value) : DbValue(static_cast<long long>(i_value)) { }
108 
110  DbValue(unsigned long i_value) : DbValue(static_cast<unsigned long long>(i_value)) { }
111 
113  DbValue(long long i_value) {
114  clear();
115  llVal = i_value;
116  }
117 
119  DbValue(unsigned long long i_value) {
120  clear();
121  ullVal = i_value;
122  }
123 
125  DbValue(float i_value) {
126  clear();
127  fVal = i_value;
128  }
129 
131  DbValue(double i_value) {
132  clear();
133  dVal = i_value;
134  }
135 
137  DbValue(long double i_value) {
138  clear();
139  dlVal = i_value;
140  }
141 
143  DbValue(bool i_value) {
144  clear();
145  isVal = i_value;
146  }
147 
149  DbValue(const char * i_value) {
150  clear();
151  szVal = i_value;
152  }
153 
155  void clear(void);
156 
158  template<typename TSrc, typename TDst> static TDst castDouble(const DbValue & i_value);
159  };
160 
163  {
164  public:
165  virtual ~IValueFormatter(void) noexcept { }
166 
175  virtual const char * formatValue(const void * i_value, bool i_isNull = false) = 0;
176  };
177 
178 
181  {
182  public:
183  virtual ~IRowProcessor(void) noexcept { }
184 
186  virtual void processRow(IRowBaseUptr & i_row) = 0;
187  };
188 }
189 
190 #endif // OM_DB_COMMON_H
row factory and setter interface to select row from database
Definition: dbCommon.h:45
virtual const type_info *const * columnTypes(void) const =0
array[rowSize] of type_info for each column, used to convert from db-type to target type
virtual void set(IRowBase *i_row, int i_column, const void *i_value) const =0
field value setter: i_row[i_column] = *i_value
virtual int size(void) const =0
return row size: number of columns
virtual IRowBase * createRow(void) const =0
create new row (tuple, struct or array) initialized with default field values
public interafce for row processing during select, ie: select and append to row list
Definition: dbCommon.h:181
virtual void processRow(IRowBaseUptr &i_row)=0
process row, ie: append to row list or aggregate.
converter for value column (parameter, accumulator or expression value) to string
Definition: dbCommon.h:163
virtual const char * formatValue(const void *i_value, bool i_isNull=false)=0
convert value to string using snprintf.
openM++ exceptions
Definition: omError.h:19
openM++ namespace
Definition: log.h:32
const char * MSSQL_DB_PROVIDER
MS SQL db-provider name.
Definition: dbExec.cpp:23
const char * SQLITE_DB_PROVIDER
SQLite db-provider name.
Definition: dbExec.cpp:14
const char * MYSQL_DB_PROVIDER
MySQL and MariaDB db-provider name.
Definition: dbExec.cpp:17
OpenmException< 4000, dbUnknownErrorMessage > DbException
db-exception
Definition: dbCommon.h:41
std::unique_ptr< IRowBase > IRowBaseUptr
unique pointer to db row
Definition: omHelper.h:238
const char dbUnknownErrorMessage[]
db-exception default error message: "unknown db error"
Definition: dbExec.cpp:38
const char * DB2_DB_PROVIDER
DB2 db-provider name.
Definition: dbExec.cpp:29
const char * PGSQL_DB_PROVIDER
PostgreSQL db-provider name.
Definition: dbExec.cpp:20
const char * ORACLE_DB_PROVIDER
Oracle db-provider name.
Definition: dbExec.cpp:26
OpenM++: public interface for errors and exceptions.
db-row abstract base
Definition: omHelper.h:233
union to pass value to database methods
Definition: dbCommon.h:73
unsigned long long ullVal
value of unsigned integer type
Definition: dbCommon.h:80
DbValue(double i_value)
value initialized by supplied double
Definition: dbCommon.h:131
DbValue(unsigned long i_value)
value initialized by supplied unsigned long
Definition: dbCommon.h:110
DbValue(bool i_value)
value initialized by supplied boolean
Definition: dbCommon.h:143
DbValue(unsigned long long i_value)
value initialized by supplied unsigned long
Definition: dbCommon.h:119
long long llVal
value of integer type
Definition: dbCommon.h:77
DbValue(unsigned int i_value)
value initialized by supplied unsigned integer
Definition: dbCommon.h:104
DbValue(int i_value)
value initialized by supplied integer
Definition: dbCommon.h:101
DbValue(long double i_value)
value initialized by supplied long double
Definition: dbCommon.h:137
DbValue(float i_value)
value initialized by supplied float
Definition: dbCommon.h:125
bool isVal
value of boolean type
Definition: dbCommon.h:83
static TDst castDouble(const DbValue &i_value)
cast between float, double and long double
double dVal
value of double type
Definition: dbCommon.h:89
DbValue(const char *i_value)
value initialized by supplied c-style string
Definition: dbCommon.h:149
DbValue(long long i_value)
value initialized by supplied long
Definition: dbCommon.h:113
const char * szVal
c-style string
Definition: dbCommon.h:95
DbValue(long i_value)
value initialized by supplied long
Definition: dbCommon.h:107
DbValue(void)
zero initialized value
Definition: dbCommon.h:98
long double dlVal
value of long double type
Definition: dbCommon.h:92
float fVal
value of float type
Definition: dbCommon.h:86