For usages of assert() that are meant to persist in Release builds (when NDEBUG is...
[oweals/minetest.git] / src / database-sqlite3.cpp
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 /*
21 SQLite format specification:
22         blocks:
23                 (PK) INT id
24                 BLOB data
25 */
26
27
28 #include "database-sqlite3.h"
29
30 #include "log.h"
31 #include "filesys.h"
32 #include "exceptions.h"
33 #include "main.h"
34 #include "settings.h"
35 #include "util/string.h"
36
37 #include <cassert>
38
39
40 #define SQLRES(s, r) \
41         if ((s) != (r)) { \
42                 throw FileNotGoodException(std::string(\
43                                         "SQLite3 database error (" \
44                                         __FILE__ ":" TOSTRING(__LINE__) \
45                                         "): ") +\
46                                 sqlite3_errmsg(m_database)); \
47         }
48 #define SQLOK(s) SQLRES(s, SQLITE_OK)
49
50 #define PREPARE_STATEMENT(name, query) \
51         SQLOK(sqlite3_prepare_v2(m_database, query, -1, &m_stmt_##name, NULL))
52
53 #define FINALIZE_STATEMENT(statement) \
54         if (sqlite3_finalize(statement) != SQLITE_OK) { \
55                 throw FileNotGoodException(std::string( \
56                         "SQLite3: Failed to finalize " #statement ": ") + \
57                          sqlite3_errmsg(m_database)); \
58         }
59
60
61 Database_SQLite3::Database_SQLite3(const std::string &savedir) :
62         m_initialized(false),
63         m_savedir(savedir),
64         m_database(NULL),
65         m_stmt_read(NULL),
66         m_stmt_write(NULL),
67         m_stmt_list(NULL),
68         m_stmt_delete(NULL)
69 {
70 }
71
72 void Database_SQLite3::beginSave() {
73         verifyDatabase();
74         SQLRES(sqlite3_step(m_stmt_begin), SQLITE_DONE);
75         sqlite3_reset(m_stmt_begin);
76 }
77
78 void Database_SQLite3::endSave() {
79         verifyDatabase();
80         SQLRES(sqlite3_step(m_stmt_end), SQLITE_DONE);
81         sqlite3_reset(m_stmt_end);
82 }
83
84 void Database_SQLite3::openDatabase()
85 {
86         if (m_database) return;
87
88         std::string dbp = m_savedir + DIR_DELIM + "map.sqlite";
89
90         // Open the database connection
91
92         if (!fs::CreateAllDirs(m_savedir)) {
93                 infostream << "Database_SQLite3: Failed to create directory \""
94                         << m_savedir << "\"" << std::endl;
95                 throw FileNotGoodException("Failed to create database "
96                                 "save directory");
97         }
98
99         bool needs_create = !fs::PathExists(dbp);
100
101         if (sqlite3_open_v2(dbp.c_str(), &m_database,
102                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
103                         NULL) != SQLITE_OK) {
104                 errorstream << "SQLite3 database failed to open: "
105                         << sqlite3_errmsg(m_database) << std::endl;
106                 throw FileNotGoodException("Cannot open database file");
107         }
108
109         if (needs_create) {
110                 createDatabase();
111         }
112
113         std::string query_str = std::string("PRAGMA synchronous = ")
114                          + itos(g_settings->getU16("sqlite_synchronous"));
115         SQLOK(sqlite3_exec(m_database, query_str.c_str(), NULL, NULL, NULL));
116 }
117
118 void Database_SQLite3::verifyDatabase()
119 {
120         if (m_initialized) return;
121
122         openDatabase();
123
124         PREPARE_STATEMENT(begin, "BEGIN");
125         PREPARE_STATEMENT(end, "COMMIT");
126         PREPARE_STATEMENT(read, "SELECT `data` FROM `blocks` WHERE `pos` = ? LIMIT 1");
127 #ifdef __ANDROID__
128         PREPARE_STATEMENT(write,  "INSERT INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
129 #else
130         PREPARE_STATEMENT(write, "REPLACE INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
131 #endif
132         PREPARE_STATEMENT(delete, "DELETE FROM `blocks` WHERE `pos` = ?");
133         PREPARE_STATEMENT(list, "SELECT `pos` FROM `blocks`");
134
135         m_initialized = true;
136
137         verbosestream << "ServerMap: SQLite3 database opened." << std::endl;
138 }
139
140 inline void Database_SQLite3::bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index)
141 {
142         SQLOK(sqlite3_bind_int64(stmt, index, getBlockAsInteger(pos)));
143 }
144
145 bool Database_SQLite3::deleteBlock(const v3s16 &pos)
146 {
147         verifyDatabase();
148
149         bindPos(m_stmt_delete, pos);
150
151         bool good = sqlite3_step(m_stmt_delete) == SQLITE_DONE;
152         sqlite3_reset(m_stmt_delete);
153
154         if (!good) {
155                 errorstream << "WARNING: deleteBlock: Block failed to delete "
156                         << PP(pos) << ": " << sqlite3_errmsg(m_database) << std::endl;
157         }
158         return good;
159 }
160
161 bool Database_SQLite3::saveBlock(const v3s16 &pos, const std::string &data)
162 {
163         verifyDatabase();
164
165 #ifdef __ANDROID__
166         /**
167          * Note: For some unknown reason SQLite3 fails to REPLACE blocks on Android,
168          * deleting them and then inserting works.
169          */
170         bindPos(m_stmt_read, pos);
171
172         if (sqlite3_step(m_stmt_read) == SQLITE_ROW) {
173                 deleteBlock(pos);
174         }
175         sqlite3_reset(m_stmt_read);
176 #endif
177
178         bindPos(m_stmt_write, pos);
179         SQLOK(sqlite3_bind_blob(m_stmt_write, 2, data.data(), data.size(), NULL));
180
181         SQLRES(sqlite3_step(m_stmt_write), SQLITE_DONE)
182         sqlite3_reset(m_stmt_write);
183
184         return true;
185 }
186
187 std::string Database_SQLite3::loadBlock(const v3s16 &pos)
188 {
189         verifyDatabase();
190
191         bindPos(m_stmt_read, pos);
192
193         if (sqlite3_step(m_stmt_read) != SQLITE_ROW) {
194                 sqlite3_reset(m_stmt_read);
195                 return "";
196         }
197         const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
198         size_t len = sqlite3_column_bytes(m_stmt_read, 0);
199
200         std::string s;
201         if (data)
202                 s = std::string(data, len);
203
204         sqlite3_step(m_stmt_read);
205         // We should never get more than 1 row, so ok to reset
206         sqlite3_reset(m_stmt_read);
207
208         return s;
209 }
210
211 void Database_SQLite3::createDatabase()
212 {
213         assert(m_database); // Pre-condition
214         SQLOK(sqlite3_exec(m_database,
215                 "CREATE TABLE IF NOT EXISTS `blocks` (\n"
216                 "       `pos` INT PRIMARY KEY,\n"
217                 "       `data` BLOB\n"
218                 ");\n",
219                 NULL, NULL, NULL));
220 }
221
222 void Database_SQLite3::listAllLoadableBlocks(std::vector<v3s16> &dst)
223 {
224         verifyDatabase();
225
226         while (sqlite3_step(m_stmt_list) == SQLITE_ROW) {
227                 dst.push_back(getIntegerAsBlock(sqlite3_column_int64(m_stmt_list, 0)));
228         }
229         sqlite3_reset(m_stmt_list);
230 }
231
232 Database_SQLite3::~Database_SQLite3()
233 {
234         FINALIZE_STATEMENT(m_stmt_read)
235         FINALIZE_STATEMENT(m_stmt_write)
236         FINALIZE_STATEMENT(m_stmt_list)
237         FINALIZE_STATEMENT(m_stmt_begin)
238         FINALIZE_STATEMENT(m_stmt_end)
239         FINALIZE_STATEMENT(m_stmt_delete)
240
241         if (sqlite3_close(m_database) != SQLITE_OK) {
242                 errorstream << "Database_SQLite3::~Database_SQLite3(): "
243                                 << "Failed to close database: "
244                                 << sqlite3_errmsg(m_database) << std::endl;
245         }
246 }
247