-
Notifications
You must be signed in to change notification settings - Fork 28
/
sqlite3_wrapper.c
537 lines (399 loc) · 11.8 KB
/
sqlite3_wrapper.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include "sqlite3.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#include <windef.h>
#include <windows.h>
#include <shlobj.h>
#include <shlwapi.h>
// Error code
#define INIT_SUCCESS 0
#define ERROR_INVALID_TERM_DATA_DIR 0x01
// GC parameters
#define MAX_GC_ITEM_COUNT 100
#define GC_EXEC_LIMIT 90
// MetaTrader4 TERMINAL_DATA_PATH
static wchar_t *terminal_data_path = NULL;
// how long wait when DB is busy
static int busy_timeout = 1000;
// pragma for journal mode
static char* journal_statement = NULL;
// pointer to memory, returned in sqlite_get_fname. Will be freed on next
// sqlite_get_fname call or sqlite_finalize
static wchar_t* previous_db_path = NULL;
struct query_result {
sqlite3 *s;
sqlite3_stmt *stmt;
};
static void *my_alloc(size_t size)
{
return HeapAlloc (GetProcessHeap (), 0, size);
}
static void *my_realloc(void *ptr, size_t size)
{
return HeapReAlloc (GetProcessHeap (), 0, ptr, size);
}
static BOOL my_free(void *ptr)
{
return HeapFree (GetProcessHeap (), 0, ptr);
}
static BOOL directory_exists (const wchar_t *path)
{
DWORD attr = GetFileAttributesW(path);
return (attr != INVALID_FILE_ATTRIBUTES &&
(attr & FILE_ATTRIBUTE_DIRECTORY));
}
static const char *unicode_to_ansi_string (const wchar_t *unicode)
{
const int ansi_bytes = WideCharToMultiByte(
CP_ACP,
WC_COMPOSITECHECK | WC_DISCARDNS | WC_SEPCHARS | WC_DEFAULTCHAR,
unicode, -1, NULL, 0, NULL, NULL);
if (ansi_bytes == 0) {
return NULL;
}
char *ansi_buf = (char *) my_alloc(ansi_bytes);
const int converted_bytes = WideCharToMultiByte(
CP_ACP,
WC_COMPOSITECHECK | WC_DISCARDNS | WC_SEPCHARS | WC_DEFAULTCHAR,
unicode, -1, ansi_buf, ansi_bytes, NULL, NULL);
if (converted_bytes == 0) {
my_free (ansi_buf);
return NULL;
}
return ansi_buf;
}
static const wchar_t *ansi_to_unicode_string (const char *ansi)
{
const int unicode_bytes = MultiByteToWideChar(
CP_ACP,
MB_COMPOSITE,
ansi, -1, NULL, 0);
if (unicode_bytes == 0) {
return NULL;
}
wchar_t *unicode_buf = (wchar_t *) my_alloc (unicode_bytes);
const int converted_bytes = MultiByteToWideChar(
CP_ACP,
MB_COMPOSITE,
ansi, -1, unicode_buf, unicode_bytes);
if (converted_bytes == 0) {
my_free (unicode_buf);
return NULL;
}
return unicode_buf;
}
static wchar_t *my_wcscat (wchar_t **dst, const wchar_t *src)
{
int dst_buf_size = 0;
if (*dst == NULL) {
dst_buf_size = wcslen (src) + 1;
*dst = (wchar_t *) my_alloc (sizeof (wchar_t) * dst_buf_size);
*dst[0] = L'\0';
}
else {
dst_buf_size = wcslen (*dst) + wcslen (src) + 1;
*dst = (wchar_t *) my_realloc (*dst, sizeof (wchar_t) * dst_buf_size);
}
return wcsncat (*dst, src, wcslen (src));
}
/* We assume that given file name is relative to MT Terminal Data Path. */
static wchar_t *build_db_path (const wchar_t *db_filename)
{
wchar_t *buf[] = { NULL };
HRESULT res;
// if path is absolute, just return it, assuming it holds full db path
if (!PathIsRelativeW (db_filename)) {
if (my_wcscat (buf, db_filename) != 0) {
return *buf;
}
else {
return NULL;
}
}
if (my_wcscat (buf, terminal_data_path) == 0) {
return NULL;
}
if (my_wcscat (buf, L"\\MQL4\\Files\\SQLite") == 0) {
return NULL;
}
if (!directory_exists (*buf)) {
CreateDirectoryW (*buf, NULL);
}
if (my_wcscat (buf, L"\\") == 0) {
return NULL;
}
if (my_wcscat (buf, db_filename) == 0) {
return NULL;
}
return *buf;
}
static void tune_db_handler (sqlite3 *s)
{
sqlite3_busy_timeout (s, busy_timeout);
if (journal_statement)
sqlite3_exec (s, journal_statement, NULL, NULL, NULL);
RegisterExtensionFunctions (s);
}
static BOOL set_terminal_data_path(const wchar_t *path)
{
if (!directory_exists (path)) {
return FALSE;
}
if (terminal_data_path) {
free (terminal_data_path);
}
terminal_data_path = _wcsdup (path);
return terminal_data_path != NULL;
}
int sqlite_initialize(const wchar_t *term_data_path)
{
if (!set_terminal_data_path (term_data_path)) {
return ERROR_INVALID_TERM_DATA_DIR;
}
return INIT_SUCCESS;
}
void sqlite_finalize()
{
if (terminal_data_path) {
free (terminal_data_path);
terminal_data_path = NULL;
}
if (previous_db_path) {
my_free(previous_db_path);
previous_db_path = NULL;
}
}
const wchar_t *__stdcall sqlite_get_fname (const wchar_t *db_filename)
{
wchar_t *db_path = build_db_path (db_filename);
if (previous_db_path != NULL)
my_free(previous_db_path);
previous_db_path = db_path;
if (!db_path)
return NULL;
return db_path;
}
int __stdcall sqlite_exec (const wchar_t *db_filename, const wchar_t *sql)
{
sqlite3 *s;
int res;
const wchar_t *db_path = build_db_path (db_filename);
if (!db_path)
return -1;
res = sqlite3_open16 (db_path, &s);
my_free ((void *)db_path);
if (res != SQLITE_OK)
return res;
tune_db_handler (s);
const char* sql_ansi = unicode_to_ansi_string (sql);
res = sqlite3_exec (s, sql_ansi, NULL, NULL, NULL);
my_free ((void *)sql_ansi);
if (res != SQLITE_OK) {
sqlite3_close (s);
return res;
}
sqlite3_close (s);
return SQLITE_OK;
}
/*
* return 1 if table exists in database, 0 oterwise. -ERROR returned on error.
*/
int __stdcall sqlite_table_exists (const wchar_t const *db_filename, const wchar_t const *table_name)
{
sqlite3 *s;
sqlite3_stmt *stmt;
char buf[256];
int res, exists;
const wchar_t *db_path = build_db_path (db_filename);
if (!db_path)
return -1;
res = sqlite3_open16 (db_path, &s);
my_free ((void *)db_path);
if (res != SQLITE_OK)
return -res;
tune_db_handler (s);
const char *table_name_ansi = unicode_to_ansi_string (table_name);
sprintf (buf, "select count(*) from sqlite_master where type='table' and name='%s'", table_name_ansi);
res = sqlite3_prepare (s, buf, sizeof (buf), &stmt, NULL);
my_free ((void *)table_name_ansi);
if (res != SQLITE_OK) {
sqlite3_close (s);
return -res;
}
res = sqlite3_step (stmt);
if (res != SQLITE_ROW) {
sqlite3_finalize (stmt);
sqlite3_close (s);
return -res;
}
exists = sqlite3_column_int (stmt, 0);
sqlite3_finalize (stmt);
sqlite3_close (s);
return exists > 0 ? 1 : 0;
}
/*
* Perform query and pack results in internal structure. Routine returns amount of data fetched and
* integer handle which can be used to sqlite_get_data. On error, return -SQLITE_ERROR.
*/
int __stdcall sqlite_query (const wchar_t *db_filename, const wchar_t *sql, int* cols)
{
sqlite3 *s;
sqlite3_stmt *stmt;
int res;
struct query_result *result;
const wchar_t* db_path = build_db_path (db_filename);
if (!db_path)
return -1;
res = sqlite3_open16 (db_path, &s);
my_free ((void *)db_path);
if (res != SQLITE_OK)
return -res;
tune_db_handler (s);
res = sqlite3_prepare16 (s, sql, wcslen (sql) * sizeof (wchar_t), &stmt, NULL);
if (res != SQLITE_OK) {
sqlite3_close (s);
return -res;
}
result = (struct query_result*)malloc (sizeof (struct query_result));
result->s = s;
result->stmt = stmt;
*cols = sqlite3_column_count (stmt);
return (int)result;
}
int __stdcall sqlite_reset (int handle)
{
struct query_result *res = (struct query_result*)handle;
int ret;
if (!res)
return 0;
ret = sqlite3_reset (res->stmt);
return ret == SQLITE_OK ? 1 : 0;
}
int __stdcall sqlite_bind_int (int handle, int col, int bind_value)
{
struct query_result *res = (struct query_result*)handle;
int ret;
if (!res)
return 0;
ret = sqlite3_bind_int (res->stmt, col, bind_value);
return ret == SQLITE_OK ? 1 : 0;
}
int __stdcall sqlite_bind_int64 (int handle, int col, __int64 bind_value)
{
struct query_result *res = (struct query_result*)handle;
int ret;
if (!res)
return 0;
ret = sqlite3_bind_int64 (res->stmt, col, bind_value);
return ret == SQLITE_OK ? 1 : 0;
}
int __stdcall sqlite_bind_double (int handle, int col, double bind_value)
{
struct query_result *res = (struct query_result*)handle;
int ret;
if (!res)
return 0;
ret = sqlite3_bind_double (res->stmt, col, bind_value);
return ret == SQLITE_OK ? 1 : 0;
}
int __stdcall sqlite_bind_text (int handle, int col, const wchar_t* bind_value)
{
struct query_result *res = (struct query_result*)handle;
int ret;
if (!res)
return 0;
ret = sqlite3_bind_text16 (res->stmt, col, bind_value, -1, SQLITE_STATIC);
return ret == SQLITE_OK ? 1 : 0;
}
int __stdcall sqlite_bind_null (int handle, int col)
{
struct query_result *res = (struct query_result*)handle;
int ret;
if (!res)
return 0;
ret = sqlite3_bind_null (res->stmt, col);
return ret == SQLITE_OK ? 1 : 0;
}
/*
* Return 1 if next row fetched, 0 if end of resultset reached
*/
int __stdcall sqlite_next_row (int handle)
{
struct query_result *res = (struct query_result*)handle;
int ret;
if (!res)
return 0;
ret = sqlite3_step (res->stmt);
return ret == SQLITE_ROW ? 1 : 0;
}
const wchar_t* __stdcall sqlite_get_col (int handle, int col)
{
struct query_result *data = (struct query_result*)handle;
if (!data)
return NULL;
// In sqlite3.h we have the following note:
// ** The pointers returned are valid until a type conversion occurs as
// ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
// ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
// ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
// ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
// ** [sqlite3_free()].
// So, it's safe to just return pointer, as mql will copy string's content into it's own buffer
// (according to docs).
return sqlite3_column_text16 (data->stmt, col);
}
int __stdcall sqlite_get_col_int (int handle, int col)
{
struct query_result *data = (struct query_result*)handle;
if (!data)
return 0;
return sqlite3_column_int (data->stmt, col);
}
__int64 __stdcall sqlite_get_col_int64 (int handle, int col)
{
struct query_result *data = (struct query_result*)handle;
if (!data)
return 0;
return sqlite3_column_int64 (data->stmt, col);
}
double __stdcall sqlite_get_col_double (int handle, int col)
{
struct query_result *data = (struct query_result*)handle;
if (!data)
return 0;
return sqlite3_column_double (data->stmt, col);
}
int __stdcall sqlite_free_query (int handle)
{
struct query_result *data = (struct query_result*)handle;
if (!data)
return 0;
if (data->stmt)
sqlite3_finalize (data->stmt);
if (data->s)
sqlite3_close (data->s);
free (data);
return 1;
}
void __stdcall sqlite_set_busy_timeout (int ms)
{
busy_timeout = ms;
}
void __stdcall sqlite_set_journal_mode (const wchar_t* mode)
{
if (journal_statement) {
free (journal_statement);
journal_statement = NULL;
}
if (!mode)
return;
const char *mode_ansi = unicode_to_ansi_string (mode);
static const char *format = "PRAGMA journal_mode=%s;";
int len = strlen (format) + strlen (mode_ansi) + 1;
journal_statement = (char*)malloc (len);
sprintf (journal_statement, format, mode);
my_free ((void *)mode_ansi);
}