3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 SQLite format specification:
28 #include "database-sqlite3.h"
32 #include "exceptions.h"
34 #include "util/string.h"
39 #define SQLRES(s, r) \
41 throw FileNotGoodException(std::string(\
42 "SQLite3 database error (" \
43 __FILE__ ":" TOSTRING(__LINE__) \
45 sqlite3_errmsg(m_database)); \
47 #define SQLOK(s) SQLRES(s, SQLITE_OK)
49 #define PREPARE_STATEMENT(name, query) \
50 SQLOK(sqlite3_prepare_v2(m_database, query, -1, &m_stmt_##name, NULL))
52 #define FINALIZE_STATEMENT(statement) \
53 if (sqlite3_finalize(statement) != SQLITE_OK) { \
54 throw FileNotGoodException(std::string( \
55 "SQLite3: Failed to finalize " #statement ": ") + \
56 sqlite3_errmsg(m_database)); \
60 Database_SQLite3::Database_SQLite3(const std::string &savedir) :
73 void Database_SQLite3::beginSave() {
75 SQLRES(sqlite3_step(m_stmt_begin), SQLITE_DONE);
76 sqlite3_reset(m_stmt_begin);
79 void Database_SQLite3::endSave() {
81 SQLRES(sqlite3_step(m_stmt_end), SQLITE_DONE);
82 sqlite3_reset(m_stmt_end);
85 void Database_SQLite3::openDatabase()
87 if (m_database) return;
89 std::string dbp = m_savedir + DIR_DELIM + "map.sqlite";
91 // Open the database connection
93 if (!fs::CreateAllDirs(m_savedir)) {
94 infostream << "Database_SQLite3: Failed to create directory \""
95 << m_savedir << "\"" << std::endl;
96 throw FileNotGoodException("Failed to create database "
100 bool needs_create = !fs::PathExists(dbp);
102 if (sqlite3_open_v2(dbp.c_str(), &m_database,
103 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
104 NULL) != SQLITE_OK) {
105 errorstream << "SQLite3 database failed to open: "
106 << sqlite3_errmsg(m_database) << std::endl;
107 throw FileNotGoodException("Cannot open database file");
114 std::string query_str = std::string("PRAGMA synchronous = ")
115 + itos(g_settings->getU16("sqlite_synchronous"));
116 SQLOK(sqlite3_exec(m_database, query_str.c_str(), NULL, NULL, NULL));
119 void Database_SQLite3::verifyDatabase()
121 if (m_initialized) return;
125 PREPARE_STATEMENT(begin, "BEGIN");
126 PREPARE_STATEMENT(end, "COMMIT");
127 PREPARE_STATEMENT(read, "SELECT `data` FROM `blocks` WHERE `pos` = ? LIMIT 1");
129 PREPARE_STATEMENT(write, "INSERT INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
131 PREPARE_STATEMENT(write, "REPLACE INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
133 PREPARE_STATEMENT(delete, "DELETE FROM `blocks` WHERE `pos` = ?");
134 PREPARE_STATEMENT(list, "SELECT `pos` FROM `blocks`");
136 m_initialized = true;
138 verbosestream << "ServerMap: SQLite3 database opened." << std::endl;
141 inline void Database_SQLite3::bindPos(sqlite3_stmt *stmt, const v3s16 &pos, int index)
143 SQLOK(sqlite3_bind_int64(stmt, index, getBlockAsInteger(pos)));
146 bool Database_SQLite3::deleteBlock(const v3s16 &pos)
150 bindPos(m_stmt_delete, pos);
152 bool good = sqlite3_step(m_stmt_delete) == SQLITE_DONE;
153 sqlite3_reset(m_stmt_delete);
156 errorstream << "WARNING: deleteBlock: Block failed to delete "
157 << PP(pos) << ": " << sqlite3_errmsg(m_database) << std::endl;
162 bool Database_SQLite3::saveBlock(const v3s16 &pos, const std::string &data)
168 * Note: For some unknown reason SQLite3 fails to REPLACE blocks on Android,
169 * deleting them and then inserting works.
171 bindPos(m_stmt_read, pos);
173 if (sqlite3_step(m_stmt_read) == SQLITE_ROW) {
176 sqlite3_reset(m_stmt_read);
179 bindPos(m_stmt_write, pos);
180 SQLOK(sqlite3_bind_blob(m_stmt_write, 2, data.data(), data.size(), NULL));
182 SQLRES(sqlite3_step(m_stmt_write), SQLITE_DONE)
183 sqlite3_reset(m_stmt_write);
188 std::string Database_SQLite3::loadBlock(const v3s16 &pos)
192 bindPos(m_stmt_read, pos);
194 if (sqlite3_step(m_stmt_read) != SQLITE_ROW) {
195 sqlite3_reset(m_stmt_read);
198 const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
199 size_t len = sqlite3_column_bytes(m_stmt_read, 0);
203 s = std::string(data, len);
205 sqlite3_step(m_stmt_read);
206 // We should never get more than 1 row, so ok to reset
207 sqlite3_reset(m_stmt_read);
212 void Database_SQLite3::createDatabase()
214 assert(m_database); // Pre-condition
215 SQLOK(sqlite3_exec(m_database,
216 "CREATE TABLE IF NOT EXISTS `blocks` (\n"
217 " `pos` INT PRIMARY KEY,\n"
223 void Database_SQLite3::listAllLoadableBlocks(std::vector<v3s16> &dst)
227 while (sqlite3_step(m_stmt_list) == SQLITE_ROW) {
228 dst.push_back(getIntegerAsBlock(sqlite3_column_int64(m_stmt_list, 0)));
230 sqlite3_reset(m_stmt_list);
233 Database_SQLite3::~Database_SQLite3()
235 FINALIZE_STATEMENT(m_stmt_read)
236 FINALIZE_STATEMENT(m_stmt_write)
237 FINALIZE_STATEMENT(m_stmt_list)
238 FINALIZE_STATEMENT(m_stmt_begin)
239 FINALIZE_STATEMENT(m_stmt_end)
240 FINALIZE_STATEMENT(m_stmt_delete)
242 if (sqlite3_close(m_database) != SQLITE_OK) {
243 errorstream << "Database_SQLite3::~Database_SQLite3(): "
244 << "Failed to close database: "
245 << sqlite3_errmsg(m_database) << std::endl;