forked from 2shady4u/godot-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgdsqlite.cpp
More file actions
1341 lines (1139 loc) · 44.3 KB
/
gdsqlite.cpp
File metadata and controls
1341 lines (1139 loc) · 44.3 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
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gdsqlite.hpp"
using namespace godot;
void SQLite::_bind_methods() {
// Methods.
ClassDB::bind_method(D_METHOD("open_db"), &SQLite::open_db);
ClassDB::bind_method(D_METHOD("close_db"), &SQLite::close_db);
ClassDB::bind_method(D_METHOD("query", "query_string"), &SQLite::query);
ClassDB::bind_method(D_METHOD("query_with_bindings", "query_string", "param_bindings"), &SQLite::query_with_bindings);
ClassDB::bind_method(D_METHOD("query_with_named_bindings", "query_string", "param_bindings"), &SQLite::query_with_named_bindings);
ClassDB::bind_method(D_METHOD("create_table", "table_name", "table_data"), &SQLite::create_table);
ClassDB::bind_method(D_METHOD("drop_table", "table_name"), &SQLite::drop_table);
ClassDB::bind_method(D_METHOD("backup_to", "destination"), &SQLite::backup_to);
ClassDB::bind_method(D_METHOD("restore_from", "source"), &SQLite::restore_from);
ClassDB::bind_method(D_METHOD("insert_row", "table_name", "row_data"), &SQLite::insert_row);
ClassDB::bind_method(D_METHOD("insert_rows", "table_name", "row_array"), &SQLite::insert_rows);
ClassDB::bind_method(D_METHOD("select_rows", "table_name", "conditions", "columns"), &SQLite::select_rows);
ClassDB::bind_method(D_METHOD("update_rows", "table_name", "conditions", "row_data"), &SQLite::update_rows);
ClassDB::bind_method(D_METHOD("delete_rows", "table_name", "conditions"), &SQLite::delete_rows);
ClassDB::bind_method(D_METHOD("create_function", "function_name", "callable", "arguments"), &SQLite::create_function);
ClassDB::bind_method(D_METHOD("import_from_json", "import_path"), &SQLite::import_from_json);
ClassDB::bind_method(D_METHOD("export_to_json", "export_path"), &SQLite::export_to_json);
ClassDB::bind_method(D_METHOD("import_from_buffer", "json_buffer"), &SQLite::import_from_buffer);
ClassDB::bind_method(D_METHOD("export_to_buffer"), &SQLite::export_to_buffer);
ClassDB::bind_method(D_METHOD("get_autocommit"), &SQLite::get_autocommit);
ClassDB::bind_method(D_METHOD("compileoption_used", "option_name"), &SQLite::compileoption_used);
ClassDB::bind_method(D_METHOD("enable_load_extension", "onoff"), &SQLite::enable_load_extension);
ClassDB::bind_method(D_METHOD("load_extension", "extension_path", "entrypoint"), &SQLite::load_extension, DEFVAL("sqlite3_extension_init"));
// Properties.
ClassDB::bind_method(D_METHOD("set_last_insert_rowid", "last_insert_rowid"), &SQLite::set_last_insert_rowid);
ClassDB::bind_method(D_METHOD("get_last_insert_rowid"), &SQLite::get_last_insert_rowid);
ADD_PROPERTY(PropertyInfo(Variant::INT, "last_insert_rowid"), "set_last_insert_rowid", "get_last_insert_rowid");
ClassDB::bind_method(D_METHOD("set_verbosity_level", "verbosity_level"), &SQLite::set_verbosity_level);
ClassDB::bind_method(D_METHOD("get_verbosity_level"), &SQLite::get_verbosity_level);
ADD_PROPERTY(PropertyInfo(Variant::INT, "verbosity_level"), "set_verbosity_level", "get_verbosity_level");
ClassDB::bind_method(D_METHOD("set_foreign_keys", "foreign_keys"), &SQLite::set_foreign_keys);
ClassDB::bind_method(D_METHOD("get_foreign_keys"), &SQLite::get_foreign_keys);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "foreign_keys"), "set_foreign_keys", "get_foreign_keys");
ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &SQLite::set_read_only);
ClassDB::bind_method(D_METHOD("get_read_only"), &SQLite::get_read_only);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "get_read_only");
ClassDB::bind_method(D_METHOD("set_path", "path"), &SQLite::set_path);
ClassDB::bind_method(D_METHOD("get_path"), &SQLite::get_path);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "path"), "set_path", "get_path");
ClassDB::bind_method(D_METHOD("set_error_message", "error_message"), &SQLite::set_error_message);
ClassDB::bind_method(D_METHOD("get_error_message"), &SQLite::get_error_message);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "error_message"), "set_error_message", "get_error_message");
ClassDB::bind_method(D_METHOD("set_default_extension", "default_extension"), &SQLite::set_default_extension);
ClassDB::bind_method(D_METHOD("get_default_extension"), &SQLite::get_default_extension);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "default_extension"), "set_default_extension", "get_default_extension");
ClassDB::bind_method(D_METHOD("set_query_result", "query_result"), &SQLite::set_query_result);
ClassDB::bind_method(D_METHOD("get_query_result"), &SQLite::get_query_result);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "query_result", PROPERTY_HINT_ARRAY_TYPE, "Dictionary"), "set_query_result", "get_query_result");
ClassDB::bind_method(D_METHOD("get_query_result_by_reference"), &SQLite::get_query_result_by_reference);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "query_result_by_reference", PROPERTY_HINT_ARRAY_TYPE, "Dictionary"), "set_query_result", "get_query_result_by_reference");
// Constants.
BIND_ENUM_CONSTANT(QUIET);
BIND_ENUM_CONSTANT(NORMAL);
BIND_ENUM_CONSTANT(VERBOSE);
BIND_ENUM_CONSTANT(VERY_VERBOSE);
BIND_CONSTANT(SQLITE_OK); /* Successful result */
/* beginning-of-error-codes */
BIND_CONSTANT(SQLITE_ERROR);
BIND_CONSTANT(SQLITE_INTERNAL);
BIND_CONSTANT(SQLITE_PERM);
BIND_CONSTANT(SQLITE_ABORT);
BIND_CONSTANT(SQLITE_BUSY);
BIND_CONSTANT(SQLITE_LOCKED);
BIND_CONSTANT(SQLITE_NOMEM);
BIND_CONSTANT(SQLITE_READONLY);
BIND_CONSTANT(SQLITE_INTERRUPT);
BIND_CONSTANT(SQLITE_IOERR);
BIND_CONSTANT(SQLITE_CORRUPT);
BIND_CONSTANT(SQLITE_NOTFOUND);
BIND_CONSTANT(SQLITE_FULL);
BIND_CONSTANT(SQLITE_CANTOPEN);
BIND_CONSTANT(SQLITE_PROTOCOL);
BIND_CONSTANT(SQLITE_EMPTY);
BIND_CONSTANT(SQLITE_SCHEMA);
BIND_CONSTANT(SQLITE_TOOBIG);
BIND_CONSTANT(SQLITE_CONSTRAINT);
BIND_CONSTANT(SQLITE_MISMATCH);
BIND_CONSTANT(SQLITE_MISUSE);
BIND_CONSTANT(SQLITE_NOLFS);
BIND_CONSTANT(SQLITE_AUTH);
BIND_CONSTANT(SQLITE_FORMAT);
BIND_CONSTANT(SQLITE_RANGE);
BIND_CONSTANT(SQLITE_NOTADB);
BIND_CONSTANT(SQLITE_NOTICE);
BIND_CONSTANT(SQLITE_WARNING);
BIND_CONSTANT(SQLITE_ROW);
BIND_CONSTANT(SQLITE_DONE);
/* end-of-error-codes */
}
SQLite::SQLite() {
db = nullptr;
query_result = TypedArray<Dictionary>();
}
SQLite::~SQLite() {
/* Clean up the function_registry */
function_registry.clear();
function_registry.shrink_to_fit();
/* Close the database connection if it is still open */
if (db) {
close_db();
}
}
bool SQLite::open_db() {
if (db) {
ERR_PRINT("GDSQLite Error: Can't open database if connection is already open!");
return false;
}
if (path.find(":memory:") == -1) {
/* Add the default_extension to the database path if no extension is present */
/* Skip if the default_extension is an empty string to allow for paths without extension */
if (path.get_extension().is_empty() && !default_extension.is_empty()) {
String ending = String(".") + default_extension;
path += ending;
}
}
path = normalize_path(path, read_only);
int rc;
const CharString utf8_path = path.utf8();
const char *char_path = utf8_path.get_data();
/* Try to open the database */
if (read_only) {
if (path.find(":memory:") == -1) {
sqlite3_vfs_register(gdsqlite_vfs(), 0);
rc = sqlite3_open_v2(char_path, &db, SQLITE_OPEN_READONLY, "godot");
} else {
ERR_PRINT("GDSQLite Error: Opening in-memory databases in read-only mode is currently not supported!");
return false;
}
} else {
/* The `SQLITE_OPEN_URI`-flag is solely required for in-memory databases with shared cache, but it is safe to use in most general cases */
/* As discussed here: https://www.sqlite.org/uri.html */
rc = sqlite3_open_v2(char_path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, NULL);
/* Identical to: `rc = sqlite3_open(char_path, &db);`*/
}
if (rc != SQLITE_OK) {
ERR_PRINT("GDSQLite Error: Can't open database: " + String::utf8(sqlite3_errmsg(db)));
return false;
} else if (verbosity_level > VerbosityLevel::QUIET) {
UtilityFunctions::print("Opened database successfully (" + path + ")");
}
/* Try to enable foreign keys. */
if (foreign_keys) {
char *zErrMsg = nullptr;
rc = sqlite3_exec(db, "PRAGMA foreign_keys=on;", NULL, NULL, &zErrMsg);
if (rc != SQLITE_OK) {
ERR_PRINT("GDSQLite Error: Can't enable foreign keys: " + String::utf8(zErrMsg));
sqlite3_free(zErrMsg);
return false;
}
}
return true;
}
bool SQLite::close_db() {
if (db) {
// Cannot close database!
if (sqlite3_close_v2(db) != SQLITE_OK) {
ERR_PRINT("GDSQLite Error: Can't close database!");
return false;
} else {
db = nullptr;
if (verbosity_level > VerbosityLevel::QUIET) {
UtilityFunctions::print("Closed database (" + path + ")");
}
return true;
}
}
ERR_PRINT("GDSQLite Error: Can't close database if connection is not open!");
return false;
}
String SQLite::normalize_path(const String p_path, const bool p_read_only) const {
if (p_read_only) {
return p_path;
}
if (p_path.contains(":memory:")) {
return p_path;
}
/* Find the real path */
return ProjectSettings::get_singleton()->globalize_path(p_path.strip_edges());
}
bool SQLite::query(const String &p_query) {
return query_with_bindings(p_query, Array());
}
bool SQLite::prepare_statement(const String &p_query, sqlite3_stmt **out_stmt, const char **pzTail, CharString &out_dummy_query) {
if (verbosity_level > VerbosityLevel::NORMAL) {
UtilityFunctions::print(p_query);
}
out_dummy_query = p_query.utf8(); // store in caller-provided CharString
const char *sql = out_dummy_query.get_data();
query_result.clear();
int rc = sqlite3_prepare_v2(db, sql, -1, out_stmt, pzTail);
const char *zErrMsg = sqlite3_errmsg(db);
error_message = String::utf8(zErrMsg);
if (rc != SQLITE_OK) {
ERR_PRINT(" --> SQL error: " + error_message);
sqlite3_finalize(*out_stmt);
return false;
}
return true;
}
bool SQLite::bind_parameter(Variant binding_value, sqlite3_stmt *stmt, int i) {
switch (binding_value.get_type()) {
case Variant::NIL:
sqlite3_bind_null(stmt, i + 1);
break;
case Variant::BOOL:
case Variant::INT:
sqlite3_bind_int64(stmt, i + 1, int64_t(binding_value));
break;
case Variant::FLOAT:
sqlite3_bind_double(stmt, i + 1, binding_value);
break;
case Variant::STRING:
{
const CharString dummy_binding = (binding_value.operator String()).utf8();
const char *binding = dummy_binding.get_data();
sqlite3_bind_text(stmt, i + 1, binding, -1, SQLITE_TRANSIENT);
}
break;
case Variant::PACKED_BYTE_ARRAY: {
PackedByteArray binding = ((const PackedByteArray &)binding_value);
/* Calling .ptr() on an empty PackedByteArray returns an error */
if (binding.size() == 0) {
sqlite3_bind_null(stmt, i + 1);
/* Identical to: `sqlite3_bind_blob64(stmt, i + 1, nullptr, 0, SQLITE_TRANSIENT);`*/
} else {
sqlite3_bind_blob64(stmt, i + 1, binding.ptr(), binding.size(), SQLITE_TRANSIENT);
}
break;
}
default:
ERR_PRINT("GDSQLite Error: Binding a parameter of type " + String(std::to_string(binding_value.get_type()).c_str()) + " (TYPE_*) is not supported!");
return false;
}
return true;
}
bool SQLite::execute_statement(sqlite3_stmt *stmt) {
if (verbosity_level > VerbosityLevel::NORMAL) {
char *expanded_sql = sqlite3_expanded_sql(stmt);
UtilityFunctions::print(String::utf8(expanded_sql));
sqlite3_free(expanded_sql);
}
// Execute the statement and iterate over all the resulting rows.
while (sqlite3_step(stmt) == SQLITE_ROW) {
Dictionary column_dict;
int argc = sqlite3_column_count(stmt);
/* Loop over all columns and add them to the Dictionary */
for (int i = 0; i < argc; i++) {
Variant column_value;
/* Check the column type and do correct casting */
switch (sqlite3_column_type(stmt, i)) {
case SQLITE_INTEGER:
column_value = Variant((int64_t)sqlite3_column_int64(stmt, i));
break;
case SQLITE_FLOAT:
column_value = Variant(sqlite3_column_double(stmt, i));
break;
case SQLITE_TEXT:
column_value = Variant(String::utf8((char *)sqlite3_column_text(stmt, i)));
break;
case SQLITE_BLOB: {
int bytes = sqlite3_column_bytes(stmt, i);
PackedByteArray arr = PackedByteArray();
arr.resize(bytes);
memcpy((void *)arr.ptrw(), (char *)sqlite3_column_blob(stmt, i), bytes);
column_value = arr;
break;
}
case SQLITE_NULL:
break;
default:
break;
}
const char *azColName = sqlite3_column_name(stmt, i);
column_dict[String::utf8(azColName)] = column_value;
}
/* Add result to query_result Array */
query_result.append(column_dict);
}
/* Clean up and delete the resources used by the prepared statement */
sqlite3_finalize(stmt);
int rc = sqlite3_errcode(db);
const char *zErrMsg = sqlite3_errmsg(db);
error_message = String::utf8(zErrMsg);
if (rc != SQLITE_OK) {
ERR_PRINT(" --> SQL error: " + error_message);
return false;
} else if (verbosity_level > VerbosityLevel::NORMAL) {
UtilityFunctions::print(" --> Query succeeded");
}
return true;
}
bool SQLite::query_with_bindings(const String &p_query, Array param_bindings) {
const char *sql, *pzTail;
sqlite3_stmt *stmt;
CharString dummy_query;
if (!prepare_statement(p_query, &stmt, &pzTail, dummy_query)) {
return false;
}
/* Check if the param_bindings size exceeds the required parameter count */
int parameter_count = sqlite3_bind_parameter_count(stmt);
if (param_bindings.size() < parameter_count) {
ERR_PRINT("GDSQLite Error: Insufficient number of parameters to satisfy required number of bindings in statement!");
sqlite3_finalize(stmt);
return false;
}
/* Bind any given parameters to the prepared statement */
for (int i = 0; i < parameter_count; i++) {
Variant binding_value = param_bindings.get(i);
if (!bind_parameter(binding_value, stmt, i)) {
sqlite3_finalize(stmt);
return false;
}
}
param_bindings = param_bindings.slice(parameter_count, param_bindings.size());
if (!execute_statement(stmt)) {
return false;
}
/* Figure out if there's a subsequent statement which needs execution */
String sTail = String::utf8(pzTail).strip_edges();
if (!sTail.is_empty()) {
return query_with_bindings(sTail, param_bindings);
}
if (!param_bindings.is_empty()) {
WARN_PRINT("GDSQLite Warning: Provided number of bindings exceeded the required number in statement! (" + String(std::to_string(param_bindings.size()).c_str()) + " unused parameter(s))");
}
return true;
}
bool SQLite::query_with_named_bindings(const String &p_query, Dictionary param_bindings) {
const char *sql, *pzTail;
sqlite3_stmt *stmt;
CharString dummy_query;
if (!prepare_statement(p_query, &stmt, &pzTail, dummy_query)) {
return false;
}
int parameter_count = sqlite3_bind_parameter_count(stmt);
/* Bind any given parameters to the prepared statement */
for (int i = 0; i < parameter_count; i++) {
const char *param_name = sqlite3_bind_parameter_name(stmt, i + 1);
if (nullptr == param_name) {
ERR_PRINT(vformat(
"GDSQLite Error: Parameter index %d is most likely nameless and can't assign named parameter!",
i + 1
));
sqlite3_finalize(stmt);
return false;
}
/* Sqlite will return the parameter name prefixed for example ?, :, $, @ but we want user to just pass in the name itself */
const char *non_prefixed_name = param_name + 1;
/* This has side effect of rechecking the dictionary for same name if its used more than once */
if (!param_bindings.has(non_prefixed_name)) {
ERR_PRINT(vformat(
"GDSQLite Error: Insufficient parameter names to satisfy bindings in statement! Missing parameter: %s",
String::utf8(param_name)
));
sqlite3_finalize(stmt);
return false;
}
Variant binding_value = param_bindings[String::utf8(non_prefixed_name)];
if (!bind_parameter(binding_value, stmt, i)) {
sqlite3_finalize(stmt);
return false;
}
}
if (!execute_statement(stmt)) {
return false;
}
/* Figure out if there's a subsequent statement which needs execution */
String sTail = String::utf8(pzTail).strip_edges();
if (!sTail.is_empty()) {
return query_with_named_bindings(sTail, param_bindings);
}
return true;
}
bool SQLite::create_table(const String &p_name, const Dictionary &p_table_dict) {
if (!validate_table_dict(p_table_dict)) {
return false;
}
String query_string, type_string, key_string, primary_string;
String integer_datatype = "int";
/* Create SQL statement */
query_string = "CREATE TABLE IF NOT EXISTS " + p_name + " (";
key_string = "";
primary_string = "";
Dictionary column_dict;
Array columns = p_table_dict.keys();
int64_t number_of_columns = columns.size();
int primary_key_columns = 0;
for (int64_t i = 0; i <= number_of_columns - 1; i++) {
column_dict = p_table_dict[columns[i]];
if (column_dict.get("primary_key", false)) {
primary_key_columns++;
}
}
for (int64_t i = 0; i <= number_of_columns - 1; i++) {
column_dict = p_table_dict[columns[i]];
query_string += (const String &)columns[i] + String(" ");
type_string = (const String &)column_dict["data_type"];
if (type_string.to_lower().begins_with(integer_datatype)) {
query_string += String("INTEGER");
} else {
query_string += type_string;
}
/* Primary key check */
if (column_dict.get("primary_key", false)) {
if (primary_key_columns == 1) {
query_string += String(" PRIMARY KEY");
/* Autoincrement check */
if (column_dict.get("auto_increment", false)) {
query_string += String(" AUTOINCREMENT");
}
} else {
if (primary_string.is_empty()) {
primary_string = (const String &)columns[i];
} else {
primary_string += String(", ") + (const String &)columns[i];
}
}
}
/* Not null check */
if (column_dict.get("not_null", false)) {
query_string += String(" NOT NULL");
}
/* Unique check */
if (column_dict.get("unique", false)) {
query_string += String(" UNIQUE");
}
/* Default check */
if (column_dict.has("default")) {
query_string += String(" DEFAULT ") + (const String &)column_dict["default"];
}
/* Apply foreign key constraint. */
if (foreign_keys) {
if (column_dict.get("foreign_key", false)) {
const String foreign_key_definition = (const String &)(column_dict["foreign_key"]);
const Array foreign_key_elements = foreign_key_definition.split(".");
if (foreign_key_elements.size() == 2) {
const String column_name = (const String &)(columns[i]);
const String foreign_key_table_name = (const String &)(foreign_key_elements[0]);
const String foreign_key_column_name = (const String &)(foreign_key_elements[1]);
key_string += String(", FOREIGN KEY (" + column_name + ") REFERENCES " + foreign_key_table_name + "(" + foreign_key_column_name + ")");
if (column_dict.has("on_delete")) {
const String foreign_key_on_delete_action = (const String &)(column_dict["on_delete"]);
key_string += String(" ON DELETE " + foreign_key_on_delete_action);
}
if (column_dict.has("on_update")) {
const String foreign_key_on_update_action = (const String &)(column_dict["on_update"]);
key_string += String(" ON UPDATE " + foreign_key_on_update_action);
}
}
}
}
if (i != number_of_columns - 1) {
query_string += ",";
}
}
if (primary_key_columns > 1) {
query_string += String(", PRIMARY KEY (") + primary_string + String(")");
}
query_string += key_string + ");";
return query(query_string);
}
bool SQLite::validate_table_dict(const Dictionary &p_table_dict) {
Dictionary column_dict;
Array columns = p_table_dict.keys();
int64_t number_of_columns = columns.size();
for (int64_t i = 0; i <= number_of_columns - 1; i++) {
if (p_table_dict[columns[i]].get_type() != Variant::DICTIONARY) {
ERR_PRINT("GDSQLite Error: All values of the table dictionary should be of type Dictionary");
return false;
}
column_dict = p_table_dict[columns[i]];
if (!column_dict.has("data_type")) {
ERR_PRINT("GDSQLite Error: The field \"data_type\" is a required part of the table dictionary");
return false;
}
if (column_dict["data_type"].get_type() != Variant::STRING) {
ERR_PRINT("GDSQLite Error: The field \"data_type\" should be of type String");
return false;
}
if (column_dict.has("default")) {
Variant::Type default_type = column_dict["default"].get_type();
CharString dummy_data_type = ((const String &)column_dict["data_type"]).utf8();
const char *char_data_type = dummy_data_type.get_data();
/* Get the type of the "datatype"-field and compare with the type of the "default"-value */
/* Some types are not checked and might be added in a future version */
Variant::Type data_type_type = default_type;
if (strcmp(char_data_type, "int") == 0) {
data_type_type = Variant::Type::INT;
} else if (strcmp(char_data_type, "text") == 0) {
data_type_type = Variant::Type::STRING;
} else if (strcmp(char_data_type, "real") == 0) {
data_type_type = Variant::Type::FLOAT;
}
if (data_type_type != default_type) {
ERR_PRINT("GDSQLite Error: The type of the field \"default\" ( " + String(std::to_string(default_type).c_str()) + " ) should be the same type as the \"datatype\"-field ( " + String(std::to_string(data_type_type).c_str()) + " )");
return false;
}
}
}
return true;
}
bool SQLite::drop_table(const String &p_name) {
String query_string;
/* Create SQL statement */
query_string = "DROP TABLE " + p_name + ";";
return query(query_string);
}
bool SQLite::backup_to(String destination_path) {
destination_path = ProjectSettings::get_singleton()->globalize_path(destination_path.strip_edges());
CharString dummy_path = destination_path.utf8();
const char *char_path = dummy_path.get_data();
sqlite3 *destination_db;
int result = sqlite3_open_v2(char_path, &destination_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, NULL);
if (result == SQLITE_OK) {
result = backup_database(db, destination_db);
}
(void)sqlite3_close_v2(destination_db);
return result == SQLITE_OK;
}
bool SQLite::restore_from(String source_path) {
source_path = ProjectSettings::get_singleton()->globalize_path(source_path.strip_edges());
CharString dummy_path = source_path.utf8();
const char *char_path = dummy_path.get_data();
sqlite3 *source_db;
int result = sqlite3_open_v2(char_path, &source_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, NULL);
if (result == SQLITE_OK) {
result = backup_database(source_db, db);
}
(void)sqlite3_close_v2(source_db);
return result == SQLITE_OK;
}
int SQLite::backup_database(sqlite3 *source_db, sqlite3 *destination_db) {
int rc;
sqlite3_backup *backup = sqlite3_backup_init(destination_db, "main", source_db, "main");
if (backup) {
(void)sqlite3_backup_step(backup, -1);
(void)sqlite3_backup_finish(backup);
}
rc = sqlite3_errcode(destination_db);
return rc;
}
bool SQLite::insert_row(const String &p_name, const Dictionary &p_row_dict) {
String query_string, key_string, value_string = "";
Array keys = p_row_dict.keys();
Array param_bindings = p_row_dict.values();
/* Create SQL statement */
query_string = "INSERT INTO " + p_name;
int64_t number_of_keys = p_row_dict.size();
for (int64_t i = 0; i <= number_of_keys - 1; i++) {
key_string += (const String &)keys[i];
value_string += "?";
if (i != number_of_keys - 1) {
key_string += ",";
value_string += ",";
}
}
query_string += " (" + key_string + ") VALUES (" + value_string + ");";
return query_with_bindings(query_string, param_bindings);
}
bool SQLite::insert_rows(const String &p_name, const Array &p_row_array) {
query("BEGIN TRANSACTION;");
int64_t number_of_rows = p_row_array.size();
for (int64_t i = 0; i <= number_of_rows - 1; i++) {
if (p_row_array[i].get_type() != Variant::DICTIONARY) {
ERR_PRINT("GDSQLite Error: All elements of the Array should be of type Dictionary");
/* Don't forget to close the transaction! */
/* Maybe we should do a rollback instead? */
query("END TRANSACTION;");
return false;
}
if (!insert_row(p_name, p_row_array[i])) {
/* Don't forget to close the transaction! */
/* Stop the error_message from being overwritten! */
String previous_error_message = error_message;
query("END TRANSACTION;");
error_message = previous_error_message;
return false;
}
}
query("END TRANSACTION;");
return true;
}
Array SQLite::select_rows(const String &p_name, const String &p_conditions, const Array &p_columns_array) {
String query_string;
/* Create SQL statement */
query_string = "SELECT ";
int64_t number_of_columns = p_columns_array.size();
for (int64_t i = 0; i <= number_of_columns - 1; i++) {
if (p_columns_array[i].get_type() != Variant::STRING) {
ERR_PRINT("GDSQLite Error: All elements of the Array should be of type String");
return query_result;
}
query_string += (const String &)p_columns_array[i];
if (i != number_of_columns - 1) {
query_string += ", ";
}
}
query_string += " FROM " + p_name;
if (!p_conditions.is_empty()) {
query_string += " WHERE " + p_conditions;
}
query_string += ";";
query(query_string);
/* Return the duplicated result */
return get_query_result();
}
bool SQLite::update_rows(const String &p_name, const String &p_conditions, const Dictionary &p_updated_row_dict) {
String query_string;
Array param_bindings;
bool success;
int64_t number_of_keys = p_updated_row_dict.size();
Array keys = p_updated_row_dict.keys();
Array values = p_updated_row_dict.values();
query("BEGIN TRANSACTION;");
/* Create SQL statement */
query_string += "UPDATE " + p_name + " SET ";
for (int64_t i = 0; i <= number_of_keys - 1; i++) {
query_string += (const String &)keys[i] + String("=?");
param_bindings.append(values[i]);
if (i != number_of_keys - 1) {
query_string += ", ";
}
}
query_string += " WHERE " + p_conditions + ";";
success = query_with_bindings(query_string, param_bindings);
/* Stop the error_message from being overwritten! */
String previous_error_message = error_message;
query("END TRANSACTION;");
error_message = previous_error_message;
return success;
}
bool SQLite::delete_rows(const String &p_name, const String &p_conditions) {
String query_string;
bool success;
query("BEGIN TRANSACTION;");
/* Create SQL statement */
query_string = "DELETE FROM " + p_name;
/* If it's empty or * everything is to be deleted */
if (!p_conditions.is_empty() && (p_conditions != (const String &)"*")) {
query_string += " WHERE " + p_conditions;
}
query_string += ";";
success = query(query_string);
/* Stop the error_message from being overwritten! */
String previous_error_message = error_message;
query("END TRANSACTION;");
error_message = previous_error_message;
return success;
}
static void function_callback(sqlite3_context *context, int argc, sqlite3_value **argv) {
void *temp = sqlite3_user_data(context);
Callable callable = *(Callable *)temp;
/* Check if the callable is valid */
if (!callable.is_valid()) {
ERR_PRINT("GDSQLite Error: Supplied function reference is invalid! Aborting callback...");
return;
}
Array argument_array = Array();
Variant argument_value;
for (int i = 0; i <= argc - 1; i++) {
sqlite3_value *value = *argv;
/* Check the value type and do correct casting */
switch (sqlite3_value_type(value)) {
case SQLITE_INTEGER:
argument_value = Variant((int64_t)sqlite3_value_int64(value));
break;
case SQLITE_FLOAT:
argument_value = Variant(sqlite3_value_double(value));
break;
case SQLITE_TEXT:
argument_value = Variant((char *)sqlite3_value_text(value));
break;
case SQLITE_BLOB: {
int bytes = sqlite3_value_bytes(value);
PackedByteArray arr = PackedByteArray();
arr.resize(bytes);
memcpy((void *)arr.ptrw(), (char *)sqlite3_value_blob(value), bytes);
argument_value = arr;
break;
}
case SQLITE_NULL:
break;
default:
break;
}
argument_array.append(argument_value);
argv += 1;
}
Variant output = callable.callv(argument_array);
switch (output.get_type()) {
case Variant::NIL:
sqlite3_result_null(context);
break;
case Variant::BOOL:
case Variant::INT:
sqlite3_result_int64(context, int64_t(output));
break;
case Variant::FLOAT:
sqlite3_result_double(context, output);
break;
case Variant::STRING:
{
const CharString dummy_binding = (output.operator String()).utf8();
const char *binding = dummy_binding.get_data();
sqlite3_result_text(context, binding, -1, SQLITE_STATIC);
}
break;
case Variant::PACKED_BYTE_ARRAY: {
PackedByteArray arr = ((const PackedByteArray &)output);
sqlite3_result_blob(context, arr.ptr(), arr.size(), SQLITE_TRANSIENT);
break;
}
default:
break;
}
}
bool SQLite::create_function(const String &p_name, const Callable &p_callable, int p_argc) {
/* The exact memory position of the std::vector's elements changes during memory reallocation (= when adding additional elements) */
/* Luckily, the pointer to the managed object (of the std::unique_ptr) won't change during execution! (= consistent) */
/* The std::unique_ptr is stored in a std::vector and is thus allocated on the heap */
function_registry.push_back(std::make_unique<Callable>(p_callable));
int rc;
CharString dummy_name = p_name.utf8();
const char *zFunctionName = dummy_name.get_data();
int nArg = p_argc;
int eTextRep = SQLITE_UTF8;
/* Get a void pointer to the managed object of the smart pointer that is stored at the back of the vector */
void *pApp = (void *)function_registry.back().get();
void (*xFunc)(sqlite3_context *, int, sqlite3_value **) = function_callback;
void (*xStep)(sqlite3_context *, int, sqlite3_value **) = NULL;
void (*xFinal)(sqlite3_context *) = NULL;
/* Create the actual function */
rc = sqlite3_create_function(db, zFunctionName, nArg, eTextRep, pApp, xFunc, xStep, xFinal);
if (rc) {
ERR_PRINT("GDSQLite Error: " + String(sqlite3_errmsg(db)));
return false;
} else if (verbosity_level > VerbosityLevel::NORMAL) {
UtilityFunctions::print("Succesfully added function \"" + p_name + "\" to function registry");
}
return true;
}
bool SQLite::import_from_json(String import_path) {
/* Add .json to the import_path String if not present */
String ending = String(".json");
if (!import_path.ends_with(ending)) {
import_path += ending;
}
/* Find the real path */
import_path = ProjectSettings::get_singleton()->globalize_path(import_path.strip_edges());
CharString dummy_path = import_path.utf8();
const char *char_path = dummy_path.get_data();
/* Open the json-file and stream its content into a stringstream */
std::ifstream ifs(char_path);
if (ifs.fail()) {
ERR_PRINT("GDSQLite Error: " + String(std::strerror(errno)) + " (" + import_path + ")");
return false;
}
std::stringstream buffer;
buffer << ifs.rdbuf();
std::string str = buffer.str();
ifs.close();
String json_string = String::utf8(str.c_str());
PackedByteArray json_buffer = json_string.to_utf8_buffer();
return import_from_buffer(json_buffer);
}
bool SQLite::export_to_json(String export_path) {
PackedByteArray json_buffer = export_to_buffer();
String json_string = json_buffer.get_string_from_utf8();
/* Add .json to the import_path String if not present */
String ending = String(".json");
if (!export_path.ends_with(ending)) {
export_path += ending;
}
/* Find the real path */
export_path = ProjectSettings::get_singleton()->globalize_path(export_path.strip_edges());
CharString dummy_path = export_path.utf8();
const char *char_path = dummy_path.get_data();
std::ofstream ofs(char_path, std::ios::trunc);
if (ofs.fail()) {
ERR_PRINT("GDSQLite Error: " + String(std::strerror(errno)) + " (" + export_path + ")");
return false;
}
CharString dummy_string = json_string.utf8();
ofs << dummy_string.get_data();
ofs.close();
return true;
}
bool SQLite::import_from_buffer(PackedByteArray json_buffer) {
/* Attempt to parse the input json_string and, if unsuccessful, throw a parse error specifying the erroneous line */
Ref<JSON> json;
json.instantiate();
String json_string = json_buffer.get_string_from_utf8();
Error error = json->parse(json_string);
if (error != Error::OK) {
/* Throw a parsing error */
ERR_PRINT("GDSQLite Error: parsing failed! reason: " + json->get_error_message() + ", at line: " + String::num_int64(json->get_error_line()));
return false;
}
Array database_array = json->get_data();
std::vector<object_struct> objects_to_import;
/* Validate the json structure and populate the tables_to_import vector */
if (!validate_json(database_array, objects_to_import)) {
return false;
}
/* Check if the database is open and, if not, attempt to open it */
if (db == nullptr) {
/* Open the database using the open_db method */
if (!open_db()) {
return false;
}
}
/* Find all views that are present in this database */
query(String("SELECT name FROM sqlite_master WHERE type = 'view';"));
TypedArray<Dictionary> old_view_array = query_result.duplicate(true);
int64_t old_number_of_views = old_view_array.size();
/* Drop all old views present in the database */
for (int64_t i = 0; i <= old_number_of_views - 1; i++) {
Dictionary view_dict = old_view_array[i];
String view_name = view_dict["name"];
String query_string = "DROP VIEW " + view_name + ";";
query(query_string);
}
/* Find all tables that are present in this database */
/* We don't care about indexes or triggers here since they get dropped automatically when their table is dropped */
query(String("SELECT name,type FROM sqlite_master WHERE type = 'table';"));
TypedArray<Dictionary> old_table_array = query_result.duplicate(true);
#ifdef SQLITE_ENABLE_FTS5
/* FTS5 creates a bunch of shadow tables that cannot be dropped manually! */
/* The virtual table is responsible for dropping these tables itself */
remove_shadow_tables(old_table_array);
#endif
int64_t old_number_of_tables = old_table_array.size();
/* Drop all old tables present in the database */
for (int64_t i = 0; i <= old_number_of_tables - 1; i++) {
Dictionary table_dict = old_table_array[i];
String table_name = table_dict["name"];
drop_table(table_name);
}
query("BEGIN TRANSACTION;");
/* foreign_keys cannot be enforced until after all rows have been added! */
query("PRAGMA defer_foreign_keys=on;");
/* Add all new tables and fill them up with all the rows */
for (auto table : objects_to_import) {
if (!query(table.sql)) {