Database.gd 2.9 KB

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