-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.cpp
545 lines (491 loc) · 11.4 KB
/
database.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
537
538
539
540
541
542
543
544
545
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#ifdef __USE_SQLITE__
#include <boost/regex.hpp>
#endif
#include <iostream>
#include <string>
#include "database.h"
#ifdef __USE_MYSQL__
#include "databasemysql.h"
#endif
#ifdef __USE_SQLITE__
#include "databasesqlite.h"
#endif
#if defined __USE_MYSQL__ && defined __USE_SQLITE__
#include "configmanager.h"
extern ConfigManager g_config;
#endif
OTSYS_THREAD_LOCKVAR DBQuery::database_lock;
DBQuery::DBQuery()
{
OTSYS_THREAD_LOCK(database_lock, NULL);
}
DBQuery::~DBQuery()
{
OTSYS_THREAD_UNLOCK(database_lock, NULL);
}
DBResult::DBResult()
{
m_numFields = 0;
m_numRows = 0;
}
DBResult::~DBResult()
{
clear();
}
#ifdef __USE_MYSQL__
void DBResult::addRow(MYSQL_ROW r, unsigned long* lengths, unsigned int num_fields)
{
RowData* rd = new RowData;
rd->row = new char*[num_fields];
rd->length = new unsigned long*[num_fields];
for(unsigned int i=0; i < num_fields; ++i)
{
if(r[i] == NULL)
{
rd->row[i] = NULL;
rd->length[i] = NULL;
continue;
}
unsigned long colLen = lengths[i];
rd->row[i] = new char[colLen + 1];
rd->length[i] = new unsigned long;
memcpy(rd->row[i], r[i], colLen + 1);
memcpy(rd->length[i], &lengths[i], sizeof(unsigned long));
}
m_listRows[m_numRows] = rd;
m_numRows++;
}
#endif
#ifdef __USE_SQLITE__
void DBResult::addRow(char **results, unsigned int num_fields)
{
RowData* rd = new RowData;
rd->row = new char*[num_fields];
rd->length = new unsigned long*[num_fields];
for(unsigned int i=0; i < num_fields; ++i)
{
if(results[i] == NULL)
{
rd->row[i] = NULL;
rd->length[i] = NULL;
continue;
}
unsigned long colLen = strlen(results[i]);
rd->row[i] = new char[colLen+1];
rd->length[i] = new unsigned long;
/*std::cout << "'" << colLen << "' : '" << results[i] << "'\n";*/
memcpy(rd->row[i], results[i], colLen+1);
*(rd->length[i]) = colLen;
}
m_listRows[m_numRows] = rd;
m_numRows++;
}
#endif
void DBResult::clear()
{
RowDataMap::iterator it;
for(it = m_listRows.begin(); it != m_listRows.end();)
{
for(unsigned int i = 0; i < m_numFields; ++i)
{
if(it->second->row[i] != NULL)
delete[] it->second->row[i];
if(it->second->length[i] != NULL)
delete it->second->length[i];
}
delete[] it->second->row;
delete[] it->second->length;
delete it->second;
m_listRows.erase(it++);
}
m_numRows = 0;
m_listNames.clear();
m_numFields = 0;
}
int32_t DBResult::getDataInt(const std::string &s, unsigned int nrow)
{
listNames_type::iterator it=m_listNames.find(s);
if(it != m_listNames.end())
{
RowDataMap::iterator it2=m_listRows.find(nrow);
if(it2 != m_listRows.end())
{
if(it2->second->row[it->second] == NULL)
return 0;
else
{
#if defined __USE_MYSQL__ && defined __USE_SQLITE__
if(g_config.getNumber(ConfigManager::SQLTYPE) == SQL_TYPE_SQLITE)
{
if(std::string("TRUE").compare(it2->second->row[it->second]) == 0) //SqLite returns a string
return 1;
}
#elif defined __USE_SQLITE__
if(std::string("TRUE").compare(it2->second->row[it->second]) == 0) //SqLite returns a string
return 1;
#endif
return atoi(it2->second->row[it->second]);
}
}
}
std::cout << "SQL ERROR DBResult::GetDataInt() " << s << std::endl;
return 0; // Failed
}
int64_t DBResult::getDataLong(const std::string &s, unsigned int nrow)
{
listNames_type::iterator it=m_listNames.find(s);
if(it != m_listNames.end())
{
RowDataMap::iterator it2=m_listRows.find(nrow);
if(it2 != m_listRows.end())
{
if(it2->second->row[it->second] == NULL)
return 0;
else
return ATOI64(it2->second->row[it->second]);
}
}
std::cout << "SQL ERROR DBResult::GetDataLong() " << s << std::endl;
return 0; // Failed
}
std::string DBResult::getDataString(const std::string &s, unsigned int nrow)
{
listNames_type::iterator it=m_listNames.find(s);
if(it != m_listNames.end())
{
RowDataMap::iterator it2=m_listRows.find(nrow);
if(it2 != m_listRows.end())
{
if(it2->second->row[it->second] == NULL)
return std::string("");
else
return std::string(it2->second->row[it->second]);
}
}
std::cout << "SQL ERROR DBResult::GetDataString() " << s << std::endl;
return std::string(""); // Failed
}
const char* DBResult::getDataBlob(const std::string &s, unsigned long& size, unsigned int nrow)
{
#if defined __USE_MYSQL__ && defined __USE_SQLITE__
if(g_config.getNumber(ConfigManager::SQLTYPE) == SQL_TYPE_SQLITE)
{
size = 0;
return NULL;
}
#elif defined __USE_SQLITE__
size = 0;
return NULL;
#endif
listNames_type::iterator it = m_listNames.find(s);
if(it != m_listNames.end())
{
RowDataMap::iterator it2 = m_listRows.find(nrow);
if(it2 != m_listRows.end())
{
if(it2->second->row[it->second] == NULL)
{
size = 0;
return NULL;
}
else
{
size = *it2->second->length[it->second];
return it2->second->row[it->second];
}
}
}
std::cout << "SQL ERROR DBResult::getDataBlob() " << s << std::endl;
size = 0;
return NULL;
}
Database* _Database::_instance = NULL;
Database* _Database::getInstance()
{
if(!_instance)
{
#if defined __USE_MYSQL__ && defined __USE_SQLITE__
if(g_config.getNumber(ConfigManager::SQLTYPE) == SQL_TYPE_MYSQL)
_instance = new DatabaseMySQL;
else
_instance = new DatabaseSqLite;
#elif defined __USE_MYSQL__
_instance = new DatabaseMySQL;
#elif defined __USE_SQLITE__
_instance = new DatabaseSqLite;
#endif
OTSYS_THREAD_LOCKVARINIT(DBQuery::database_lock);
}
return _instance;
}
#ifdef __USE_SQLITE__
void escape_string(std::string & s)
{
if(!s.size())
return;
for(unsigned int i = 0; i < s.size(); i++)
{
switch(s[i])
{
case '\0': // Must be escaped for "mysql"
s[i] = '\\';
s.insert(i, "0", 1);
i++;
break;
case '\n': // Must be escaped for logs
s[i] = '\\';
s.insert(i, "n", 1);
i++;
break;
case '\r':
s[i] = '\\';
s.insert(i, "r", 1);
i++;
break;
case '\\':
s[i] = '\\';
s.insert(i, "\\", 1);
i++;
break;
case '\"':
s[i] = '\\';
s.insert(i, "\"", 1);
i++;
break;
case '\'': // Better safe than sorry
s[i] = '\\';
s.insert(i, "\'", 1);
i++;
break;
case '\032': // This gives problems on Win32
s[i] = '\\';
s.insert(i, "Z", 1);
i++;
break;
default:
break;
}
}
}
#endif
std::string _Database::getUpdateQueryLimit(uint32_t limit/* = 1*/)
{
#if defined __USE_MYSQL__ && defined __USE_SQLITE__
if(g_config.getNumber(ConfigManager::SQLTYPE) == SQL_TYPE_MYSQL)
{
std::stringstream ss;
ss << " LIMIT " << limit;
return ss.str();
}
else
return "";
#elif defined __USE_MYSQL__
std::stringstream ss;
ss << " LIMIT " << limit;
return ss.str();
#elif defined __USE_SQLITE__
return "";
#endif
}
std::string _Database::escapeString(const std::string &s)
{
#if defined __USE_MYSQL__ && defined __USE_SQLITE__
if(g_config.getNumber(ConfigManager::SQLTYPE) == SQL_TYPE_MYSQL)
return escapeString(s.c_str(), s.size());
else
{
std::string r = std::string(s);
escape_string(r);
return r;
}
#elif defined __USE_MYSQL__
return escapeString(s.c_str(), s.size());
#elif defined __USE_SQLITE__
std::string r = std::string(s);
escape_string(r);
return r;
#endif
}
#ifdef __USE_SQLITE__
std::string _Database::escapePatternString(const std::string &s)
{
std::string r = std::string(s);
escape_string(r);
//escape % and _ because they can manipulate the pattern.
r = boost::regex_replace(r, boost::regex("%"), "\\%");
r = boost::regex_replace(r, boost::regex("_"), "\\_");
return r;
}
#endif
// probably the mysql_escape_string version should be dropped as it's less generic
// but i'm keeping it atm
std::string _Database::escapeString(const char* s, unsigned long size)
{
if(s == NULL)
return std::string("");
std::string r;
#if defined __USE_MYSQL__ && defined __USE_SQLITE__
if(g_config.getNumber(ConfigManager::SQLTYPE) == SQL_TYPE_MYSQL)
{
char* output = new char[size * 2 + 1];
mysql_escape_string(output, s, size);
r = std::string(output);
delete[] output;
}
else
{
r = std::string(s);
escape_string(r);
}
#elif defined __USE_MYSQL__
char* output = new char[size * 2 + 1];
mysql_escape_string(output, s, size);
r = std::string(output);
delete[] output;
#elif defined __USE_SQLITE__
r = std::string(s);
escape_string(r);
#endif
return r;
}
DBTransaction::DBTransaction(Database* database)
{
m_database = database;
m_state = STATE_NO_START;
}
DBTransaction::~DBTransaction()
{
if(m_state == STATE_START)
{
if(!m_database->rollback())
{
//TODO: What to do here?
}
}
}
bool DBTransaction::start()
{
DBQuery query;
#if defined __USE_MYSQL__ && defined __USE_SQLITE__
if(g_config.getNumber(ConfigManager::SQLTYPE) == SQL_TYPE_MYSQL)
query << "START TRANSACTION;";
else
query << "BEGIN;";
#elif defined __USE_MYSQL__
query << "START TRANSACTION;";
#elif defined __USE_SQLITE__
query << "BEGIN;";
#endif
if(m_database->executeQuery(query))
{
m_state = STATE_START;
return true;
}
return false;
}
bool DBTransaction::success()
{
if(m_state == STATE_START)
{
m_state = STEATE_COMMIT;
return m_database->commit();
}
else
return false;
}
DBSplitInsert::DBSplitInsert(Database* database)
{
m_database = database;
}
DBSplitInsert::~DBSplitInsert()
{
//
}
void DBSplitInsert::clear()
{
m_buffer.clear();
m_buffer.reserve(10240);
}
void DBSplitInsert::setQuery(const std::string& query)
{
clear();
m_query = query;
}
bool DBSplitInsert::addRow(const std::string& row)
{
#if defined __USE_MYSQL__ && defined __USE_SQLITE__
if(g_config.getNumber(ConfigManager::SQLTYPE) == SQL_TYPE_MYSQL)
{
int size = m_buffer.size();
if(size == 0)
m_buffer = row;
else if(size > 8192)
{
if(!internalExecuteQuery())
return false;
m_buffer = row;
}
else
m_buffer += "," + row;
return true;
}
else
{
m_buffer = row;
bool ret = internalExecuteQuery();
m_buffer = "";
return ret;
}
#elif defined __USE_MYSQL__
int size = m_buffer.size();
if(size == 0)
m_buffer = row;
else if(size > 8192)
{
if(!internalExecuteQuery())
return false;
m_buffer = row;
}
else
m_buffer += "," + row;
return true;
#elif defined __USE_SQLITE__
m_buffer = row;
bool ret = internalExecuteQuery();
m_buffer = "";
return ret;
#endif
}
bool DBSplitInsert::internalExecuteQuery()
{
DBQuery subquery;
subquery << m_query;
subquery << m_buffer;
return m_database->executeQuery(subquery);
}
bool DBSplitInsert::executeQuery()
{
if(m_buffer.size() != 0)
return internalExecuteQuery();
return true;
}