Skip to content

Commit 761561a

Browse files
committed
Add views to json import/export methods
1 parent 48e787e commit 761561a

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/gdsqlite.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ bool SQLite::import_from_json(String import_path) {
816816
/* Open the json-file and stream its content into a stringstream */
817817
std::ifstream ifs(char_path);
818818
if (ifs.fail()) {
819-
UtilityFunctions::printerr("GDSQLite Error: Failed to open specified json-file (" + import_path + ")");
819+
UtilityFunctions::printerr("GDSQLite Error: " + String(std::strerror(errno)) + " (" + import_path + ")");
820820
return false;
821821
}
822822
std::stringstream buffer;
@@ -849,19 +849,32 @@ bool SQLite::import_from_json(String import_path) {
849849
}
850850
}
851851

852+
/* Find all views that are present in this database */
853+
query(String("SELECT name FROM sqlite_master WHERE type = 'view';"));
854+
TypedArray<Dictionary> old_view_array = query_result.duplicate(true);
855+
int64_t old_number_of_views = old_view_array.size();
856+
/* Drop all old views present in the database */
857+
for (int64_t i = 0; i <= old_number_of_views - 1; i++) {
858+
Dictionary view_dict = old_view_array[i];
859+
String view_name = view_dict["name"];
860+
String query_string = "DROP VIEW " + view_name + ";";
861+
862+
query(query_string);
863+
}
864+
852865
/* Find all tables that are present in this database */
853866
/* We don't care about indexes or triggers here since they get dropped automatically when their table is dropped */
854867
query(String("SELECT name FROM sqlite_master WHERE type = 'table';"));
855-
TypedArray<Dictionary> old_database_array = query_result.duplicate(true);
868+
TypedArray<Dictionary> old_table_array = query_result.duplicate(true);
856869
#ifdef SQLITE_ENABLE_FTS5
857870
/* FTS5 creates a bunch of shadow tables that cannot be dropped manually! */
858871
/* The virtual table is responsible for dropping these tables itself */
859-
remove_shadow_tables(old_database_array);
872+
remove_shadow_tables(old_table_array);
860873
#endif
861-
int64_t old_number_of_tables = old_database_array.size();
874+
int64_t old_number_of_tables = old_table_array.size();
862875
/* Drop all old tables present in the database */
863876
for (int64_t i = 0; i <= old_number_of_tables - 1; i++) {
864-
Dictionary table_dict = old_database_array[i];
877+
Dictionary table_dict = old_table_array[i];
865878
String table_name = table_dict["name"];
866879

867880
drop_table(table_name);
@@ -923,7 +936,7 @@ bool SQLite::import_from_json(String import_path) {
923936

924937
bool SQLite::export_to_json(String export_path) {
925938
/* Get all names and sql templates for all tables present in the database */
926-
query(String("SELECT name,sql,type FROM sqlite_master WHERE type = 'table' OR type = 'index' OR type = 'trigger';"));
939+
query(String("SELECT name,sql,type FROM sqlite_master;"));
927940
TypedArray<Dictionary> database_array = query_result.duplicate(true);
928941
#ifdef SQLITE_ENABLE_FTS5
929942
/* FTS5 creates a bunch of shadow tables that should NOT be exported! */
@@ -989,7 +1002,7 @@ bool SQLite::export_to_json(String export_path) {
9891002

9901003
std::ofstream ofs(char_path, std::ios::trunc);
9911004
if (ofs.fail()) {
992-
UtilityFunctions::printerr("GDSQLite Error: Can't open specified json-file, file does not exist or is locked");
1005+
UtilityFunctions::printerr("GDSQLite Error: " + String(std::strerror(errno)) + " (" + export_path + ")");
9931006
return false;
9941007
}
9951008
Ref<JSON> json;
@@ -1049,11 +1062,13 @@ bool SQLite::validate_json(const Array &database_array, std::vector<object_struc
10491062
new_object.row_array = temp_dict["row_array"];
10501063
} else if (temp_dict["type"] == String("index")) {
10511064
new_object.type = INDEX;
1065+
} else if (temp_dict["type"] == String("view")) {
1066+
new_object.type = TRIGGER;
10521067
} else if (temp_dict["type"] == String("trigger")) {
10531068
new_object.type = TRIGGER;
10541069
} else {
10551070
/* Did not find the necessary key! */
1056-
UtilityFunctions::printerr("GDSQlite Error: The value of key \"type\" is restricted to \"table\", \"index\" or \"trigger\"");
1071+
UtilityFunctions::printerr("GDSQlite Error: The value of key \"type\" is restricted to \"table\", \"index\", \"view\" or \"trigger\"");
10571072
return false;
10581073
}
10591074

src/gdsqlite.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace godot {
2323
enum OBJECT_TYPE {
2424
TABLE,
2525
INDEX,
26+
VIEW,
2627
TRIGGER
2728
};
2829
struct object_struct {

0 commit comments

Comments
 (0)