blob: c6ddcbfefda50b220b0eefbbf9831e04a10d6d91 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include <memory>
#include <functional>
#include <sqlite3.h>
#include "backend/Dungeon.h"
#include "generate_dungeon.h"
#include "Exception.h"
using namespace std;
class DB {
typedef unique_ptr<sqlite3, function<void(sqlite3*)>> unique_sqlite3;
typedef unique_ptr<sqlite3_stmt, function<void(sqlite3_stmt*)>> unique_sqlite3_stmt;
public:
DB(const string & path) {
sqlite3 * db = NULL;
int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READONLY, NULL);
this->db = {
db,
[] (sqlite3 * db) {
sqlite3_close_v2(db);
},
};
if (ret != SQLITE_OK)
throw Exception("sqlite3_open_v2");
}
unique_sqlite3_stmt prepare(const string & query) {
sqlite3_stmt * stmt = NULL;
int ret = sqlite3_prepare_v2(this->db.get(), query.c_str(), query.size(), &stmt, NULL);
unique_sqlite3_stmt uniq_stmt = {
stmt,
[] (sqlite3_stmt * stmt) {
sqlite3_finalize(stmt);
},
};
if (ret != SQLITE_OK)
throw Exception("sqlite3_prepare_v2");
return uniq_stmt;
}
private:
unique_sqlite3 db = NULL;
};
unique_ptr<Dungeon> generate_dungeon() {
unique_ptr<Dungeon> dungeon = make_unique<Dungeon>();
DB db { "kerkersendraken.db" };
return dungeon;
}
|