godot-sqlite icon indicating copy to clipboard operation
godot-sqlite copied to clipboard

Syntax errors for empty strings and string using syntax restricted symbols

Open 2shady4u opened this issue 3 years ago • 0 comments

Creation of the following table_dict results in syntax errors of the query:

var table_dict : Dictionary = Dictionary()
table_dict["last_name"] = {"data_type":"char(5)", "default": "Silver"}
table_dict["first_name"] = {"data_type":"char(5)", "default": "Long John"}
table_dict["area"] = {"data_type":"char(5)", "default": ""}
table_dict["color"] = {"data_type":"char(255)", "default": "0,0,0,0"}

Encapsulating all strings with single quotation marks seems to fix the issue:

var table_dict : Dictionary = Dictionary()
table_dict["last_name"] = {"data_type":"char(5)", "default": "'Silver'"}
table_dict["first_name"] = {"data_type":"char(5)", "default": "'Long John'"}
table_dict["area"] = {"data_type":"char(5)", "default": "''"}
table_dict["color"] = {"data_type":"char(255)", "default": "'0,0,0,0'"}

However, the subtleties of adding these quotation marks is never mentioned anywhere in the documentation. To make it even more confusing, the "Silver" default value (without single quotation marks) actually works and creates the exact same default value in the database.

As it is difficult to know the user's intention, it is quite a hassle to add these quotation marks in the plugin itself as it might not even by the user's intention to add them. . . (since SQLite uses dynamic typing and column types aren't enforced)

Proposed convention:

  • Always encapsulate strings in single quotation marks, especially when:
    • The string is empty
    • The string contains syntax restricted symbols such as commas or spaces

In both cases the user is recommended to find other ways of setting his/her default value, such as:

  • Leaving the default value empty, which results in a NULL value instead of an empty string and is also easily checked for in code.
  • If fixed arrays are to be parsed, switch those arrays to single fields. For example, in case of a 3D position, it is recommended and easy to just make 3 separate columns instead (position_x, position_y, position_z).

For non-fixed length arrays, it might be possible to use this. Although I'm not sure if the cost of implementing and maintaining these structures is worth it in the long run?

2shady4u avatar Apr 02 '21 10:04 2shady4u