2014-03-05 21:41:27 +01:00
|
|
|
#include "db-sqlite3.h"
|
|
|
|
#include <stdexcept>
|
2014-03-05 21:46:40 +01:00
|
|
|
#include <unistd.h> // for usleep
|
2014-03-05 21:41:27 +01:00
|
|
|
|
2014-03-28 18:42:37 +01:00
|
|
|
#define SQLRES(f, good) \
|
|
|
|
result = (sqlite3_##f);\
|
|
|
|
if (result != good) {\
|
2014-03-28 21:47:19 +01:00
|
|
|
throw std::runtime_error(sqlite3_errmsg(db));\
|
2014-03-28 18:42:37 +01:00
|
|
|
}
|
|
|
|
#define SQLOK(f) SQLRES(f, SQLITE_OK)
|
|
|
|
|
2014-03-05 21:41:27 +01:00
|
|
|
DBSQLite3::DBSQLite3(const std::string &mapdir) {
|
2014-03-28 18:42:37 +01:00
|
|
|
int result;
|
2014-03-05 21:41:27 +01:00
|
|
|
std::string db_name = mapdir + "map.sqlite";
|
2014-03-28 18:42:37 +01:00
|
|
|
|
2014-03-28 21:47:19 +01:00
|
|
|
SQLOK(open_v2(db_name.c_str(), &db, SQLITE_OPEN_READONLY | SQLITE_OPEN_PRIVATECACHE, 0))
|
2014-03-28 18:42:37 +01:00
|
|
|
|
2014-03-28 21:47:19 +01:00
|
|
|
SQLOK(prepare_v2(db,
|
2014-03-28 18:42:37 +01:00
|
|
|
"SELECT pos, data FROM blocks WHERE (pos >= ? AND pos <= ?)",
|
|
|
|
-1, &stmt_get_blocks, NULL))
|
|
|
|
|
2014-03-28 21:47:19 +01:00
|
|
|
SQLOK(prepare_v2(db,
|
2014-03-28 18:42:37 +01:00
|
|
|
"SELECT pos FROM blocks",
|
|
|
|
-1, &stmt_get_block_pos, NULL))
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DBSQLite3::~DBSQLite3() {
|
2014-03-28 18:42:37 +01:00
|
|
|
int result;
|
|
|
|
SQLOK(finalize(stmt_get_blocks));
|
|
|
|
SQLOK(finalize(stmt_get_block_pos));
|
|
|
|
|
2014-03-28 21:47:19 +01:00
|
|
|
SQLOK(close(db));
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int64_t> DBSQLite3::getBlockPos() {
|
|
|
|
std::vector<int64_t> vec;
|
2014-03-28 21:47:19 +01:00
|
|
|
int result;
|
2014-03-28 18:42:37 +01:00
|
|
|
while ((result = sqlite3_step(stmt_get_block_pos)) != SQLITE_DONE) {
|
|
|
|
if (result == SQLITE_ROW) {
|
|
|
|
int64_t blockpos = sqlite3_column_int64(stmt_get_block_pos, 0);
|
|
|
|
vec.push_back(blockpos);
|
|
|
|
} else if (result == SQLITE_BUSY) { // Wait some time and try again
|
|
|
|
usleep(10000);
|
|
|
|
} else {
|
2014-03-28 21:47:19 +01:00
|
|
|
throw std::runtime_error(sqlite3_errmsg(db));
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-28 21:47:19 +01:00
|
|
|
SQLOK(reset(stmt_get_block_pos));
|
2014-03-05 21:41:27 +01:00
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBBlockList DBSQLite3::getBlocksOnZ(int zPos)
|
|
|
|
{
|
|
|
|
DBBlockList blocks;
|
|
|
|
|
2014-03-28 21:47:19 +01:00
|
|
|
int64_t psMin = (static_cast<sqlite3_int64>(zPos) * 16777216L) - 0x800000;
|
|
|
|
int64_t psMax = (static_cast<sqlite3_int64>(zPos) * 16777216L) + 0x7fffff;
|
2014-03-05 21:41:27 +01:00
|
|
|
|
2014-03-28 18:42:37 +01:00
|
|
|
sqlite3_bind_int64(stmt_get_blocks, 1, psMin);
|
|
|
|
sqlite3_bind_int64(stmt_get_blocks, 2, psMax);
|
2014-03-05 21:41:27 +01:00
|
|
|
|
2014-03-28 21:47:19 +01:00
|
|
|
int result;
|
2014-03-28 18:42:37 +01:00
|
|
|
while ((result = sqlite3_step(stmt_get_blocks)) != SQLITE_DONE) {
|
|
|
|
if (result == SQLITE_ROW) {
|
|
|
|
int64_t blocknum = sqlite3_column_int64(stmt_get_blocks, 0);
|
|
|
|
const unsigned char *data = reinterpret_cast<const unsigned char *>(sqlite3_column_blob(stmt_get_blocks, 1));
|
|
|
|
int size = sqlite3_column_bytes(stmt_get_blocks, 1);
|
2014-03-05 21:41:27 +01:00
|
|
|
blocks.push_back(DBBlock(blocknum, std::basic_string<unsigned char>(data, size)));
|
2014-03-05 21:46:40 +01:00
|
|
|
} else if (result == SQLITE_BUSY) { // Wait some time and try again
|
|
|
|
usleep(10000);
|
|
|
|
} else {
|
2014-03-28 21:47:19 +01:00
|
|
|
throw std::runtime_error(sqlite3_errmsg(db));
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-28 18:42:37 +01:00
|
|
|
SQLOK(reset(stmt_get_blocks));
|
2014-03-05 21:41:27 +01:00
|
|
|
|
|
|
|
return blocks;
|
|
|
|
}
|
|
|
|
|
2014-03-28 18:42:37 +01:00
|
|
|
#undef SQLRES
|
|
|
|
#undef SQLOK
|
|
|
|
|