-
Notifications
You must be signed in to change notification settings - Fork 461
/
Copy pathdb_base.cpp
536 lines (465 loc) · 13.7 KB
/
db_base.cpp
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
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2023 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
#include "config.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <mysql.h>
#include "error_numbers.h"
#include "str_util.h"
#include "str_replace.h"
#include "db_base.h"
#include "sched_msgs.h"
bool g_print_queries = false;
DB_CONN::DB_CONN() {
mysql = 0;
}
int DB_CONN::open(
char* db_name, char* db_host, char* db_user, char* dbpassword
) {
mysql = mysql_init(0);
if (!mysql) return ERR_DB_CANT_INIT;
// MySQL's support for the reconnect option has changed over time:
// see http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html
// and http://dev.mysql.com/doc/refman/5.1/en/mysql-options.html
//
// v < 5.0.13: not supported
// 5.0.13 <= v < 5.0.19: set option after real_connect()
// 5.0.19 < v < 5.1: set option before real_connect()
// 5.1.0 <= v < 5.1.6: set option after real_connect()
// 5.1.6 <= v: set option before real_connect()
// 5.1.6 < v < 8.0.34: set option after real_connect()
// 8.0.34 <= v: reconnect feature deprecated
// https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-34.html#mysqld-8-0-34-deprecation-removal
int v = MYSQL_VERSION_ID;
bool set_opt_before = false, set_opt_after = false;
if (v < 50013 ) {
} else if (v < 50019) {
set_opt_after = true;
} else if (v < 50100) {
set_opt_before = true;
} else if (v < 50106) {
set_opt_after = true;
} else if (v < 80034) {
set_opt_before = true;
}
if (set_opt_before) {
bool mbReconnect = 1;
mysql_options(mysql, MYSQL_OPT_RECONNECT, &mbReconnect);
}
// parse hostname:port
//
char host[256];
int port = 0;
safe_strcpy(host, db_host);
char* p = strchr(host, ':');
if (p) {
*p = 0;
port = atoi(p+1);
}
// CLIENT_FOUND_ROWS means that the # of affected rows for an update
// is the # matched by the where, rather than the # actually changed
//
MYSQL* mysql2 = mysql_real_connect(
mysql, host, db_user, dbpassword, db_name, port, 0, CLIENT_FOUND_ROWS
);
if (mysql2 == 0) {
boinc::fprintf(stderr, "mysql_real_connect(): %d: %s\n", mysql_errno(mysql), mysql_error(mysql));
return ERR_DB_CANT_CONNECT;
}
mysql = mysql2;
if (set_opt_after) {
bool mbReconnect = 1;
mysql_options(mysql, MYSQL_OPT_RECONNECT, &mbReconnect);
}
return 0;
}
void DB_CONN::close() {
if (mysql) mysql_close(mysql);
}
int DB_CONN::set_isolation_level(ISOLATION_LEVEL level) {
const char* level_str;
char query[256];
switch(level) {
case READ_UNCOMMITTED:
level_str = "READ UNCOMMITTED";
break;
case READ_COMMITTED:
level_str = "READ COMMITTED";
break;
case REPEATABLE_READ:
level_str = "REPEATABLE READ";
break;
case SERIALIZABLE:
level_str = "SERIALIZABLE";
break;
default:
return -1;
}
sprintf(query, "SET SESSION TRANSACTION ISOLATION LEVEL %s", level_str);
return do_query(query);
}
int DB_CONN::do_query(const char* p) {
int retval;
if (g_print_queries) {
#ifdef _USING_FCGI_
log_messages.printf(MSG_NORMAL, "query: %s\n", p);
#else
fprintf(stderr, "query: %s\n", p);
#endif
}
retval = mysql_query(mysql, p);
if (retval) {
boinc::fprintf(stderr, "Database error: %s\nquery=%s\n", error_string(), p);
}
return retval;
}
// returns the number of rows matched by an update query
//
int DB_CONN::affected_rows() {
unsigned long x = (unsigned long)mysql_affected_rows(mysql);
//fprintf(stderr, "x: %lu i: %d\n", x, (int)x);
return (int)x;
}
DB_ID_TYPE DB_CONN::insert_id() {
int retval;
MYSQL_ROW row;
MYSQL_RES* rp;
retval = do_query("select LAST_INSERT_ID()");
if (retval) return retval;
rp = mysql_store_result(mysql);
row = mysql_fetch_row(rp);
DB_ID_TYPE x = atol(row[0]);
mysql_free_result(rp);
return x;
}
void DB_CONN::print_error(const char* p) {
boinc::fprintf(stderr, "%s: Database error: %s\n", p, error_string());
}
const char* DB_CONN::error_string() {
return mysql?mysql_error(mysql):"Not connected";
}
int DB_CONN::start_transaction() {
return do_query("START TRANSACTION");
}
int DB_CONN::commit_transaction() {
return do_query("COMMIT");
}
int DB_CONN::rollback_transaction() {
return do_query("ROLLBACK");
}
int DB_CONN::ping() {
int retval = mysql_ping(mysql);
if (retval) return ERR_DB_CANT_CONNECT;
return 0;
}
DB_BASE::DB_BASE(const char *tn, DB_CONN* p) : db(p), table_name(tn) {
}
DB_ID_TYPE DB_BASE::get_id() { return 0;}
void DB_BASE::db_print(char*) {}
void DB_BASE::db_parse(MYSQL_ROW&) {}
int DB_BASE::insert() {
char vals[MAX_QUERY_LEN*2], query[MAX_QUERY_LEN*2];
db_print(vals);
sprintf(query, "insert into %s set %s", table_name, vals);
return db->do_query(query);
}
int DB_BASE::insert_batch(std::string& values) {
std::string query;
query = "insert into " + std::string(table_name) + " values " + values;
return db->do_query(query.c_str());
}
int DB_BASE::affected_rows() {
return db->affected_rows();
}
//////////// FUNCTIONS FOR TABLES THAT HAVE AN ID FIELD ///////
int DB_BASE::lookup_id(DB_ID_TYPE id) {
char query[MAX_QUERY_LEN];
int retval;
MYSQL_ROW row;
MYSQL_RES* rp;
sprintf(query, "select * from %s where id=%lu", table_name, id);
retval = db->do_query(query);
if (retval) return retval;
rp = mysql_store_result(db->mysql);
if (!rp) return -1;
row = mysql_fetch_row(rp);
if (row) db_parse(row);
mysql_free_result(rp);
if (row == 0) return ERR_DB_NOT_FOUND;
// don't bother checking for uniqueness here
return 0;
}
// update an entire record
//
int DB_BASE::update() {
char vals[MAX_QUERY_LEN], query[MAX_QUERY_LEN];
db_print(vals);
sprintf(query, "update %s set %s where id=%lu", table_name, vals, get_id());
int retval = db->do_query(query);
if (retval) return retval;
if (db->affected_rows() != 1) return ERR_DB_NOT_FOUND;
return 0;
}
// update one or more fields
// "clause" is something like "foo=5, blah='xxx'" or "foo=foo+5"
//
int DB_BASE::update_field(const char* clause, const char* where_clause) {
char query[MAX_QUERY_LEN];
if (where_clause) {
sprintf(query, "update %s set %s where id=%lu and %s",
table_name, clause, get_id(), where_clause
);
} else {
sprintf(query, "update %s set %s where id=%lu",
table_name, clause, get_id()
);
}
return db->do_query(query);
}
// delete record
//
int DB_BASE::delete_from_db() {
char query[MAX_QUERY_LEN];
sprintf(query, "delete from %s where id=%lu", table_name, get_id());
return db->do_query(query);
}
int DB_BASE::delete_from_db_multi(const char* where_clause) {
char query[MAX_QUERY_LEN];
sprintf(query, "delete from %s where %s", table_name, where_clause);
return db->do_query(query);
}
int DB_BASE::get_field_ints(const char* fields, int nfields, int* vals) {
char query[MAX_QUERY_LEN];
int retval;
MYSQL_ROW row;
MYSQL_RES* rp;
sprintf(query,
"select %s from %s where id=%lu", fields, table_name, get_id()
);
retval = db->do_query(query);
if (retval) return retval;
rp = mysql_store_result(db->mysql);
if (!rp) return -1;
row = mysql_fetch_row(rp);
if (row) {
for (int i=0; i<nfields; i++) {
vals[i] = atoi(row[i]);
}
}
mysql_free_result(rp);
if (row == 0) return ERR_DB_NOT_FOUND;
return 0;
}
int DB_BASE::get_field_str(const char* field, char* buf, int buflen) {
char query[MAX_QUERY_LEN];
int retval;
MYSQL_ROW row;
MYSQL_RES* rp;
sprintf(query,
"select %s from %s where id=%lu", field, table_name, get_id()
);
retval = db->do_query(query);
if (retval) return retval;
rp = mysql_store_result(db->mysql);
if (!rp) return -1;
row = mysql_fetch_row(rp);
if (row && row[0]) {
strlcpy(buf, row[0], buflen);
}
mysql_free_result(rp);
if (row == 0) return ERR_DB_NOT_FOUND;
return 0;
}
int DB_BASE::max_id(DB_ID_TYPE& n, const char* clause) {
char query[MAX_QUERY_LEN];
sprintf(query, "select max(id) from %s %s", table_name, clause);
return get_long(query, n);
}
/////////////// FUNCTIONS THAT DON'T REQUIRE AN ID FIELD ///////////////
int DB_BASE::lookup(const char* clause) {
char query[MAX_QUERY_LEN];
int retval;
MYSQL_ROW row;
MYSQL_RES* rp;
sprintf(query, "select * from %s %s", table_name, clause);
retval = db->do_query(query);
if (retval) return retval;
rp = mysql_store_result(db->mysql);
if (!rp) return -1;
row = mysql_fetch_row(rp);
if (row) db_parse(row);
mysql_free_result(rp);
if (row == 0) return ERR_DB_NOT_FOUND;
return 0;
}
int DB_BASE::update_fields_noid(const char* set_clause, const char* where_clause) {
char query[MAX_QUERY_LEN];
sprintf(query,
"update %s set %s where %s",
table_name, set_clause, where_clause
);
int retval = db->do_query(query);
if (retval) return retval;
return 0;
}
// Note: AFAIK, if one enumeration is active you can't do another one
//
int DB_BASE::enumerate(const char* clause, bool use_use_result) {
int x;
char query[MAX_QUERY_LEN];
MYSQL_ROW row;
if (!cursor.active) {
cursor.active = true;
sprintf(query, "select * from %s %s", table_name, clause);
x = db->do_query(query);
if (x) return mysql_errno(db->mysql);
// if you use mysql_use_result() here,
// any other transactions will fail
//
if (use_use_result) {
cursor.rp = mysql_use_result(db->mysql);
} else {
cursor.rp = mysql_store_result(db->mysql);
}
if (!cursor.rp) return mysql_errno(db->mysql);
}
row = mysql_fetch_row(cursor.rp);
if (!row) {
mysql_free_result(cursor.rp);
cursor.active = false;
x = mysql_errno(db->mysql);
if (x) return x;
return ERR_DB_NOT_FOUND;
} else {
db_parse(row);
}
return 0;
}
// call this to end an enumeration before reaching end
//
int DB_BASE::end_enumerate() {
if (cursor.active) {
mysql_free_result(cursor.rp);
cursor.active = false;
}
return 0;
}
int DB_BASE::get_long(const char* query, long& n) {
int retval;
MYSQL_ROW row;
MYSQL_RES* resp;
retval = db->do_query(query);
if (retval) return retval;
resp = mysql_store_result(db->mysql);
if (!resp) return ERR_DB_NOT_FOUND;
row = mysql_fetch_row(resp);
if (!row || !row[0]) {
retval = ERR_DB_NOT_FOUND;
} else {
n = atol(row[0]);
}
mysql_free_result(resp);
return retval;
}
int DB_CONN::get_double(const char* query, double& x) {
int retval;
MYSQL_ROW row;
MYSQL_RES* resp;
retval = do_query(query);
if (retval) return retval;
resp = mysql_store_result(mysql);
if (!resp) return ERR_DB_NOT_FOUND;
row = mysql_fetch_row(resp);
if (!row || !row[0]) {
retval = ERR_DB_NOT_FOUND;
} else {
x = atof(row[0]);
}
mysql_free_result(resp);
return retval;
}
int DB_BASE::count(long& n, const char* clause) {
char query[MAX_QUERY_LEN];
sprintf(query, "select count(*) from %s %s", table_name, clause);
return get_long(query, n);
}
int DB_BASE::sum(double& x, const char* field, const char* clause) {
char query[MAX_QUERY_LEN];
sprintf(query, "select sum(%s) from %s %s", field, table_name, clause);
return db->get_double(query, x);
}
DB_BASE_SPECIAL::DB_BASE_SPECIAL(DB_CONN* p) : db(p) {
}
// convert a string into a form that allows it to be used
// in SQL queries delimited by single quotes:
// replace ' with \', '\' with '\\'
//
void escape_string(char* field, int len) {
char buf[MAX_QUERY_LEN];
char* q = buf, *p = field;
if (len > MAX_QUERY_LEN) {
len = MAX_QUERY_LEN;
}
// Make sure that final result won't overflow field[].
//
while (*p && q < buf+len-3) {
if (*p == '\'') {
// this does ' to \' transformation
//
*q++ = '\\';
*q++ = '\'';
} else if (*p == '\\') {
// this does \ to \\ transformation
//
*q++ = '\\';
*q++ = '\\';
} else {
// this handles all other characters
//
*q++ = *p;
}
p++;
}
*q = 0;
strlcpy(field, buf, len);
}
// undo the above process
// (len not used because this doesn't expand the string)
//
void unescape_string(char* p, int /*len*/) {
char* q = p;
while (*p) {
if (*p == '\\') {
p++;
if (!*p) break; // SHOULD NEVER HAPPEN!
}
*q++ = *p++;
}
*q='\0';
}
// replace _ with \\_, % with \\%
//
void escape_mysql_like_pattern(const char* in, char* out) {
while (*in) {
if (*in == '_' || *in == '%') {
*out++ = '\\';
*out++ = '\\';
}
*out++ = *in++;
}
}