@@ -23,6 +23,7 @@ void SQLite::_register_methods()
23
23
24
24
register_property<SQLite, String>(" path" , &SQLite::set_path, &SQLite::get_path, " default" );
25
25
register_property<SQLite, bool >(" verbose_mode" , &SQLite::verbose_mode, false );
26
+ register_property<SQLite, String>(" error_message" , &SQLite::error_message, " " );
26
27
register_property<SQLite, Array>(" query_result" , &SQLite::query_result, Array ());
27
28
}
28
29
@@ -107,7 +108,7 @@ bool SQLite::import_from_json(String import_path)
107
108
108
109
/* Attempt to open the json and, if unsuccessful, throw a parse error specifying the erroneous line */
109
110
Ref<JSONParseResult> result = JSON::get_singleton ()->parse (json_string);
110
- if (result->get_error ()!= Error::OK)
111
+ if (result->get_error () != Error::OK)
111
112
{
112
113
/* Throw a parsing error */
113
114
Godot::print (" GDSQLite Error: parsing failed! reason: " + result->get_error_string () + " , at line: " + String::num_int64 (result->get_error_line ()));
@@ -153,65 +154,64 @@ bool SQLite::import_from_json(String import_path)
153
154
return true ;
154
155
}
155
156
157
+ bool SQLite::export_to_json (String export_path)
158
+ {
159
+ /* Get all names and sql templates for all tables present in the database */
160
+ query (String (" SELECT name,sql FROM sqlite_master WHERE type = 'table';" ));
161
+ int number_of_tables = query_result.size ();
162
+ Array database_dict;
163
+ for (int i = 0 ; i <= number_of_tables - 1 ; i++)
164
+ {
165
+ /* Deep copy of the Dictionary is required as Dictionaries are passed with reference */
166
+ /* Without doing this, future queries would overwrite the table information */
167
+ database_dict.append (deep_copy (query_result[i]));
168
+ }
156
169
157
- bool SQLite::export_to_json (String export_path)
158
- {
159
- /* Get all names and sql templates for all tables present in the database */
160
- query (String (" SELECT name,sql FROM sqlite_master WHERE type = 'table';" ));
161
- int number_of_tables = query_result.size ();
162
- Array database_dict;
163
- for (int i = 0 ; i <= number_of_tables - 1 ; i++)
164
- {
165
- /* Deep copy of the Dictionary is required as Dictionaries are passed with reference */
166
- /* Without doing this, future queries would overwrite the table information */
167
- database_dict.append (deep_copy (query_result[i]));
168
- }
169
-
170
170
/* Add .json to the import_path String if not present */
171
- String ending = String (" .json" );
172
- if (!export_path.ends_with (ending))
173
- {
174
- export_path += ending;
171
+ String ending = String (" .json" );
172
+ if (!export_path.ends_with (ending))
173
+ {
174
+ export_path += ending;
175
175
}
176
176
/* Find the real path */
177
177
export_path = ProjectSettings::get_singleton ()->globalize_path (export_path.strip_edges ());
178
-
179
- /* CharString object goes out-of-scope when not assigned explicitely */
180
- const CharString dummy_path = export_path.utf8 ();
181
- const char *char_path = dummy_path.get_data ();
182
-
183
- std::ofstream ofs (char_path, std::ios::trunc );
178
+
179
+ /* CharString object goes out-of-scope when not assigned explicitely */
180
+ const CharString dummy_path = export_path.utf8 ();
181
+ const char *char_path = dummy_path.get_data ();
182
+
183
+ std::ofstream ofs (char_path, std::ios::trunc );
184
184
if (ofs.fail ())
185
185
{
186
186
Godot::print (" GDSQLite Error: Can't open specified json-file, file does not exist or is locked" );
187
187
return false ;
188
188
}
189
189
190
- /* Construct a Dictionary for each table, convert it to JSON and write it to file */
191
- ofs << " [" ;
192
- for (int i = 0 ; i <= number_of_tables - 1 ; i++)
193
- {
194
- Dictionary table_dict = database_dict[i];
195
- String json_string, query_string;
196
-
197
- query_string = " SELECT * FROM " + (const String &)table_dict[" name" ] + " ;" ;
198
- query (query_string);
199
- table_dict[" row_array" ] = query_result;
200
-
190
+ /* Construct a Dictionary for each table, convert it to JSON and write it to file */
191
+ ofs << " [" ;
192
+ for (int i = 0 ; i <= number_of_tables - 1 ; i++)
193
+ {
194
+ Dictionary table_dict = database_dict[i];
195
+ String json_string, query_string;
196
+
197
+ query_string = " SELECT * FROM " + (const String &)table_dict[" name" ] + " ;" ;
198
+ query (query_string);
199
+ table_dict[" row_array" ] = query_result;
200
+
201
201
json_string = JSON::get_singleton ()->print (table_dict);
202
- /* CharString object goes out-of-scope when not assigned explicitely */
203
- const CharString dummy_string = json_string.utf8 ();
204
- const char *json_char = dummy_string.get_data ();
205
- ofs << json_char;
206
- if (i != number_of_tables - 1 )
207
- {
208
- ofs << " ," ;
209
- }
210
- }
211
- ofs << " ]" ;
212
- ofs.close ();
213
- return true ;
214
- }
202
+ /* CharString object goes out-of-scope when not assigned explicitely */
203
+ const CharString dummy_string = json_string.utf8 ();
204
+ const char *json_char = dummy_string.get_data ();
205
+ ofs << json_char;
206
+ if (i != number_of_tables - 1 )
207
+ {
208
+ ofs << " ," ;
209
+ }
210
+ }
211
+ ofs << " ]" ;
212
+ ofs.close ();
213
+ return true ;
214
+ }
215
215
216
216
void SQLite::close_db ()
217
217
{
@@ -246,7 +246,7 @@ static int callback(void *closure, int argc, char **argv, char **azColName)
246
246
switch (sqlite3_column_type (stmt, i))
247
247
{
248
248
case SQLITE_INTEGER:
249
- column_value = Variant (sqlite3_column_int (stmt, i));
249
+ column_value = Variant (( int ) sqlite3_column_int64 (stmt, i));
250
250
break ;
251
251
252
252
case SQLITE_FLOAT:
@@ -289,9 +289,10 @@ bool SQLite::query(String p_query)
289
289
/* Execute SQL statement */
290
290
rc = sqlite3_exec (db, sql, callback, (void *)this , &zErrMsg);
291
291
292
+ error_message = String (zErrMsg);
292
293
if (rc != SQLITE_OK)
293
294
{
294
- Godot::print (" --> SQL error: " + String (zErrMsg) );
295
+ Godot::print (" --> SQL error: " + error_message );
295
296
sqlite3_free (zErrMsg);
296
297
return false ;
297
298
}
@@ -305,7 +306,8 @@ bool SQLite::query(String p_query)
305
306
bool SQLite::create_table (String p_name, Dictionary p_table_dict)
306
307
{
307
308
308
- String query_string;
309
+ String query_string, type_string;
310
+ String integer_datatype = " int" ;
309
311
/* Create SQL statement */
310
312
query_string = " CREATE TABLE IF NOT EXISTS " + p_name + " (" ;
311
313
@@ -320,15 +322,39 @@ bool SQLite::create_table(String p_name, Dictionary p_table_dict)
320
322
Godot::print (" GDSQLite Error: The field 'data_type' is a required part of the table dictionary" );
321
323
return false ;
322
324
}
323
- query_string += (const String &)columns[i] + " " + column_dict[" data_type" ];
324
-
325
+ query_string += (const String &)columns[i] + " " ;
326
+ type_string = (const String &)column_dict[" data_type" ];
327
+ if (type_string.to_lower ().begins_with (integer_datatype))
328
+ {
329
+ query_string += String (" INTEGER" );
330
+ }
331
+ else
332
+ {
333
+ query_string += type_string;
334
+ }
335
+ /* Will be cleaned up whenever godot-cpp receives decent Dictionary get() with default... */
336
+ /* Primary key check */
325
337
if (column_dict.has (" primary_key" ))
326
338
{
327
339
if (column_dict[" primary_key" ])
328
340
{
329
341
query_string += String (" PRIMARY KEY" );
330
342
}
331
343
}
344
+ /* Default check */
345
+ if (column_dict.has (" default" ))
346
+ {
347
+ query_string += String (" DEFAULT " ) + (const String &)column_dict[" default" ];
348
+ }
349
+ /* Autoincrement check */
350
+ if (column_dict.has (" auto_increment" ))
351
+ {
352
+ if (column_dict[" auto_increment" ])
353
+ {
354
+ query_string += String (" AUTOINCREMENT" );
355
+ }
356
+ }
357
+ /* Not null check */
332
358
if (column_dict.has (" not_null" ))
333
359
{
334
360
if (column_dict[" not_null" ])
@@ -379,7 +405,7 @@ bool SQLite::insert_row(String p_name, Dictionary p_row_dict)
379
405
{
380
406
value_string += (const String &)values[i];
381
407
}
382
- if (i!= number_of_keys- 1 )
408
+ if (i != number_of_keys - 1 )
383
409
{
384
410
key_string += " ," ;
385
411
value_string += " ," ;
@@ -558,4 +584,4 @@ Dictionary SQLite::deep_copy(Dictionary original_dict)
558
584
copy_dict[keys[i]] = original_dict[keys[i]];
559
585
}
560
586
return copy_dict;
561
- }
587
+ }
0 commit comments