-
Notifications
You must be signed in to change notification settings - Fork 1
/
tidb.cc
679 lines (584 loc) · 22.8 KB
/
tidb.cc
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#include <stdexcept>
#include <cassert>
#include <cstring>
#include "tidb.hh"
#include <iostream>
#include <set>
#ifndef HAVE_BOOST_REGEX
#include <regex>
#else
#include <boost/regex.hpp>
using boost::regex;
using boost::smatch;
using boost::regex_match;
#endif
using namespace std;
static regex e_unknown_database(".*Unknown database.*");
static regex e_crash(".*Lost connection.*");
extern "C" {
#include <mysql/mysql.h>
#include <unistd.h>
}
tidb_connection::tidb_connection(string db, unsigned int port)
{
test_db = db;
test_port = port;
if (!mysql_init(&mysql))
throw std::runtime_error(string(mysql_error(&mysql)) + " in tidb_connection!");
// password null: blank (empty) password field
if (mysql_real_connect(&mysql, "127.0.0.1", "root", NULL, test_db.c_str(), test_port, NULL, 0))
return; // success
string err = mysql_error(&mysql);
if (!regex_match(err, e_unknown_database))
throw std::runtime_error("BUG!!!" + string(mysql_error(&mysql)) + " in tidb_connection!");
// error caused by unknown database, so create one
cerr << test_db + " does not exist, use default db" << endl;
if (!mysql_real_connect(&mysql, "127.0.0.1", "root", NULL, NULL, port, NULL, 0))
throw std::runtime_error(string(mysql_error(&mysql)) + " in tidb_connection!");
cerr << "create database " + test_db << endl;
string create_sql = "create database " + test_db + "; ";
if (mysql_real_query(&mysql, create_sql.c_str(), create_sql.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in tidb_connection!");
auto res = mysql_store_result(&mysql);
mysql_free_result(res);
cerr << "use database" + test_db << endl;
string use_sql = "use " + test_db + "; ";
if (mysql_real_query(&mysql, use_sql.c_str(), use_sql.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in tidb_connection!");
res = mysql_store_result(&mysql);
mysql_free_result(res);
}
tidb_connection::~tidb_connection()
{
mysql_close(&mysql);
}
schema_tidb::schema_tidb(string db, unsigned int port)
: tidb_connection(db, port)
{
// cerr << "Loading tables...";
string get_table_query = "SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES \
WHERE TABLE_SCHEMA='" + db + "' AND \
TABLE_TYPE='BASE TABLE' ORDER BY 1;";
if (mysql_real_query(&mysql, get_table_query.c_str(), get_table_query.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in schema_tidb (load table list)!");
auto result = mysql_store_result(&mysql);
while (auto row = mysql_fetch_row(result)) {
table tab(row[0], "main", true, true);
tables.push_back(tab);
}
mysql_free_result(result);
// cerr << "done." << endl;
// cerr << "Loading views...";
string get_view_query = "select distinct table_name from information_schema.views \
where table_schema='" + db + "' order by 1;";
if (mysql_real_query(&mysql, get_view_query.c_str(), get_view_query.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in schema_tidb (load view list)!");
result = mysql_store_result(&mysql);
while (auto row = mysql_fetch_row(result)) {
table tab(row[0], "main", false, false);
tables.push_back(tab);
}
mysql_free_result(result);
// cerr << "done." << endl;
// cerr << "Loading indexes...";
string get_index_query = "SELECT DISTINCT INDEX_NAME FROM INFORMATION_SCHEMA.STATISTICS \
WHERE TABLE_SCHEMA='" + db + "' AND \
NON_UNIQUE=1 AND \
INDEX_NAME <> COLUMN_NAME AND \
INDEX_NAME <> 'PRIMARY' ORDER BY 1;";
if (mysql_real_query(&mysql, get_index_query.c_str(), get_index_query.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in schema_tidb (load index list)!");
result = mysql_store_result(&mysql);
while (auto row = mysql_fetch_row(result)) {
indexes.push_back(row[0]);
}
mysql_free_result(result);
// cerr << "done." << endl;
// cerr << "Loading columns and constraints...";
for (auto& t : tables) {
string get_column_query = "SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS \
WHERE TABLE_NAME='" + t.ident() + "' AND \
TABLE_SCHEMA='" + db + "' ORDER BY ORDINAL_POSITION;";
if (mysql_real_query(&mysql, get_column_query.c_str(), get_column_query.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in schema_tidb (load table " + t.ident() + ")!");
result = mysql_store_result(&mysql);
while (auto row = mysql_fetch_row(result)) {
column c(row[0], sqltype::get(row[1]));
t.columns().push_back(c);
}
mysql_free_result(result);
}
// cerr << "done." << endl;
booltype = sqltype::get("tinyint");
inttype = sqltype::get("integer");
realtype = sqltype::get("double");
texttype = sqltype::get("text");
#define BINOP(n, a, b, r) do {\
op o(#n, a, b, r); \
register_operator(o); \
} while(0)
BINOP(||, texttype, texttype, texttype);
BINOP(*, inttype, inttype, inttype);
BINOP(/, inttype, inttype, inttype);
BINOP(/, realtype, realtype, realtype);
BINOP(DIV, inttype, inttype, inttype);
BINOP(%, inttype, inttype, inttype);
BINOP(+, inttype, inttype, inttype);
BINOP(-, inttype, inttype, inttype);
BINOP(>>, inttype, inttype, inttype);
BINOP(<<, inttype, inttype, inttype);
BINOP(&, inttype, inttype, inttype);
BINOP(|, inttype, inttype, inttype);
BINOP(<, inttype, inttype, booltype);
BINOP(<=, inttype, inttype, booltype);
BINOP(>, inttype, inttype, booltype);
BINOP(>=, inttype, inttype, booltype);
// BINOP(IS NOT, inttype, inttype, booltype);
// BINOP(IS NOT, realtype, realtype, booltype);
// BINOP(IS NOT, texttype, texttype, booltype);
// BINOP(IS NOT, booltype, booltype, booltype);
// BINOP(IS, inttype, inttype, booltype);
// BINOP(IS, realtype, realtype, booltype);
// BINOP(IS, texttype, texttype, booltype);
// BINOP(IS, booltype, booltype, booltype);
BINOP(=, inttype, inttype, booltype);
BINOP(<>, inttype, inttype, booltype);
BINOP(and, booltype, booltype, booltype);
BINOP(or, booltype, booltype, booltype);
BINOP(xor, booltype, booltype, booltype);
#define FUNC(n, r) do { \
routine proc("", "", r, #n); \
register_routine(proc); \
} while(0)
#define FUNC1(n, r, a) do { \
routine proc("", "", r, #n); \
proc.argtypes.push_back(a); \
register_routine(proc); \
} while(0)
#define FUNC2(n, r, a, b) do { \
routine proc("", "", r, #n); \
proc.argtypes.push_back(a); \
proc.argtypes.push_back(b); \
register_routine(proc); \
} while(0)
#define FUNC3(n, r, a, b, c) do { \
routine proc("", "", r, #n); \
proc.argtypes.push_back(a); \
proc.argtypes.push_back(b); \
proc.argtypes.push_back(c); \
register_routine(proc); \
} while(0)
#define FUNC4(n, r, a, b, c, d) do { \
routine proc("", "", r, #n); \
proc.argtypes.push_back(a); \
proc.argtypes.push_back(b); \
proc.argtypes.push_back(c); \
proc.argtypes.push_back(d); \
register_routine(proc); \
} while(0)
#define FUNC5(n, r, a, b, c, d, e) do { \
routine proc("", "", r, #n); \
proc.argtypes.push_back(a); \
proc.argtypes.push_back(b); \
proc.argtypes.push_back(c); \
proc.argtypes.push_back(d); \
proc.argtypes.push_back(e); \
register_routine(proc); \
} while(0)
// tidb numeric
FUNC(PI, realtype);
FUNC1(abs, inttype, inttype);
FUNC1(abs, realtype, realtype);
FUNC1(hex, texttype, texttype);
FUNC1(length, inttype, texttype);
FUNC1(lower, texttype, texttype);
FUNC1(ltrim, texttype, texttype);
FUNC1(quote, texttype, texttype);
FUNC1(round, inttype, realtype);
FUNC1(rtrim, texttype, texttype);
FUNC1(trim, texttype, texttype);
FUNC1(upper, texttype, texttype);
// add for tidb string
FUNC1(ASCII, inttype, texttype);
FUNC1(BIN, texttype, inttype);
FUNC1(BIT_LENGTH, inttype, texttype);
FUNC1(CHAR, texttype, inttype);
FUNC1(CHAR_LENGTH, inttype, texttype);
FUNC1(SPACE, texttype, inttype);
FUNC1(REVERSE, texttype, texttype);
FUNC1(ORD, inttype, texttype);
FUNC1(OCT, texttype, inttype);
FUNC1(UNHEX, texttype, texttype);
// tidb numeric
FUNC1(EXP, realtype, realtype);
FUNC1(SQRT, realtype, realtype);
FUNC1(LN, realtype, realtype);
FUNC1(LOG, realtype, realtype);
FUNC1(TAN, realtype, realtype);
FUNC1(COT, realtype, realtype);
FUNC1(SIN, realtype, realtype);
FUNC1(COS, realtype, realtype);
FUNC1(ATAN, realtype, realtype);
FUNC1(ASIN, realtype, realtype);
FUNC1(ACOS, realtype, realtype);
FUNC1(RADIANS, realtype, realtype);
FUNC1(DEGREES, realtype, realtype);
FUNC1(CEILING, inttype, realtype);
FUNC1(FLOOR, inttype, realtype);
FUNC1(ROUND, inttype, realtype);
FUNC1(SIGN, inttype, realtype);
FUNC1(SIGN, inttype, inttype);
FUNC1(CRC32, inttype, texttype);
FUNC2(instr, inttype, texttype, texttype);
FUNC2(round, realtype, realtype, inttype);
FUNC2(substr, texttype, texttype, inttype);
// tidb string
FUNC2(INSTR, inttype, texttype, texttype);
FUNC2(LEFT, texttype, texttype, inttype);
FUNC2(RIGHT, texttype, texttype, inttype);
FUNC2(REPEAT, texttype, texttype, inttype);
FUNC2(STRCMP, inttype, texttype, texttype);
// tidb numeric
FUNC2(POW, realtype, realtype, realtype);
FUNC2(LOG, realtype, realtype, realtype);
FUNC2(MOD, inttype, inttype, inttype);
FUNC2(ROUND, realtype, realtype, inttype);
FUNC2(TRUNCATE, realtype, realtype, inttype);
FUNC3(substr, texttype, texttype, inttype, inttype);
FUNC3(replace, texttype, texttype, texttype, texttype);
// add for tidb
FUNC3(CONCAT, texttype, texttype, texttype, texttype);
FUNC3(LPAD, texttype, texttype, inttype, texttype);
FUNC3(RPAD, texttype, texttype, inttype, texttype);
FUNC3(REPLACE, texttype, texttype, texttype, texttype);
FUNC3(SUBSTRING, texttype, texttype, inttype, inttype);
// add for tidb
FUNC4(CONCAT_WS, texttype, texttype, texttype, texttype, texttype);
FUNC4(ELT, texttype, inttype, texttype, texttype, texttype);
FUNC4(FIELD, inttype, texttype, texttype, texttype, texttype);
FUNC4(INSERT, texttype, texttype, inttype, inttype, texttype);
FUNC5(EXPORT_SET, texttype, inttype, texttype, texttype, texttype, inttype);
#define AGG1(n, r, a) do { \
routine proc("", "", r, #n); \
proc.argtypes.push_back(a); \
register_aggregate(proc); \
} while(0)
#define AGG3(n, r, a, b, c, d) do { \
routine proc("", "", r, #n); \
proc.argtypes.push_back(a); \
proc.argtypes.push_back(b); \
proc.argtypes.push_back(c); \
proc.argtypes.push_back(d); \
register_aggregate(proc); \
} while(0)
#define AGG(n, r) do { \
routine proc("", "", r, #n); \
register_aggregate(proc); \
} while(0)
AGG1(avg, inttype, inttype);
AGG1(avg, realtype, realtype);
AGG(count, inttype);
AGG1(count, inttype, realtype);
AGG1(count, inttype, texttype);
AGG1(count, inttype, inttype);
AGG1(max, realtype, realtype);
AGG1(max, inttype, inttype);
AGG1(min, realtype, realtype);
AGG1(min, inttype, inttype);
AGG1(sum, realtype, realtype);
AGG1(sum, inttype, inttype);
#define WIN(n, r) do { \
routine proc("", "", r, #n); \
register_windows(proc); \
} while(0)
#define WIN1(n, r, a) do { \
routine proc("", "", r, #n); \
proc.argtypes.push_back(a); \
register_windows(proc); \
} while(0)
#define WIN2(n, r, a, b) do { \
routine proc("", "", r, #n); \
proc.argtypes.push_back(a); \
proc.argtypes.push_back(b); \
register_windows(proc); \
} while(0)
#ifndef TEST_CLICKHOUSE
// ranking window function
WIN(CUME_DIST, realtype);
WIN(DENSE_RANK, inttype);
WIN1(NTILE, inttype, inttype);
WIN(RANK, inttype);
WIN(ROW_NUMBER, inttype);
WIN(PERCENT_RANK, realtype);
// value window function
WIN1(FIRST_VALUE, inttype, inttype);
WIN1(FIRST_VALUE, realtype, realtype);
WIN1(FIRST_VALUE, texttype, texttype);
WIN1(LAST_VALUE, inttype, inttype);
WIN1(LAST_VALUE, realtype, realtype);
WIN1(LAST_VALUE, texttype, texttype);
// WIN1(LAG, inttype, inttype);
// WIN1(LAG, realtype, realtype);
// WIN1(LAG, texttype, texttype);
// WIN2(LEAD, inttype, inttype, inttype);
// WIN2(LEAD, realtype, realtype, inttype);
// WIN2(LEAD, texttype, texttype, inttype);
#endif
internaltype = sqltype::get("internal");
arraytype = sqltype::get("ARRAY");
true_literal = "1 = 1";
false_literal = "0 <> 0";
generate_indexes();
// enable "atomic_subselect" use specific tables
for (auto &t: tables) {
set<sqltype *> type_set_in_table;
for (auto &c: t.columns()) { // filter repeated column types
assert(c.type);
type_set_in_table.insert(c.type);
}
for (auto uniq_type : type_set_in_table) {
tables_with_columns_of_type.insert(pair<sqltype*, table*>(uniq_type, &t));
}
}
// enable operator
for (auto &o: operators) {
operators_returning_type.insert(pair<sqltype*, op*>(o.result, &o));
}
// enable aggregate function
for(auto &r: aggregates) {
assert(r.restype);
aggregates_returning_type.insert(pair<sqltype*, routine*>(r.restype, &r));
}
// enable routine function
for(auto &r: routines) {
assert(r.restype);
routines_returning_type.insert(pair<sqltype*, routine*>(r.restype, &r));
if(!r.argtypes.size())
parameterless_routines_returning_type.insert(pair<sqltype*, routine*>(r.restype, &r));
}
// enable window function
for(auto &r: windows) {
assert(r.restype);
windows_returning_type.insert(pair<sqltype*, routine*>(r.restype, &r));
}
}
void schema_tidb::update_schema()
{
// cerr << "Loading tables...";
string get_table_query = "SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES \
WHERE TABLE_SCHEMA='" + test_db + "' AND \
TABLE_TYPE='BASE TABLE' ORDER BY 1;";
if (mysql_real_query(&mysql, get_table_query.c_str(), get_table_query.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in schema_tidb (load table list)!");
auto result = mysql_store_result(&mysql);
while (auto row = mysql_fetch_row(result)) {
table tab(row[0], "main", true, true);
tables.push_back(tab);
}
mysql_free_result(result);
// cerr << "done." << endl;
// cerr << "Loading views...";
string get_view_query = "select distinct table_name from information_schema.views \
where table_schema='" + test_db + "' order by 1;";
if (mysql_real_query(&mysql, get_view_query.c_str(), get_view_query.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in schema_tidb (load view list)!");
result = mysql_store_result(&mysql);
while (auto row = mysql_fetch_row(result)) {
table tab(row[0], "main", false, false);
tables.push_back(tab);
}
mysql_free_result(result);
// cerr << "done." << endl;
// cerr << "Loading indexes...";
string get_index_query = "SELECT DISTINCT INDEX_NAME FROM INFORMATION_SCHEMA.STATISTICS \
WHERE TABLE_SCHEMA='" + test_db + "' AND \
NON_UNIQUE=1 AND \
INDEX_NAME <> COLUMN_NAME AND \
INDEX_NAME <> 'PRIMARY' ORDER BY 1;";
if (mysql_real_query(&mysql, get_index_query.c_str(), get_index_query.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in schema_tidb (load index list)!");
result = mysql_store_result(&mysql);
while (auto row = mysql_fetch_row(result)) {
indexes.push_back(row[0]);
}
mysql_free_result(result);
// cerr << "done." << endl;
// cerr << "Loading columns and constraints...";
for (auto& t : tables) {
string get_column_query = "SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS \
WHERE TABLE_NAME='" + t.ident() + "' AND \
TABLE_SCHEMA='" + test_db + "' ORDER BY ORDINAL_POSITION;";
if (mysql_real_query(&mysql, get_column_query.c_str(), get_column_query.size()))
throw std::runtime_error(string(mysql_error(&mysql)) + " in schema_tidb (load table " + t.ident() + ")!");
result = mysql_store_result(&mysql);
while (auto row = mysql_fetch_row(result)) {
column c(row[0], sqltype::get(row[1]));
t.columns().push_back(c);
}
mysql_free_result(result);
}
// cerr << "done." << endl;
return;
}
dut_tidb::dut_tidb(string db, unsigned int port)
: tidb_connection(db, port)
{
}
void dut_tidb::test(const std::string &stmt, vector<vector<string>>* output, int* affected_row_num)
{
if (mysql_real_query(&mysql, stmt.c_str(), stmt.size())) {
string err = mysql_error(&mysql);
auto result = mysql_store_result(&mysql);
mysql_free_result(result);
if (err.find("Commands out of sync") != string::npos) {// occasionally happens, retry the statement again
cerr << err << ", repeat the statement again" << endl;
test(stmt, output, affected_row_num);
return;
}
if (regex_match(err, e_crash)) {
throw std::runtime_error("BUG!!! " + err + " in mysql::test");
}
throw std::runtime_error(err + " in mysql::test");
}
if (affected_row_num)
*affected_row_num = mysql_affected_rows(&mysql);
auto result = mysql_store_result(&mysql);
if (output && result) {
auto row_num = mysql_num_rows(result);
if (row_num == 0) {
mysql_free_result(result);
return;
}
auto column_num = mysql_num_fields(result);
while (auto row = mysql_fetch_row(result)) {
vector<string> row_output;
for (int i = 0; i < column_num; i++) {
string str;
if (row[i] == NULL)
str = "NULL";
else
str = row[i];
row_output.push_back(str);
}
output->push_back(row_output);
}
}
mysql_free_result(result);
}
void dut_tidb::reset(void)
{
string drop_sql = "drop database if exists " + test_db + "; ";
if (mysql_real_query(&mysql, drop_sql.c_str(), drop_sql.size())) {
string err = mysql_error(&mysql);
throw std::runtime_error(err + " in mysql::reset");
}
auto res_sql = mysql_store_result(&mysql);
mysql_free_result(res_sql);
string create_sql = "create database " + test_db + "; ";
if (mysql_real_query(&mysql, create_sql.c_str(), create_sql.size())) {
string err = mysql_error(&mysql);
throw std::runtime_error(err + " in mysql::reset");
}
res_sql = mysql_store_result(&mysql);
mysql_free_result(res_sql);
string use_sql = "use " + test_db + "; ";
if (mysql_real_query(&mysql, use_sql.c_str(), use_sql.size())) {
string err = mysql_error(&mysql);
throw std::runtime_error(err + " in mysql::reset");
}
res_sql = mysql_store_result(&mysql);
mysql_free_result(res_sql);
}
void dut_tidb::backup(void)
{
string mysql_dump = "mysqldump -h 127.0.0.1 -P " + to_string(test_port) + " -u root " + test_db + " > /tmp/mysql_bk.sql";
int ret = system(mysql_dump.c_str());
if (ret != 0) {
cerr << "backup fail in dut_tidb::backup!!" << endl;
throw std::runtime_error("backup fail in dut_tidb::backup");
}
}
void dut_tidb::reset_to_backup(void)
{
reset();
string bk_file = "/tmp/mysql_bk.sql";
if (access(bk_file.c_str(), F_OK ) == -1)
return;
mysql_close(&mysql);
string mysql_source = "mysql -h 127.0.0.1 -P " + to_string(test_port) + " -u root -D " + test_db + " < /tmp/mysql_bk.sql";
if (system(mysql_source.c_str()) == -1)
throw std::runtime_error(string("system() error, return -1") + " in dut_tidb::reset_to_backup!");
if (!mysql_real_connect(&mysql, "127.0.0.1", "root", NULL, test_db.c_str(), test_port, NULL, 0))
throw std::runtime_error(string(mysql_error(&mysql)) + " in dut_tidb::reset_to_backup!");
}
int dut_tidb::save_backup_file(string path)
{
string cp_cmd = "cp /tmp/mysql_bk.sql " + path;
return system(cp_cmd.c_str());
}
int dut_tidb::use_backup_file(string backup_file)
{
string cp_cmd = "cp " + backup_file + " /tmp/mysql_bk.sql";
return system(cp_cmd.c_str());
}
void dut_tidb::get_content(vector<string>& tables_name, map<string, vector<vector<string>>>& content)
{
for (auto& table:tables_name) {
vector<vector<string>> table_content;
auto query = "SELECT * FROM " + table + " ORDER BY 1;";
if (mysql_real_query(&mysql, query.c_str(), query.size())) {
string err = mysql_error(&mysql);
cerr << "Cannot get content of " + table + " in mysql::get_content" << endl;
cerr << "Error: " + err + " in mysql::get_content" << endl;
// throw std::runtime_error(err + " in mysql::get_content");
continue;
}
auto result = mysql_store_result(&mysql);
if (result) {
auto column_num = mysql_num_fields(result);
while (auto row = mysql_fetch_row(result)) {
vector<string> row_output;
for (int i = 0; i < column_num; i++) {
string str;
if (row[i] == NULL)
str = "NULL";
else
str = row[i];
row_output.push_back(str);
}
table_content.push_back(row_output);
}
}
mysql_free_result(result);
content[table] = table_content;
}
}
string dut_tidb::commit_stmt() {
return "COMMIT";
}
string dut_tidb::abort_stmt() {
return "ROLLBACK";
}
string dut_tidb::begin_stmt() {
return "BEGIN OPTIMISTIC";
}
pid_t dut_tidb::fork_db_server()
{
// pid_t child = fork();
// if (child < 0) {
// throw std::runtime_error(string("Fork db server fail") + " in dut_tidb::fork_db_server!");
// }
// if (child == 0) {
// char *server_argv[128];
// int i = 0;
// server_argv[i++] = (char *)"/root/.tiup/bin/tiup"; // path of tiup
// server_argv[i++] = (char *)"playground";
// server_argv[i++] = NULL;
// execv(server_argv[0], server_argv);
// cerr << "fork tidb server fail in dut_tidb::fork_db_server" << endl;
// }
// sleep(30);
// cout << "server pid: " << child << endl;
// return child;
return -1;
}