-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysqlmgr.hpp
374 lines (312 loc) · 7.13 KB
/
mysqlmgr.hpp
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
#pragma once
#include <string>
#include <mysql.h>
#include <memory.h>
class Stmt;
class Result;
class Stmt
{
private:
MYSQL_STMT* stmt;
size_t count;
MYSQL_BIND* params;
long unsigned int str_length;
public:
Stmt() : stmt(0), count(0), params(0) {}
Stmt(MYSQL_STMT* stmt)
: stmt(stmt), count(mysql_stmt_param_count(stmt)), params(new MYSQL_BIND[count])
{
memset(params, 0, count * sizeof(MYSQL_BIND));
}
~Stmt()
{
if (stmt) mysql_stmt_close(stmt);
delete [] params;
}
Stmt(const Stmt&) = delete;
Stmt& operator=(const Stmt&) = delete;
Stmt(Stmt&& x) : stmt(x.stmt), count(x.count), params(x.params), str_length(x.str_length)
{
x.stmt = 0;
x.params = nullptr;
}
Stmt& operator=(Stmt&& x)
{
stmt = x.stmt;
count = x.count;
params = x.params;
str_length = x.str_length;
x.stmt = 0;
x.params = nullptr;
return *this;
}
operator bool()
{
return !!stmt;
}
inline int step()//the same to execute
{
return execute();
}
inline int execute() //run sql
{
mysql_stmt_bind_param(stmt, params);
return mysql_stmt_execute(stmt);
}
void bind_param(int i, enum_field_types buffer_type, void* buffer, int buffer_length, my_bool* is_null, long unsigned int* length)
{
if (i >= count)
{
printf("Stmt::bind_param error! index:%d, count:%d \n", i, count);
return;
}
MYSQL_BIND& b = params[i];
b.buffer_type = buffer_type;
b.buffer = (char*)buffer;
b.buffer_length = buffer_length;
b.is_null = is_null;
b.length = length;
}
inline int bindint64(int i, int64_t x)
{
bind_param(i, MYSQL_TYPE_LONGLONG, (char*)&x, 0, 0, 0);
}
inline void bindint(int i, const int& x)
{
bind_param(i, x);
}
//char *x should long live tot execute()
inline void bindtext(int i, const char* x, int len)
{
bind_param(i, x, len);
}
//string x cann't be a temp string
inline void bindstring(int i, const std::string& x)
{
const char* c = x.c_str();
bind_param(i, MYSQL_TYPE_STRING, (char*)c, x.size(), 0, &(params[i].buffer_length));
}
private:
inline void bind_param(int i, const int& x)
{
bind_param(i, MYSQL_TYPE_LONG, (char*)&x, 0, 0, 0);
}
inline void bind_param(int i, const std::string& x)
{
const char* c = x.c_str();
bind_param(i, MYSQL_TYPE_STRING, (char*)c, x.size(), 0, &(params[i].buffer_length));
}
inline void bind_param(int i, const char* x, int len)
{
bind_param(i, MYSQL_TYPE_STRING, (char*)x, len, 0, &(params[i].buffer_length));
}
};
class Row
{
private:
MYSQL_ROW row;
unsigned long *lengths;
public:
Row() : row(nullptr), lengths(nullptr) {}
Row(MYSQL_ROW row, unsigned long* lengths) : row(row), lengths(lengths) {}
Row(Row&& x) : row(x.row), lengths(x.lengths)
{
x.row = nullptr;
x.lengths = nullptr;
}
Row& operator=(Row&& x)
{
using std::swap;
swap(*this, x);
return *this;
}
operator bool()
{
return !!row;
}
std::string operator[](size_t n)
{
if (n >= *lengths)
return "";
return std::string(row[n], lengths[n]);
}
std::string columntext(size_t n)
{
return std::string(row[n], lengths[n]);
}
int columnint(size_t n)
{
auto str = std::string(row[n], lengths[n]);
return std::stoi(str);
}
int columnint64(size_t n)
{
auto str = std::string(row[n], lengths[n]);
return std::stoll(str);
}
};
class Result
{
private:
MYSQL_RES* res;
int num_fields;
public:
Result() : res(0), num_fields(0) {}
Result(MYSQL_RES* res) : res(res), num_fields(mysql_num_fields(res)) {}
Result(Result&& r)
{
res = r.res;
r.res = 0;
num_fields = r.num_fields;
}
~Result()
{
if (res) mysql_free_result(res);
}
operator bool()
{
return !!res;
}
Result& operator=(Result&& r)
{
mysql_free_result(res);
res = r.res;
num_fields = r.num_fields;
r.res = 0;
return *this;
}
Result& operator=(const Result&) = delete;
Result(const Result&) = delete;
Row fetch_row()
{
MYSQL_ROW row = mysql_fetch_row(res);
unsigned long* lengths = mysql_fetch_lengths(res);
return Row{row, lengths};
}
inline Row next()
{
return fetch_row();
}
};
class Mysqlmgr
{
private:
Mysqlmgr()
{
handle = mysql_init(0);
//https://dev.mysql.com/doc/refman/5.7/en/c-api-auto-reconnect.html
//set auto reconnect flag. When mysql restarted, we will auto reconnect
my_bool reconnect = 1;
mysql_options(handle, MYSQL_OPT_RECONNECT, &reconnect);
}
MYSQL* handle;
Mysqlmgr(const Mysqlmgr&) = delete;//no copy
Mysqlmgr& operator=(const Mysqlmgr&) = delete;//no assignment
public:
inline static Mysqlmgr& getinstance()
{
static Mysqlmgr instance;
return instance;
};
~Mysqlmgr()
{
if (handle)
{
mysql_close(handle);
}
}
bool connect(const char* host, const char* user, const char* password,
const char* db, unsigned int port, const char* unix_socket, unsigned long client_flag)
{
MYSQL* h = mysql_real_connect(handle, host, user, password,
db, port, unix_socket, client_flag);
if (h)
return true;
else
return false;
}
Stmt getstmt(const std::string &s)
{
MYSQL_STMT* stmt = mysql_stmt_init(handle);
int x = mysql_stmt_prepare(stmt, s.c_str(), s.size());
if (x != 0)
{
return Stmt{};
}
return Stmt{ stmt };
}
inline Result query(const std::string &s)
{
int x = mysql_real_query(handle, s.c_str(), s.size());
if (x != 0)
{
return Result{};
}
return use_result();
}
int execSQL(const std::string &sql)
{
if (!mysql_real_query(handle, sql.c_str(), (unsigned long)sql.length()))
{
return (int)mysql_affected_rows(handle);
}
else
{
return -1;
}
}
inline int more_results()
{
return mysql_more_results(handle);
}
inline int next_result()
{
int x = mysql_next_result(handle);
return x;
}
inline Result use_result()
{
MYSQL_RES* result = mysql_use_result(handle);
if (!result)
{
Result{};
}
return Result{ result };
}
const char* geterror()
{
return mysql_error(handle);
}
int ping()
{
if (!mysql_ping(handle))
{
return 0;
}
return -1;
}
int create_db(const std::string& strName)
{
std::string temp;
temp = "CREATE DATABASE ";
temp += strName;
if (!mysql_real_query(handle, strName.c_str(), (unsigned long)temp.length() ))
return 0;
else
return -1;
}
int drop_db(const std::string& strName)
{
std::string temp;
temp = "DROP DATABASE ";
temp += strName;
if (!mysql_real_query(handle, temp.c_str(), (unsigned long)temp.length()))
{
return 0;
}
else
{
return -1;
}
}
};