-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuery.h
426 lines (338 loc) · 12.3 KB
/
Query.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
* Query.h
*
* Rewritten / author: 2016-02-19 / [email protected]
* Published / author: 2005-08-12 / [email protected]
* Copyright (C) 2015-2018 Michael Griffin
* Copyright (C) 2001-2006 Anders Hedstrom
* This program is made available under the terms of the GNU GPL.
*/
#ifndef _QUERY_H_SQLITE
#define _QUERY_H_SQLITE
#include <sqlite3.h>
#include <iostream>
#include <ctime>
#include <string>
#include <map>
#include <vector>
#include <memory>
#include <stdint.h>
#include <sstream>
namespace SQLW
{
/** SQL Statement execute / result. */
class Query
{
public:
/** Constructor accepting reference to database object. */
Query(Database& dbin);
/** Constructor accepting reference to database object
and query string to execute. */
Query(Database& dbin,const std::string& sql);
~Query();
/** Check if database object is connectable. */
bool isConnected();
/** Return reference to database object. */
Database& getDatabase() const;
/** Return string containing last query executed. */
const std::string& getLastQuery();
/** execute() returns true if query is successful,
does not store result. */
bool execute(const std::string& sql);
/** Execute query and store result. */
sqlite3_stmt *getResult(const std::string& sql);
/** Free stored result, must be called after get_result() before calling
execute()/get_result() again. */
void freeResult();
/** Fetch next result row.
\return false if there was no row to fetch (end of rows) */
// Fill the Resource with the current row, also incriments to next row.
bool fetchRow();
/** Get id of last insert. */
sqlite_int64 getInsertId();
/** Returns 0 if there are no rows to fetch. */
long getNumRows();
/** Number of columns in current result. */
int getNumCols();
/** Last error string. */
std::string getError();
/** Last error code. */
int getErrorCode();
/** Check if column x in current row is null. */
bool isNull(int x);
/**
* @brief Execute Query and Get first Result as the following.
*/
/** Execute query and return first result as a string. */
const char *exeGetCharString(const std::string& sql);
/** Execute query and return first result as a long integer. */
long exeGetResultLong(const std::string& sql);
/** Execute query and return first result as a double. */
double exeGetResultDouble(const std::string& sql);
/**
* @brief On FetchRow, get the current column values as.
*/
/** Return column named x as a string value. */
const char *getString(const std::string& x);
/** Return column x as a string value. */
const char *getString(int x);
/** Return next column as a string value - see rowcount. */
const char *getString();
/**
* @brief On FetchRow, get the current column values as.
*/
/** Return column named x as a long integer. */
long getValue(const std::string& x);
/** Return column x as a long integer. */
long getValue(int x);
/** Return next column as a long integer - see rowcount. */
long getValue();
/**
* @brief On FetchRow, get the current column values as.
*/
/** Return column named x as an unsigned long integer. */
unsigned long getUnsignedValue(const std::string& x);
/** Return column x as an unsigned long integer. */
unsigned long getUnsignedValue(int x);
/** Return next column as an unsigned long integer. */
unsigned long getUnsignedValue();
/**
* @brief On FetchRow, get the current column values as.
*/
/** Return column named x as a 64-bit integer value. */
int64_t getBigInt(const std::string& x);
/** Return column x as a 64-bit integer value. */
int64_t getBigInt(int x);
/** Return next column as a 64-bit integer value. */
int64_t getBigInt();
/** Return column named x as an unsigned 64-bit integer value. */
uint64_t getUnsignedBitInt(const std::string& x);
/** Return column x as an unsigned 64-bit integer value. */
uint64_t getUnsignedBitInt(int x);
/** Return next column as an unsigned 64-bit integer value. */
uint64_t getUnsignedBitInt();
/**
* @brief On FetchRow, get the current column values as.
*/
/** Return column named x as a double. */
double getNumber(const std::string& x);
/** Return column x as a double. */
double getNumber(int x);
/** Return next column as a double. */
double getNumber();
/**
* @brief On FetchRow, Get Field Value by Column Name and Type
*/
// Unsigned Int 32 bit
void getFieldValue(uint32_t &type, int index)
{
//UNUSED(type);
type = static_cast<uint32_t>(sqlite3_column_int(res, index));
}
// Int
void getFieldValue(int &type, int index)
{
//UNUSED(type);
type = static_cast<int>(sqlite3_column_int(res, index));
}
// Long or Time_T for Date/Time.
void getFieldValue(long &type, int index)
{
type = static_cast<long>(sqlite3_column_int64(res, index));
}
// Long Long
void getFieldValue(long long &type, int index)
{
type = static_cast<long long>(sqlite3_column_int64(res, index));
}
// Double
void getFieldValue(double &type, int index)
{
type = static_cast<double>(sqlite3_column_double(res, index));
}
// Float
void getFieldValue(float &type, int index)
{
type = static_cast<float>(sqlite3_column_double(res, index));
}
// Long Double
void getFieldValue(long double &type, int index)
{
type = static_cast<long double>(sqlite3_column_double(res, index));
}
// Std::String
void getFieldValue(std::string &type, int index)
{
const char *text_result = reinterpret_cast<const char *>(sqlite3_column_text(res , index));
type = text_result;
}
// Single Character
void getFieldValue(char &type, int index)
{
const char *result = reinterpret_cast<const char *>(sqlite3_column_text(res , index));
type = result[0];
}
// Char *
const char *getFieldValue(char *, int index)
{
const char *result = reinterpret_cast<const char *>(sqlite3_column_text(res , index));
return result;
}
/*
// Char *, or BLOB. sqlite3_column_blob ? test this lateron!
const char *getFieldValue(char *type, int index)
{
if (strcmp(type, "BLOB") == 0)
{
// Test if we need to get len in bytes then cutoff!
const char *result = reinterpret_cast<const char *>(sqlite3_column_blob(res , index));
return result;
}
const char *result = reinterpret_cast<const char *>(sqlite3_column_text(res , index));
return result;
}*/
// Handle Boolean Values.
void getFieldValue(bool &type, int index)
{
int result = static_cast<int>(sqlite3_column_int(res , index));
if (result == 1)
type = true;
else
type = false;
}
/**
* @brief Template to Get Field Value by Column Name and Populate Type passed.
*/
template <typename TT, typename T>
T getFieldByName(const TT &tt, T &t)
{
// Grab the index of the Matching Field Name
std::map<std::string, int>::iterator it;
int index = 0;
if (m_nmap.empty())
{
return t;
}
it = m_nmap.find(tt);
if (it != m_nmap.end())
{
index = it->second;
--index;
}
else
{
return t;
}
if (index >= 0)
{
switch(sqlite3_column_type(res, index))
{
case SQLITE_NULL:
break;
default :
// Template to pull field value dynamically from overloads.
getFieldValue(t, index);
return t;
}
}
return t;
}
std::string getFieldType(float &)
{
std::string result("%f");
return result;
}
std::string getFieldType(double &)
{
std::string result("%d");
return result;
}
std::string getFieldType(long long &)
{
// %llu or lld ?!?
std::string result("%llu");
return result;
}
std::string getFieldType(char &)
{
std::string result("%Q");
return result;
}
std::string getFieldType(bool &)
{
std::string result("%d");
return result;
}
std::string getFieldType(char *)
{
std::string result("%Q");
return result;
}
std::string getFieldType(std::string &)
{
std::string result("%Q");
return result;
}
std::string getFieldType(long &)
{
std::string result("%ld");
return result;
}
std::string getFieldType(int &)
{
std::string result("%d");
return result;
}
std::string getFieldType(uint32_t &)
{
std::string result("%d");
return result;
}
/**
* @brief This takes a Description, and a Type, for translation inserts statements (Column) VALUES (TYPE)
*/
template <typename TT, typename T>
std::pair<std::string, std::string> translateFieldName(const TT &tt, T &t)
{
std::pair<std::string, std::string> pair;
// Template to pull field type, int = %d, text = '%q' etc..
pair = std::make_pair(tt, getFieldType(t));
return pair;
}
/**
* Create a new Executate Transaction
*/
bool executeTransaction(const std::vector<std::string> &statements);
private:
/** Hide the copy constructor. */
/*
Query(const Query& q)
: m_db(q.getDatabase())
{
std::cout << "Query Constructor: q.getDatabase()" << std::endl;
*this = q;
}*/
/** Hide the assignment operator. */
/*
Query& operator=(const Query&)
{
std::cout << "Query Constructor: return *this" << std::endl;
return *this;
}*/
/** Print error to debug class. */
void queryError(const std::string&);
Database& m_db; ///< Reference to database object
Database::DatabasePool *odb; ///< Connection pool handle
sqlite3_stmt *res; ///< Stored result
bool row; ///< true if fetch_row succeeded
short rowcount; ///< Current column pointer in result
std::string m_tmpstr; ///< Used to store result in get_string() call
std::string m_last_query; ///< Last query executed
int cache_rc; ///< Cached result after call to get_result()
bool cache_rc_valid; ///< Indicates cache_rc is valid
int m_row_count; ///< 0 if get_result() returned no rows
std::map<std::string, int> m_nmap; ///< map translating column names to index
int m_num_cols; ///< number of columns in result
};
} // namespace SQLW
#endif // _QUERY_H