]> git.lizzy.rs Git - minetest.git/blob - src/database-sqlite3.h
Remove legacy unused define DIGGING_PARTICLES_AMOUNT
[minetest.git] / src / 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 #ifndef DATABASE_SQLITE3_HEADER
21 #define DATABASE_SQLITE3_HEADER
22
23 #include <cstring>
24 #include <string>
25 #include "database.h"
26 #include "exceptions.h"
27
28 extern "C" {
29 #include "sqlite3.h"
30 }
31
32 class Database_SQLite3 : public Database
33 {
34 public:
35         virtual ~Database_SQLite3();
36
37         void beginSave();
38         void endSave();
39
40         bool initialized() const { return m_initialized; }
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*>(sqlite3_column_text(s, iCol));
76                 return std::string(text ? text : "");
77         }
78
79         inline s32 sqlite_to_int(sqlite3_stmt *s, int iCol)
80         {
81                 return sqlite3_column_int(s, iCol);
82         }
83
84         inline u32 sqlite_to_uint(sqlite3_stmt *s, int iCol)
85         {
86                 return (u32) sqlite3_column_int(s, iCol);
87         }
88
89         inline float sqlite_to_float(sqlite3_stmt *s, int iCol)
90         {
91                 return (float) sqlite3_column_double(s, iCol);
92         }
93
94         inline const v3f sqlite_to_v3f(sqlite3_stmt *s, int iCol)
95         {
96                 return v3f(sqlite_to_float(s, iCol), sqlite_to_float(s, iCol + 1),
97                                 sqlite_to_float(s, iCol + 2));
98         }
99
100         // Query verifiers helpers
101         inline void sqlite3_vrfy(int s, const std::string &m = "", int r = SQLITE_OK) const
102         {
103                 if (s != r)
104                         throw DatabaseException(m + ": " + sqlite3_errmsg(m_database));
105         }
106
107         inline void sqlite3_vrfy(const int s, const int r, const std::string &m = "") const
108         {
109                 sqlite3_vrfy(s, m, r);
110         }
111
112         // Create the database structure
113         virtual void createDatabase() = 0;
114         virtual void initStatements() = 0;
115
116         sqlite3 *m_database;
117 private:
118         // Open the database
119         void openDatabase();
120
121         bool m_initialized;
122
123         std::string m_savedir;
124         std::string m_dbname;
125
126         sqlite3_stmt *m_stmt_begin;
127         sqlite3_stmt *m_stmt_end;
128
129         s64 m_busy_handler_data[2];
130
131         static int busyHandler(void *data, int count);
132 };
133
134 class MapDatabaseSQLite3 : private Database_SQLite3, public MapDatabase
135 {
136 public:
137         MapDatabaseSQLite3(const std::string &savedir);
138         virtual ~MapDatabaseSQLite3();
139
140         bool saveBlock(const v3s16 &pos, const std::string &data);
141         void loadBlock(const v3s16 &pos, std::string *block);
142         bool deleteBlock(const v3s16 &pos);
143         void listAllLoadableBlocks(std::vector<v3s16> &dst);
144
145         void beginSave() { Database_SQLite3::beginSave(); }
146         void endSave() { Database_SQLite3::endSave(); }
147 protected:
148         virtual void createDatabase();
149         virtual void initStatements();
150
151 private:
152         void bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index = 1);
153
154         // Map
155         sqlite3_stmt *m_stmt_read;
156         sqlite3_stmt *m_stmt_write;
157         sqlite3_stmt *m_stmt_list;
158         sqlite3_stmt *m_stmt_delete;
159 };
160
161 class PlayerDatabaseSQLite3 : private Database_SQLite3, public PlayerDatabase
162 {
163 public:
164         PlayerDatabaseSQLite3(const std::string &savedir);
165         virtual ~PlayerDatabaseSQLite3();
166
167         void savePlayer(RemotePlayer *player);
168         bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
169         bool removePlayer(const std::string &name);
170         void listPlayers(std::vector<std::string> &res);
171
172 protected:
173         virtual void createDatabase();
174         virtual void initStatements();
175
176 private:
177         bool playerDataExists(const std::string &name);
178
179         // Players
180         sqlite3_stmt *m_stmt_player_load;
181         sqlite3_stmt *m_stmt_player_add;
182         sqlite3_stmt *m_stmt_player_update;
183         sqlite3_stmt *m_stmt_player_remove;
184         sqlite3_stmt *m_stmt_player_list;
185         sqlite3_stmt *m_stmt_player_load_inventory;
186         sqlite3_stmt *m_stmt_player_load_inventory_items;
187         sqlite3_stmt *m_stmt_player_add_inventory;
188         sqlite3_stmt *m_stmt_player_add_inventory_items;
189         sqlite3_stmt *m_stmt_player_remove_inventory;
190         sqlite3_stmt *m_stmt_player_remove_inventory_items;
191         sqlite3_stmt *m_stmt_player_metadata_load;
192         sqlite3_stmt *m_stmt_player_metadata_remove;
193         sqlite3_stmt *m_stmt_player_metadata_add;
194 };
195
196 #endif