OpenM++ runtime library (libopenm)
dbValue.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_VALUE_H
9#define DB_VALUE_H
10
12#include "helper.h"
13#include "md5.h"
14
15using namespace std;
16
17namespace openm
18{
21 {
22 public:
27 DbValueSetter(const type_info & i_type);
28
30 void set(const void * i_value, DbValue & o_dbVal);
31
33 const type_info & valueType(void) const { return typeOf; };
34
35 private:
37 const type_info & typeOf;
38
40 function<void(const void * i_value, DbValue & o_dbVal)> doSetValue;
41
43 template<typename TValue> static void setValue(const void * i_value, DbValue & o_dbVal)
44 {
45 TValue val = *static_cast<const TValue *>(i_value);
46 o_dbVal = DbValue(val);
47 }
48
49 private:
50 // DbValueSetter(const DbValueSetter &) = delete;
51 DbValueSetter & operator=(const DbValueSetter &) = delete;
52 };
53
55 template<typename TValue>
56 int FormatHandler(const void * i_value, size_t i_size, char * io_buffer, const char * i_format)
57 {
58 TValue val = *static_cast<const TValue *>(i_value);
59 return snprintf(io_buffer, i_size, i_format, val);
60 }
61
63 template<typename TValue>
64 int FloatSqlFormatHandler(const void * i_value, size_t i_size, char * io_buffer, const char * i_format)
65 {
66 TValue val = *static_cast<const TValue *>(i_value);
67
68 if (isfinite(val)) {
69 return snprintf(io_buffer, i_size, i_format, val);
70 }
71 // else copy "NULL" into output buffer
72 strncpy(io_buffer, "NULL", i_size);
73 io_buffer[i_size - 1] = '\0';
74 return (int)strnlen(io_buffer, i_size);
75 }
76
78 extern int BoolFormatHandler(const void * i_value, size_t i_size, char * io_buffer);
79
81 extern int BoolSqlFormatHandler(const void * i_value, size_t i_size, char * io_buffer);
82
84 extern int StrFormatHandler(const void * i_value, size_t i_size, char * io_buffer);
85
87 extern int StrSqlFormatHandler(const void * i_value, size_t i_size, char * io_buffer);
88
89 inline const char * nullValueString = "null";
90 inline const char * trueValueString = "true";
91 inline const char * falseValueString = "false";
94 template<const size_t valueLen>
96 {
97 public:
104 ValueFormatterBase(const type_info & i_type, bool i_isSqlFormat = false, const char * i_doubleFormat = "") :
105 typeOf(i_type),
106 doFormatValue(nullptr)
107 {
108 if (typeOf == typeid(char)) {
109 doFormatValue = bind(FormatHandler<char>, placeholders::_1, placeholders::_2, placeholders::_3, "%hhd");
110 }
111 if (typeOf == typeid(unsigned char)) {
112 doFormatValue = bind(FormatHandler<unsigned char>, placeholders::_1, placeholders::_2, placeholders::_3, "%hhu");
113 }
114 if (typeOf == typeid(short)) {
115 doFormatValue = bind(FormatHandler<short>, placeholders::_1, placeholders::_2, placeholders::_3, "%hd");
116 }
117 if (typeOf == typeid(unsigned short)) {
118 doFormatValue = bind(FormatHandler<unsigned short>, placeholders::_1, placeholders::_2, placeholders::_3, "%hu");
119 }
120 if (typeOf == typeid(int)) {
121 doFormatValue = bind(FormatHandler<int>, placeholders::_1, placeholders::_2, placeholders::_3, "%d");
122 }
123 if (typeOf == typeid(unsigned int)) {
124 doFormatValue = bind(FormatHandler<unsigned int>, placeholders::_1, placeholders::_2, placeholders::_3, "%u");
125 }
126 if (typeOf == typeid(long)) {
127 doFormatValue = bind(FormatHandler<long>, placeholders::_1, placeholders::_2, placeholders::_3, "%ld");
128 }
129 if (typeOf == typeid(unsigned long)) {
130 doFormatValue = bind(FormatHandler<unsigned long>, placeholders::_1, placeholders::_2, placeholders::_3, "%lu");
131 }
132 if (typeOf == typeid(long long)) {
133 doFormatValue = bind(FormatHandler<long long>, placeholders::_1, placeholders::_2, placeholders::_3, "%lld");
134 }
135 if (typeOf == typeid(unsigned long long)) {
136 doFormatValue = bind(FormatHandler<unsigned long long>, placeholders::_1, placeholders::_2, placeholders::_3, "%llu");
137 }
138 if (typeOf == typeid(int8_t)) {
139 doFormatValue = bind(FormatHandler<int8_t>, placeholders::_1, placeholders::_2, placeholders::_3, "%hhd");
140 }
141 if (typeOf == typeid(uint8_t)) {
142 doFormatValue = bind(FormatHandler<uint8_t>, placeholders::_1, placeholders::_2, placeholders::_3, "%hhu");
143 }
144 if (typeOf == typeid(int16_t)) {
145 doFormatValue = bind(FormatHandler<int16_t>, placeholders::_1, placeholders::_2, placeholders::_3, "%hd");
146 }
147 if (typeOf == typeid(uint16_t)) {
148 doFormatValue = bind(FormatHandler<uint16_t>, placeholders::_1, placeholders::_2, placeholders::_3, "%hu");
149 }
150 if (typeOf == typeid(int32_t)) {
151 doFormatValue = bind(FormatHandler<int32_t>, placeholders::_1, placeholders::_2, placeholders::_3, "%d");
152 }
153 if (typeOf == typeid(uint32_t)) {
154 doFormatValue = bind(FormatHandler<uint32_t>, placeholders::_1, placeholders::_2, placeholders::_3, "%u");
155 }
156 if (typeOf == typeid(int64_t)) {
157 doFormatValue = bind(FormatHandler<int64_t>, placeholders::_1, placeholders::_2, placeholders::_3, "%lld");
158 }
159 if (typeOf == typeid(uint64_t)) {
160 doFormatValue = bind(FormatHandler<uint64_t>, placeholders::_1, placeholders::_2, placeholders::_3, "%llu");
161 }
162
163 if (!i_isSqlFormat) { // not a SQL constant: simple snprintf formatted values
164
165 if (typeOf == typeid(bool)) {
166 doFormatValue = BoolFormatHandler;
167 }
168 if (typeOf == typeid(float)) {
169 doFormatValue = bind(
170 FormatHandler<float>,
171 placeholders::_1, placeholders::_2, placeholders::_3,
172 ((i_doubleFormat != nullptr && i_doubleFormat[0] != '\0') ? i_doubleFormat : "%.15g")
173 );
174 }
175 if (typeOf == typeid(double)) {
176 doFormatValue = bind(
177 FormatHandler<double>,
178 placeholders::_1, placeholders::_2, placeholders::_3,
179 ((i_doubleFormat != nullptr && i_doubleFormat[0] != '\0') ? i_doubleFormat : "%.15g")
180 );
181 }
182 if (typeOf == typeid(long double)) {
183 doFormatValue = bind(
184 FormatHandler<long double>,
185 placeholders::_1, placeholders::_2, placeholders::_3,
186 ((i_doubleFormat != nullptr && i_doubleFormat[0] != '\0') ? i_doubleFormat : "%.15g")
187 );
188 }
189 if (typeOf == typeid(string)) {
190 doFormatValue = StrFormatHandler;
191 }
192 }
193 else { // SQL constant: 'quoted strings', '1' or '0' for booleans, NULL if floats are not finite
194
195 if (typeOf == typeid(bool)) {
196 doFormatValue = BoolSqlFormatHandler;
197 }
198 if (typeOf == typeid(float)) {
199 doFormatValue = bind(
200 FloatSqlFormatHandler<float>,
201 placeholders::_1, placeholders::_2, placeholders::_3,
202 ((i_doubleFormat != nullptr && i_doubleFormat[0] != '\0') ? i_doubleFormat : "%.15g")
203 );
204 }
205 if (typeOf == typeid(double)) {
206 doFormatValue = bind(
207 FloatSqlFormatHandler<double>,
208 placeholders::_1, placeholders::_2, placeholders::_3,
209 ((i_doubleFormat != nullptr && i_doubleFormat[0] != '\0') ? i_doubleFormat : "%.15g")
210 );
211 }
212 if (typeOf == typeid(long double)) {
213 doFormatValue = bind(
214 FloatSqlFormatHandler<long double>,
215 placeholders::_1, placeholders::_2, placeholders::_3,
216 ((i_doubleFormat != nullptr && i_doubleFormat[0] != '\0') ? i_doubleFormat : "%.15g")
217 );
218 }
219 if (typeOf == typeid(string)) {
220 doFormatValue = StrSqlFormatHandler;
221 }
222 }
223
224 if (doFormatValue == nullptr)
225 throw DbException("invalid value type to convert to string"); // conversion to target type is not supported
226 }
227
233 ValueFormatterBase(const type_info & i_type, const char * i_doubleFormat = "") :
234 ValueFormatterBase(i_type, false, i_doubleFormat) {}
235
244 const char * formatValue(const void * i_value, bool i_isNull = false) override
245 {
246 if (i_isNull || i_value == nullptr) return nullValueString;
247
248 doFormatValue(i_value, valueLen, valueStr);
249 return valueStr;
250 }
251
252 private:
254 const type_info & typeOf;
255
257 char valueStr[valueLen + 1] = "";
258
259 // value to string converter handler
260 function<int(const void * i_value, size_t i_size, char * io_buffer)> doFormatValue;
261
262 private:
263 ValueFormatterBase(const ValueFormatterBase &) = delete;
264 ValueFormatterBase & operator=(const ValueFormatterBase &) = delete;
265 };
266
267 typedef ValueFormatterBase<OM_STR_DB_MAX> ValueFormatter;
272 {
273 public:
275 ValueArray(const type_info & i_type, size_t i_size);
276
278 ~ValueArray(void) noexcept { try { cleanup(); } catch (...) {} }
279
281 void cleanup(void) noexcept;
282
284 const type_info & typeOf(void) const { return valueType; }
285
287 const size_t sizeOf(void) const { return valueCount; }
288
290 void * ptr(void) const { return valueArr; }
291
292 private:
293 const type_info & valueType; // value type
294 const size_t valueCount; // size of array: number of values
295 void * valueArr; // array data
296
297 // create new array of primitive values and fill it with NaN
298 template<typename TVal> TVal * newValueArray(size_t i_size)
299 {
300 TVal * pData = new TVal[i_size];
301 fill(static_cast<TVal *>(pData), &(static_cast<TVal *>(pData))[i_size], numeric_limits<TVal>::quiet_NaN());
302 return pData;
303 }
304
305 private:
306 ValueArray(const ValueArray & i_src) = delete;
307 ValueArray & operator=(const ValueArray & i_src) = delete;
308 };
309
311 struct ValueRow : public IRowBase
312 {
315
317 unique_ptr<int32_t[]> idArr;
318
321
323 const type_info & typeOf;
324
327
329 string strVal;
330
332 ValueRow(int i_idCount, const type_info & i_type);
333 ~ValueRow(void) noexcept { }
334 };
335
338 {
339 public:
341 ValueRowAdapter(int i_idCount, const type_info & i_type);
342
343 // IRowAdapter interface implementation
344 IRowBase * createRow(void) const override { return new ValueRow(idCount, typeOf); }
345 int size(void) const override { return idCount + 1; }
346 const type_info * const * columnTypes(void) const override { return typeVec.data(); }
347
349 void set(IRowBase * i_row, int i_column, const void * i_value) const override;
350
351 private:
353 int idCount;
354
356 const type_info & typeOf;
357
358 vector<const type_info *> typeVec; // column types
359
360 // set value column method handler
361 function<void(const void * i_value, DbValue & o_dbVal)> doSetValue;
362
363 // value column setter casting for numeric and boolean types
364 template<typename TValue> static void setValueColumn(const void * i_value, DbValue & o_dbVal)
365 {
366 TValue val = *static_cast<const TValue *>(i_value);
367 o_dbVal = DbValue(val);
368 }
369
370 private:
371 ValueRowAdapter(const ValueRowAdapter &) = delete;
372 ValueRowAdapter & operator=(const ValueRowAdapter &) = delete;
373 };
374
377 {
378 public:
380 ValueRowDigester(int i_idCount, const type_info & i_type, MD5 * io_md5, const char * i_doubleFormat = "");
381
383 void processRow(IRowBaseUptr & i_row) override;
384
385 private:
387 int idCount;
388
390 const type_info & typeOf;
391
393 MD5 * md5;
394
396 unique_ptr<IValueFormatter> fmtValue;
397
398 private:
399 ValueRowDigester(const ValueRowDigester &) = delete;
400 ValueRowDigester & operator=(const ValueRowDigester &) = delete;
401 };
402}
403
404#endif // DB_VALUE_H
compute MD5 hash
Definition: md5.h:38
set DbValue by casting a pointer to the value
Definition: dbValue.h:21
void set(const void *i_value, DbValue &o_dbVal)
set DbValue by casting a pointer to the value
Definition: dbValue.cpp:115
DbValueSetter(const type_info &i_type)
create DbValue setter.
Definition: dbValue.cpp:83
const type_info & valueType(void) const
return value type
Definition: dbValue.h:33
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
converter for value column (parameter, accumulator or expression value) to string
Definition: dbCommon.h:163
false boolean value as string
Definition: dbValue.h:96
ValueFormatterBase(const type_info &i_type, bool i_isSqlFormat=false, const char *i_doubleFormat="")
converter for value column into string.
Definition: dbValue.h:104
ValueFormatterBase(const type_info &i_type, const char *i_doubleFormat="")
converter for value column into string.
Definition: dbValue.h:233
const char * formatValue(const void *i_value, bool i_isNull=false) override
IValueFormatter interface implementation: convert value to string using snprintf.
Definition: dbValue.h:244
Row adapter to select row from value table (parameter, accumulator or expression)
Definition: dbValue.h:338
IRowBase * createRow(void) const override
create new row (tuple, struct or array) initialized with default field values
Definition: dbValue.h:344
const type_info *const * columnTypes(void) const override
array[rowSize] of type_info for each column, used to convert from db-type to target type
Definition: dbValue.h:346
int size(void) const override
return row size: number of columns
Definition: dbValue.h:345
void set(IRowBase *i_row, int i_column, const void *i_value) const override
IRowAdapter interface implementation: set column value.
Definition: dbValue.cpp:229
ValueRowAdapter(int i_idCount, const type_info &i_type)
new row adapter for value table row, use std::string type for VARCHAR input parameters
Definition: dbValue.cpp:189
row processor to calculate digest of value table row (parameter, accumulator or expression)
Definition: dbValue.h:377
void processRow(IRowBaseUptr &i_row) override
IRowProcessor implementation: append row to digest.
Definition: dbValue.cpp:265
ValueRowDigester(int i_idCount, const type_info &i_type, MD5 *io_md5, const char *i_doubleFormat="")
new row digester for value table row, use std::string type for VARCHAR input parameters
Definition: dbValue.cpp:256
OpenM++ data library: public interface for db common structures.
OpenM++ common helper utilities.
openM++ namespace
Definition: log.h:32
const char * trueValueString
NULL value as string.
Definition: dbValue.h:90
int BoolSqlFormatHandler(const void *i_value, size_t i_size, char *io_buffer)
convert bool value to SQL constant: return "1" or "0"
Definition: dbValue.cpp:54
int StrSqlFormatHandler(const void *i_value, size_t i_size, char *io_buffer)
convert string value into SQL constant: return 'quoted source value'
Definition: dbValue.cpp:72
OpenmException< 4000, dbUnknownErrorMessage > DbException
db-exception
Definition: dbCommon.h:41
std::unique_ptr< IRowBase > IRowBaseUptr
unique pointer to db row
Definition: omHelper.h:236
ValueFormatterBase< OM_CODE_DB_MAX > ShortFormatter
parameter, accumulator or expression value formatter
Definition: dbValue.h:268
int FormatHandler(const void *i_value, size_t i_size, char *io_buffer, const char *i_format)
convert value to string using snprintf: integer and float values.
Definition: dbValue.h:56
int BoolFormatHandler(const void *i_value, size_t i_size, char *io_buffer)
convert bool value to string: return "true" or "false"
Definition: dbValue.cpp:44
const char * falseValueString
true boolean value as string
Definition: dbValue.h:91
int FloatSqlFormatHandler(const void *i_value, size_t i_size, char *io_buffer, const char *i_format)
convert value to string using snprintf: integer and float values.
Definition: dbValue.h:64
int StrFormatHandler(const void *i_value, size_t i_size, char *io_buffer)
convert value to string: make a copy of source string
Definition: dbValue.cpp:64
db-row abstract base
Definition: omHelper.h:231
microdata csv value formatter: long strings are not supported
Definition: dbValue.h:272
ValueArray(const type_info &i_type, size_t i_size)
create new array of values or array of string
Definition: dbValue.cpp:124
const size_t sizeOf(void) const
array size: value count
Definition: dbValue.h:287
void cleanup(void) noexcept
cleanup resources: free memory
Definition: dbValue.cpp:156
const type_info & typeOf(void) const
type of value
Definition: dbValue.h:284
void * ptr(void) const
return pointer to array
Definition: dbValue.h:290
~ValueArray(void) noexcept
cleanup resources: free memory
Definition: dbValue.h:278
value table row: parameter, accumulators or expression tables
Definition: dbValue.h:312
DbValue dbVal
value if type is numeric: integer, double, boolean etc.
Definition: dbValue.h:326
int idCount
key size: dimension count, acc_id, sub_id, expr_id
Definition: dbValue.h:314
const type_info & typeOf
value type, use std::string type for VARCHAR input parameters
Definition: dbValue.h:323
unique_ptr< int32_t[]> idArr
key columns: dimemnsions id and acc_id, sub_id, expr_id
Definition: dbValue.h:317
bool isNotNull
if true then value is NOT NULL
Definition: dbValue.h:320
ValueRow(int i_idCount, const type_info &i_type)
create empty row of value table
Definition: dbValue.cpp:171
string strVal
value if type is string
Definition: dbValue.h:329
union to pass value to database methods
Definition: dbCommon.h:73