From 5fe963de8f7edd086897ed26e576aa42762c4bf5 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 27 Apr 2024 21:07:30 +0200 Subject: [PATCH] Prepared statements demo. --- database/prepared_statements/Main.gd | 122 ++++++++++++++++++ database/prepared_statements/Main.tscn | 6 + database/prepared_statements/default_env.tres | 7 + database/prepared_statements/icon.png | Bin 0 -> 3238 bytes database/prepared_statements/icon.png.import | 35 +++++ .../prepared_statements/project.pandemonium | 25 ++++ 6 files changed, 195 insertions(+) create mode 100644 database/prepared_statements/Main.gd create mode 100644 database/prepared_statements/Main.tscn create mode 100644 database/prepared_statements/default_env.tres create mode 100644 database/prepared_statements/icon.png create mode 100644 database/prepared_statements/icon.png.import create mode 100644 database/prepared_statements/project.pandemonium 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 0000000000000000000000000000000000000000..5eaa3d1ad38ef8667b54c5ae78ec23c708023ae7 GIT binary patch literal 3238 zcmV;X3|aGuP)Px>U`a$lRCt`-n|pBF*Hyvm$tcH&MlOwxf#49O%W zO`rif1P5@ZO$wy#5N4SE1!>Y^lBQuW$+(Y%QYbYLAc4j$xFNOku;YhfS;n$0*?PaV zTJ7#{f4_VAVLTzyN*zgi-rs7Pc41^>WQH!LKZL_!fZYPVE`(Tq^;Ny{P)bc8#FHrXH-ke% zz?1>P;V=UB3n6x1Qw^^Ylu|!Z@adtUAw(z?LesR*X+r$fd`8nW@!K|a5m4K#clm~;PQYli71Aq|1rwK8n0o$e-l~P2b z(QBpW6-P80MJYA=r&|nQ%k($f=PQZZyROU7@Gzd|Q56njS=N%|^bZWs)!mKbIILZ> zhRRUrvh*?x1Ka1D@dAXn#XyM2G{aIAoeiZFZKqCgi_$R=^!4^J-cV0fW}7V-=Pz8K zX(Yy4-9q_H-fKTk#VzHRy)-S$`p5uKJ4c!jV&RA)kw_$1=Ye0-F*S`k!>0OT4;MN* z3K}n^r0vWZ){VufFieCHXhLJ1ZW52jFI&I)b1ZqJbJA)2V(zvX)D6~;PH^t**+s{9 zb$9cLM4Es$SH^N(Cy_{$R9CnH25?=MY>wpDbedxchKGl9j`ci`Q2#K?w1s(xj$rae zFo2R0p|aD;b%T)$ow%->mEP0c%?jPjO4EcuP9{sG=0|c%E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