Database.gd 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. extends Node
  2. func Create(db,key,value):
  3. var truncatekey = ''
  4. var truncatevalue = ''
  5. for i in key.size()-1:
  6. truncatekey += str(key[i]) + ','
  7. for i in value.size()-1:
  8. truncatevalue += str(value[i]) + ','
  9. truncatekey += key.back()
  10. truncatevalue += value.back()
  11. var db_command = ("""
  12. insert into %s (%s) values (%s);
  13. """ % [db, truncatekey, truncatevalue])
  14. print(db_command)
  15. database.execute(db_command)
  16. func Read(db, key, value):
  17. var db_command = ("""
  18. select * from %s where %s = %s;
  19. """ % [db, key, value])
  20. print(db_command)
  21. var datas := database.execute(db_command)
  22. var ret
  23. for data in datas:
  24. ret = (data.data_row)
  25. return ret
  26. func Update(db, key, value, wherekey, wherevalue):
  27. var db_command = ("""
  28. update %s set %s = %s where %s = %s;
  29. """ % [db, key, value, wherekey, wherevalue])
  30. print(db_command)
  31. database.execute(db_command)
  32. func Delete(db, key, value):
  33. var db_command = ("""
  34. delete from %s where %s = %s;
  35. """ % [db, key, value])
  36. print(db_command)
  37. database.execute(db_command)
  38. var database := PostgreSQLClient.new()
  39. const USER = "godot"
  40. const PASSWORD = "godotisfree"
  41. const HOST = "localhost"
  42. const PORT = 5432 # Default postgres port
  43. const DATABASE = "godot" # Database name
  44. func _ready():
  45. print('Database is being loaded...')
  46. var _error := database.connect("connection_established", self, "_executer")
  47. _error = database.connect("authentication_error", self, "_authentication_error")
  48. _error = database.connect("connection_closed", self, "_close")
  49. _error = database.connect_to_host("postgresql://%s:%s@%s:%d/%s" % [USER, PASSWORD, HOST, PORT, DATABASE])
  50. func _physics_process(_delta: float) -> void:
  51. database.poll()
  52. func _authentication_error(error_object: Dictionary) -> void:
  53. prints("Error connection to database:", error_object["message"])
  54. func _executer() -> void:
  55. database.execute("""
  56. create table login_db(
  57. id serial primary key,
  58. email varchar(50) unique not null,
  59. username varchar(50) unique not null,
  60. password varchar(50) not null,
  61. salt varchar(50) not null,
  62. creation_date varchar(50) not null
  63. );
  64. """)
  65. # Create('niggers',["crime","executiondate"],["'das rite'","'2017-06-01'"])
  66. # Update('niggers',"executiondate","'2019-08-07'" , "crime", "'das rite'")
  67. # print(Read('niggers',"crime","'das rite'"))
  68. # Delete('niggers',"crime","'das rite'")
  69. # print(Read('niggers',"crime","'das rite'"))
  70. func _close(clean_closure := true) -> void:
  71. prints("DB CLOSE,", "Clean closure:", clean_closure)
  72. func _exit_tree() -> void:
  73. print('closing')
  74. database.close()