-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmysql_traits.hpp
More file actions
300 lines (246 loc) · 6.03 KB
/
mysql_traits.hpp
File metadata and controls
300 lines (246 loc) · 6.03 KB
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
/*
Mysql C++ traits for sqlgg
by ygrek
2014-06-08
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
For more information, please refer to <http://unlicense.org/>
*/
#include <mysql/mysql.h>
#define SQLGG_STR(x) x
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#if defined(SQLGG_DEBUG)
#include <stdio.h>
#endif
struct mysql_traits
{
typedef int Int;
typedef std::string Text;
typedef Text Any;
struct row_t
{
template<class T> T* alloc(size_t const count) { return (T*)calloc(count,sizeof(T)); }
row_t(MYSQL_STMT* stmt, size_t count) : count(count), stmt(stmt)
{
bind = alloc<MYSQL_BIND>(count);
error = alloc<my_bool>(count);
length = alloc<unsigned long>(count);
is_null = alloc<my_bool>(count);
}
~row_t()
{
free(bind);
free(error);
free(length);
free(is_null);
}
const size_t count;
MYSQL_STMT* stmt;
MYSQL_BIND* bind;
unsigned long* length;
my_bool* error;
my_bool* is_null;
};
typedef row_t& row;
typedef MYSQL* connection;
static void get_column_Int(row r, int index, Int& data)
{
// nothing
}
static void get_column_Text(row r, int index, Text& data)
{
unsigned long const& length = r.length[index];
MYSQL_BIND& bind = r.bind[index];
if (length > 0)
{
data.resize(length);
bind.buffer = (void*)(data.c_str());
bind.buffer_length = length;
mysql_stmt_fetch_column(r.stmt, r.bind, index, 0);
}
//data.resize(length);
}
static void bind_column_Int(row r, int index, Int& data)
{
MYSQL_BIND& bind = r.bind[index];
data = 0;
bind.buffer_type = MYSQL_TYPE_LONG;
bind.buffer = (void*)&data;
bind.is_null = &r.is_null[index];
bind.length = &r.length[index];
bind.error = &r.error[index];
}
static void bind_column_Text(row r, int index, Text& data)
{
MYSQL_BIND& bind = r.bind[index];
bind.buffer_type = MYSQL_TYPE_STRING;
bind.buffer = 0;
bind.buffer_length = 0;
bind.is_null = &r.is_null[index];
bind.length = &r.length[index];
bind.error = &r.error[index];
}
static void set_param(row r, const Text& val, int index)
{
MYSQL_BIND& bind = r.bind[index];
r.length[index] = val.size();
bind.length = &r.length[index];
bind.buffer_length = val.size();
bind.buffer_type = MYSQL_TYPE_STRING;
bind.buffer = (void*)val.c_str();
}
static void set_param(row r, const Int& val, int index)
{
MYSQL_BIND& bind = r.bind[index];
bind.buffer_type = MYSQL_TYPE_LONG;
bind.buffer = (void*)&val;
}
class mysql_stmt
{
public:
mysql_stmt(MYSQL_STMT* stmt) : stmt(stmt)
{
}
void close()
{
if (stmt) mysql_stmt_close(stmt);
stmt = NULL;
}
virtual ~mysql_stmt()
{
close();
}
/*
mysql_stmt& operator=(MYSQL_STMT* v)
{
close();
stmt = v;
}
*/
operator MYSQL_STMT*()
{
return stmt;
}
private:
MYSQL_STMT* stmt;
};
struct no_params
{
void set_params(row) {}
enum { count = 0 };
};
template<class T>
struct no_binder
{
void get(row,T) {}
void bind(row) {}
enum { count = 0 };
};
class statement
{
private:
connection db;
char const* sql;
mysql_stmt stmt;
bool ready;
public:
statement(connection aDb, char const* sql) : db(aDb), sql(sql), stmt(mysql_stmt_init(aDb))
{
ready = false;
}
bool prepare()
{
if (ready)
{
return true;
}
if (!stmt)
{
#if defined(SQLGG_DEBUG)
fprintf(stderr, " mysql_stmt_init(), out of memory\n");
#endif
return false;
}
if (mysql_stmt_prepare(stmt, sql, strlen(sql)))
{
#if defined(SQLGG_DEBUG)
fprintf(stderr, " mysql_stmt_prepare(), failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
#endif
return false;
}
ready = true;
return true;
}
template<class T, class Binder, class Params>
bool select(T result, Binder binder, Params params)
{
prepare();
if (Params::count != mysql_stmt_param_count(stmt))
{
#if defined(SQLGG_DEBUG)
fprintf(stderr, " wrong params count %u != %lu\n",Params::count,mysql_stmt_param_count(stmt));
#endif
return false;
}
if (Binder::count != mysql_stmt_field_count(stmt))
{
#if defined(SQLGG_DEBUG)
fprintf(stderr, " wrong number of columns %u != %u\n",Binder::count,mysql_stmt_field_count(stmt));
#endif
return false;
}
row_t r_params(stmt,Params::count);
params.set_params(r_params);
if (mysql_stmt_bind_param(stmt, r_params.bind))
{
#if defined(SQLGG_DEBUG)
fprintf(stderr, " mysql_stmt_bind_param() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
#endif
return false;
}
if (mysql_stmt_execute(stmt))
{
#if defined(SQLGG_DEBUG)
fprintf(stderr, " mysql_stmt_execute(), failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
#endif
return false;
}
row_t r(stmt,Binder::count);
if (0 != Binder::count)
{
binder.bind(r);
if (mysql_stmt_bind_result(stmt, r.bind))
{
#if defined(SQLGG_DEBUG)
fprintf(stderr, " mysql_stmt_bind_result() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
#endif
return false;
}
}
while (true)
{
int res = mysql_stmt_fetch(stmt);
if (0 != res && MYSQL_DATA_TRUNCATED != res) break;
binder.get(r,result);
}
return true;
}
template<class Params>
bool execute(Params params)
{
int dummy = 0;
return select(dummy,no_binder<int>(),params);
}
}; // statement
}; // mysql_traits