]> git.lizzy.rs Git - dragonfireclient.git/blob - src/database/database-sqlite3.h
Merge pull request #59 from PrairieAstronomer/readme_irrlicht_change
[dragonfireclient.git] / src / database / database-sqlite3.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include <cstring>
23 #include <string>
24 #include "database.h"
25 #include "exceptions.h"
26
27 extern "C" {
28 #include "sqlite3.h"
29 }
30
31 class Database_SQLite3 : public Database
32 {
33 public:
34         virtual ~Database_SQLite3();
35
36         void beginSave();
37         void endSave();
38
39         bool initialized() const { return m_initialized; }
40 protected:
41         Database_SQLite3(const std::string &savedir, const std::string &dbname);
42
43         // Open and initialize the database if needed
44         void verifyDatabase();
45
46         // Convertors
47         inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const std::string &str) const
48         {
49                 sqlite3_vrfy(sqlite3_bind_text(s, iCol, str.c_str(), str.size(), NULL));
50         }
51
52         inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const char *str) const
53         {
54                 sqlite3_vrfy(sqlite3_bind_text(s, iCol, str, strlen(str), NULL));
55         }
56
57         inline void int_to_sqlite(sqlite3_stmt *s, int iCol, int val) const
58         {
59                 sqlite3_vrfy(sqlite3_bind_int(s, iCol, val));
60         }
61
62         inline void int64_to_sqlite(sqlite3_stmt *s, int iCol, s64 val) const
63         {
64                 sqlite3_vrfy(sqlite3_bind_int64(s, iCol, (sqlite3_int64) val));
65         }
66
67         inline void double_to_sqlite(sqlite3_stmt *s, int iCol, double val) const
68         {
69                 sqlite3_vrfy(sqlite3_bind_double(s, iCol, val));
70         }
71
72         inline std::string sqlite_to_string(sqlite3_stmt *s, int iCol)
73         {
74                 const char* text = reinterpret_cast<const char*>(sqlite3_column_text(s, iCol));
75                 return std::string(text ? text : "");
76         }
77
78         inline s32 sqlite_to_int(sqlite3_stmt *s, int iCol)
79         {
80                 return sqlite3_column_int(s, iCol);
81         }
82
83         inline u32 sqlite_to_uint(sqlite3_stmt *s, int iCol)
84         {
85                 return (u32) sqlite3_column_int(s, iCol);
86         }
87
88         inline s64 sqlite_to_int64(sqlite3_stmt *s, int iCol)
89         {
90                 return (s64) sqlite3_column_int64(s, iCol);
91         }
92
93         inline u64 sqlite_to_uint64(sqlite3_stmt *s, int iCol)
94         {
95                 return (u64) sqlite3_column_int64(s, iCol);
96         }
97
98         inline float sqlite_to_float(sqlite3_stmt *s, int iCol)
99         {
100                 return (float) sqlite3_column_double(s, iCol);
101         }
102
103         inline const v3f sqlite_to_v3f(sqlite3_stmt *s, int iCol)
104         {
105                 return v3f(sqlite_to_float(s, iCol), sqlite_to_float(s, iCol + 1),
106                                 sqlite_to_float(s, iCol + 2));
107         }
108
109         // Query verifiers helpers
110         inline void sqlite3_vrfy(int s, const std::string &m = "", int r = SQLITE_OK) const
111         {
112                 if (s != r)
113                         throw DatabaseException(m + ": " + sqlite3_errmsg(m_database));
114         }
115
116         inline void sqlite3_vrfy(const int s, const int r, const std::string &m = "") const
117         {
118                 sqlite3_vrfy(s, m, r);
119         }
120
121         // Create the database structure
122         virtual void createDatabase() = 0;
123         virtual void initStatements() = 0;
124
125         sqlite3 *m_database = nullptr;
126 private:
127         // Open the database
128         void openDatabase();
129
130         bool m_initialized = false;
131
132         std::string m_savedir = "";
133         std::string m_dbname = "";
134
135         sqlite3_stmt *m_stmt_begin = nullptr;
136         sqlite3_stmt *m_stmt_end = nullptr;
137
138         s64 m_busy_handler_data[2];
139
140         static int busyHandler(void *data, int count);
141 };
142
143 class MapDatabaseSQLite3 : private Database_SQLite3, public MapDatabase
144 {
145 public:
146         MapDatabaseSQLite3(const std::string &savedir);
147         virtual ~MapDatabaseSQLite3();
148
149         bool saveBlock(const v3s16 &pos, const std::string &data);
150         void loadBlock(const v3s16 &pos, std::string *block);
151         bool deleteBlock(const v3s16 &pos);
152         void listAllLoadableBlocks(std::vector<v3s16> &dst);
153
154         void beginSave() { Database_SQLite3::beginSave(); }
155         void endSave() { Database_SQLite3::endSave(); }
156 protected:
157         virtual void createDatabase();
158         virtual void initStatements();
159
160 private:
161         void bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index = 1);
162
163         // Map
164         sqlite3_stmt *m_stmt_read = nullptr;
165         sqlite3_stmt *m_stmt_write = nullptr;
166         sqlite3_stmt *m_stmt_list = nullptr;
167         sqlite3_stmt *m_stmt_delete = nullptr;
168 };
169
170 class PlayerDatabaseSQLite3 : private Database_SQLite3, public PlayerDatabase
171 {
172 public:
173         PlayerDatabaseSQLite3(const std::string &savedir);
174         virtual ~PlayerDatabaseSQLite3();
175
176         void savePlayer(RemotePlayer *player);
177         bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
178         bool removePlayer(const std::string &name);
179         void listPlayers(std::vector<std::string> &res);
180
181 protected:
182         virtual void createDatabase();
183         virtual void initStatements();
184
185 private:
186         bool playerDataExists(const std::string &name);
187
188         // Players
189         sqlite3_stmt *m_stmt_player_load = nullptr;
190         sqlite3_stmt *m_stmt_player_add = nullptr;
191         sqlite3_stmt *m_stmt_player_update = nullptr;
192         sqlite3_stmt *m_stmt_player_remove = nullptr;
193         sqlite3_stmt *m_stmt_player_list = nullptr;
194         sqlite3_stmt *m_stmt_player_load_inventory = nullptr;
195         sqlite3_stmt *m_stmt_player_load_inventory_items = nullptr;
196         sqlite3_stmt *m_stmt_player_add_inventory = nullptr;
197         sqlite3_stmt *m_stmt_player_add_inventory_items = nullptr;
198         sqlite3_stmt *m_stmt_player_remove_inventory = nullptr;
199         sqlite3_stmt *m_stmt_player_remove_inventory_items = nullptr;
200         sqlite3_stmt *m_stmt_player_metadata_load = nullptr;
201         sqlite3_stmt *m_stmt_player_metadata_remove = nullptr;
202         sqlite3_stmt *m_stmt_player_metadata_add = nullptr;
203 };
204
205 class AuthDatabaseSQLite3 : private Database_SQLite3, public AuthDatabase
206 {
207 public:
208         AuthDatabaseSQLite3(const std::string &savedir);
209         virtual ~AuthDatabaseSQLite3();
210
211         virtual bool getAuth(const std::string &name, AuthEntry &res);
212         virtual bool saveAuth(const AuthEntry &authEntry);
213         virtual bool createAuth(AuthEntry &authEntry);
214         virtual bool deleteAuth(const std::string &name);
215         virtual void listNames(std::vector<std::string> &res);
216         virtual void reload();
217
218 protected:
219         virtual void createDatabase();
220         virtual void initStatements();
221
222 private:
223         virtual void writePrivileges(const AuthEntry &authEntry);
224
225         sqlite3_stmt *m_stmt_read = nullptr;
226         sqlite3_stmt *m_stmt_write = nullptr;
227         sqlite3_stmt *m_stmt_create = nullptr;
228         sqlite3_stmt *m_stmt_delete = nullptr;
229         sqlite3_stmt *m_stmt_list_names = nullptr;
230         sqlite3_stmt *m_stmt_read_privs = nullptr;
231         sqlite3_stmt *m_stmt_write_privs = nullptr;
232         sqlite3_stmt *m_stmt_delete_privs = nullptr;
233         sqlite3_stmt *m_stmt_last_insert_rowid = nullptr;
234 };
235
236 class ModMetadataDatabaseSQLite3 : private Database_SQLite3, public ModMetadataDatabase
237 {
238 public:
239         ModMetadataDatabaseSQLite3(const std::string &savedir);
240         virtual ~ModMetadataDatabaseSQLite3();
241
242         virtual bool getModEntries(const std::string &modname, StringMap *storage);
243         virtual bool setModEntry(const std::string &modname,
244                 const std::string &key, const std::string &value);
245         virtual bool removeModEntry(const std::string &modname, const std::string &key);
246         virtual void listMods(std::vector<std::string> *res);
247
248         virtual void beginSave() { Database_SQLite3::beginSave(); }
249         virtual void endSave() { Database_SQLite3::endSave(); }
250
251 protected:
252         virtual void createDatabase();
253         virtual void initStatements();
254
255 private:
256         sqlite3_stmt *m_stmt_get = nullptr;
257         sqlite3_stmt *m_stmt_set = nullptr;
258         sqlite3_stmt *m_stmt_remove = nullptr;
259 };