]> git.lizzy.rs Git - dragonfireclient.git/blob - src/database/database-sqlite3.h
Make Lint Happy
[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
41 protected:
42         Database_SQLite3(const std::string &savedir, const std::string &dbname);
43
44         // Open and initialize the database if needed
45         void verifyDatabase();
46
47         // Convertors
48         inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const std::string &str) const
49         {
50                 sqlite3_vrfy(sqlite3_bind_text(s, iCol, str.c_str(), str.size(), NULL));
51         }
52
53         inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const char *str) const
54         {
55                 sqlite3_vrfy(sqlite3_bind_text(s, iCol, str, strlen(str), NULL));
56         }
57
58         inline void int_to_sqlite(sqlite3_stmt *s, int iCol, int val) const
59         {
60                 sqlite3_vrfy(sqlite3_bind_int(s, iCol, val));
61         }
62
63         inline void int64_to_sqlite(sqlite3_stmt *s, int iCol, s64 val) const
64         {
65                 sqlite3_vrfy(sqlite3_bind_int64(s, iCol, (sqlite3_int64)val));
66         }
67
68         inline void double_to_sqlite(sqlite3_stmt *s, int iCol, double val) const
69         {
70                 sqlite3_vrfy(sqlite3_bind_double(s, iCol, val));
71         }
72
73         inline std::string sqlite_to_string(sqlite3_stmt *s, int iCol)
74         {
75                 const char *text = reinterpret_cast<const char *>(
76                                 sqlite3_column_text(s, iCol));
77                 return std::string(text ? text : "");
78         }
79
80         inline s32 sqlite_to_int(sqlite3_stmt *s, int iCol)
81         {
82                 return sqlite3_column_int(s, iCol);
83         }
84
85         inline u32 sqlite_to_uint(sqlite3_stmt *s, int iCol)
86         {
87                 return (u32)sqlite3_column_int(s, iCol);
88         }
89
90         inline s64 sqlite_to_int64(sqlite3_stmt *s, int iCol)
91         {
92                 return (s64)sqlite3_column_int64(s, iCol);
93         }
94
95         inline u64 sqlite_to_uint64(sqlite3_stmt *s, int iCol)
96         {
97                 return (u64)sqlite3_column_int64(s, iCol);
98         }
99
100         inline float sqlite_to_float(sqlite3_stmt *s, int iCol)
101         {
102                 return (float)sqlite3_column_double(s, iCol);
103         }
104
105         inline const v3f sqlite_to_v3f(sqlite3_stmt *s, int iCol)
106         {
107                 return v3f(sqlite_to_float(s, iCol), sqlite_to_float(s, iCol + 1),
108                                 sqlite_to_float(s, iCol + 2));
109         }
110
111         // Query verifiers helpers
112         inline void sqlite3_vrfy(
113                         int s, const std::string &m = "", int r = SQLITE_OK) const
114         {
115                 if (s != r)
116                         throw DatabaseException(m + ": " + sqlite3_errmsg(m_database));
117         }
118
119         inline void sqlite3_vrfy(
120                         const int s, const int r, const std::string &m = "") const
121         {
122                 sqlite3_vrfy(s, m, r);
123         }
124
125         // Create the database structure
126         virtual void createDatabase() = 0;
127         virtual void initStatements() = 0;
128
129         sqlite3 *m_database = nullptr;
130
131 private:
132         // Open the database
133         void openDatabase();
134
135         bool m_initialized = false;
136
137         std::string m_savedir = "";
138         std::string m_dbname = "";
139
140         sqlite3_stmt *m_stmt_begin = nullptr;
141         sqlite3_stmt *m_stmt_end = nullptr;
142
143         s64 m_busy_handler_data[2];
144
145         static int busyHandler(void *data, int count);
146 };
147
148 class MapDatabaseSQLite3 : private Database_SQLite3, public MapDatabase
149 {
150 public:
151         MapDatabaseSQLite3(const std::string &savedir);
152         virtual ~MapDatabaseSQLite3();
153
154         bool saveBlock(const v3s16 &pos, const std::string &data);
155         void loadBlock(const v3s16 &pos, std::string *block);
156         bool deleteBlock(const v3s16 &pos);
157         void listAllLoadableBlocks(std::vector<v3s16> &dst);
158
159         void beginSave() { Database_SQLite3::beginSave(); }
160         void endSave() { Database_SQLite3::endSave(); }
161
162 protected:
163         virtual void createDatabase();
164         virtual void initStatements();
165
166 private:
167         void bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index = 1);
168
169         // Map
170         sqlite3_stmt *m_stmt_read = nullptr;
171         sqlite3_stmt *m_stmt_write = nullptr;
172         sqlite3_stmt *m_stmt_list = nullptr;
173         sqlite3_stmt *m_stmt_delete = nullptr;
174 };
175
176 class PlayerDatabaseSQLite3 : private Database_SQLite3, public PlayerDatabase
177 {
178 public:
179         PlayerDatabaseSQLite3(const std::string &savedir);
180         virtual ~PlayerDatabaseSQLite3();
181
182         void savePlayer(RemotePlayer *player);
183         bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
184         bool removePlayer(const std::string &name);
185         void listPlayers(std::vector<std::string> &res);
186
187 protected:
188         virtual void createDatabase();
189         virtual void initStatements();
190
191 private:
192         bool playerDataExists(const std::string &name);
193
194         // Players
195         sqlite3_stmt *m_stmt_player_load = nullptr;
196         sqlite3_stmt *m_stmt_player_add = nullptr;
197         sqlite3_stmt *m_stmt_player_update = nullptr;
198         sqlite3_stmt *m_stmt_player_remove = nullptr;
199         sqlite3_stmt *m_stmt_player_list = nullptr;
200         sqlite3_stmt *m_stmt_player_load_inventory = nullptr;
201         sqlite3_stmt *m_stmt_player_load_inventory_items = nullptr;
202         sqlite3_stmt *m_stmt_player_add_inventory = nullptr;
203         sqlite3_stmt *m_stmt_player_add_inventory_items = nullptr;
204         sqlite3_stmt *m_stmt_player_remove_inventory = nullptr;
205         sqlite3_stmt *m_stmt_player_remove_inventory_items = nullptr;
206         sqlite3_stmt *m_stmt_player_metadata_load = nullptr;
207         sqlite3_stmt *m_stmt_player_metadata_remove = nullptr;
208         sqlite3_stmt *m_stmt_player_metadata_add = nullptr;
209 };
210
211 class AuthDatabaseSQLite3 : private Database_SQLite3, public AuthDatabase
212 {
213 public:
214         AuthDatabaseSQLite3(const std::string &savedir);
215         virtual ~AuthDatabaseSQLite3();
216
217         virtual bool getAuth(const std::string &name, AuthEntry &res);
218         virtual bool saveAuth(const AuthEntry &authEntry);
219         virtual bool createAuth(AuthEntry &authEntry);
220         virtual bool deleteAuth(const std::string &name);
221         virtual void listNames(std::vector<std::string> &res);
222         virtual void reload();
223
224 protected:
225         virtual void createDatabase();
226         virtual void initStatements();
227
228 private:
229         virtual void writePrivileges(const AuthEntry &authEntry);
230
231         sqlite3_stmt *m_stmt_read = nullptr;
232         sqlite3_stmt *m_stmt_write = nullptr;
233         sqlite3_stmt *m_stmt_create = nullptr;
234         sqlite3_stmt *m_stmt_delete = nullptr;
235         sqlite3_stmt *m_stmt_list_names = nullptr;
236         sqlite3_stmt *m_stmt_read_privs = nullptr;
237         sqlite3_stmt *m_stmt_write_privs = nullptr;
238         sqlite3_stmt *m_stmt_delete_privs = nullptr;
239         sqlite3_stmt *m_stmt_last_insert_rowid = nullptr;
240 };