Final Minetest - Please use Linux & Windows build kits at: https://downloads.minetest.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

database-sqlite3.h 7.1KB

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