This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqlite3helper.cpp
461 lines (392 loc) · 11.5 KB
/
sqlite3helper.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
#include "stdafx.h"
#include "sqlite3helper.h"
#include "settings.h"
lua_State *Sqlite3Helper::g_L = nullptr;
Sqlite3Helper::Sqlite3Helper() : m_db(nullptr) {}
Sqlite3Helper::~Sqlite3Helper()
{
if (isDatabaseOpened())
closeDatabaseConnection();
m_db = nullptr;
QFile::remove(m_dbFile);
}
void Sqlite3Helper::bind(sqlite3_stmt *pVM, int nParam, const QString &sValue)
{
bind(pVM, nParam, sValue.toStdString().c_str());
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, const char *szParam, const char *szValue)
{
int nParam = bindParameterIndex(mpVM, szParam);
bind(mpVM, nParam, szValue);
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, const char *szParam, int const nValue)
{
int nParam = bindParameterIndex(mpVM, szParam);
bind(mpVM, nParam, nValue);
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, const char *szParam, int64_t const nValue)
{
int nParam = bindParameterIndex(mpVM, szParam);
bind(mpVM, nParam, nValue);
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, const char *szParam, double const dwValue)
{
int nParam = bindParameterIndex(mpVM, szParam);
bind(mpVM, nParam, dwValue);
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, const char *szParam, unsigned char const *blobValue, int nLen)
{
int nParam = bindParameterIndex(mpVM, szParam);
bind(mpVM, nParam, blobValue, nLen);
}
void Sqlite3Helper::bindNull(sqlite3_stmt *mpVM, const char *szParam)
{
int nParam = bindParameterIndex(mpVM, szParam);
bindNull(mpVM, nParam);
}
sqlite3_stmt *Sqlite3Helper::compile(const QString &szSQL)
{
return compile(szSQL.toStdString().c_str());
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, int nParam, const char *szValue)
{
int nRes = sqlite3_bind_text(mpVM, nParam, szValue, -1, SQLITE_TRANSIENT);
if (nRes != SQLITE_OK)
{
qDebug() << "Error binding string param";
}
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, int nParam, const int nValue)
{
int nRes = sqlite3_bind_int(mpVM, nParam, nValue);
if (nRes != SQLITE_OK)
{
qDebug() << "Error binding int param";
}
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, int nParam, const int64_t nValue)
{
int nRes = sqlite3_bind_int64(mpVM, nParam, nValue);
if (nRes != SQLITE_OK)
{
qDebug() << "Error binding int param";
}
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, int nParam, const double dValue)
{
int nRes = sqlite3_bind_double(mpVM, nParam, dValue);
if (nRes != SQLITE_OK)
{
qDebug() << "Error binding double param";
}
}
void Sqlite3Helper::bind(sqlite3_stmt *mpVM, int nParam, const unsigned char *blobValue, int nLen)
{
int nRes = sqlite3_bind_blob(mpVM, nParam, (const void *)blobValue, nLen, SQLITE_TRANSIENT);
if (nRes != SQLITE_OK)
{
qDebug() << "Error binding blob param";
}
}
void Sqlite3Helper::bindNull(sqlite3_stmt *mpVM, int nParam)
{
int nRes = sqlite3_bind_null(mpVM, nParam);
if (nRes != SQLITE_OK)
{
qDebug() << "Error binding NULL param";
}
}
int Sqlite3Helper::bindParameterIndex(sqlite3_stmt *mpVM, const char *szParam)
{
int nParam = sqlite3_bind_parameter_index(mpVM, szParam);
if (!nParam)
{
qDebug() << "Parameter '" << szParam << "' is not valid for this statement";
}
return nParam;
}
void Sqlite3Helper::bind(sqlite3_stmt *pVM, const char *szParam, const QString &sValue)
{
bind(pVM, szParam, sValue.toStdString().c_str());
}
sqlite3_stmt *Sqlite3Helper::compile(const char *szSQL)
{
sqlite3_stmt *pVM = nullptr;
int res = sqlite3_prepare(m_db, szSQL, -1, &pVM, nullptr);
if (res != SQLITE_OK)
{
qDebug() << "prepare SQL statement " << szSQL << " failed:" << res << (const char *)sqlite3_errmsg(m_db);
}
return pVM;
}
int Sqlite3Helper::execDML(const QString &szSQL)
{
return execDML(szSQL.toStdString().c_str());
}
int Sqlite3Helper::execDML(const char *szSQL)
{
int nRet;
do
{
sqlite3_stmt *pVM = compile(szSQL);
if (!pVM)
{
qDebug() << "VM null pointer";
return -1;
}
nRet = sqlite3_step(pVM);
if (nRet == SQLITE_ERROR)
{
qDebug() << (const char *)sqlite3_errmsg(m_db);
sqlite3_finalize(pVM);
break;
}
nRet = sqlite3_finalize(pVM);
} while (nRet == SQLITE_SCHEMA);
return nRet;
}
int Sqlite3Helper::execDML(sqlite3_stmt *pVM)
{
if (!isDatabaseOpened())
{
qDebug() << "database is not opened";
return -1;
}
if (!pVM)
{
qDebug() << "VM null pointer";
return -1;
}
if (sqlite3_step(pVM) == SQLITE_DONE)
{
int nRowsChanged = sqlite3_changes(m_db);
if (sqlite3_finalize(pVM) != SQLITE_OK)
{
qDebug() << (const char *)sqlite3_errmsg(m_db);
return -1;
}
return nRowsChanged;
}
else
{
if (sqlite3_finalize(pVM) != SQLITE_OK)
{
qDebug() << (const char *)sqlite3_errmsg(m_db);
}
}
return -1;
}
int Sqlite3Helper::execQuery(sqlite3_stmt *pVM, bool &eof)
{
int nRet = sqlite3_step(pVM);
if (nRet == SQLITE_DONE)
{ // no rows
sqlite3_finalize(pVM);
eof = true;
return nRet;
}
else if (nRet == SQLITE_ROW)
{ // at least 1 row
eof = false;
return nRet;
}
nRet = sqlite3_finalize(pVM);
if (nRet == SQLITE_SCHEMA)
return nRet;
qDebug() << (const char *)sqlite3_errmsg(m_db);
return -1;
}
bool Sqlite3Helper::nextRow(sqlite3_stmt *pVM, bool &eof)
{
int nRet = sqlite3_step(pVM);
if (nRet == SQLITE_DONE)
{
// no rows
if (sqlite3_finalize(pVM) != SQLITE_OK)
{
qDebug() << (const char *)sqlite3_errmsg(m_db);
}
eof = true;
}
else if (nRet == SQLITE_ROW)
{
// more rows, nothing to do
eof = false;
}
else
{
if (sqlite3_finalize(pVM) != SQLITE_OK)
{
qDebug() << (const char *)sqlite3_errmsg(m_db);
return false;
}
}
return true;
}
bool Sqlite3Helper::isDatabaseOpened()
{
return m_db != nullptr;
}
bool Sqlite3Helper::closeDatabaseConnection()
{
if (!m_db)
{
qDebug() << "Database is not opened!";
return false;
}
int result = sqlite3_close_v2(m_db);
if (result != SQLITE_OK)
{
qDebug() << "Close DB failed: " << sqlite3_errmsg(m_db);
return false;
}
qDebug() << "Database is closed!";
m_db = nullptr;
return true;
}
int Sqlite3Helper::checkExists(const QString &field, const QString &name)
{
int nRet = 0;
do
{
sqlite3_stmt *pVM = compile("SELECT COUNT(*) FROM sqlite_master WHERE type = ? AND name = ?;");
if (!pVM)
{
return -1;
}
bind(pVM, 1, field.toStdString().c_str());
bind(pVM, 2, name.toStdString().c_str());
bool eof = false;
nRet = execQuery(pVM, eof);
if (nRet == SQLITE_DONE || nRet == SQLITE_ROW)
{
int size = 0;
while (!eof)
{
size = (int)sqlite3_column_int(pVM, 0);
if (!nextRow(pVM, eof))
break;
}
if (size > 0)
{
qDebug() << "found expected " << field << ":" << name;
return size;
}
qDebug() << "not found expected " << field << ":" << name;
return 0;
}
} while (nRet == SQLITE_SCHEMA);
return 0;
}
bool Sqlite3Helper::openDatabase(const QString &name)
{
if (sqlite3_open_v2(name.toStdString().c_str(), &m_db, SQLITE_OPEN_READWRITE, nullptr) != SQLITE_OK)
{
qDebug() << "Cannot open the database.";
return false;
}
bindCustomFunctions();
m_dbFile = name;
qDebug() << "The database is opened.";
return true;
}
bool Sqlite3Helper::createDatabase(const QString &name)
{
if (sqlite3_open_v2(name.toStdString().c_str(), &m_db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, nullptr) != SQLITE_OK)
{
qDebug() << "Cannot create the database.";
return false;
}
bindCustomFunctions();
m_dbFile = name;
qDebug() << "The database is created.";
return true;
}
bool Sqlite3Helper::beginTransaction()
{
return execDML("BEGIN TRANSACTION;") == SQLITE_OK;
}
bool Sqlite3Helper::endTransaction()
{
return execDML("COMMIT TRANSACTION;") == SQLITE_OK;
}
bool Sqlite3Helper::rollbackTransaction()
{
return execDML("ROLLBACK TRANSACTION;") == SQLITE_OK;
}
bool Sqlite3Helper::vacuum()
{
return execDML("VACUUM;") == SQLITE_OK;
}
void Sqlite3Helper::setLuaState(lua_State *L)
{
Sqlite3Helper::g_L = L;
}
void Sqlite3Helper::bindCustomFunctions()
{
sqlite3_initialize();
sqlite3_create_function_v2(m_db, "regexp", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, this, &Sqlite3Helper::qtRegexp, NULL, NULL, NULL);
sqlite3_create_function_v2(m_db, "match", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, this, &Sqlite3Helper::luaMatch, NULL, NULL, NULL);
sqlite3_create_function_v2(m_db, "glob", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, this, &Sqlite3Helper::levelGlob, NULL, NULL, NULL);
}
void Sqlite3Helper::setRegexpPattern(const QString &pattern)
{
QRegularExpression re(pattern, QRegularExpression::CaseInsensitiveOption);
if (re.isValid())
{
m_regexp.swap(re);
m_regexp.optimize();
}
}
void Sqlite3Helper::levelGlob(sqlite3_context *ctx, int, sqlite3_value **argv)
{
QString text(reinterpret_cast<const char *>(sqlite3_value_text(argv[1])));
QMap<QString, bool> v {
{"FATAL", g_settings->fatalEnabled()},
{"ERROR", g_settings->errorEnabled()},
{"WARN", g_settings->warnEnabled()},
{"INFO", g_settings->infoEnabled()},
{"DEBUG", g_settings->debugEnabled()},
{"TRACE", g_settings->traceEnabled()},
};
bool b = v[text];
if (b)
{
sqlite3_result_int(ctx, 1);
}
else
{
sqlite3_result_int(ctx, 0);
}
}
void Sqlite3Helper::qtRegexp(sqlite3_context *ctx, int, sqlite3_value **argv)
{
QString text(reinterpret_cast<const char *>(sqlite3_value_text(argv[1])));
Sqlite3Helper *pThis = reinterpret_cast<Sqlite3Helper *>(sqlite3_user_data(ctx));
bool b = text.contains(pThis->m_regexp);
if (b)
{
sqlite3_result_int(ctx, 1);
}
else
{
sqlite3_result_int(ctx, 0);
}
}
void Sqlite3Helper::luaMatch(sqlite3_context *ctx, int, sqlite3_value **argv)
{
const char *text = reinterpret_cast<const char *>(sqlite3_value_text(argv[1]));
lua_getglobal(g_L, "match");
lua_pushstring(g_L, text);
/* call the function with 1 arguments, return 1 result */
if (lua_pcall(g_L, 1, 1, 0) != 0)
{
qDebug() << QString(lua_tostring(g_L, -1));
lua_pop(g_L, 1); /* pop error message from the stack */
sqlite3_result_int(ctx, 0);
return;
}
int res = static_cast<int>(lua_tointeger(g_L, -1));
lua_pop(g_L, 1);
sqlite3_result_int(ctx, res);
}