database-postgresql.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <string>
  18. #include <libpq-fe.h>
  19. #include "database.h"
  20. #include "util/basic_macros.h"
  21. class Settings;
  22. class Database_PostgreSQL: public Database
  23. {
  24. public:
  25. Database_PostgreSQL(const std::string &connect_string);
  26. ~Database_PostgreSQL();
  27. void beginSave();
  28. void endSave();
  29. bool initialized() const;
  30. protected:
  31. // Conversion helpers
  32. inline int pg_to_int(PGresult *res, int row, int col)
  33. {
  34. return atoi(PQgetvalue(res, row, col));
  35. }
  36. inline u32 pg_to_uint(PGresult *res, int row, int col)
  37. {
  38. return (u32) atoi(PQgetvalue(res, row, col));
  39. }
  40. inline float pg_to_float(PGresult *res, int row, int col)
  41. {
  42. return (float) atof(PQgetvalue(res, row, col));
  43. }
  44. inline v3s16 pg_to_v3s16(PGresult *res, int row, int col)
  45. {
  46. return v3s16(
  47. pg_to_int(res, row, col),
  48. pg_to_int(res, row, col + 1),
  49. pg_to_int(res, row, col + 2)
  50. );
  51. }
  52. inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
  53. const void **params,
  54. const int *paramsLengths = NULL, const int *paramsFormats = NULL,
  55. bool clear = true, bool nobinary = true)
  56. {
  57. return checkResults(PQexecPrepared(m_conn, stmtName, paramsNumber,
  58. (const char* const*) params, paramsLengths, paramsFormats,
  59. nobinary ? 1 : 0), clear);
  60. }
  61. inline PGresult *execPrepared(const char *stmtName, const int paramsNumber,
  62. const char **params, bool clear = true, bool nobinary = true)
  63. {
  64. return execPrepared(stmtName, paramsNumber,
  65. (const void **)params, NULL, NULL, clear, nobinary);
  66. }
  67. void createTableIfNotExists(const std::string &table_name, const std::string &definition);
  68. void verifyDatabase();
  69. // Database initialization
  70. void connectToDatabase();
  71. virtual void createDatabase() = 0;
  72. virtual void initStatements() = 0;
  73. inline void prepareStatement(const std::string &name, const std::string &sql)
  74. {
  75. checkResults(PQprepare(m_conn, name.c_str(), sql.c_str(), 0, NULL));
  76. }
  77. const int getPGVersion() const { return m_pgversion; }
  78. private:
  79. // Database connectivity checks
  80. void ping();
  81. // Database usage
  82. PGresult *checkResults(PGresult *res, bool clear = true);
  83. // Attributes
  84. std::string m_connect_string;
  85. PGconn *m_conn = nullptr;
  86. int m_pgversion = 0;
  87. };
  88. class MapDatabasePostgreSQL : private Database_PostgreSQL, public MapDatabase
  89. {
  90. public:
  91. MapDatabasePostgreSQL(const std::string &connect_string);
  92. virtual ~MapDatabasePostgreSQL() = default;
  93. bool saveBlock(const v3s16 &pos, const std::string &data);
  94. void loadBlock(const v3s16 &pos, std::string *block);
  95. bool deleteBlock(const v3s16 &pos);
  96. void listAllLoadableBlocks(std::vector<v3s16> &dst);
  97. void beginSave() { Database_PostgreSQL::beginSave(); }
  98. void endSave() { Database_PostgreSQL::endSave(); }
  99. protected:
  100. virtual void createDatabase();
  101. virtual void initStatements();
  102. };
  103. class PlayerDatabasePostgreSQL : private Database_PostgreSQL, public PlayerDatabase
  104. {
  105. public:
  106. PlayerDatabasePostgreSQL(const std::string &connect_string);
  107. virtual ~PlayerDatabasePostgreSQL() = default;
  108. void savePlayer(RemotePlayer *player);
  109. bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
  110. bool removePlayer(const std::string &name);
  111. void listPlayers(std::vector<std::string> &res);
  112. protected:
  113. virtual void createDatabase();
  114. virtual void initStatements();
  115. private:
  116. bool playerDataExists(const std::string &playername);
  117. };