-
Notifications
You must be signed in to change notification settings - Fork 7
/
DBTest_Postgres.gd
83 lines (55 loc) · 2.1 KB
/
DBTest_Postgres.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
extends Node
var database := PostgreSQLClient.new()
#TODO: ADD RELEVANT VALUES BELOW FOR TESTING!
const USER = 'ernie'#"postgres"
const PASSWORD = 'myPasswordGoesHere' #TODO: replace with correct password
const HOST = 'localhost'#"localhost"
const PORT = 5432 # Default postgres port
const DATABASE = 'main'#"postgres"
func _ready() -> void:
print("Test is running")
func _init() -> void:
print("Init running")
print("USER: " + str(USER) + "; HOST: " + str(HOST) + "; PORT: " + str(PORT) + "; DATABASE: " + str(DATABASE))
var _error := database.connect("connection_established", self, "_executer")
_error = database.connect("authentication_error", self, "_authentication_error")
_error = database.connect("connection_closed", self, "_close")
#Connection to the database
_error = database.connect_to_host("postgresql://%s:%s@%s:%d/%s" % [USER, PASSWORD, HOST, PORT, DATABASE])
func _physics_process(_delta: float) -> void:
print("Physics running")
database.poll()
func _authentication_error(error_object: Dictionary) -> void:
prints("Error connection to database:", error_object["message"])
func _executer() -> void:
print("Executor running")
print(database.parameter_status)
# var datas := database.execute("""
# BEGIN;
# /*Helloworld*/
# SELECT concat('Hello', 'World');
# COMMIT;
# """)
var datas := database.execute("""
BEGIN;
SELECT * FROM test_characters;
COMMIT;
""")
#The datas variable contains an array of PostgreSQLQueryResult object.
for data in datas:
#Specifies the number of fields in a row (can be zero).
print(data.number_of_fields_in_a_row)
# This is usually a single word that identifies which SQL command was completed.
# note: the "BEGIN" and "COMMIT" commands return empty values
print(data.command_tag)
print(data.row_description)
print(data.data_row)
prints("Notice:", data.notice)
if not database.error_object.empty():
prints("Error:", database.error_object)
database.close()
func _close(clean_closure := true) -> void:
prints("DB CLOSE,", "Clean closure:", clean_closure)
func _exit_tree() -> void:
print("Exit running")
database.close()