Database.gd 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. extends Node
  2. # CRUD
  3. var crud = {}
  4. func Create(key,value):
  5. if crud.has(key):
  6. print('key already exist')
  7. return
  8. crud[key] = value
  9. func Read(key):
  10. if not crud.has(key):
  11. print('key doesent exist')
  12. return
  13. return crud[key]
  14. func Update(key,value):
  15. if not crud.has(key):
  16. print('key doesent exist')
  17. return
  18. crud[key] = value
  19. func delete(key):
  20. if not crud.has(key):
  21. print('key doesent exist')
  22. return
  23. crud.erase(key)
  24. var database := PostgreSQLClient.new()
  25. const USER = "godot"
  26. const PASSWORD = "godotisfree"
  27. const HOST = "localhost"
  28. const PORT = 5432 # Default postgres port
  29. const DATABASE = "godot" # Database name
  30. func _ready():
  31. print('Database is being loaded...')
  32. var _error := database.connect("connection_established", self, "_executer")
  33. _error = database.connect("authentication_error", self, "_authentication_error")
  34. _error = database.connect("connection_closed", self, "_close")
  35. _error = database.connect_to_host("postgresql://%s:%s@%s:%d/%s" % [USER, PASSWORD, HOST, PORT, DATABASE])
  36. func _physics_process(_delta: float) -> void:
  37. database.poll()
  38. func _authentication_error(error_object: Dictionary) -> void:
  39. prints("Error connection to database:", error_object["message"])
  40. func _executer() -> void:
  41. print(database.parameter_status)
  42. var datas := database.execute("""
  43. select * from niggers;
  44. """)
  45. #The datas variable contains an array of PostgreSQLQueryResult object.
  46. for data in datas:
  47. #Specifies the number of fields in a row (can be zero).
  48. print(data.number_of_fields_in_a_row)
  49. # This is usually a single word that identifies which SQL command was completed.
  50. # note: the "BEGIN" and "COMMIT" commands return empty values
  51. print(data.command_tag)
  52. print(data.row_description)
  53. print(data.data_row)
  54. prints("Notice:", data.notice)
  55. if not database.error_object.empty():
  56. prints("Error:", database.error_object)
  57. database.close()
  58. func _close(clean_closure := true) -> void:
  59. prints("DB CLOSE,", "Clean closure:", clean_closure)
  60. func _exit_tree() -> void:
  61. database.close()