aboutsummaryrefslogtreecommitdiff
path: root/frontend/DB.cpp
blob: 82933b887e2fec08c8f9310579deab687c0ca7a6 (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
#include "DB.h"
#include "Exception.h"

DB::DB(const std::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");
}

DB::unique_sqlite3_stmt DB::prepare(const std::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;
}