Skip to content

Commit 792189a

Browse files
committed
Add better schema validation for create_table
1 parent 2e60ba7 commit 792189a

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

demo/icon.png.import

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.cte
1616
[params]
1717

1818
compress/mode=0
19+
compress/high_quality=false
1920
compress/lossy_quality=0.7
2021
compress/hdr_compression=1
21-
compress/bptc_ldr=0
2222
compress/normal_map=0
2323
compress/channel_pack=0
2424
mipmaps/generate=false

src/gdsqlite.cpp

+34-5
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ bool SQLite::query_with_bindings(const String &p_query, Array param_bindings)
364364

365365
bool SQLite::create_table(const String &p_name, const Dictionary &p_table_dict)
366366
{
367+
if (!validate_table_dict(p_table_dict)) {
368+
return false;
369+
}
370+
367371
String query_string, type_string, key_string;
368372
String integer_datatype = "int";
369373
/* Create SQL statement */
@@ -376,11 +380,6 @@ bool SQLite::create_table(const String &p_name, const Dictionary &p_table_dict)
376380
for (int64_t i = 0; i <= number_of_columns - 1; i++)
377381
{
378382
column_dict = p_table_dict[columns[i]];
379-
if (!column_dict.has("data_type"))
380-
{
381-
UtilityFunctions::printerr("GDSQLite Error: The field \"data_type\" is a required part of the table dictionary");
382-
return false;
383-
}
384383
query_string += (const String &)columns[i] + String(" ");
385384
type_string = (const String &)column_dict["data_type"];
386385
if (type_string.to_lower().begins_with(integer_datatype))
@@ -445,6 +444,36 @@ bool SQLite::create_table(const String &p_name, const Dictionary &p_table_dict)
445444
return query(query_string);
446445
}
447446

447+
bool SQLite::validate_table_dict(const Dictionary &p_table_dict) {
448+
Dictionary column_dict;
449+
Array columns = p_table_dict.keys();
450+
int64_t number_of_columns = columns.size();
451+
for (int64_t i = 0; i <= number_of_columns - 1; i++)
452+
{
453+
if (p_table_dict[columns[i]].get_type() != Variant::DICTIONARY) {
454+
UtilityFunctions::printerr("GDSQLite Error: All values of the table dictionary should be of type Dictionary");
455+
return false;
456+
}
457+
458+
column_dict = p_table_dict[columns[i]];
459+
if (!column_dict.has("data_type"))
460+
{
461+
UtilityFunctions::printerr("GDSQLite Error: The field \"data_type\" is a required part of the table dictionary");
462+
return false;
463+
}
464+
465+
if (column_dict.has("default"))
466+
{
467+
if (column_dict["default"].get_type() != Variant::STRING) {
468+
UtilityFunctions::printerr("GDSQLite Error: The field \"default\" should be of type String");
469+
return false;
470+
}
471+
}
472+
}
473+
474+
return true;
475+
}
476+
448477
bool SQLite::drop_table(const String &p_name)
449478
{
450479
String query_string;

src/gdsqlite.h

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace godot
3939

4040
private:
4141
bool validate_json(const Array &import_json, std::vector<object_struct> &tables_to_import);
42+
bool validate_table_dict(const Dictionary &p_table_dict);
4243

4344
sqlite3 *db;
4445
std::vector<std::unique_ptr<Callable>> function_registry;

0 commit comments

Comments
 (0)