Move files to subdirectories (#6599)
[oweals/minetest.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 float sqlite_to_float(sqlite3_stmt *s, int iCol)
89         {
90                 return (float) sqlite3_column_double(s, iCol);
91         }
92
93         inline const v3f sqlite_to_v3f(sqlite3_stmt *s, int iCol)
94         {
95                 return v3f(sqlite_to_float(s, iCol), sqlite_to_float(s, iCol + 1),
96                                 sqlite_to_float(s, iCol + 2));
97         }
98
99         // Query verifiers helpers
100         inline void sqlite3_vrfy(int s, const std::string &m = "", int r = SQLITE_OK) const
101         {
102                 if (s != r)
103                         throw DatabaseException(m + ": " + sqlite3_errmsg(m_database));
104         }
105
106         inline void sqlite3_vrfy(const int s, const int r, const std::string &m = "") const
107         {
108                 sqlite3_vrfy(s, m, r);
109         }
110
111         // Create the database structure
112         virtual void createDatabase() = 0;
113         virtual void initStatements() = 0;
114
115         sqlite3 *m_database = nullptr;
116 private:
117         // Open the database
118         void openDatabase();
119
120         bool m_initialized = false;
121
122         std::string m_savedir = "";
123         std::string m_dbname = "";
124
125         sqlite3_stmt *m_stmt_begin = nullptr;
126         sqlite3_stmt *m_stmt_end = nullptr;
127
128         s64 m_busy_handler_data[2];
129
130         static int busyHandler(void *data, int count);
131 };
132
133 class MapDatabaseSQLite3 : private Database_SQLite3, public MapDatabase
134 {
135 public:
136         MapDatabaseSQLite3(const std::string &savedir);
137         virtual ~MapDatabaseSQLite3();
138
139         bool saveBlock(const v3s16 &pos, const std::string &data);
140         void loadBlock(const v3s16 &pos, std::string *block);
141         bool deleteBlock(const v3s16 &pos);
142         void listAllLoadableBlocks(std::vector<v3s16> &dst);
143
144         void beginSave() { Database_SQLite3::beginSave(); }
145         void endSave() { Database_SQLite3::endSave(); }
146 protected:
147         virtual void createDatabase();
148         virtual void initStatements();
149
150 private:
151         void bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index = 1);
152
153         // Map
154         sqlite3_stmt *m_stmt_read = nullptr;
155         sqlite3_stmt *m_stmt_write = nullptr;
156         sqlite3_stmt *m_stmt_list = nullptr;
157         sqlite3_stmt *m_stmt_delete = nullptr;
158 };
159
160 class PlayerDatabaseSQLite3 : private Database_SQLite3, public PlayerDatabase
161 {
162 public:
163         PlayerDatabaseSQLite3(const std::string &savedir);
164         virtual ~PlayerDatabaseSQLite3();
165
166         void savePlayer(RemotePlayer *player);
167         bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
168         bool removePlayer(const std::string &name);
169         void listPlayers(std::vector<std::string> &res);
170
171 protected:
172         virtual void createDatabase();
173         virtual void initStatements();
174
175 private:
176         bool playerDataExists(const std::string &name);
177
178         // Players
179         sqlite3_stmt *m_stmt_player_load = nullptr;
180         sqlite3_stmt *m_stmt_player_add = nullptr;
181         sqlite3_stmt *m_stmt_player_update = nullptr;
182         sqlite3_stmt *m_stmt_player_remove = nullptr;
183         sqlite3_stmt *m_stmt_player_list = nullptr;
184         sqlite3_stmt *m_stmt_player_load_inventory = nullptr;
185         sqlite3_stmt *m_stmt_player_load_inventory_items = nullptr;
186         sqlite3_stmt *m_stmt_player_add_inventory = nullptr;
187         sqlite3_stmt *m_stmt_player_add_inventory_items = nullptr;
188         sqlite3_stmt *m_stmt_player_remove_inventory = nullptr;
189         sqlite3_stmt *m_stmt_player_remove_inventory_items = nullptr;
190         sqlite3_stmt *m_stmt_player_metadata_load = nullptr;
191         sqlite3_stmt *m_stmt_player_metadata_remove = nullptr;
192         sqlite3_stmt *m_stmt_player_metadata_add = nullptr;
193 };