-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQL.h
304 lines (243 loc) · 5.15 KB
/
MySQL.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
#ifndef CPP_MYSQL_H
#define CPP_MYSQL_H
#include <mysql/mysql.h>
#include <new>
#include <cstdio>
#include <cerrno>
#include <cstdint>
#include <cstring>
#include <cstdlib>
#include <cassert>
namespace CppMySQL {
#define SetErr(...) \
do { \
snprintf(err_, sizeof(err_), __VA_ARGS__); \
} while (0)
#define SetMySQLErrFallback(...) \
do { \
const char *___err = mysql_error(conn_); \
\
if (___err && ___err[0]) \
SetErr("%s", ___err); \
else \
SetErr(__VA_ARGS__); \
} while (0)
class MySQLStmt;
class MySQLResult;
class MySQL {
public:
inline MySQL(const char *host, const char *user = nullptr,
const char *passwd = nullptr, const char *dbname = nullptr,
uint32_t port = 0) noexcept:
host_(host),
user_(user),
passwd_(passwd),
dbname_(dbname),
port_(port)
{
ClearErr();
}
~MySQL(void) noexcept;
int Init(void) noexcept;
int Connect(void) noexcept;
MySQLStmt *Prepare(size_t nr_bind, const char *q, size_t qlen) noexcept;
inline MySQLStmt *Prepare(size_t nr_bind, const char *q) noexcept
{
return Prepare(nr_bind, q, strlen(q));
}
inline unsigned int Errno(void) noexcept
{
return mysql_errno(conn_);
}
const char *Error(void) noexcept;
inline int RealQuery(const char *q, unsigned long len) noexcept
{
return mysql_real_query(conn_, q, len);
}
inline int RealQuery(const char *q) noexcept
{
return mysql_real_query(conn_, q, strlen(q));
}
MySQLResult *StoreResult(void) noexcept;
inline void Close(void) noexcept
{
mysql_close(conn_);
conn_ = nullptr;
}
inline MYSQL *Conn(void) noexcept
{
return conn_;
}
inline void ClearErr(void) noexcept
{
err_[0] = '\0';
}
private:
MYSQL *conn_ = nullptr;
const char *host_;
const char *user_;
const char *passwd_;
const char *dbname_;
uint16_t port_ = 0;
char err_[128];
};
#define MYSQL_ROW_END_PTR (reinterpret_cast<MYSQL_ROW>(-1ul))
#define IS_MYSQL_ROW_END(ARG) ((ARG) == MYSQL_ROW_END_PTR)
class MySQL;
class MySQLResult {
public:
inline MySQLResult(MySQL *mysql, MYSQL_RES *res) noexcept:
res_(res),
mysql_(mysql)
{
}
inline ~MySQLResult(void) noexcept
{
mysql_free_result(res_);
}
inline uint64_t NumRows(void) noexcept
{
return mysql_num_rows(res_);
}
MYSQL_ROW FetchRow(void) noexcept;
private:
MYSQL_RES *res_;
MySQL *mysql_;
};
class MySQLStmtResult {
public:
inline MySQLStmtResult(MYSQL_STMT *stmt, MYSQL_RES *res,
MYSQL_BIND *bind) noexcept:
res_(res),
stmt_(stmt),
bind_(bind)
{
}
inline MYSQL_BIND *Bind(size_t i)
{
return &bind_[i];
}
inline MYSQL_BIND *Bind(size_t i, enum enum_field_types type, void *buf,
size_t buflen, bool *is_null = nullptr,
size_t *len = nullptr) noexcept
{
MYSQL_BIND *b = &bind_[i];
b->buffer_type = type;
b->buffer = buf;
b->buffer_length = buflen;
b->is_null = is_null;
b->length = len;
return b;
}
inline int StoreResult(void) noexcept
{
return mysql_stmt_store_result(stmt_);
}
inline int BindResult(void) noexcept
{
return mysql_stmt_bind_result(stmt_, bind_);
}
inline size_t NumFields(void) noexcept
{
return mysql_num_fields(res_);
}
inline int Fetch(void) noexcept
{
return mysql_stmt_fetch(stmt_);
}
inline ~MySQLStmtResult(void) noexcept
{
free(bind_);
mysql_free_result(res_);
}
private:
MYSQL_RES *res_;
MYSQL_STMT *stmt_;
MYSQL_BIND *bind_;
};
class MySQLStmt {
public:
inline MySQLStmt(MySQL *mysql, MYSQL_STMT *stmt):
stmt_(stmt),
bind_(nullptr),
mysql_(mysql)
{
ClearErr();
}
inline MySQLStmt(MySQL *mysql, MYSQL_STMT *stmt, MYSQL_BIND *bind):
stmt_(stmt),
bind_(bind),
mysql_(mysql)
{
ClearErr();
}
inline void ClearErr(void) noexcept
{
err_[0] = '\0';
}
inline MYSQL_BIND *Bind(size_t i)
{
return &bind_[i];
}
inline ~MySQLStmt(void) noexcept
{
mysql_stmt_close(stmt_);
if (bind_)
free(bind_);
}
inline MYSQL_BIND *BindStr(size_t i, const char *str, size_t len)
noexcept
{
MYSQL_BIND *b = &bind_[i];
b->buffer_type = MYSQL_TYPE_STRING;
b->buffer = const_cast<void *>(static_cast<const void *>(str));
b->buffer_length = len;
return b;
}
inline MYSQL_BIND *BindStr(size_t i, const char *str) noexcept
{
return BindStr(i, str, strlen(str));
}
inline MYSQL_BIND *Bind(size_t i, enum enum_field_types type,
void *buf = nullptr, size_t buflen = 0) noexcept
{
MYSQL_BIND *b = &bind_[i];
b->buffer_type = type;
b->buffer = buf;
b->buffer_length = buflen;
return b;
}
inline int BindStmt(void) noexcept
{
return mysql_stmt_bind_param(stmt_, bind_);
}
inline int Execute(void) noexcept
{
return mysql_stmt_execute(stmt_);
}
inline unsigned int Errno(void) noexcept
{
return mysql_stmt_errno(stmt_);
}
const char *Error(void) noexcept;
inline unsigned int MySQLErrno(void) noexcept
{
return mysql_errno(mysql_->Conn());
}
inline const char *MySQLError(void) noexcept
{
return mysql_error(mysql_->Conn());
}
MySQLStmtResult *StoreResult(void) noexcept;
private:
MYSQL_STMT *stmt_;
MYSQL_BIND *bind_;
MySQL *mysql_;
char err_[128];
};
#if !defined(KEEP_CPPMYSQL_HELPER_MACROS)
#undef SetErr
#undef SetMySQLErrFallback
#endif
} /* namespace CppMySQL */
#endif /* #ifndef CPP_MYSQL_H */