-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.h
403 lines (341 loc) · 14.2 KB
/
sql.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
#ifndef COT_CQL_H
#define COT_SQL_H
#include <cstdarg>
#include <cstring>
#include <stdint.h>
#include <map>
#include <vector>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/thread.hpp>
#include <mysql/mysql.h>
#include "cot/exception.h"
#include "cot/type.h"
#include "cot/list.h"
namespace cot {
class Parameter
{
public:
static std::string stringify() { return "?"; }
};
class SqlQueryPart
{
public:
static std::string stringify() { return ""; }
};
template<class __MetaField1, class __MetaField2 = Parameter>
class SqlCmp: public SqlQueryPart
{
public:
static std::string stringify() { return ""; }
typedef TypeNil typeList;
};
template<class __MetaField1>
class SqlCmp<__MetaField1, Parameter>: public SqlQueryPart
{
public:
static std::string stringify() { return ""; }
typedef TypePair<typename __MetaField1::itemType, TypeNil> typeList;
};
template<class __MetaField1, class __MetaField2 = Parameter>
class Lt: public SqlCmp<__MetaField1, __MetaField2>
{
public:
static std::string stringify() {
return __MetaField1::stringify() + " < " + __MetaField2::stringify();
}
};
template<class __MetaField1, class __MetaField2 = Parameter>
class Eq: public SqlCmp<__MetaField1, __MetaField2>
{
public:
static std::string stringify() {
return __MetaField1::stringify() + " = " + __MetaField2::stringify();
}
};
template<class __MetaField1, class __MetaField2 = Parameter>
class Gt: public SqlCmp<__MetaField1, __MetaField2>
{
public:
static std::string stringify() {
return __MetaField1::stringify() + " > " + __MetaField2::stringify();
}
};
template<class __First, class __Second>
class SqlBinOp: public SqlQueryPart
{
public:
static std::string stringify() { return ""; }
typedef
typename Append<typename __First::typeList, typename __Second::typeList>::typeList
typeList;
};
template<class __First, class __Second>
class And: public SqlBinOp<__First, __Second>
{
public:
static std::string stringify() {
return std::string("(") + __First::stringify() + " and " + __Second::stringify() + ")";
}
};
template<class __First, class __Second>
class Or: public SqlBinOp<__First, __Second>
{
public:
static std::string stringify() {
return std::string("(") + __First::stringify() + " or " + __Second::stringify() + ")";
}
};
template<class __BooleanExp>
class Where: public SqlQueryPart
{
public:
static std::string stringify() {
return std::string("where ") + __BooleanExp::stringify();
}
typedef typename __BooleanExp::typeList typeList;
};
class All: public SqlQueryPart
{
public:
static std::string stringify() { return ""; }
typedef TypeNil typeList;
};
template<class __Model, class __Where>
class Select: public SqlQueryPart
{
private:
typedef typename __Where::typeList paramTypeList;
typedef typename __Model::typeList resultTypeList;
static const int paramMemSize = MemCount<paramTypeList>::value;
static const int resultMemSize = __Model::totalFieldSize;
static const int paramCount = Length<paramTypeList>::value;
static const int resultCount = __Model::fieldCount;
class Resource
{
public:
bool initialized;
uint8_t * paramMem;
MYSQL_BIND * paramBindMem;
int * paramDataLength;
uint8_t * resultMem;
MYSQL_BIND * resultBindMem;
int * resultDataLength;
MYSQL_STMT * statement;
Resource() {
initialized = false;
}
~Resource() {
mysql_stmt_close(statement);
delete[] paramMem;
delete[] resultMem;
delete[] paramBindMem;
delete[] resultBindMem;
delete[] paramDataLength;
delete[] resultDataLength;
}
};
static boost::thread_specific_ptr<Resource> resource;
static void prepare(MYSQL_BIND * bind, const AbstractValue * value, int * length, bool is_input,
my_bool * is_null = NULL,
my_bool is_unsigned = 0,
my_bool * error = NULL) {
std::memset(bind, 0, sizeof(bind));
if (is_input) {
bind->buffer_type = (enum_field_types)value->get_mysql_in_type_code();
} else {
bind->buffer_type = (enum_field_types)value->get_mysql_out_type_code();
}
bind->buffer = value->get_data_address();
bind->buffer_length = value->get_max_data_length();
bind->length = (unsigned long *)length;
bind->is_null = is_null;
bind->is_unsigned = is_unsigned;
bind->error = error;
}
class ParamPrepareProcedureParam
{
public:
int offset;
int index;
Resource * resource;
};
template<class __Item>
class ParamPrepareProcedure
{
public:
static void exec(void * param_ptr) {
ParamPrepareProcedureParam * param = (ParamPrepareProcedureParam *)param_ptr;
boost::scoped_ptr<AbstractValue> value(new __Item);
value->bind(param->resource->paramMem + param->offset, value->get_max_length());
param->resource->paramDataLength[param->index] = value->get_data_length();
prepare(param->resource->paramBindMem + param->index,
value.get(),
param->resource->paramDataLength + param->index,
true);
param->offset += value->get_max_length();
param->index += 1;
}
};
class ResultPrepareProcedureParam
{
public:
int offset;
int index;
Resource * resource;
};
template<class __Item>
class ResultPrepareProcedure
{
public:
static void exec(void * param_ptr) {
ResultPrepareProcedureParam * param = (ResultPrepareProcedureParam *)param_ptr;
boost::scoped_ptr<AbstractValue> value(new __Item);
value->bind(param->resource->resultMem + param->offset, value->get_max_length());
param->resource->resultDataLength[param->index] = value->get_max_data_length();
prepare(param->resource->resultBindMem + param->index,
value.get(),
param->resource->resultDataLength + param->index,
false);
param->offset += value->get_max_length();
param->index -= 1;
}
};
class ParamCopyProcedureParam
{
public:
int offset;
int index;
va_list * ap;
Resource * resource;
};
template<class __Item>
class ParamCopyProcedure
{
public:
static void exec(void * param_ptr) {
ParamCopyProcedureParam * param = (ParamCopyProcedureParam *)param_ptr;
typedef typename __Item::ctype ctype;
ctype cvalue = va_arg(*param->ap, ctype);
boost::scoped_ptr<AbstractValue> value(__Item::from_ctype(cvalue));
value->bind(param->resource->paramMem + param->offset, value->get_max_length());
param->resource->paramDataLength[param->index] = value->get_data_length();
prepare(param->resource->paramBindMem + param->index,
value.get(),
param->resource->paramDataLength + param->index,
true);
param->offset += __Item::max_length;
param->index += 1;
}
};
class ResultCopyProcedureParam
{
public:
int offset;
int index;
__Model * model;
Resource * resource;
};
template<class __Item>
class ResultCopyProcedure
{
public:
static void exec(void * param_ptr) {
ResultCopyProcedureParam * param = (ResultCopyProcedureParam *)param_ptr;
typedef typename __Item::itemType ValueType;
boost::scoped_ptr<AbstractValue> value(ValueType::from_dump(param->resource->resultMem + param->offset));
__Item::setCppValue(param->model, GET_CPP_REPR(ValueType, value));
param->offset += ValueType::max_length;
param->index -= 1;
}
};
public:
// It seems that there is no solution to the problem of
// removing the first parameter of the variadic function.
static std::vector< boost::shared_ptr<__Model> > with(int n, ...) {
va_list ap;
va_start(ap, n);
if (resource.get() == NULL || !resource->initialized) {
resource.reset(new Resource());
// 0. Preparing statement
resource->statement = mysql_stmt_init(Connection::connection());
std::string theQuery = stringify();
char * theQueryCstr = new char[theQuery.length() + 1];
std::strncpy(theQueryCstr, theQuery.c_str(), theQuery.length());
if (mysql_stmt_prepare(resource->statement, theQueryCstr, theQuery.length()) != 0) {
delete[] theQueryCstr;
throw new Exception(std::string("mysql_stmt_prepare(): ") + \
mysql_stmt_error(resource->statement));
}
delete[] theQueryCstr;
// 1. Parameters
resource->paramMem = new uint8_t[paramMemSize]; // throws an exception if fails, ok for us
resource->paramBindMem = new MYSQL_BIND[paramCount]; // throws an exception if fails, ok for us
resource->paramDataLength = new int[paramCount]; // throws an exception if fails, ok for us
ParamPrepareProcedureParam ppp_param;
ppp_param.offset = 0;
ppp_param.index = 0;
ppp_param.resource = resource.get();
Exec<paramTypeList, ParamPrepareProcedure>::exec(&ppp_param);
if (mysql_stmt_bind_param(resource->statement, resource->paramBindMem) != 0) {
throw new Exception(std::string("mysql_stmt_bind_param(): ") + \
mysql_stmt_error(resource->statement));
}
// 2. Result
resource->resultMem = new uint8_t[resultMemSize]; // throws an exception if fails, ok for us
resource->resultBindMem = new MYSQL_BIND[resultCount]; // throws an exception if fails, ok for us
resource->resultDataLength = new int[resultCount]; // throws an exception if fails, ok for us
ResultPrepareProcedureParam rpp_param;
rpp_param.offset = 0;
rpp_param.index = resultCount - 1;
rpp_param.resource = resource.get();
Exec<resultTypeList, ResultPrepareProcedure>::exec(&rpp_param);
if (mysql_stmt_bind_result(resource->statement, resource->resultBindMem) != 0) {
throw new Exception(std::string("mysql_stmt_bind_result(): ") + \
mysql_stmt_error(resource->statement));
}
resource->initialized = true;
}
// copy the given variadic parameter list into the param bind memory
ParamCopyProcedureParam pcp_param;
pcp_param.offset = 0;
pcp_param.index = 0;
pcp_param.ap = ≈
pcp_param.resource = resource.get();
Exec<paramTypeList, ParamCopyProcedure>::exec(&pcp_param);
// execute the query
if (mysql_stmt_execute(resource->statement) != 0) {
throw new Exception(std::string("mysql_stmt_execute(): ") + \
mysql_stmt_error(resource->statement));
}
// fetching the result
std::vector< boost::shared_ptr<__Model> > result;
int retcode;
while ((retcode = mysql_stmt_fetch(resource->statement)) == 0 || retcode == MYSQL_DATA_TRUNCATED) {
ResultCopyProcedureParam rcp_param;
rcp_param.model = new __Model;
rcp_param.offset = 0;
rcp_param.index = resultCount - 1;
rcp_param.resource = resource.get();
Exec<typename __Model::fieldList, ResultCopyProcedure>::exec(&rcp_param);
result.push_back(boost::shared_ptr<__Model>(rcp_param.model));
}
if (retcode != MYSQL_NO_DATA) {
throw new Exception(std::string("mysql_stmt_fetch(): ") + \
mysql_stmt_error(resource->statement));
}
va_end(ap);
return result;
}
static std::string stringify() {
return std::string("select * from ") + \
__Model::stringify() + \
" " + \
__Where::stringify();
}
};
template<class __Model, class __Where>
boost::thread_specific_ptr< typename Select<__Model, __Where>::Resource >
Select<__Model, __Where>::resource;
} // namespace cot
#endif // COT_SQL_H