diff --git a/database/prepared_statements/Main.gd b/database/prepared_statements/Main.gd new file mode 100644 index 0000000..073b5a5 --- /dev/null +++ b/database/prepared_statements/Main.gd @@ -0,0 +1,122 @@ +extends Node + +export(String) var database_location : String = "user://database.sqlite" + +func _ready() -> void: + DatabaseManager.connect("initialized", self, "on_databases_initialized", [], CONNECT_ONESHOT) + + var d : Directory = Directory.new() + var bd : String = database_location.get_base_dir() + var loc : String = d.get_filesystem_abspath_for(bd).append_path(database_location.get_file()) + + PLogger.log_message("Database file location: " + loc) + PLogger.log_message("(Editor->Project->Open User Data Folder)") + + var file : File = File.new() + if !file.file_exists(loc): + PLogger.log_message("Database file doesn't exists, will run migrations!") + PLogger.log_message("(Editor->Project->Open User Data Folder)") + call_deferred("migrate") + else: + DatabaseManager.call_deferred("initialized") + + var db : SQLite3Database = SQLite3Database.new() + db.connection_string = loc + DatabaseManager.add_database(db) + +func migrate() -> void: + PLogger.log_message("Running migrations!") + DatabaseManager.connect("migration", self, "_migration") + DatabaseManager.migrate(true, false, 0) + + DatabaseManager.call_deferred("initialized") + +func on_databases_initialized() -> void: + # Load sessions after the databases are initialized + # This happens on the Main node. + call_deferred("load_data") + +func _migration(clear: bool, should_seed: bool, pseed: int) -> void: + randomize() + + var tb : TableBuilder = DatabaseManager.ddb.get_connection().get_table_builder() + + tb.create_table("data_table"); + tb.integer("id").auto_increment().next_row(); + tb.varchar("data_varchar", 60).not_null().next_row(); + tb.text("data_text").not_null().next_row(); + tb.integer("data_int").not_null().next_row(); + tb.real_double("data_double").not_null().next_row(); + tb.primary_key("id"); + tb.ccreate_table(); + tb.run_query(); + + print("Running:") + print(tb.result) + + var qb : QueryBuilder = DatabaseManager.ddb.get_connection().get_query_builder() + qb.insert("data_table", "data_varchar,data_text,data_int,data_double").values() + qb.valph().valph().valph().valph() + qb.cvalues() + qb.end_command() + print("Prepared statement:") + print(qb.result) + + var ps : PreparedStatement = qb.create_prepared_statement() + ps.prepare() + + print("Inserting 10 values!") + + for i in range(10): + ps.reset() + + ps.bind_text(1, "vc" + str(randi())) + ps.bind_text(2, "text" + str(randi())) + ps.bind_int(3, randi()) + ps.bind_double(4, randf() * 100000) + ps.step() + + + +func load_data() -> void: + print("Querying data from table using prepared statements:") + + var qb : QueryBuilder = DatabaseManager.ddb.get_connection().get_query_builder() + + var qr : QueryResult = qb.select("id").from("data_table").run() + + var ids : PoolIntArray = PoolIntArray() + + while qr.next_row(): + ids.push_back(qr.get_cell_int(0)) + + qb.reset() + + qb.select("id,data_varchar,data_text,data_int,data_double").from("data_table") + qb.where().wph("id") + qb.end_command() + print("Query prepared statement:") + print(qb.result) + + var ps : PreparedStatement = qb.create_prepared_statement() + ps.prepare() + + print("Querying rows one by one:") + for index in ids: + ps.reset() + + ps.bind_int(1, index) + ps.step() + + print("ps.column_count(): " + str(ps.column_count())) + print("id: " + str(ps.column_int(0))) + print("data_varchar: " + str(ps.column_text(1))) + print("data_text: " + str(ps.column_text(2))) + print("data_int: " + str(ps.column_int(3))) + print("data_double: " + str(ps.column_double(4))) + + + + + + diff --git a/database/prepared_statements/Main.tscn b/database/prepared_statements/Main.tscn new file mode 100644 index 0000000..85dfcb0 --- /dev/null +++ b/database/prepared_statements/Main.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource path="res://Main.gd" type="Script" id=4] + +[node name="Main" type="Node"] +script = ExtResource( 4 ) diff --git a/database/prepared_statements/default_env.tres b/database/prepared_statements/default_env.tres new file mode 100644 index 0000000..8419635 --- /dev/null +++ b/database/prepared_statements/default_env.tres @@ -0,0 +1,7 @@ +[gd_resource type="Environment3D" load_steps=2 format=3] + +[sub_resource type="ProceduralSky" id=1] + +[resource] +background_mode = 2 +background_sky = SubResource( 1 ) diff --git a/database/prepared_statements/icon.png b/database/prepared_statements/icon.png new file mode 100644 index 0000000..5eaa3d1 Binary files /dev/null and b/database/prepared_statements/icon.png differ diff --git a/database/prepared_statements/icon.png.import b/database/prepared_statements/icon.png.import new file mode 100644 index 0000000..a4c02e6 --- /dev/null +++ b/database/prepared_statements/icon.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.png" +dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/database/prepared_statements/project.pandemonium b/database/prepared_statements/project.pandemonium new file mode 100644 index 0000000..cf040d2 --- /dev/null +++ b/database/prepared_statements/project.pandemonium @@ -0,0 +1,25 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=4 + +[application] + +config/name="Database Prepared Statements" +run/main_scene="res://Main.tscn" +config/icon="res://icon.png" + +[physics] + +common/enable_pause_aware_picking=true + +[rendering] + +vram_compression/import_etc=true +vram_compression/import_etc2=false +environment/default_environment="res://default_env.tres"