This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite-connection-core.c
473 lines (410 loc) · 9.96 KB
/
sqlite-connection-core.c
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
/**
* SQLite connection core - low-level SQLite connection C library API
*/
#include "sqlite-connection-core.h"
#include <stdbool.h>
#include <stddef.h>
#include <math.h>
#include <string.h>
#include "sqlite3.h"
// **Ugly hack** to make Xcode happy for iOS:
#ifdef SCC_INCLUDE_FAKE_CRYPTO_KEY
#include "sqlite-fake-crypto-key.h"
#endif
#define DEFAULT_MAX_SCC_CONNECTIONS 1000
#ifdef SCC_MAXIMUM_CONNECTIONS
#define MAX_SCC_RECORD_COUNT SCC_MAXIMUM_CONNECTIONS
#else
#define MAX_SCC_RECORD_COUNT DEFAULT_MAX_SCC_CONNECTIONS
#endif
#define FIRST_SCC_RECORD_ID 1
bool is_initialized = false;
// use sqlite3_mutex ref:
// https://www.sqlite.org/c3ref/mutex_alloc.html
sqlite3_mutex * _open_mutex = NULL;
#define START_OPEN_MUTEX() do { \
sqlite3_mutex_enter(_open_mutex); \
} while (false)
#define END_OPEN_MUTEX() do { \
sqlite3_mutex_leave(_open_mutex); \
} while (false)
// General SCC record comment(s):
// It is not expected for multiple threads to require access
// from the *same* SCC record in parallel.
// Multi-threaded access should be done from different SCC connections.
// Keeping the _st_mutex lock for an entire read or write option
// is not expected to cause any potential issues.
typedef struct scc_record_s {
sqlite3 * db;
sqlite3_mutex * _st_mutex;
sqlite3_stmt * _st;
} * scc_record_ref;
#define START_REC_ST_MUTEX(r) do { \
sqlite3_mutex_enter(r->_st_mutex); \
} while (false)
#define END_REC_ST_MUTEX(r) do { \
sqlite3_mutex_leave(r->_st_mutex); \
} while (false)
static struct scc_record_s scc_record_list[MAX_SCC_RECORD_COUNT] = { NULL };
static int _scc_record_count = FIRST_SCC_RECORD_ID;
/**
* NOT expected to be thread-safe, should be run in the main thread
*/
void scc_init()
{
if (is_initialized) return;
_open_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
is_initialized = true;
}
int scc_open_connection(const char * filename, int flags)
{
int connection_id = -1;
sqlite3 * db = NULL;
int open_result = SQLITE_ERROR;
if (!is_initialized) {
// BOGUS
return -1;
}
START_OPEN_MUTEX();
if (_scc_record_count < MAX_SCC_RECORD_COUNT) {
connection_id = _scc_record_count;
++_scc_record_count;
}
if (connection_id != -1) {
open_result = sqlite3_open_v2(filename, &db, flags, NULL);
#ifndef NO_SCC_DBCONFIG_DEFENSIVE
// extra-safe ref:
// - https://www.sqlite.org/c3ref/c_dbconfig_defensive.html#sqlitedbconfigdefensive
// - https://www.sqlite.org/releaselog/3_26_0.html
// - https://github.com/xpbrew/cordova-sqlite-storage-help/issues/34#issuecomment-597821628
// with a 4th argument needed ref:
// - http://sqlite.1065341.n5.nabble.com/sqlite3-db-config-documentation-issue-td106755.html
if (open_result == SQLITE_OK) {
open_result = sqlite3_db_config(db, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
// just in case:
if (open_result != SQLITE_OK) {
sqlite3_close(db);
}
}
#endif
if (open_result != SQLITE_OK) {
// release the internal resource:
--_scc_record_count;
}
}
END_OPEN_MUTEX();
if (open_result == SQLITE_OK) {
scc_record_list[connection_id].db = db;
scc_record_list[connection_id]._st_mutex =
sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
scc_record_list[connection_id]._st = NULL;
}
return (open_result == SQLITE_OK) ? connection_id : -open_result;
}
int scc_key(int connection_id, const char * key)
{
#ifdef NO_SCC_CRYPTO_KEY
// SIGNAL ERROR:
return -1;
#else
if (connection_id < 0) {
// BOGUS
return 0;
} else {
scc_record_ref r = &scc_record_list[connection_id];
return sqlite3_key(r->db, key, strlen(key));
}
#endif
}
int scc_begin_statement(int connection_id, const char * statement)
{
if (connection_id < 0) {
return 21; // SQLite abuse
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
int rc;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st != NULL) {
rc = 21; // SQLite abuse
} else {
sqlite3_stmt * st;
rc = sqlite3_prepare_v2(r->db, statement, -1, &st, NULL);
r->_st = st;
}
END_REC_ST_MUTEX(r);
return rc;
}
}
int scc_bind_text(int connection_id, int index, const char * text)
{
if (connection_id < 0) {
return 21; // SQLite abuse
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
int rc;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
rc = 21; // SQLite abuse
} else {
rc = sqlite3_bind_text(st, index, text, -1, SQLITE_TRANSIENT);
}
END_REC_ST_MUTEX(r);
return rc;
}
}
int scc_bind_double(int connection_id, int index, double value)
{
if (connection_id < 0) {
return 21; // SQLite abuse
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
int rc;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
rc = 21; // SQLite abuse
} else {
rc = sqlite3_bind_double(st, index, value);
}
END_REC_ST_MUTEX(r);
return rc;
}
}
int scc_bind_long(int connection_id, int index, scc_long_long value)
{
if (connection_id < 0) {
return 21; // SQLite abuse
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
int rc;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
rc = 21; // SQLite abuse
} else {
rc = sqlite3_bind_int64(st, index, value);
}
END_REC_ST_MUTEX(r);
return rc;
}
}
int scc_bind_null(int connection_id, int index)
{
if (connection_id < 0) {
return 21; // SQLite abuse
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
int rc;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
rc = 21; // SQLite abuse
} else {
rc = sqlite3_bind_null(st, index);
}
END_REC_ST_MUTEX(r);
return rc;
}
}
int scc_step(int connection_id)
{
if (connection_id < 0) {
return 21; // SQLite abuse
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
int rc;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
rc = 21; // SQLite abuse
} else {
rc = sqlite3_step(st);
}
END_REC_ST_MUTEX(r);
return rc;
}
}
const char * scc_get_last_error_message(int connection_id)
{
if (connection_id < 0) {
// BOGUS
return "invalid connection id";
} else {
scc_record_ref r = &scc_record_list[connection_id];
return sqlite3_errmsg(r->db);
}
}
int scc_get_column_count(int connection_id)
{
if (connection_id < 0) {
// BOGUS
return -1;
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
int rc;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
// BOGUS
rc = -1;
} else {
rc = sqlite3_column_count(st);
}
END_REC_ST_MUTEX(r);
return rc;
}
}
const char * scc_get_column_name(int connection_id, int column)
{
if (connection_id < 0) {
// BOGUS
return NULL;
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
const char * value;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
value = "";
} else {
value = sqlite3_column_name(st, column);
}
END_REC_ST_MUTEX(r);
return value;
}
}
int scc_get_column_type(int connection_id, int column)
{
if (connection_id < 0) {
// BOGUS
return -1;
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
int value;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
// BOGUS
value = -1;
} else {
value = sqlite3_column_type(st, column);
}
END_REC_ST_MUTEX(r);
return value;
}
}
const char * scc_get_column_text(int connection_id, int column)
{
if (connection_id < 0) {
// BOGUS
return NULL;
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
const char * value;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
value = NULL;
} else {
value = (const char *)sqlite3_column_text(st, column);
// TBD quick workaround solution to avoid a possible crash:
if (value == NULL) value = "";
}
END_REC_ST_MUTEX(r);
return value;
}
}
double scc_get_column_double(int connection_id, int column)
{
if (connection_id < 0) {
// BOGUS
return NAN;
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
double value;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
// BOGUS
value = NAN;
} else {
value = sqlite3_column_double(st, column);
}
END_REC_ST_MUTEX(r);
return value;
}
}
scc_long_long scc_get_column_long(int connection_id, int column)
{
if (connection_id < 0) {
// BOGUS
return 0;
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
scc_long_long value;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
// BOGUS
value = 0;
} else {
value = sqlite3_column_int64(st, column);
}
END_REC_ST_MUTEX(r);
return value;
}
}
int scc_end_statement(int connection_id)
{
if (connection_id < 0) {
return 21; // SQLite abuse
} else {
scc_record_ref r = &scc_record_list[connection_id];
sqlite3_stmt * st;
int rc;
START_REC_ST_MUTEX(r);
st = r->_st;
if (st == NULL) {
rc = 21; // SQLite abuse
} else {
rc = sqlite3_finalize(st);
r->_st = NULL;
}
END_REC_ST_MUTEX(r);
return rc;
}
}
int scc_get_total_changes(int connection_id)
{
if (connection_id < 0) {
// BOGUS
return 0;
} else {
scc_record_ref r = &scc_record_list[connection_id];
return sqlite3_total_changes(r->db);
}
}
int scc_get_last_insert_rowid(int connection_id)
{
if (connection_id < 0) {
// BOGUS
return 0;
} else {
scc_record_ref r = &scc_record_list[connection_id];
return sqlite3_last_insert_rowid(r->db);
}
}