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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/data/sqlite3_adapter.h"
#include "gate/results.h"
#include "gate/arrays.h"
#include "gate/debugging.h"
#include "gate/data/adapter_base.h"
#if defined(GATE_EXTLIB_SQLITE3)
# define GATE_DATA_SQLITE3_LIB_IMPL 1
#else
# if defined(GATE_SYS_WIN) || defined(GATE_SYS_POSIX)
# if !defined(GATE_DATA_SQLITE3_SHAREDLIB)
# define GATE_DATA_SQLITE3_SHAREDLIB 1
# endif
# define GATE_DATA_SQLITE3_LIB_IMPL 1
# else
# define GATE_DATA_SQLITE3_NO_IMPL 1
# endif
#endif
#if defined(GATE_DATA_SQLITE3_LIB_IMPL)
#if !defined(GATE_DATA_SQLITE3_SHAREDLIB)
# include "sqlite3.h"
#else
# include "gate/libraries.h"
typedef struct gate_sqlite3_stmt_impl sqlite3_stmt;
typedef struct gate_sqlite3_impl sqlite3;
typedef gate_int64_t sqlite_int64;
#define SQLITE_INTEGER 1
#define SQLITE_FLOAT 2
#define SQLITE_BLOB 4
#define SQLITE_NULL 5
#ifdef SQLITE_TEXT
# undef SQLITE_TEXT
#else
# define SQLITE_TEXT 3
#endif
#define SQLITE3_TEXT 3
#define SQLITE_OK 0
#define SQLITE_ERROR 1
#define SQLITE_INTERNAL 2
#define SQLITE_PERM 3
#define SQLITE_ABORT 4
#define SQLITE_BUSY 5
#define SQLITE_LOCKED 6
#define SQLITE_NOMEM 7
#define SQLITE_READONLY 8
#define SQLITE_INTERRUPT 9
#define SQLITE_IOERR 10
#define SQLITE_CORRUPT 11
#define SQLITE_NOTFOUND 12
#define SQLITE_FULL 13
#define SQLITE_CANTOPEN 14
#define SQLITE_PROTOCOL 15
#define SQLITE_EMPTY 16
#define SQLITE_SCHEMA 17
#define SQLITE_TOOBIG 18
#define SQLITE_CONSTRAINT 19
#define SQLITE_MISMATCH 20
#define SQLITE_MISUSE 21
#define SQLITE_NOLFS 22
#define SQLITE_AUTH 23
#define SQLITE_FORMAT 24
#define SQLITE_RANGE 25
#define SQLITE_NOTADB 26
#define SQLITE_NOTICE 27
#define SQLITE_WARNING 28
#define SQLITE_ROW 100
#define SQLITE_DONE 101
#define SQLITE_OPEN_READONLY 0x00000001
#define SQLITE_OPEN_READWRITE 0x00000002
#define SQLITE_OPEN_CREATE 0x00000004
#define SQLITE_OPEN_DELETEONCLOSE 0x00000008
#define SQLITE_OPEN_EXCLUSIVE 0x00000010
#define SQLITE_OPEN_AUTOPROXY 0x00000020
#define SQLITE_OPEN_URI 0x00000040
#define SQLITE_OPEN_MEMORY 0x00000080
#define SQLITE_OPEN_MAIN_DB 0x00000100
#define SQLITE_OPEN_TEMP_DB 0x00000200
#define SQLITE_OPEN_TRANSIENT_DB 0x00000400
#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800
#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000
#define SQLITE_OPEN_SUBJOURNAL 0x00002000
#define SQLITE_OPEN_SUPER_JOURNAL 0x00004000
#define SQLITE_OPEN_NOMUTEX 0x00008000
#define SQLITE_OPEN_FULLMUTEX 0x00010000
#define SQLITE_OPEN_SHAREDCACHE 0x00020000
#define SQLITE_OPEN_PRIVATECACHE 0x00040000
#define SQLITE_OPEN_WAL 0x00080000
#define SQLITE_OPEN_NOFOLLOW 0x01000000
#define SQLITE_OPEN_EXRESCODE 0x02000000
#endif
typedef struct gate_sqlite3_api_class
{
int (*column_type) (sqlite3_stmt* pStmt, int i);
int (*column_count) (sqlite3_stmt* pStmt);
const char* (*column_name) (sqlite3_stmt* pStmt, int N);
sqlite_int64 (*column_int64) (sqlite3_stmt* pStmt, int i);
double (*column_double) (sqlite3_stmt* pStmt, int i);
int (*column_bytes) (sqlite3_stmt* pStmt, int i);
const void* (*column_blob) (sqlite3_stmt* pStmt, int i);
const unsigned char*(*column_text) (sqlite3_stmt* pStmt, int i);
int (*bind_parameter_count) (sqlite3_stmt* pStmt);
int (*bind_parameter_index) (sqlite3_stmt* pStmt, const char* zName);
const char* (*bind_parameter_name) (sqlite3_stmt* pStmt, int i);
int (*bind_blob) (sqlite3_stmt* pStmt, int i, const void* value, int len, void(*dtor)(void*));
int (*bind_double) (sqlite3_stmt* pStmt, int i, double value);
int (*bind_int64) (sqlite3_stmt* pStmt, int i, sqlite_int64 value);
int (*bind_null) (sqlite3_stmt* pStmt, int i);
int (*bind_text) (sqlite3_stmt* pStmt, int i, const char* text, int len, void(*dtor)(void*));
int (*prepare) (sqlite3* db, const char* zSql, int nBytes, sqlite3_stmt** ppStmt, const char** pzTail);
int (*clear_bindings) (sqlite3_stmt*);
int (*reset) (sqlite3_stmt* pStmt);
int (*changes) (sqlite3* db);
int (*close) (sqlite3* db);
int (*open) (const char* zFilename, sqlite3** ppDb);
int (*open_v2) (const char* zFilename, sqlite3** ppDb, int flags, const char* zVfs);
int (*finalize) (sqlite3_stmt* pStmt);
int (*step) (sqlite3_stmt* pStmt);
const char* (*errmsg) (sqlite3* db);
} gate_sqlite3_api_t;
static gate_sqlite3_api_t sqlite3_api = GATE_INIT_EMPTY;
#if !defined(GATE_DATA_SQLITE3_SHAREDLIB)
#if defined(GATE_DEBUG_MODE)
# /* define GATE_DATA_SQLITE3_DEBUGGING 1 */
#endif
#if defined(GATE_DATA_SQLITE3_DEBUGGING)
void errorLogCallback(void* pArg, int iErrCode, const char* zMsg)
{
printf("SQLITE (%d): %s\n", iErrCode, zMsg);
}
#endif
static gate_result_t gate_data_sqlite3_api_init()
{
static gate_atomic_flag_t sqlite3_api_initialized = GATE_ATOMIC_FLAG_INIT;
if (gate_atomic_flag_set(&sqlite3_api_initialized) == 0)
{
sqlite3_api.column_type = &sqlite3_column_type;
sqlite3_api.column_count = &sqlite3_column_count;
sqlite3_api.column_name = &sqlite3_column_name;
sqlite3_api.column_int64 = &sqlite3_column_int64;
sqlite3_api.column_double = &sqlite3_column_double;
sqlite3_api.column_bytes = &sqlite3_column_bytes;
sqlite3_api.column_blob = &sqlite3_column_blob;
sqlite3_api.column_text = &sqlite3_column_text;
sqlite3_api.bind_parameter_count = &sqlite3_bind_parameter_count;
sqlite3_api.bind_parameter_index = &sqlite3_bind_parameter_index;
sqlite3_api.bind_parameter_name = &sqlite3_bind_parameter_name;
sqlite3_api.bind_blob = &sqlite3_bind_blob;
sqlite3_api.bind_double = &sqlite3_bind_double;
sqlite3_api.bind_int64 = &sqlite3_bind_int64;
sqlite3_api.bind_null = &sqlite3_bind_null;
sqlite3_api.bind_text = &sqlite3_bind_text;
sqlite3_api.prepare = &sqlite3_prepare;
sqlite3_api.clear_bindings = &sqlite3_clear_bindings;
sqlite3_api.reset = &sqlite3_reset;
sqlite3_api.changes = &sqlite3_changes;
sqlite3_api.close = &sqlite3_close;
sqlite3_api.open = &sqlite3_open;
sqlite3_api.open_v2 = &sqlite3_open_v2;
sqlite3_api.finalize = &sqlite3_finalize;
sqlite3_api.step = &sqlite3_step;
sqlite3_api.errmsg = &sqlite3_errmsg;
#if defined(GATE_DATA_SQLITE3_DEBUGGING)
sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, NULL);
#endif
}
return GATE_RESULT_OK;
}
#else
static char const* gate_data_sqlite3_lib_names[] = {
#if defined(GATE_SYS_WIN)
"sqlite3.dll"
#else
"libsqlite3.so.0"
#endif
#if defined(GATE_DATA_SQLITE3_LIB_ALTERNATIVES)
,
GATE_DATA_SQLITE3_LIB_ALTERNATIVES
#endif
};
static gate_size_t const gate_data_sqlite3_lib_names_count
= sizeof(gate_data_sqlite3_lib_names) / sizeof(gate_data_sqlite3_lib_names[0]);
static gate_result_t gate_data_sqlite3_api_load(gate_library_t lib)
{
#define SQL3_LOAD_FUNC(name, func_ptr) \
ret = gate_library_get_function_name(lib, name, func_ptr); \
if(GATE_FAILED(ret)) return ret;
gate_result_t ret;
SQL3_LOAD_FUNC("sqlite3_column_type", &sqlite3_api.column_type);
SQL3_LOAD_FUNC("sqlite3_column_count", &sqlite3_api.column_count);
SQL3_LOAD_FUNC("sqlite3_column_name", &sqlite3_api.column_name);
SQL3_LOAD_FUNC("sqlite3_column_int64", &sqlite3_api.column_int64);
SQL3_LOAD_FUNC("sqlite3_column_double", &sqlite3_api.column_double);
SQL3_LOAD_FUNC("sqlite3_column_bytes", &sqlite3_api.column_bytes);
SQL3_LOAD_FUNC("sqlite3_column_blob", &sqlite3_api.column_blob);
SQL3_LOAD_FUNC("sqlite3_column_text", &sqlite3_api.column_text);
SQL3_LOAD_FUNC("sqlite3_bind_parameter_count", &sqlite3_api.bind_parameter_count);
SQL3_LOAD_FUNC("sqlite3_bind_parameter_index", &sqlite3_api.bind_parameter_index);
SQL3_LOAD_FUNC("sqlite3_bind_parameter_name", &sqlite3_api.bind_parameter_name);
SQL3_LOAD_FUNC("sqlite3_bind_blob", &sqlite3_api.bind_blob);
SQL3_LOAD_FUNC("sqlite3_bind_double", &sqlite3_api.bind_double);
SQL3_LOAD_FUNC("sqlite3_bind_int64", &sqlite3_api.bind_int64);
SQL3_LOAD_FUNC("sqlite3_bind_null", &sqlite3_api.bind_null);
SQL3_LOAD_FUNC("sqlite3_bind_text", &sqlite3_api.bind_text);
SQL3_LOAD_FUNC("sqlite3_prepare", &sqlite3_api.prepare);
SQL3_LOAD_FUNC("sqlite3_clear_bindings", &sqlite3_api.clear_bindings);
SQL3_LOAD_FUNC("sqlite3_reset", &sqlite3_api.reset);
SQL3_LOAD_FUNC("sqlite3_changes", &sqlite3_api.changes);
SQL3_LOAD_FUNC("sqlite3_close", &sqlite3_api.close);
SQL3_LOAD_FUNC("sqlite3_open", &sqlite3_api.open);
SQL3_LOAD_FUNC("sqlite3_open_v2", &sqlite3_api.open_v2);
SQL3_LOAD_FUNC("sqlite3_finalize", &sqlite3_api.finalize);
SQL3_LOAD_FUNC("sqlite3_step", &sqlite3_api.step);
SQL3_LOAD_FUNC("sqlite3_errmsg", &sqlite3_api.errmsg);
return GATE_RESULT_OK;
}
static gate_result_t gate_data_sqlite3_api_init()
{
static gate_atomic_flag_t sqlite3_api_initialized = GATE_ATOMIC_FLAG_INIT;
gate_string_t libname = GATE_STRING_INIT_EMPTY;
gate_size_t index;
gate_library_t lib = NULL;
gate_result_t result;
if (gate_atomic_flag_set(&sqlite3_api_initialized) == true)
{
/* already loaded */
return GATE_RESULT_OK;
}
for (index = 0; index != gate_data_sqlite3_lib_names_count; ++index)
{
gate_string_create_static(&libname, gate_data_sqlite3_lib_names[index]);
result = gate_library_open(&libname, &lib, GATE_LIBRARY_FLAG_DEFAULT);
if (GATE_FAILED(result))
{
continue;
}
result = gate_data_sqlite3_api_load(lib);
if (GATE_SUCCEEDED(result))
{
/* library loaded -> OK */
return GATE_RESULT_OK;
}
gate_library_close(lib);
}
/* no library loaded -> reset */
gate_atomic_flag_clear(&sqlite3_api_initialized);
return GATE_RESULT_NOTAVAILABLE;
}
#endif
static gate_result_t sqlite3_error_to_gate_result(int sql3_error)
{
switch (sql3_error)
{
case SQLITE_OK: return GATE_RESULT_OK;
case SQLITE_ROW: return GATE_RESULT_OK_PARTIAL;
case SQLITE_ERROR: return GATE_RESULT_FAILED;
case SQLITE_INTERNAL: return GATE_RESULT_UNKNOWNERROR;
case SQLITE_PERM: return GATE_RESULT_PERMISSIONDENIED;
case SQLITE_ABORT: return GATE_RESULT_CANCELED;
case SQLITE_BUSY: return GATE_RESULT_BLOCKED;
case SQLITE_LOCKED: return GATE_RESULT_LOCKED;
case SQLITE_NOMEM: return GATE_RESULT_OUTOFMEMORY;
case SQLITE_READONLY: return GATE_RESULT_ACCESSDENIED;
case SQLITE_INTERRUPT: return GATE_RESULT_EXECUTIONINTERRUPTED;
case SQLITE_IOERR: return GATE_RESULT_DEVICEERROR;
case SQLITE_CORRUPT: return GATE_RESULT_INVALIDDATA;
case SQLITE_NOTFOUND: return GATE_RESULT_INVALIDARG;
case SQLITE_FULL: return GATE_RESULT_OUTOFRESOURCES;
case SQLITE_CANTOPEN: return GATE_RESULT_NOTAVAILABLE;
case SQLITE_PROTOCOL: return GATE_RESULT_NOTREADY;
case SQLITE_SCHEMA: return GATE_RESULT_PRECONDITIONFAILED;
case SQLITE_TOOBIG: return GATE_RESULT_OVERFLOW;
case SQLITE_CONSTRAINT: return GATE_RESULT_EXECUTIONFAILED;
case SQLITE_MISMATCH: return GATE_RESULT_INCORRECTTYPE;
case SQLITE_MISUSE: return GATE_RESULT_INVALIDSTATE;
case SQLITE_NOLFS: return GATE_RESULT_NOTSUPPORTED;
case SQLITE_AUTH: return GATE_RESULT_SECURITYBREACH;
case SQLITE_RANGE: return GATE_RESULT_OUTOFBOUNDS;
case SQLITE_NOTADB: return GATE_RESULT_INVALIDCONTENT;
case SQLITE_DONE: return GATE_RESULT_ENDOFSTREAM;
default: return GATE_RESULT_FAILED;
}
}
static gate_bool_t gate_data_reader_sqlite3_is_valid(gate_data_reader_base_t* reader)
{
sqlite3_stmt* stmt = (sqlite3_stmt*)reader->native_handles[0];
return stmt != NULL;
}
static gate_result_t gate_data_reader_sqlite3_close(gate_data_reader_base_t* reader)
{
gate_result_t ret = GATE_RESULT_OK;
sqlite3_stmt* stmt = (sqlite3_stmt*)reader->native_handles[0];<--- Variable 'stmt' is assigned a value that is never used.
do
{
gate_mem_clear(reader->native_handles, sizeof(reader->native_handles));
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
static gate_result_t gate_data_reader_sqlite3_next(gate_data_reader_base_t* reader)
{
gate_result_t ret = GATE_RESULT_FAILED;
sqlite3_stmt* stmt = (sqlite3_stmt*)reader->native_handles[0];
int sql3_result;<--- The scope of the variable 'sql3_result' can be reduced. [+]The scope of the variable 'sql3_result' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_size_t ndx;
int sql3_type;
gate_int64_t i64;
gate_real64_t r64;
gate_value_t* ptr_value;
int byte_count;
gate_uint8_t const* byte_ptr;
char const* text_ptr;
gate_arraylist_t arraylist = NULL;
gate_array_t arr = GATE_INIT_EMPTY;
gate_string_t str = GATE_STRING_INIT_EMPTY;
do
{
if (!gate_data_reader_base_is_valid(reader))
{
ret = GATE_RESULT_ENDOFSTREAM;
break;
}
sql3_result = sqlite3_api.step(stmt);
if (SQLITE_DONE == sql3_result)
{
/* end of data stream reached */
ret = GATE_RESULT_ENDOFSTREAM;
gate_data_reader_base_close(reader);
break;
}
if (SQLITE_ROW != sql3_result)
{
/* data read error occured */
ret = GATE_RESULT_FAILED;
GATE_DEBUG_TRACE_MSG_VALUE("sqlite3_step() failed", sql3_result);
break;
}
/* succeeded: */
for (ndx = 0; ndx != reader->field_count; ++ndx)
{
ptr_value = &reader->values[ndx];
gate_value_release(ptr_value);
sql3_type = sqlite3_api.column_type(stmt, (int)ndx);
switch (sql3_type)
{
case SQLITE_INTEGER:
i64 = (gate_int64_t)sqlite3_api.column_int64(stmt, (int)ndx);
gate_value_create(GATE_TYPE_I64, &i64, ptr_value);
break;
case SQLITE_FLOAT:
r64 = (gate_real64_t)sqlite3_api.column_double(stmt, (int)ndx);
gate_value_create(GATE_TYPE_R64, &r64, ptr_value);
break;
case SQLITE_BLOB:
byte_count = sqlite3_api.column_bytes(stmt, (int)ndx);
byte_ptr = (gate_uint8_t const*)sqlite3_api.column_blob(stmt, (int)ndx);
arraylist = gate_arraylist_create(1, byte_ptr, (gate_size_t)byte_count, NULL, NULL);
gate_array_create(&arr, arraylist);
gate_value_create(GATE_TYPE_ARRAY, &arr, ptr_value);
gate_array_release(&arr);
break;
case SQLITE_TEXT:
byte_count = sqlite3_api.column_bytes(stmt, (int)ndx);
text_ptr = (char const*)sqlite3_api.column_text(stmt, (int)ndx);
gate_string_create(&str, text_ptr, (gate_size_t)byte_count);
gate_value_create(GATE_TYPE_STRING, &str, ptr_value);
gate_string_release(&str);
break;
default:
case SQLITE_NULL:
/* already released */
break;
}
}
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
static gate_result_t gate_data_reader_sqlite3_init(gate_data_reader_base_t* reader)
{
gate_result_t ret = GATE_RESULT_OK;
sqlite3_stmt* stmt = (sqlite3_stmt*)reader->native_handles[0];
gate_size_t ndx;
char const* ptr_name;<--- The scope of the variable 'ptr_name' can be reduced. [+]The scope of the variable 'ptr_name' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
reader->field_count = (gate_size_t)sqlite3_api.column_count(stmt);
if (reader->field_count > sizeof(reader->names) / sizeof(reader->names[0]))
{
reader->field_count = sizeof(reader->names) / sizeof(reader->names[0]);
}
for (ndx = 0; ndx != reader->field_count; ++ndx)
{
ptr_name = sqlite3_api.column_name(stmt, (int)ndx);
gate_string_create(&reader->names[ndx], ptr_name, gate_str_length(ptr_name));
}
return ret;
}
static gate_result_t sqlite3_execute_statement(sqlite3* db, sqlite3_stmt* stmt, gate_int32_t* affected_rows)
{
int sql3_result = 0;
do
{
sql3_result = sqlite3_api.step(stmt);
} while (sql3_result == SQLITE_ROW);
switch (sql3_result)
{
case SQLITE_DONE:
{
if (affected_rows != NULL)
{
*affected_rows = (gate_int32_t)sqlite3_api.changes(db);
}
return GATE_RESULT_OK;
}
case SQLITE_BUSY:
{
GATE_DEBUG_TRACE("sqlite DB is busy");
return GATE_RESULT_LOCKED;
}
case SQLITE_ERROR:
{
GATE_DEBUG_TRACE(sqlite3_api.errmsg(db));
return GATE_RESULT_FAILED;
}
default:
{
GATE_DEBUG_TRACE_MSG_VALUE("Unknown sqlite3_step() result", sql3_result);
return GATE_RESULT_UNKNOWNERROR;
}
}
}
static gate_result_t sqlite3_query_statement(gate_data_statement_t* ptr_statement, sqlite3_stmt* stmt, gate_data_reader_t** ptr_reader)
{
gate_data_reader_t* reader = NULL;
gate_data_handles_t handles = GATE_INIT_EMPTY;
handles[0] = (void*)stmt;
reader = gate_data_reader_base_create(
ptr_statement, handles,
&gate_data_reader_sqlite3_init,
&gate_data_reader_sqlite3_is_valid,
&gate_data_reader_sqlite3_next,
&gate_data_reader_sqlite3_close);
if (reader == NULL)
{
return GATE_RESULT_OUTOFMEMORY;
}
if (ptr_reader)
{
*ptr_reader = reader;
reader = NULL;
}
else
{
gate_object_release(reader);
}
return GATE_RESULT_OK;
}
static gate_result_t sqlite3stmt_init(gate_data_statement_base_t* impl)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_string_t cmd = GATE_STRING_INIT_EMPTY;
sqlite3* db = impl->native_handles[0];<--- Variable 'db' is assigned a value that is never used.
sqlite3_stmt* stmt = impl->native_handles[1];
int cnt;<--- The scope of the variable 'cnt' can be reduced. [+]The scope of the variable 'cnt' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
do
{
cnt = sqlite3_api.bind_parameter_count(stmt);
if (cnt >= 0)
{
impl->param_count = (gate_size_t)cnt;
}
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
static void sqlite3stmt_close(gate_data_statement_base_t* impl)
{
sqlite3_stmt* sql3_statement = (sqlite3_stmt*)impl->native_handles[1];
impl->native_handles[1] = NULL;
if (sql3_statement != NULL)
{
sqlite3_api.finalize(sql3_statement);
}
}
static gate_result_t sql3_bind_int(sqlite3_stmt* stmt, int index, gate_int64_t value)
{
int sql3_result = sqlite3_api.bind_int64(stmt, index, value);
return sqlite3_error_to_gate_result(sql3_result);
}
static gate_result_t sql3_bind_real(sqlite3_stmt* stmt, int index, gate_real64_t value)
{
int sql3_result = sqlite3_api.bind_double(stmt, index, value);
return sqlite3_error_to_gate_result(sql3_result);
}
static gate_result_t sql3_bind_blob(sqlite3_stmt* stmt, int index, void const* data, gate_size_t len)
{
int sql3_result;
char* buffer = NULL;
if ((len == 0) || (data == NULL))
{
sql3_result = sqlite3_api.bind_blob(stmt, index, NULL, 0, NULL);
}
else
{
buffer = gate_mem_alloc(len);
if (buffer == NULL)
{
return GATE_RESULT_OUTOFMEMORY;
}
gate_mem_copy(buffer, data, len);
sql3_result = sqlite3_api.bind_blob(stmt, index, buffer, (int)len, &gate_mem_dealloc);
if (sql3_result != SQLITE_OK)
{
gate_mem_dealloc(buffer);
}
}
return sqlite3_error_to_gate_result(sql3_result);
}
static gate_result_t sql3_bind_text(sqlite3_stmt* stmt, int index, char const* text, gate_size_t len)
{
int sql3_result;
char* buffer = NULL;
if ((len == 0) || (text == NULL))
{
sql3_result = sqlite3_api.bind_text(stmt, index, NULL, 0, NULL);
}
else
{
buffer = gate_mem_alloc(len + 1);
if (buffer == NULL)
{
return GATE_RESULT_OUTOFMEMORY;
}
gate_mem_copy(buffer, text, len);
buffer[len] = 0;
sql3_result = sqlite3_api.bind_text(stmt, index, buffer, (int)len, &gate_mem_dealloc);
if (sql3_result != SQLITE_OK)
{
gate_mem_dealloc(buffer);
}
}
return sqlite3_error_to_gate_result(sql3_result);
}
static gate_result_t sqlite3stmt_set_param(gate_data_statement_base_t* self, gate_size_t param_index, gate_value_t const* value)
{
gate_result_t ret = GATE_RESULT_FAILED;
sqlite3_stmt* stmt = (sqlite3_stmt*)self->native_handles[1];
int param_count;
int sql3_result = SQLITE_MISMATCH;<--- The scope of the variable 'sql3_result' can be reduced. [+]The scope of the variable 'sql3_result' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
int index = (int)param_index;
gate_cstrbuffer8_t buffer;
gate_size_t len;
gate_datetime_t dt = GATE_INIT_EMPTY;
param_count = sqlite3_api.bind_parameter_count(stmt);
if ((index == 0) || (index > param_count))
{
return GATE_RESULT_OUTOFBOUNDS;
}
if ((value == NULL) || (value->value_type == GATE_TYPE_EMPTY))
{
sql3_result = sqlite3_api.bind_null(stmt, index);
return sqlite3_error_to_gate_result(sql3_result);
}
else
{
switch (value->value_type)
{
case GATE_TYPE_BOOL: return sql3_bind_int(stmt, index, value->content.bool_value);
case GATE_TYPE_I8: return sql3_bind_int(stmt, index, value->content.i8_value);
case GATE_TYPE_UI8: return sql3_bind_int(stmt, index, value->content.ui8_value);
case GATE_TYPE_I16: return sql3_bind_int(stmt, index, value->content.i16_value);
case GATE_TYPE_UI16: return sql3_bind_int(stmt, index, value->content.ui16_value);
case GATE_TYPE_I32: return sql3_bind_int(stmt, index, value->content.i32_value);
case GATE_TYPE_UI32: return sql3_bind_int(stmt, index, value->content.ui32_value);
case GATE_TYPE_I64: return sql3_bind_int(stmt, index, value->content.i64_value);
case GATE_TYPE_UI64: return sql3_bind_int(stmt, index, value->content.ui64_value);
case GATE_TYPE_R32: return sql3_bind_real(stmt, index, value->content.r32_value);
case GATE_TYPE_R64: return sql3_bind_real(stmt, index, value->content.r64_value);
case GATE_TYPE_BLOB: return sql3_bind_blob(stmt, index, value->content.blob_value.data, value->content.blob_value.length);
case GATE_TYPE_CSTR: return sql3_bind_text(stmt, index, value->content.cstring_value, gate_str_length(value->content.cstring_value));
case GATE_TYPE_STRING: return sql3_bind_text(stmt, index, value->content.string_value.str, value->content.string_value.length);
case GATE_TYPE_WSTR:
{
if (NULL == gate_cstrbuffer_create_wstr(&buffer, value->content.wstring_value, gate_wstr_length(value->content.wstring_value)))
{
return GATE_RESULT_OUTOFMEMORY;
}
ret = sql3_bind_text(stmt, index, gate_cstrbuffer_get(&buffer), gate_cstrbuffer_length(&buffer));
gate_cstrbuffer_destroy(&buffer);
return ret;
}
case GATE_TYPE_GUID:
{
ret = gate_guid_to_string(&value->content.guid_value, buffer.local_buffer);
GATE_RETURN_IF_FAILED(ret);
return sql3_bind_text(stmt, index, buffer.local_buffer, gate_str_length(buffer.local_buffer));
}
case GATE_TYPE_DATE:
{
dt.date = value->content.date_value;
len = sizeof(buffer.local_buffer);
ret = gate_date_to_string(&dt, 0, "{YYYY}-{MM}-{DD}", buffer.local_buffer, &len);
GATE_RETURN_IF_FAILED(ret);
return sql3_bind_text(stmt, index, buffer.local_buffer, len);
}
case GATE_TYPE_DATETIME:
{
len = sizeof(buffer.local_buffer);
ret = gate_date_to_string(&value->content.datetime_value, 0, "{YYYY}-{MM}-{DD} {hh}:{mm}:{ss}", buffer.local_buffer, &len);
GATE_RETURN_IF_FAILED(ret);
return sql3_bind_text(stmt, index, buffer.local_buffer, len);
}
case GATE_TYPE_TIME:
{
ret = gate_time_to_datetime(&value->content.time_value, &dt);
GATE_RETURN_IF_FAILED(ret);
len = sizeof(buffer.local_buffer);
ret = gate_date_to_string(&dt, 0, "{YYYY}-{MM}-{DD} {hh}:{mm}:{ss}", buffer.local_buffer, &len);
GATE_RETURN_IF_FAILED(ret);
return sql3_bind_text(stmt, index, buffer.local_buffer, len);
}
case GATE_TYPE_PROPERTY:
default:
return GATE_RESULT_INCORRECTTYPE;
}
}
return ret;
}
static gate_result_t sqlite3stmt_set_named_param(gate_data_statement_base_t* self, gate_string_t const* name, gate_value_t const* value)
{
gate_result_t ret = GATE_RESULT_FAILED;
sqlite3_stmt* sql3_statement = (sqlite3_stmt*)self->native_handles[1];
int param_count;
int param_ndx;
char const* param_name;<--- The scope of the variable 'param_name' can be reduced. [+]The scope of the variable 'param_name' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
param_count = sqlite3_api.bind_parameter_count(sql3_statement);
for (param_ndx = 1; param_ndx <= param_count; ++param_ndx)
{
param_name = sqlite3_api.bind_parameter_name(sql3_statement, param_ndx);
if (gate_string_equals_str(name, param_name))
{
/* named parameter found, access it by index */
return sqlite3stmt_set_param(self, (gate_size_t)param_ndx, value);
}
}
/* no parameter was found */
return GATE_RESULT_NOMATCH;
}
static gate_result_t sqlite3stmt_reset(gate_data_statement_base_t* self)
{
sqlite3_stmt* sql3_statement = (sqlite3_stmt*)self->native_handles[1];
int sql3_result;
sql3_result = sqlite3_api.clear_bindings(sql3_statement);
if (sql3_result != SQLITE_OK)
{
return sqlite3_error_to_gate_result(sql3_result);
}
sql3_result = sqlite3_api.reset(sql3_statement);
if (sql3_result != SQLITE_OK)
{
return sqlite3_error_to_gate_result(sql3_result);
}
return GATE_RESULT_OK;
}
static gate_result_t sqlite3stmt_execute(gate_data_statement_base_t* self, gate_int32_t* affected_rows)
{
gate_result_t ret = GATE_RESULT_FAILED;
sqlite3* db = (sqlite3*)self->native_handles[0];
sqlite3_stmt* stmt = (sqlite3_stmt*)self->native_handles[1];
return sqlite3_execute_statement(db, stmt, affected_rows);
}
static gate_result_t sqlite3stmt_query(gate_data_statement_base_t* self, gate_data_reader_t** ptr_reader)
{
sqlite3_stmt* sql3_statement = (sqlite3_stmt*)self->native_handles[1];
return sqlite3_query_statement((gate_data_statement_t*)self, sql3_statement, ptr_reader);
}
static gate_result_t gate_data_connection_sqlite3_create_statement(
gate_data_connection_base_t* self,
gate_string_t const* sql,
gate_data_statement_t** ptr_statement)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_data_handles_t handles;
gate_string_t cmd = GATE_STRING_INIT_EMPTY;
sqlite3* db = self->native_handles[0];
sqlite3_stmt* sql3_statement = NULL;
int sql3_result;<--- The scope of the variable 'sql3_result' can be reduced. [+]The scope of the variable 'sql3_result' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_data_statement_base_t* ptr_stmt_base = NULL;
do
{
if (gate_string_is_empty(sql))
{
ret = GATE_RESULT_INVALIDARG;
break;
}
if (sql->str[sql->length] == '\0')
{
gate_string_duplicate(&cmd, sql);
}
else
{
if (NULL == gate_string_create(&cmd, gate_string_ptr(sql, 0), gate_string_length(sql)))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
gate_mem_clear(&handles[0], sizeof(handles));
sql3_result = sqlite3_api.prepare(db, cmd.str, (int)cmd.length, &sql3_statement, NULL);
if (sql3_result != SQLITE_OK)
{
ret = GATE_RESULT_FAILED;
GATE_DEBUG_TRACE_MSG_VALUE("sqlite3_prepare() failed", sql3_result);
break;
}
handles[0] = (void*)db;
handles[1] = (void*)sql3_statement;
ret = gate_data_statement_base_create(
self, handles, sql,
&sqlite3stmt_init,
&sqlite3stmt_close,
&sqlite3stmt_set_param,
&sqlite3stmt_set_named_param,
&sqlite3stmt_reset,
&sqlite3stmt_execute,
&sqlite3stmt_query,
&ptr_stmt_base);
if (GATE_FAILED(ret))
{
sqlite3_api.finalize(sql3_statement);
break;
}
/* success case: */
if (ptr_statement)
{
*ptr_statement = (gate_data_statement_t*)ptr_stmt_base;
ptr_stmt_base = NULL;
}
} while (0);
if (ptr_stmt_base != NULL)
{
gate_object_release(ptr_stmt_base);
ptr_stmt_base = NULL;
}
gate_string_release(&cmd);
return ret;
}
static gate_result_t gate_data_connection_sqlite3_init(gate_data_connection_base_t* self)
{
//static gate_string_t const init_commands = GATE_STRING_INIT_STATIC("PRAGMA foreign_keys = ON");
//gate_data_connection_execute(self, &init_commands, NULL);
return GATE_RESULT_OK;
}
static void gate_data_connection_sqlite3_close(gate_data_connection_base_t* self)
{
sqlite3* db = (sqlite3*)self->native_handles[0];
if (db != NULL)
{
sqlite3_api.close(db);
}
gate_mem_clear(self->native_handles, sizeof(self->native_handles));
}
gate_result_t gate_data_adapter_sqlite3_create(gate_string_t const* path, gate_enumint_t flags, gate_data_connection_t** ptr_connection)
{
gate_result_t ret = GATE_RESULT_FAILED;
char file_path[GATE_MAX_FILEPATH_LENGTH];
sqlite3* db = NULL;
int sql3_result;<--- The scope of the variable 'sql3_result' can be reduced. [+]The scope of the variable 'sql3_result' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
int sql3_flags = 0;
gate_data_handles_t handles = GATE_INIT_EMPTY;
do
{
ret = gate_data_sqlite3_api_init();
GATE_BREAK_IF_FAILED(ret);
if (GATE_FLAG_ENABLED(flags, GATE_DATA_SQLITE3_FLAG_READONLY))
{
sql3_flags = SQLITE_OPEN_READONLY;
}
else
{
sql3_flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
}
GATE_FLAG_SET(sql3_flags, SQLITE_OPEN_NOMUTEX, !(GATE_FLAG_ENABLED(flags, GATE_DATA_SQLITE3_FLAG_SERIALIZED)));
GATE_FLAG_SET(sql3_flags, SQLITE_OPEN_FULLMUTEX, GATE_FLAG_ENABLED(flags, GATE_DATA_SQLITE3_FLAG_SERIALIZED));
GATE_FLAG_SET(sql3_flags, SQLITE_OPEN_SHAREDCACHE, GATE_FLAG_ENABLED(flags, GATE_DATA_SQLITE3_FLAG_SHAREDCACHE));
GATE_FLAG_SET(sql3_flags, SQLITE_OPEN_MEMORY, GATE_FLAG_ENABLED(flags, GATE_DATA_SQLITE3_FLAG_INMEMORY));
gate_string_to_buffer(path, file_path, sizeof(file_path));
sql3_result = sqlite3_api.open_v2(file_path, &db, sql3_flags, NULL);
if (sql3_result != SQLITE_OK)
{
GATE_DEBUG_TRACE_MSG_VALUE("sqlite3_open() failed", sql3_result);
ret = GATE_RESULT_FAILED;
break;
}
handles[0] = (void*)db;
ret = gate_data_connection_base_create(handles,
&gate_data_connection_sqlite3_init,
&gate_data_connection_sqlite3_close,
//&gate_data_connection_sqlite3_execute,
//&gate_data_connection_sqlite3_query,
&gate_data_connection_sqlite3_create_statement,
ptr_connection);
GATE_BREAK_IF_FAILED(ret);
db = NULL;
} while (0);
if (db != NULL)
{
sqlite3_api.close(db);
}
return ret;
}
#endif /* GATE_DATA_SQLITE3_LIB_IMPL */
#if defined(GATE_DATA_SQLITE3_NO_IMPL)
gate_result_t gate_data_adapter_sqlite3_create(
gate_string_t const* path, gate_enumint_t flags,
gate_data_connection_t** ptr_connection)
{
(void)path;
(void)flags;
(void)ptr_connection;
return GATE_RESULT_NOTIMPLEMENTED;
}
#endif /* GATE_DATA_SQLITE3_NO_IMPL */
|