-
Notifications
You must be signed in to change notification settings - Fork 1
/
Database.cpp
341 lines (291 loc) · 7.29 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
/**
* Database.cpp
*
* Rewritten / author: 2016-02-19 / mrmisticismo@hotmail.com
* Published / author: 2005-08-12 / grymse@alhem.net
* Copyright (C) 2015-2018 Michael Griffin
* Copyright (C) 2001-2006 Anders Hedstrom
* This program is made available under the terms of the GNU GPL.
*/
#include <sqlite3.h>
#include <stdio.h>
#ifdef _WIN32
//#pragma warning(disable:4786)
#endif
#include <iostream>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#include <sstream>
#include "IError.h"
#include "Database.h"
namespace SQLW
{
Database::Database(const std::string& database, IError *error)
: m_database(database)
, m_errhandler(error)
, m_mutex(m_mutex)
, m_is_mutex(false)
{
std::cout << "Database Created" << std::endl;
}
Database::Database(Mutex& m, const std::string& database, IError *error)
: m_database(database)
, m_errhandler(error)
, m_mutex(m)
, m_is_mutex(true)
{
std::cout << "Database Created w/ Mutex" << std::endl;
}
Database::~Database()
{
std::cout << "~Database" << std::endl;
// Check Open Databases and pop off stack!
m_database_pool::iterator it;
while(m_opendbs.size())
{
it = m_opendbs.begin();
DatabasePool *p = *it;
if(p->busy)
{
databaseError("destroying Database object before Query object");
}
delete p;
m_opendbs.erase(it);
}
}
void Database::errorHandler(IError *p)
{
m_errhandler = p;
}
Database::DatabasePool *Database::addDatabasePool()
{
MutexLock lck(m_mutex, m_is_mutex);
DatabasePool *odb = nullptr;
for(m_database_pool::iterator it = m_opendbs.begin(); it != m_opendbs.end(); ++it)
{
odb = *it;
if(!odb->busy)
{
break;
}
else
{
odb = nullptr;
}
}
if(!odb)
{
odb = new DatabasePool;
if(!odb)
{
databaseError("addDatabasePool: DatabasePool couldn't be created");
return nullptr;
}
// Look into shared chache more!
// int sqlite3_enable_shared_cache(int);
// SQLITE_OPEN_SHAREDCACHE.
int rc = sqlite3_open_v2(m_database.c_str(), &odb->db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
if(rc)
{
databaseError("Can't open database: %s\n", sqlite3_errmsg(odb -> db));
delete odb;
return nullptr;
}
odb->busy = true;
m_opendbs.push_back(odb);
}
else
{
odb->busy = true;
}
return odb;
}
void Database::freeDatabasePool(Database::DatabasePool *odb)
{
MutexLock lck(m_mutex, m_is_mutex);
if(odb)
{
odb->busy = false;
}
}
void Database::databaseError(const char *format, ...)
{
if(m_errhandler)
{
va_list ap;
char errstr[5000];
va_start(ap, format);
#ifdef WIN32
vsprintf(errstr, format, ap);
#else
vsnprintf(errstr, 5000, format, ap);
#endif
va_end(ap);
m_errhandler->databaseError(*this, errstr);
}
}
void Database::databaseError(Query& q,const char *format, ...)
{
if(m_errhandler)
{
va_list ap;
char errstr[5000];
va_start(ap, format);
#ifdef WIN32
vsprintf(errstr, format, ap);
#else
vsnprintf(errstr, 5000, format, ap);
#endif
va_end(ap);
m_errhandler->databaseError(*this, q, errstr);
}
}
void Database::databaseError(Query& q,const std::string& msg)
{
if(m_errhandler)
{
m_errhandler->databaseError(*this, q, msg);
}
}
bool Database::isConnected()
{
DatabasePool *odb = addDatabasePool();
if(!odb)
{
return false;
}
freeDatabasePool(odb);
return true;
}
Database::MutexLock::MutexLock(Mutex& mutex, bool use)
: m_mutex(mutex)
, m_is_inuse(use)
{
if(m_is_inuse)
{
m_mutex.MutexLock();
}
}
Database::MutexLock::~MutexLock()
{
if(m_is_inuse)
{
m_mutex.Unlock();
}
}
Database::Mutex::Mutex()
{
#ifdef _WIN32
m_mutex = ::CreateMutex(nullptr, FALSE, nullptr);
#else
pthread_mutex_init(&m_mutex, nullptr);
#endif
}
Database::Mutex::~Mutex()
{
#ifdef _WIN32
::CloseHandle(m_mutex);
#else
pthread_mutex_destroy(&m_mutex);
#endif
}
void Database::Mutex::MutexLock()
{
#ifdef _WIN32
DWORD d = WaitForSingleObject(m_mutex, INFINITE);
UNUSED(d);
// %! check 'd' for result
#else
pthread_mutex_lock(&m_mutex);
#endif
}
void Database::Mutex::Unlock()
{
#ifdef _WIN32
::ReleaseMutex(m_mutex);
#else
pthread_mutex_unlock(&m_mutex);
#endif
}
/*
* Should not be used, use sqlite3_mprintf()
* To avoid Injection Attacks.
*/
std::string Database::safeString(const std::string& str)
{
std::string str2;
for(size_t i = 0; i < str.size(); i++)
{
switch(str[i])
{
case '\'': // Double Single Quotes
str2 += "\'\'";
break;
case '\\': // Double Back Slashes.
str2 += "\\\\";
break;
case '\0': // Skip nullptrs, not allowed!
break;
default:
str2 += str[i];
}
}
return str2;
}
std::string Database::xmlSafeString(const std::string& str)
{
std::string str2;
for(size_t i = 0; i < str.size(); i++)
{
switch(str[i])
{
case '&':
str2 += "&";
break;
case '<':
str2 += "<";
break;
case '>':
str2 += ">";
break;
case '"':
str2 += """;
break;
case '\'':
str2 += "'";
break;
default:
str2 += str[i];
}
}
return str2;
}
// Convert String to signed int64
int64_t Database::alphaToBigInt(const std::string& str)
{
std::istringstream ss(str);
int64_t bitInt;
ss >> bitInt;
if (ss.fail())
{
bitInt = 0;
}
return bitInt;
}
// Convert String to Unsigned int64
uint64_t Database::alphaToUnsignedBitInt(const std::string& str)
{
std::istringstream ss(str);
uint64_t bitInt;
ss >> bitInt;
if (ss.fail())
{
bitInt = 0;
}
return bitInt;
}
} // namespace SQLW {