-
Notifications
You must be signed in to change notification settings - Fork 0
/
SF_Sqlite.cpp
406 lines (295 loc) · 11.6 KB
/
SF_Sqlite.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
#include "SF_Sqlite.h"
#include "SF_Sqlite_Helper.h"
enum EXECUTION_TYPE { ROWS, ROWS_COUNT, SCALAR };
//Used in communication to the call back.
//Pointer points to usable data storage
struct Execution_Package
{
EXECUTION_TYPE type;
char* pointer;
int resultCount;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Forward Declarations///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static int callback(void *data, int argc, char **argv, char **ColumnNames);
namespace INTERNAL
{
static const SF_SQLITE_CODES::ERRORS executeScalar(sqlite3* connection, const std::string& query, char** result);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Internal Use Only//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Internal functions
//Hides implementation. Instead of exposing it as private or using the pimpl idiom, I just use
//a namespace with static functions.
//NOTE: This breaks polymorphism but this class is not supposed be inherited. Use aggregation instead.
namespace INTERNAL
{
static const SF_SQLITE_CODES::ERRORS executeScalar(sqlite3* connection, const std::string& query, char** result)
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
//This is OK, but hacky. Too much forward dependence on how the call back works
char holder;
//Package to send to the callback
Execution_Package execution;
execution.type = EXECUTION_TYPE::SCALAR;
execution.pointer = &holder;
char* error = 0;
sqlite3_exec(connection, query.c_str(), callback, &execution, &error);
*result = execution.pointer;
SF_SQLITE_PRINT_QUERY(query);
SF_SQLITE_PRINT_QUERY_ERROR(error);
return SF_SQLITE_CODES::ERRORS::NO_ERROR;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Main Implementation////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//This will not open connection as it is too risky that it may
//cause issues in the constructor. Never run code which can
//throw exeptions or cause errors in the constructors
SF_Sqlite::SF_Sqlite( const std::string & name)
: connection(0), databaseName(name), isExernalConnection(false)
{
}
SF_Sqlite::SF_Sqlite( sqlite3* externalConnection )
: connection(externalConnection), databaseName("EXTERNAL CONNECTION"), isExernalConnection(true)
{
}
SF_Sqlite::SF_Sqlite( const SF_Sqlite& copy)
: connection(copy.connection), databaseName(copy.databaseName), isExernalConnection(copy.isExernalConnection)
{
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::connect()
{
if (!this->isExernalConnection)
{
SF_SQLITE_IS_CONNECTED_ESCAPE(connection);
int nativeError = sqlite3_open(this->databaseName.c_str(), &this->connection);
SF_SQLITE_CODES::ERRORS sf_error = SF_SQLITE_PROCESS_SQLITE_NATIVE_ERROR(nativeError);
if (sf_error == SF_SQLITE_CODES::ERRORS::CANTOPEN)
{
printf("Database failed to open \n");
}
return sf_error;
}
return SF_SQLITE_CODES::ERRORS::NO_ERROR;
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::disconnect()
{
if (!this->isExernalConnection)
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
int error = sqlite3_close(this->connection);
this->connection = 0;
return SF_SQLITE_PROCESS_SQLITE_NATIVE_ERROR(error);
}
return SF_SQLITE_CODES::ERRORS::NO_ERROR;
}
static int callback(void *data, int argc, char **argv, char **ColumnNames)
{
Execution_Package* package = (Execution_Package*)data;
package->resultCount = argc;
if (argc == 0)
{
return 0;
}
if (package->type == EXECUTION_TYPE::ROWS)
{
std::list<SF_Sqlite_Row>* rows = (std::list<SF_Sqlite_Row>*)package->pointer;
SF_Sqlite_Row row;
for (int x = 0; x < argc; ++x)
{
row[ColumnNames[x]] = argv[x];
}
rows->push_back(row);
}
else
{
*package->pointer = **argv;
}
return 0;
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::execute(const std::string& query, SF_Sqlite_Result& result) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
//Package to send to the callback
Execution_Package execution;
execution.type = EXECUTION_TYPE::ROWS;
execution.pointer = (char*)&result;
char* error = 0;
int nativeError = sqlite3_exec(this->connection, query.c_str(), callback, &execution, &error);
SF_SQLITE_PRINT_QUERY(query);
SF_SQLITE_PRINT_QUERY_ERROR(error);
return SF_SQLITE_PROCESS_SQLITE_NATIVE_ERROR(nativeError);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::execute(const std::string& query,
const std::vector<SF_Sqlite_Parameter>& params,
SF_Sqlite_Result& result) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
std::string finalQuery;
sf_sqlite_buildParamedQuery(finalQuery, query, params);
return this->execute(finalQuery, result);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::execute(const std::string& query) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
char* error = 0;
int nativeError = sqlite3_exec(this->connection, query.c_str(), 0, 0, &error);
SF_SQLITE_PRINT_QUERY(query);
SF_SQLITE_PRINT_QUERY_ERROR(error);
return SF_SQLITE_PROCESS_SQLITE_NATIVE_ERROR(nativeError);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::executeScalar(const std::string& query, int& result) const
{
char* resultReturn = 0;
SF_SQLITE_CODES::ERRORS error = INTERNAL::executeScalar(this->connection, query, &resultReturn);
result = (int)*resultReturn - '0';
return error;
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::executeScalar(const std::string& query, char& result) const
{
char* resultReturn = 0;
SF_SQLITE_CODES::ERRORS error = INTERNAL::executeScalar(this->connection, query, &resultReturn);
result = (char)*resultReturn;
return error;
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::executeCount(const std::string& query, int& result)
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
SF_Sqlite_Result returnResults;
SF_SQLITE_CODES::ERRORS error = this->execute(query, returnResults);
if (SF_SQLITE_SUCCESS(error))
{
result = (int)returnResults.size();
}
return error;
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::insertRecord(const std::string& table, const std::vector<std::string>& values) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
std::string insertIntoString;
sf_sqlite_buildInsertIntoTableHeader(insertIntoString, table);
std::string insertValuesString;
sf_sqlite_buildInsertValues(insertValuesString, values);
std::string query;
query += insertIntoString;
query += insertValuesString;
SF_SQLITE_PRINT_QUERY(query);
return this->execute(query);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::insertRecord(const std::string& table, const std::vector<SF_Sqlite_Column_Data_Pair>& values) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
std::string insertIntoString;
sf_sqlite_buildInsertIntoTableHeader(insertIntoString, table);
std::string insertValuesString;
sf_sqlite_buildInsertValues(insertValuesString, values);
std::string query;
query += insertIntoString;
query += insertValuesString;
return this->execute(query);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::getRecords(const std::string& table,
SF_Sqlite_Result& result) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
std::string selectFrom;
std::string columnSelectString;
sf_sqlite_buildSelectColumnList(columnSelectString);
std::string query;
sf_sqlite_buildFinalSelectNoWhereQuery(query, table, columnSelectString) ;
return this->execute(query, result);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::getRecords(const std::string& table,
const std::vector<std::string>& columns,
SF_Sqlite_Result& result) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
std::string columnSelectString;
sf_sqlite_buildSelectColumnList(columnSelectString, columns);
std::string query;
query += columnSelectString;
return this->execute(query, result);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::getRecords(const std::string& table,
const std::vector<std::string>& columns,
const std::vector<SF_Sqlite_Column_Data_Pair>& whereValues,
SF_Sqlite_Result& result) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
std::string whereListString;
sf_sqlite_buildWhereColumnList(whereListString, whereValues);
std::string selectListString = " ";
sf_sqlite_buildSelectColumnList(selectListString, columns);
std::string query;
sf_sqlite_buildFinalSelectAllQuery(query, table, selectListString, whereListString);
return this->execute(query, result);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::updateRecord(const std::string& table,
const std::vector<SF_Sqlite_Column_Data_Pair> setValues,
const std::vector<SF_Sqlite_Column_Data_Pair> whereValues) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
std::string whereListString;
sf_sqlite_buildWhereColumnList(whereListString, whereValues);
std::string setListString;
sf_sqlite_buildSetValues(setListString, setValues);
std::string query;
sf_sqlite_buildFinalUpdateQuery(query, table, whereListString, setListString);
return this->execute(query);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::dropTable(const std::string& table) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
std::string query;
sf_sqlite_buildFinalDropTable(query, table);
return this->execute(query);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::createTable(const std::string& name,
const std::vector<SF_Sqlite_Column_Type_Pair>& columns) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
std::string createString;
sf_sqlite_buildCreateTable(createString, name);
std::string columnString;
sf_sqlite_buildCreateTableColumns(columnString, columns);
std::string query;
query += createString;
query += columnString;
return this->execute(query);
}
const SF_SQLITE_CODES::ERRORS SF_Sqlite::tableExists(const std::string& table, SF_Bool& result) const
{
SF_SQLITE_IS_NOT_CONNECTED_ESCAPE(connection);
int returnValue;
SF_SQLITE_CODES::ERRORS error = this->executeScalar("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='" + table + "';", returnValue);
if (SF_SQLITE_SUCCESS(error))
{
result = returnValue > 0;
}
return error;
}
SF_Sqlite& SF_Sqlite::operator=(const SF_Sqlite& other)
{
this->connection = other.connection;
this->isExernalConnection = other.isExernalConnection;
this->databaseName = other.databaseName;
return *this;
}
SF_Sqlite::~SF_Sqlite()
{
/*
if (connection)
{
this->disconnect();
}*/
if (connection)
{
#if DEBUG_SQLITE
printf("SF_Sqlite being destroyed but the connection is not closed. SF_Sqlite will not close the connection for you. Please call .disconnect() on this object before \n");
#endif
}
}