-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlxx.h
412 lines (374 loc) · 13.1 KB
/
mysqlxx.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
///////////////////////////////////////////////////////////////////////////////
/// \author (c) Anthony Fieroni ([email protected])
/// 2017, Plovdiv, Bulgaria
///
/// \license The MIT License (MIT)
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////
#ifndef _MYSQLXX_H_
#define _MYSQLXX_H_
#include "sqlxx.h"
#include <mysql/mysql.h>
#include <mysql/errmsg.h>
namespace mysqlxx {
/*
* Database class
*/
class db
{
public:
db(std::string const& name) : db_(nullptr), name_(name), open_(false) {}
~db() { close(); }
db(db&&) = delete; // no move
db(db const&) = delete; // no copy
db& operator=(db&&) = delete; // no assignment
db& operator=(db const&) = delete; // no assignment
// MySQL access
#ifdef USE_SHARED_CONNECTION
sqlxx::connection_lock<::MYSQL> operator()() const {
return { mutex_, db_ };
}
#else
inline ::MYSQL* operator()() const { return db_; }
#endif
// open (connect) the database
bool open(char const* host, char const* user, char const* pass, char const* name = nullptr) {
#ifdef USE_SHARED_CONNECTION
sqlxx::connection_lock<::MYSQL> lock(mutex_, db_);
#endif
static library_init init;
if (open_) return true;
db_ = ::mysql_init(nullptr);
if (!name) name = name_.c_str();
else name_ = name;
open_ = !!db_;
if (open_) {
::my_bool reconnect = 1; unsigned long trunc = 0;
::mysql_options(db_, MYSQL_OPT_RECONNECT, &reconnect);
::mysql_options(db_, MYSQL_REPORT_DATA_TRUNCATION, &trunc);
}
if (db_ && (!::mysql_real_connect(db_, host, user, pass, name, 0, nullptr, 0)
|| ::mysql_query(db_, std::string("use " + name_ + ';').c_str()) != 0)) {
::mysql_close(db_);
open_ = false;
db_ = nullptr;
}
return open_;
}
// close the database
void close() {
#ifdef USE_SHARED_CONNECTION
sqlxx::connection_lock<::MYSQL> lock(mutex_, db_);
#endif
if (!open_) return;
::mysql_close(db_);
open_ = false;
db_ = nullptr;
}
// returns true if the database is open
inline bool is_open() const { return open_; }
// MySQL version
inline std::string version() { return std::string(MYSQL_BASE_VERSION); }
// database defragmentation
void vacuum() {
std::string query("SELECT Concat('OPTIMIZE TABLE ',TABLE_NAME, ';') ");
query += "FROM INFORMATION_SCHEMA.TABLES WHERE table_schema='" + name_ + '\'';
::mysql_query((*this)(), query.c_str());
}
private:
struct library_init {
library_init() { ::mysql_library_init(0, nullptr, nullptr); }
~library_init() { ::mysql_library_end(); }
};
::MYSQL* db_; // associated db
std::string name_; // db name
bool open_; // db open status
#ifdef USE_SHARED_CONNECTION
mutable std::mutex mutex_;
#endif
};
class statement : public sqlxx::statement {
public:
statement(db const& db, ::MYSQL_STMT* stmt) : db_(db), res_(nullptr), stmt_(stmt) {
#ifdef USE_SHARED_CONNECTION
auto&& lock = db_();
#endif
switch(::mysql_stmt_errno(stmt_)) {
case 0: result_ = SQL_OK; break;
case CR_COMMANDS_OUT_OF_SYNC: result_ = SQL_IMPROPER; return;
case CR_OUT_OF_MEMORY: result_ = SQL_NO_MEMORY; return;
case CR_SERVER_GONE_ERROR:
case CR_SERVER_LOST: result_ = SQL_SERVER_LOST; return;
case CR_UNKNOWN_ERROR:
default: result_ = SQL_UNKNOWN_ERROR; return;
}
if ((res_ = ::mysql_stmt_result_metadata(stmt_))) {
num_ = ::mysql_num_fields(res_);
}
last_id_ = ::mysql_stmt_insert_id(stmt_);
affected_rows_ = ::mysql_stmt_affected_rows(stmt_);
std::int64_t(affected_rows_) < 0 && (affected_rows_ = 0);
}
statement(statement&&) = delete;
statement(statement const&) = delete;
statement& operator=(statement&&) = delete;
statement& operator=(statement const&) = delete;
~statement() override {
#ifdef USE_SHARED_CONNECTION
auto&& lock = db_();
#endif
if (res_) ::mysql_free_result(res_);
::mysql_stmt_close(stmt_);
}
sqlxx::row next() override {
if (!res_ || !num_) return {};
sqlxx::row row;
std::vector<MYSQL_BIND> mbinds(num_);
for(auto &bind : mbinds) {
bind.length = &bind.buffer_length;
}
#ifdef USE_SHARED_CONNECTION
auto&& lock = db_();
#endif
::mysql_stmt_bind_result(stmt_, mbinds.data());
int res = ::mysql_stmt_fetch(stmt_);
if (res == 1 || res == MYSQL_NO_DATA) return {};
for (size_t i = 0; i < num_; ++i) {
auto &bind = mbinds[i];
auto field = ::mysql_fetch_field_direct(res_, i);
switch (field->type)
{
case MYSQL_TYPE_TINY: {
char i8 = 0;
bind.buffer_type = field->type;
bind.buffer = reinterpret_cast<void *>(&i8);
::mysql_stmt_fetch_column(stmt_, &bind, i, 0);
row.emplace_back(std::int64_t(i8), field->org_name);
} break;
case MYSQL_TYPE_SHORT: {
short i16 = 0;
bind.buffer_type = field->type;
bind.buffer = reinterpret_cast<void *>(&i16);
::mysql_stmt_fetch_column(stmt_, &bind, i, 0);
row.emplace_back(std::int64_t(i16), field->org_name);
} break;
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG: {
int i32 = 0;
bind.buffer_type = field->type;
bind.buffer = reinterpret_cast<void *>(&i32);
::mysql_stmt_fetch_column(stmt_, &bind, i, 0);
row.emplace_back(std::int64_t(i32), field->org_name);
} break;
case MYSQL_TYPE_LONGLONG: {
std::int64_t i64 = 0;
bind.buffer_type = field->type;
bind.buffer = reinterpret_cast<void *>(&i64);
::mysql_stmt_fetch_column(stmt_, &bind, i, 0);
row.emplace_back(i64, field->org_name);
} break;
case MYSQL_TYPE_FLOAT: {
float f;
bind.buffer_type = field->type;
bind.buffer = reinterpret_cast<void *>(&f);
::mysql_stmt_fetch_column(stmt_, &bind, i, 0);
row.emplace_back(double(f), field->org_name);
} break;
case MYSQL_TYPE_DOUBLE: {
double d;
bind.buffer_type = field->type;
bind.buffer = reinterpret_cast<void *>(&d);
::mysql_stmt_fetch_column(stmt_, &bind, i, 0);
row.emplace_back(d, field->org_name);
} break;
case MYSQL_TYPE_STRING: case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_BLOB: if (field->charsetnr == 63) {
blob v(bind.buffer_length);
bind.buffer = const_cast<std::uint8_t *>(v.data());
::mysql_stmt_fetch_column(stmt_, &bind, i, 0);
row.emplace_back(std::move(v), field->org_name);
} else {
std::string s(bind.buffer_length, '\0');
bind.buffer = const_cast<char *>(s.data());
::mysql_stmt_fetch_column(stmt_, &bind, i, 0);
row.emplace_back(std::move(s), field->org_name);
} break;
case MYSQL_TYPE_NULL:
row.emplace_back(field->org_name);
break;
default:
row.emplace_back(std::int64_t(0), field->org_name);
break;
}
}
return row;
}
void first() override {
#ifdef USE_SHARED_CONNECTION
auto&& lock = db_();
#endif
::mysql_stmt_data_seek(stmt_, 0);
}
result_type result() const override { return result_; };
std::uint64_t last_id() const override { return last_id_; };
std::uint64_t affected_rows() const override { return affected_rows_; };
private:
db const& db_;
size_t num_ = 0;
::MYSQL_RES* res_;
::MYSQL_STMT* stmt_;
result_type result_;
std::uint64_t last_id_ = 0;
std::uint64_t affected_rows_ = 0;
};
/*
* Representation of a transaction
*/
class transaction {
public:
transaction(::MYSQL* db) : db_(db) {
finished_ = !begin();
}
transaction(transaction&& t) : db_(t.db_), finished_(t.finished_) {
t.finished_ = true;
}
~transaction() { rollback(); }
transaction(transaction const&) = delete;
transaction& operator=(transaction&&) = delete;
transaction& operator=(transaction const&) = delete;
bool begin() {
int err = ::mysql_query(db_, "BEGIN;");
return err == 0;
}
bool commit() {
if (finished_) return true;
int err = ::mysql_query(db_, "COMMIT;");
finished_ = err == 0;
return finished_;
}
bool rollback() {
if (finished_) return true;
int err = ::mysql_query(db_, "ROLLBACK;");
finished_ = err == 0;
return finished_;
}
private:
::MYSQL* db_;
bool finished_;
};
class query : public sqlxx::query {
public:
query(db&& db) = delete;
query(db const& db) : db_(db) {}
query(db const& db, std::string const& str) : sqlxx::query(str), db_(db) {}
private:
int do_bind(::MYSQL_STMT* stmt, std::vector<sqlxx::field_type> binds) {
auto cnt = ::mysql_stmt_param_count(stmt);
if (!cnt) return ::mysql_stmt_execute(stmt);
std::vector<MYSQL_BIND> mbinds(cnt);
for (size_t i = 0; i < cnt; ++i) {
if (i >= binds.size()) continue;
auto &mbind = mbinds[i];
auto const& bind = binds[i];
if (bind.type() == SQL_BLOB) {
std::string const& v = bind;
mbind.buffer_type = MYSQL_TYPE_BLOB;
mbind.buffer = const_cast<char *>(v.data());
mbind.buffer_length = v.size();
mbind.is_unsigned = static_cast<::my_bool>(1);
}
else if (bind.type() == SQL_TEXT) {
std::string const& s = bind;
mbind.buffer_type = MYSQL_TYPE_STRING;
mbind.buffer = const_cast<char *>(s.data());
mbind.buffer_length = s.size();
}
else if (bind.type() == SQL_NULL) {
mbind.buffer_type = MYSQL_TYPE_NULL;
}
else if (bind.type() == SQL_INTEGER) {
std::int64_t const& i64 = bind;
if (i64 > std::numeric_limits<int>::max()
|| i64 < std::numeric_limits<int>::min()) {
mbind.buffer_type = MYSQL_TYPE_LONGLONG;
} else
if (i64 > std::numeric_limits<short>::max()
|| i64 < std::numeric_limits<short>::min()) {
mbind.buffer_type = MYSQL_TYPE_LONG;
} else
if (i64 > std::numeric_limits<char>::max()
|| i64 < std::numeric_limits<char>::min()) {
mbind.buffer_type = MYSQL_TYPE_SHORT;
} else
mbind.buffer_type = MYSQL_TYPE_TINY;
mbind.buffer = const_cast<std::int64_t *>(&i64);
}
else if (bind.type() == SQL_FLOAT) {
double const& d = bind;
mbind.buffer_type = MYSQL_TYPE_DOUBLE;
mbind.buffer = const_cast<double *>(&d);
}
}
::mysql_stmt_bind_param(stmt, mbinds.data());
return ::mysql_stmt_execute(stmt);
}
sqlxx::cursor execute_impl(char const* query, std::vector<sqlxx::field_type> bind) override {
auto transaction_lock = [&]() {
auto&& lock = db_();
transaction tr(lock);
::MYSQL_STMT* stmt = ::mysql_stmt_init(lock);
if (sqlxx::query_has_results(query)) {
unsigned long attr = CURSOR_TYPE_READ_ONLY,
rows = std::numeric_limits<unsigned long>::max();
::mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, &attr);
::mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS, &rows);
}
if (::mysql_stmt_prepare(stmt, query, strlen(query)) == 0) {
do_bind(stmt, std::move(bind)) == 0 && tr.commit();
}
return stmt;
};
return { std::make_shared<statement>(db_, transaction_lock()) };
}
db const& db_;
};
class connection : public sqlxx::connection {
public:
static std::unique_ptr<sqlxx::connection> create(char const* host,
char const* user,
char const* pass,
char const* name) {
std::unique_ptr<connection> con{ new connection(host, user, pass, name) };
if (!con->db_.is_open()) con.reset();
return con;
}
void vacuum() override { db_.vacuum(); }
std::string version() override { return db_.version(); }
std::unique_ptr<sqlxx::query> query(std::string const& str) override {
return std::unique_ptr<mysqlxx::query>{ new mysqlxx::query(db_, str) };
}
private:
db db_;
connection(char const* host, char const* user, char const* pass, char const* name)
: db_{ name ? name : "" } {
db_.open(host, user, pass);
}
};
} // namespace mysqlxx
#endif // _MYSQLXX_H_