Skip to content

Commit

Permalink
Add better schema validation for create_table
Browse files Browse the repository at this point in the history
  • Loading branch information
2shady4u committed Jun 18, 2023
1 parent 2e60ba7 commit 792189a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion demo/icon.png.import
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.cte
[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/bptc_ldr=0
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
Expand Down
39 changes: 34 additions & 5 deletions src/gdsqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ bool SQLite::query_with_bindings(const String &p_query, Array param_bindings)

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;
String integer_datatype = "int";
/* Create SQL statement */
Expand All @@ -376,11 +380,6 @@ bool SQLite::create_table(const String &p_name, const Dictionary &p_table_dict)
for (int64_t i = 0; i <= number_of_columns - 1; i++)
{
column_dict = p_table_dict[columns[i]];
if (!column_dict.has("data_type"))
{
UtilityFunctions::printerr("GDSQLite Error: The field \"data_type\" is a required part of the table dictionary");
return false;
}
query_string += (const String &)columns[i] + String(" ");
type_string = (const String &)column_dict["data_type"];
if (type_string.to_lower().begins_with(integer_datatype))
Expand Down Expand Up @@ -445,6 +444,36 @@ bool SQLite::create_table(const String &p_name, const Dictionary &p_table_dict)
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) {
UtilityFunctions::printerr("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"))
{
UtilityFunctions::printerr("GDSQLite Error: The field \"data_type\" is a required part of the table dictionary");
return false;
}

if (column_dict.has("default"))
{
if (column_dict["default"].get_type() != Variant::STRING) {
UtilityFunctions::printerr("GDSQLite Error: The field \"default\" should be of type String");
return false;
}
}
}

return true;
}

bool SQLite::drop_table(const String &p_name)
{
String query_string;
Expand Down
1 change: 1 addition & 0 deletions src/gdsqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace godot

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

sqlite3 *db;
std::vector<std::unique_ptr<Callable>> function_registry;
Expand Down

0 comments on commit 792189a

Please sign in to comment.