-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.cpp
More file actions
89 lines (78 loc) · 2.57 KB
/
db.cpp
File metadata and controls
89 lines (78 loc) · 2.57 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "pch.hpp"
#include "db.hpp"
#include "sqlite.hpp"
#include "result_code.hpp"
#include <cassert>
namespace be::sqlite {
///////////////////////////////////////////////////////////////////////////////
Db::Db(const S& path)
: Db(path.c_str()) { }
///////////////////////////////////////////////////////////////////////////////
Db::Db(const char* path) {
sqlite3* con = nullptr;
int result = sqlite3_open(path, &con);
if (result != SQLITE_OK) {
if (con) {
RecoverableTrace e(ext_result_code(result), sqlite3_errmsg(con));
sqlite3_close(con);
throw e;
} else {
throw RecoverableTrace(ext_result_code(result));
}
}
con_ = sqlite3_ptr(con);
}
///////////////////////////////////////////////////////////////////////////////
Db::Db(const S& path, int flags)
: Db(path.c_str(), flags) { }
///////////////////////////////////////////////////////////////////////////////
Db::Db(const char* path, int flags) {
sqlite3* con = nullptr;
int result = sqlite3_open_v2(path, &con, flags, nullptr);
if (result != SQLITE_OK) {
if (con) {
RecoverableTrace e(ext_result_code(result), sqlite3_errmsg(con));
sqlite3_close(con);
throw e;
} else {
throw RecoverableTrace(ext_result_code(result));
}
}
con_ = sqlite3_ptr(con);
}
///////////////////////////////////////////////////////////////////////////////
Db::Db(const S& path, int flags, const S& vfs_name)
: Db(path.c_str(), flags, vfs_name.c_str()) { }
///////////////////////////////////////////////////////////////////////////////
Db::Db(const char* path, int flags, const char* vfs_name) {
sqlite3* con = nullptr;
int result = sqlite3_open_v2(path, &con, flags, vfs_name);
if (result != SQLITE_OK) {
if (con) {
RecoverableTrace e(ext_result_code(result), sqlite3_errmsg(con));
sqlite3_close(con);
throw e;
} else {
throw RecoverableTrace(ext_result_code(result));
}
}
con_ = sqlite3_ptr(con);
}
///////////////////////////////////////////////////////////////////////////////
Db::operator bool() const {
return !!con_;
}
///////////////////////////////////////////////////////////////////////////////
sqlite3* Db::raw() {
return con_.get();
}
///////////////////////////////////////////////////////////////////////////////
void Db::deleter::operator()(sqlite3* con) const {
if (con) {
assert(sqlite3_next_stmt(con, NULL) == NULL);
int result = sqlite3_close(con);
assert(result == SQLITE_OK);
BE_IGNORE(result);
}
}
} // be::sqlite