-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLite.cpp
More file actions
187 lines (166 loc) · 5.52 KB
/
SQLite.cpp
File metadata and controls
187 lines (166 loc) · 5.52 KB
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
/*
* SqLite.cpp
*
* Created on: Sept. 28, 2015
* \copyright 2015 DCBlaha. Distributed under the Mozilla Public License 2.0.
*/
#include "SQLite.h"
void SQLiteImporter::loadSymbols()
{
loadModuleSymbol("sqlite3_open", (ModuleProcPtr*)&sqlite3_open);
loadModuleSymbol("sqlite3_close", (ModuleProcPtr*)&sqlite3_close);
loadModuleSymbol("sqlite3_exec", (ModuleProcPtr*)&sqlite3_exec);
#if(DEBUG_CALLBACK)
loadModuleSymbol("sqlite3_trace_v2", (ModuleProcPtr*)&sqlite3_trace_v2);
#endif
#if(DEBUG_LOG)
loadModuleSymbol("sqlite3_config", (ModuleProcPtr*)&sqlite3_config);
#endif
loadModuleSymbol("sqlite3_prepare_v2", (ModuleProcPtr*)&sqlite3_prepare_v2);
loadModuleSymbol("sqlite3_clear_bindings", (ModuleProcPtr*)&sqlite3_clear_bindings);
loadModuleSymbol("sqlite3_errmsg", (ModuleProcPtr*)&sqlite3_errmsg);
loadModuleSymbol("sqlite3_finalize", (ModuleProcPtr*)&sqlite3_finalize);
loadModuleSymbol("sqlite3_step", (ModuleProcPtr*)&sqlite3_step);
loadModuleSymbol("sqlite3_reset", (ModuleProcPtr*)&sqlite3_reset);
loadModuleSymbol("sqlite3_bind_parameter_index", (ModuleProcPtr*)&sqlite3_bind_parameter_index);
loadModuleSymbol("sqlite3_bind_parameter_count", (ModuleProcPtr*)&sqlite3_bind_parameter_count);
loadModuleSymbol("sqlite3_bind_blob", (ModuleProcPtr*)&sqlite3_bind_blob);
loadModuleSymbol("sqlite3_bind_null", (ModuleProcPtr*)&sqlite3_bind_null);
loadModuleSymbol("sqlite3_bind_int", (ModuleProcPtr*)&sqlite3_bind_int);
loadModuleSymbol("sqlite3_bind_int64", (ModuleProcPtr*)&sqlite3_bind_int64);
loadModuleSymbol("sqlite3_bind_double", (ModuleProcPtr*)&sqlite3_bind_double);
loadModuleSymbol("sqlite3_bind_text", (ModuleProcPtr*)&sqlite3_bind_text);
loadModuleSymbol("sqlite3_column_type", (ModuleProcPtr*)&sqlite3_column_type);
loadModuleSymbol("sqlite3_column_int", (ModuleProcPtr*)&sqlite3_column_int);
loadModuleSymbol("sqlite3_column_int64", (ModuleProcPtr*)&sqlite3_column_int64);
loadModuleSymbol("sqlite3_column_double", (ModuleProcPtr*)&sqlite3_column_double);
loadModuleSymbol("sqlite3_column_blob", (ModuleProcPtr*)&sqlite3_column_blob);
loadModuleSymbol("sqlite3_column_bytes", (ModuleProcPtr*)&sqlite3_column_bytes);
loadModuleSymbol("sqlite3_column_text", (ModuleProcPtr*)&sqlite3_column_text);
loadModuleSymbol("sqlite3_mutex_alloc", (ModuleProcPtr*)&sqlite3_mutex_alloc);
loadModuleSymbol("sqlite3_mutex_free", (ModuleProcPtr*)&sqlite3_mutex_free);
loadModuleSymbol("sqlite3_mutex_enter", (ModuleProcPtr*)&sqlite3_mutex_enter);
loadModuleSymbol("sqlite3_mutex_try", (ModuleProcPtr*)&sqlite3_mutex_try);
loadModuleSymbol("sqlite3_mutex_leave", (ModuleProcPtr*)&sqlite3_mutex_leave);
// This must be called for returned error strings.
loadModuleSymbol("sqlite3_free", (ModuleProcPtr*)&sqlite3_free);
}
#if(DEBUG_CALLBACK)
static int sqlite_xCallback(unsigned T, void*C, void*P, void*X)
{
return 0;
}
#endif
#if(DEBUG_LOG)
#include <stdio.h>
static void errorLogCallback(void * pStatement, int iErrCode, const char *zMsg)
{
printf("%s\n(%d) %s\n",*(char**)pStatement, iErrCode, zMsg);
}
#endif
bool SQLite::loadDbLib(char const *libName)
{
close();
bool success = Module::open(libName);
if(success)
{
loadSymbols();
}
return success;
}
int SQLite::openDb(char const *dbName)
{
int retCode = handleRetCode(sqlite3_open(dbName, &mDb));
if(IS_SQLITE_OK(retCode))
{
#if(DEBUG_CALLBACK)
if(sqlite3_trace_v2)
{
unsigned uMask = 0x0f;
void *pCtx = this;
sqlite3_trace_v2(mDb, uMask, sqlite_xCallback, pCtx);
}
#endif
#if(DEBUG_LOG)
#define SQLITE_CONFIG_LOG 16
//#define SQLITE_CONFIG_SQLLOG 21
void *pStatement = nullptr;
SQLITE_CONFIG_SQLLOG
sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, pStatement);
#endif
}
return retCode;
}
void SQLite::closeDb()
{
if(mDb)
{
handleRetCode(sqlite3_close(mDb));
mDb = nullptr;
}
}
int SQLite::execDb(const char *sql)
{
int retCode = sqlite3_exec(mDb, sql, &resultsCallback, this, nullptr);
return handleRetCode(retCode);
}
int SQLite::resultsCallback(void *customData, int numColumns,
char **colValues, char **colNames)
{
SQLite *sqlite = static_cast<SQLite*>(customData);
if(sqlite->mListener)
{
sqlite->mListener->SQLResultCallback(numColumns, colValues, colNames);
}
return 0;
}
int SQLite::handleRetCode(int retCode)
{
if(IS_SQLITE_ERROR(retCode) && mListener)
{
mListener->SQLError(retCode, sqlite3_errmsg(mDb));
}
return(retCode);
}
SQLiteStatement::~SQLiteStatement()
{
closeStatement();
}
void SQLiteStatement::closeStatement()
{
if(mStatement)
{
// These should not be needed since the finalize should clear memory.
// clearBindings();
// reset();
mDb.handleRetCode(mDb.sqlite3_finalize(mStatement));
mStatement = nullptr;
}
}
int SQLiteStatement::set(char const *query)
{
closeStatement();
int res = mDb.sqlite3_prepare_v2(mDb.getDb(), query, -1, &mStatement, nullptr);
return mDb.handleRetCode(res);
}
int SQLiteStatement::step()
{
int res = mDb.sqlite3_step(mStatement);
return mDb.handleRetCode(res);
}
void SQLiteTransaction::begin()
{
if(!mInTransaction)
{
mDb.execDb("BEGIN TRANSACTION");
mInTransaction = true;
}
}
void SQLiteTransaction::end()
{
if(mInTransaction)
{
mDb.execDb("END TRANSACTION");
mInTransaction = false;
}
}