database-sqlite3.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <cstring>
  18. #include <string>
  19. #include "database.h"
  20. #include "exceptions.h"
  21. extern "C" {
  22. #include "sqlite3.h"
  23. }
  24. class Database_SQLite3 : public Database
  25. {
  26. public:
  27. virtual ~Database_SQLite3();
  28. void beginSave();
  29. void endSave();
  30. bool initialized() const { return m_initialized; }
  31. protected:
  32. Database_SQLite3(const std::string &savedir, const std::string &dbname);
  33. // Open and initialize the database if needed
  34. void verifyDatabase();
  35. // Convertors
  36. inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const std::string &str) const
  37. {
  38. sqlite3_vrfy(sqlite3_bind_text(s, iCol, str.c_str(), str.size(), NULL));
  39. }
  40. inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const char *str) const
  41. {
  42. sqlite3_vrfy(sqlite3_bind_text(s, iCol, str, strlen(str), NULL));
  43. }
  44. inline void int_to_sqlite(sqlite3_stmt *s, int iCol, int val) const
  45. {
  46. sqlite3_vrfy(sqlite3_bind_int(s, iCol, val));
  47. }
  48. inline void int64_to_sqlite(sqlite3_stmt *s, int iCol, s64 val) const
  49. {
  50. sqlite3_vrfy(sqlite3_bind_int64(s, iCol, (sqlite3_int64) val));
  51. }
  52. inline void double_to_sqlite(sqlite3_stmt *s, int iCol, double val) const
  53. {
  54. sqlite3_vrfy(sqlite3_bind_double(s, iCol, val));
  55. }
  56. inline std::string sqlite_to_string(sqlite3_stmt *s, int iCol)
  57. {
  58. const char* text = reinterpret_cast<const char*>(sqlite3_column_text(s, iCol));
  59. return std::string(text ? text : "");
  60. }
  61. inline s32 sqlite_to_int(sqlite3_stmt *s, int iCol)
  62. {
  63. return sqlite3_column_int(s, iCol);
  64. }
  65. inline u32 sqlite_to_uint(sqlite3_stmt *s, int iCol)
  66. {
  67. return (u32) sqlite3_column_int(s, iCol);
  68. }
  69. inline float sqlite_to_float(sqlite3_stmt *s, int iCol)
  70. {
  71. return (float) sqlite3_column_double(s, iCol);
  72. }
  73. inline const v3f sqlite_to_v3f(sqlite3_stmt *s, int iCol)
  74. {
  75. return v3f(sqlite_to_float(s, iCol), sqlite_to_float(s, iCol + 1),
  76. sqlite_to_float(s, iCol + 2));
  77. }
  78. // Query verifiers helpers
  79. inline void sqlite3_vrfy(int s, const std::string &m = "", int r = SQLITE_OK) const
  80. {
  81. if (s != r)
  82. throw DatabaseException(m + ": " + sqlite3_errmsg(m_database));
  83. }
  84. inline void sqlite3_vrfy(const int s, const int r, const std::string &m = "") const
  85. {
  86. sqlite3_vrfy(s, m, r);
  87. }
  88. // Create the database structure
  89. virtual void createDatabase() = 0;
  90. virtual void initStatements() = 0;
  91. sqlite3 *m_database = nullptr;
  92. private:
  93. // Open the database
  94. void openDatabase();
  95. bool m_initialized = false;
  96. std::string m_savedir = "";
  97. std::string m_dbname = "";
  98. sqlite3_stmt *m_stmt_begin = nullptr;
  99. sqlite3_stmt *m_stmt_end = nullptr;
  100. s64 m_busy_handler_data[2];
  101. static int busyHandler(void *data, int count);
  102. };
  103. class MapDatabaseSQLite3 : private Database_SQLite3, public MapDatabase
  104. {
  105. public:
  106. MapDatabaseSQLite3(const std::string &savedir);
  107. virtual ~MapDatabaseSQLite3();
  108. bool saveBlock(const v3s16 &pos, const std::string &data);
  109. void loadBlock(const v3s16 &pos, std::string *block);
  110. bool deleteBlock(const v3s16 &pos);
  111. void listAllLoadableBlocks(std::vector<v3s16> &dst);
  112. void beginSave() { Database_SQLite3::beginSave(); }
  113. void endSave() { Database_SQLite3::endSave(); }
  114. protected:
  115. virtual void createDatabase();
  116. virtual void initStatements();
  117. private:
  118. void bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index = 1);
  119. // Map
  120. sqlite3_stmt *m_stmt_read = nullptr;
  121. sqlite3_stmt *m_stmt_write = nullptr;
  122. sqlite3_stmt *m_stmt_list = nullptr;
  123. sqlite3_stmt *m_stmt_delete = nullptr;
  124. };
  125. class PlayerDatabaseSQLite3 : private Database_SQLite3, public PlayerDatabase
  126. {
  127. public:
  128. PlayerDatabaseSQLite3(const std::string &savedir);
  129. virtual ~PlayerDatabaseSQLite3();
  130. void savePlayer(RemotePlayer *player);
  131. bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
  132. bool removePlayer(const std::string &name);
  133. void listPlayers(std::vector<std::string> &res);
  134. protected:
  135. virtual void createDatabase();
  136. virtual void initStatements();
  137. private:
  138. bool playerDataExists(const std::string &name);
  139. // Players
  140. sqlite3_stmt *m_stmt_player_load = nullptr;
  141. sqlite3_stmt *m_stmt_player_add = nullptr;
  142. sqlite3_stmt *m_stmt_player_update = nullptr;
  143. sqlite3_stmt *m_stmt_player_remove = nullptr;
  144. sqlite3_stmt *m_stmt_player_list = nullptr;
  145. sqlite3_stmt *m_stmt_player_load_inventory = nullptr;
  146. sqlite3_stmt *m_stmt_player_load_inventory_items = nullptr;
  147. sqlite3_stmt *m_stmt_player_add_inventory = nullptr;
  148. sqlite3_stmt *m_stmt_player_add_inventory_items = nullptr;
  149. sqlite3_stmt *m_stmt_player_remove_inventory = nullptr;
  150. sqlite3_stmt *m_stmt_player_remove_inventory_items = nullptr;
  151. sqlite3_stmt *m_stmt_player_metadata_load = nullptr;
  152. sqlite3_stmt *m_stmt_player_metadata_remove = nullptr;
  153. sqlite3_stmt *m_stmt_player_metadata_add = nullptr;
  154. };