Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
database-sqlite3.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <cstring>
8#include <string>
9#include "database.h"
10#include "exceptions.h"
11
12extern "C" {
13#include "sqlite3.h"
14}
15
17{
18public:
19 virtual ~Database_SQLite3();
20
21 void beginSave();
22 void endSave();
23
24 bool initialized() const { return m_initialized; }
25protected:
26 Database_SQLite3(const std::string &savedir, const std::string &dbname);
27
28 // Open and initialize the database if needed
29 void verifyDatabase();
30
31 // Convertors
32 inline void str_to_sqlite(sqlite3_stmt *s, int iCol, std::string_view str) const
33 {
34 sqlite3_vrfy(sqlite3_bind_text(s, iCol, str.data(), str.size(), NULL));
35 }
36
37 inline void int_to_sqlite(sqlite3_stmt *s, int iCol, int val) const
38 {
39 sqlite3_vrfy(sqlite3_bind_int(s, iCol, val));
40 }
41
42 inline void int64_to_sqlite(sqlite3_stmt *s, int iCol, s64 val) const
43 {
44 sqlite3_vrfy(sqlite3_bind_int64(s, iCol, (sqlite3_int64) val));
45 }
46
47 inline void double_to_sqlite(sqlite3_stmt *s, int iCol, double val) const
48 {
49 sqlite3_vrfy(sqlite3_bind_double(s, iCol, val));
50 }
51
52 // Note that the return value is only valid until the statement is stepped or reset.
53 inline std::string_view sqlite_to_string_view(sqlite3_stmt *s, int iCol)
54 {
55 const char* text = reinterpret_cast<const char*>(sqlite3_column_text(s, iCol));
56 return text ? std::string_view(text) : std::string_view();
57 }
58
59 // Avoid using this in favor of `sqlite_to_string_view`.
60 inline std::string sqlite_to_string(sqlite3_stmt *s, int iCol)
61 {
62 return std::string(sqlite_to_string_view(s, iCol));
63 }
64
65 // Converts a BLOB-type column into a string_view (null byte safe).
66 // Note that the return value is only valid until the statement is stepped or reset.
67 inline std::string_view sqlite_to_blob(sqlite3_stmt *s, int iCol)
68 {
69 const char *data = reinterpret_cast<const char*>(sqlite3_column_blob(s, iCol));
70 if (!data)
71 return std::string_view();
72 size_t len = sqlite3_column_bytes(s, iCol);
73 return std::string_view(data, len);
74 }
75
76 inline s32 sqlite_to_int(sqlite3_stmt *s, int iCol)
77 {
78 return sqlite3_column_int(s, iCol);
79 }
80
81 inline u32 sqlite_to_uint(sqlite3_stmt *s, int iCol)
82 {
83 return (u32) sqlite3_column_int(s, iCol);
84 }
85
86 inline s64 sqlite_to_int64(sqlite3_stmt *s, int iCol)
87 {
88 return (s64) sqlite3_column_int64(s, iCol);
89 }
90
91 inline u64 sqlite_to_uint64(sqlite3_stmt *s, int iCol)
92 {
93 return (u64) sqlite3_column_int64(s, iCol);
94 }
95
96 inline float sqlite_to_float(sqlite3_stmt *s, int iCol)
97 {
98 return (float) sqlite3_column_double(s, iCol);
99 }
100
101 inline const v3f sqlite_to_v3f(sqlite3_stmt *s, int iCol)
102 {
103 return v3f(sqlite_to_float(s, iCol), sqlite_to_float(s, iCol + 1),
104 sqlite_to_float(s, iCol + 2));
105 }
106
107 // Query verifiers helpers
108 inline void sqlite3_vrfy(int s, std::string_view m = "", int r = SQLITE_OK) const
109 {
110 if (s != r) {
111 std::string msg(m);
112 msg.append(": ").append(sqlite3_errmsg(m_database));
113 throw DatabaseException(msg);
114 }
115 }
116
117 inline void sqlite3_vrfy(const int s, const int r, std::string_view m = "") const
118 {
119 sqlite3_vrfy(s, m, r);
120 }
121
122 // Create the database structure
123 virtual void createDatabase() = 0;
124 virtual void initStatements() = 0;
125
126 sqlite3 *m_database = nullptr;
127private:
128 // Open the database
129 void openDatabase();
130
131 bool m_initialized = false;
132
133 std::string m_savedir = "";
134 std::string m_dbname = "";
135
136 sqlite3_stmt *m_stmt_begin = nullptr;
137 sqlite3_stmt *m_stmt_end = nullptr;
138
140
141 static int busyHandler(void *data, int count);
142};
143
145{
146public:
147 MapDatabaseSQLite3(const std::string &savedir);
148 virtual ~MapDatabaseSQLite3();
149
150 bool saveBlock(const v3s16 &pos, std::string_view data);
151 void loadBlock(const v3s16 &pos, std::string *block);
152 bool deleteBlock(const v3s16 &pos);
153 void listAllLoadableBlocks(std::vector<v3s16> &dst);
154
157protected:
158 virtual void createDatabase();
159 virtual void initStatements();
160
161private:
162 void bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index = 1);
163
164 // Map
165 sqlite3_stmt *m_stmt_read = nullptr;
166 sqlite3_stmt *m_stmt_write = nullptr;
167 sqlite3_stmt *m_stmt_list = nullptr;
168 sqlite3_stmt *m_stmt_delete = nullptr;
169};
170
172{
173public:
174 PlayerDatabaseSQLite3(const std::string &savedir);
175 virtual ~PlayerDatabaseSQLite3();
176
177 void savePlayer(RemotePlayer *player);
178 bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
179 bool removePlayer(const std::string &name);
180 void listPlayers(std::vector<std::string> &res);
181
182protected:
183 virtual void createDatabase();
184 virtual void initStatements();
185
186private:
187 bool playerDataExists(const std::string &name);
188
189 // Players
190 sqlite3_stmt *m_stmt_player_load = nullptr;
191 sqlite3_stmt *m_stmt_player_add = nullptr;
192 sqlite3_stmt *m_stmt_player_update = nullptr;
193 sqlite3_stmt *m_stmt_player_remove = nullptr;
194 sqlite3_stmt *m_stmt_player_list = nullptr;
195 sqlite3_stmt *m_stmt_player_load_inventory = nullptr;
196 sqlite3_stmt *m_stmt_player_load_inventory_items = nullptr;
197 sqlite3_stmt *m_stmt_player_add_inventory = nullptr;
198 sqlite3_stmt *m_stmt_player_add_inventory_items = nullptr;
199 sqlite3_stmt *m_stmt_player_remove_inventory = nullptr;
201 sqlite3_stmt *m_stmt_player_metadata_load = nullptr;
202 sqlite3_stmt *m_stmt_player_metadata_remove = nullptr;
203 sqlite3_stmt *m_stmt_player_metadata_add = nullptr;
204};
205
207{
208public:
209 AuthDatabaseSQLite3(const std::string &savedir);
210 virtual ~AuthDatabaseSQLite3();
211
212 virtual bool getAuth(const std::string &name, AuthEntry &res);
213 virtual bool saveAuth(const AuthEntry &authEntry);
214 virtual bool createAuth(AuthEntry &authEntry);
215 virtual bool deleteAuth(const std::string &name);
216 virtual void listNames(std::vector<std::string> &res);
217 virtual void reload();
218
219protected:
220 virtual void createDatabase();
221 virtual void initStatements();
222
223private:
224 virtual void writePrivileges(const AuthEntry &authEntry);
225
226 sqlite3_stmt *m_stmt_read = nullptr;
227 sqlite3_stmt *m_stmt_write = nullptr;
228 sqlite3_stmt *m_stmt_create = nullptr;
229 sqlite3_stmt *m_stmt_delete = nullptr;
230 sqlite3_stmt *m_stmt_list_names = nullptr;
231 sqlite3_stmt *m_stmt_read_privs = nullptr;
232 sqlite3_stmt *m_stmt_write_privs = nullptr;
233 sqlite3_stmt *m_stmt_delete_privs = nullptr;
234 sqlite3_stmt *m_stmt_last_insert_rowid = nullptr;
235};
236
238{
239public:
240 ModStorageDatabaseSQLite3(const std::string &savedir);
242
243 virtual void getModEntries(const std::string &modname, StringMap *storage);
244 virtual void getModKeys(const std::string &modname, std::vector<std::string> *storage);
245 virtual bool getModEntry(const std::string &modname,
246 const std::string &key, std::string *value);
247 virtual bool hasModEntry(const std::string &modname, const std::string &key);
248 virtual bool setModEntry(const std::string &modname,
249 const std::string &key,std::string_view value);
250 virtual bool removeModEntry(const std::string &modname, const std::string &key);
251 virtual bool removeModEntries(const std::string &modname);
252 virtual void listMods(std::vector<std::string> *res);
253
255 virtual void endSave() { Database_SQLite3::endSave(); }
256
257protected:
258 virtual void createDatabase();
259 virtual void initStatements();
260
261private:
262 sqlite3_stmt *m_stmt_get_all = nullptr;
263 sqlite3_stmt *m_stmt_get_keys = nullptr;
264 sqlite3_stmt *m_stmt_get = nullptr;
265 sqlite3_stmt *m_stmt_has = nullptr;
266 sqlite3_stmt *m_stmt_set = nullptr;
267 sqlite3_stmt *m_stmt_remove = nullptr;
268 sqlite3_stmt *m_stmt_remove_all = nullptr;
269};
Definition database-sqlite3.h:207
virtual void listNames(std::vector< std::string > &res)
Definition database-sqlite3.cpp:719
sqlite3_stmt * m_stmt_list_names
Definition database-sqlite3.h:230
virtual bool deleteAuth(const std::string &name)
Definition database-sqlite3.cpp:705
virtual void createDatabase()
Definition database-sqlite3.cpp:602
sqlite3_stmt * m_stmt_read
Definition database-sqlite3.h:226
sqlite3_stmt * m_stmt_write
Definition database-sqlite3.h:227
sqlite3_stmt * m_stmt_delete
Definition database-sqlite3.h:229
sqlite3_stmt * m_stmt_create
Definition database-sqlite3.h:228
virtual bool getAuth(const std::string &name, AuthEntry &res)
Definition database-sqlite3.cpp:643
AuthDatabaseSQLite3(const std::string &savedir)
Definition database-sqlite3.cpp:584
virtual ~AuthDatabaseSQLite3()
Definition database-sqlite3.cpp:589
sqlite3_stmt * m_stmt_delete_privs
Definition database-sqlite3.h:233
virtual void reload()
Definition database-sqlite3.cpp:729
sqlite3_stmt * m_stmt_read_privs
Definition database-sqlite3.h:231
virtual bool createAuth(AuthEntry &authEntry)
Definition database-sqlite3.cpp:683
virtual void initStatements()
Definition database-sqlite3.cpp:627
virtual void writePrivileges(const AuthEntry &authEntry)
Definition database-sqlite3.cpp:734
sqlite3_stmt * m_stmt_last_insert_rowid
Definition database-sqlite3.h:234
sqlite3_stmt * m_stmt_write_privs
Definition database-sqlite3.h:232
virtual bool saveAuth(const AuthEntry &authEntry)
Definition database-sqlite3.cpp:666
Definition database.h:61
Definition exceptions.h:40
Definition database-sqlite3.h:17
std::string_view sqlite_to_blob(sqlite3_stmt *s, int iCol)
Definition database-sqlite3.h:67
void openDatabase()
Definition database-sqlite3.cpp:124
bool initialized() const
Definition database-sqlite3.h:24
sqlite3 * m_database
Definition database-sqlite3.h:126
virtual ~Database_SQLite3()
Definition database-sqlite3.cpp:174
void str_to_sqlite(sqlite3_stmt *s, int iCol, std::string_view str) const
Definition database-sqlite3.h:32
std::string_view sqlite_to_string_view(sqlite3_stmt *s, int iCol)
Definition database-sqlite3.h:53
void sqlite3_vrfy(int s, std::string_view m="", int r=SQLITE_OK) const
Definition database-sqlite3.h:108
std::string m_dbname
Definition database-sqlite3.h:134
float sqlite_to_float(sqlite3_stmt *s, int iCol)
Definition database-sqlite3.h:96
virtual void initStatements()=0
s64 m_busy_handler_data[2]
Definition database-sqlite3.h:139
void int64_to_sqlite(sqlite3_stmt *s, int iCol, s64 val) const
Definition database-sqlite3.h:42
void verifyDatabase()
Definition database-sqlite3.cpp:160
void endSave()
Definition database-sqlite3.cpp:116
sqlite3_stmt * m_stmt_end
Definition database-sqlite3.h:137
sqlite3_stmt * m_stmt_begin
Definition database-sqlite3.h:136
std::string sqlite_to_string(sqlite3_stmt *s, int iCol)
Definition database-sqlite3.h:60
s32 sqlite_to_int(sqlite3_stmt *s, int iCol)
Definition database-sqlite3.h:76
u64 sqlite_to_uint64(sqlite3_stmt *s, int iCol)
Definition database-sqlite3.h:91
s64 sqlite_to_int64(sqlite3_stmt *s, int iCol)
Definition database-sqlite3.h:86
void double_to_sqlite(sqlite3_stmt *s, int iCol, double val) const
Definition database-sqlite3.h:47
virtual void createDatabase()=0
bool m_initialized
Definition database-sqlite3.h:131
Database_SQLite3(const std::string &savedir, const std::string &dbname)
Definition database-sqlite3.cpp:102
u32 sqlite_to_uint(sqlite3_stmt *s, int iCol)
Definition database-sqlite3.h:81
void sqlite3_vrfy(const int s, const int r, std::string_view m="") const
Definition database-sqlite3.h:117
static int busyHandler(void *data, int count)
Definition database-sqlite3.cpp:56
const v3f sqlite_to_v3f(sqlite3_stmt *s, int iCol)
Definition database-sqlite3.h:101
void int_to_sqlite(sqlite3_stmt *s, int iCol, int val) const
Definition database-sqlite3.h:37
std::string m_savedir
Definition database-sqlite3.h:133
void beginSave()
Definition database-sqlite3.cpp:108
Definition database.h:15
Definition database-sqlite3.h:145
sqlite3_stmt * m_stmt_list
Definition database-sqlite3.h:167
sqlite3_stmt * m_stmt_write
Definition database-sqlite3.h:166
sqlite3_stmt * m_stmt_delete
Definition database-sqlite3.h:168
void listAllLoadableBlocks(std::vector< v3s16 > &dst)
Definition database-sqlite3.cpp:279
void beginSave()
Definition database-sqlite3.h:155
virtual void createDatabase()
Definition database-sqlite3.cpp:201
void endSave()
Definition database-sqlite3.h:156
sqlite3_stmt * m_stmt_read
Definition database-sqlite3.h:165
void loadBlock(const v3s16 &pos, std::string *block)
Definition database-sqlite3.cpp:260
virtual ~MapDatabaseSQLite3()
Definition database-sqlite3.cpp:192
MapDatabaseSQLite3(const std::string &savedir)
Definition database-sqlite3.cpp:186
bool deleteBlock(const v3s16 &pos)
Definition database-sqlite3.cpp:230
virtual void initStatements()
Definition database-sqlite3.cpp:214
bool saveBlock(const v3s16 &pos, std::string_view data)
Definition database-sqlite3.cpp:246
void bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index=1)
Definition database-sqlite3.cpp:224
Definition database.h:23
Definition database-sqlite3.h:238
ModStorageDatabaseSQLite3(const std::string &savedir)
Definition database-sqlite3.cpp:747
sqlite3_stmt * m_stmt_set
Definition database-sqlite3.h:266
virtual void getModEntries(const std::string &modname, StringMap *storage)
Definition database-sqlite3.cpp:792
virtual bool getModEntry(const std::string &modname, const std::string &key, std::string *value)
Definition database-sqlite3.cpp:822
sqlite3_stmt * m_stmt_get
Definition database-sqlite3.h:264
virtual ~ModStorageDatabaseSQLite3()
Definition database-sqlite3.cpp:752
sqlite3_stmt * m_stmt_has
Definition database-sqlite3.h:265
virtual bool removeModEntries(const std::string &modname)
Definition database-sqlite3.cpp:892
sqlite3_stmt * m_stmt_get_all
Definition database-sqlite3.h:262
sqlite3_stmt * m_stmt_remove_all
Definition database-sqlite3.h:268
virtual void getModKeys(const std::string &modname, std::vector< std::string > *storage)
Definition database-sqlite3.cpp:807
sqlite3_stmt * m_stmt_remove
Definition database-sqlite3.h:267
virtual void initStatements()
Definition database-sqlite3.cpp:778
virtual void beginSave()
Definition database-sqlite3.h:254
virtual bool removeModEntry(const std::string &modname, const std::string &key)
Definition database-sqlite3.cpp:876
sqlite3_stmt * m_stmt_get_keys
Definition database-sqlite3.h:263
virtual void endSave()
Definition database-sqlite3.h:255
virtual void createDatabase()
Definition database-sqlite3.cpp:763
virtual void listMods(std::vector< std::string > *res)
Definition database-sqlite3.cpp:905
virtual bool hasModEntry(const std::string &modname, const std::string &key)
Definition database-sqlite3.cpp:842
virtual bool setModEntry(const std::string &modname, const std::string &key, std::string_view value)
Definition database-sqlite3.cpp:859
Definition database.h:74
Definition database-sqlite3.h:172
sqlite3_stmt * m_stmt_player_add_inventory
Definition database-sqlite3.h:197
sqlite3_stmt * m_stmt_player_load_inventory_items
Definition database-sqlite3.h:196
bool playerDataExists(const std::string &name)
Definition database-sqlite3.cpp:407
sqlite3_stmt * m_stmt_player_remove_inventory_items
Definition database-sqlite3.h:200
bool loadPlayer(RemotePlayer *player, PlayerSAO *sao)
Definition database-sqlite3.cpp:506
virtual ~PlayerDatabaseSQLite3()
Definition database-sqlite3.cpp:299
sqlite3_stmt * m_stmt_player_load_inventory
Definition database-sqlite3.h:195
bool removePlayer(const std::string &name)
Definition database-sqlite3.cpp:559
virtual void initStatements()
Definition database-sqlite3.cpp:372
void listPlayers(std::vector< std::string > &res)
Definition database-sqlite3.cpp:570
sqlite3_stmt * m_stmt_player_metadata_load
Definition database-sqlite3.h:201
sqlite3_stmt * m_stmt_player_add_inventory_items
Definition database-sqlite3.h:198
virtual void createDatabase()
Definition database-sqlite3.cpp:318
sqlite3_stmt * m_stmt_player_remove
Definition database-sqlite3.h:193
sqlite3_stmt * m_stmt_player_remove_inventory
Definition database-sqlite3.h:199
sqlite3_stmt * m_stmt_player_list
Definition database-sqlite3.h:194
sqlite3_stmt * m_stmt_player_update
Definition database-sqlite3.h:192
sqlite3_stmt * m_stmt_player_load
Definition database-sqlite3.h:190
sqlite3_stmt * m_stmt_player_metadata_add
Definition database-sqlite3.h:203
void savePlayer(RemotePlayer *player)
Definition database-sqlite3.cpp:416
sqlite3_stmt * m_stmt_player_metadata_remove
Definition database-sqlite3.h:202
PlayerDatabaseSQLite3(const std::string &savedir)
Definition database-sqlite3.cpp:293
sqlite3_stmt * m_stmt_player_add
Definition database-sqlite3.h:191
Definition database.h:41
Definition player_sao.h:56
Definition remoteplayer.h:26
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
core::vector3df v3f
Definition irr_v3d.h:11
std::unordered_map< std::string, std::string > StringMap
Definition string.h:65
Definition database.h:52