Newer
Older
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
#include <vereign/sqlite/statement.hh>
#include <vereign/sqlite/errors.hh>
#include <fmt/format.h>
#include <sqlite3.h>
namespace vereign::sqlite {
Statement::Statement(sqlite3* db, sqlite3_stmt* stmt)
: db_{db},
stmt_{stmt}
{}
void Statement::Finalize() {
sqlite3_finalize(stmt_);
}
Statement::~Statement() {
sqlite3_finalize(stmt_);
}
void Statement::BindBlob(int index, bytes::View blob) {
auto rc = sqlite3_bind_blob64(stmt_, index, blob.CharData(), blob.Size(), SQLITE_STATIC);
if (rc != SQLITE_OK) {
throw Error{rc, fmt::format("bind blob parameter failed, err: {}", sqlite3_errmsg(db_))};
}
}
void Statement::BindText(int index, const std::string& text) {
auto rc = sqlite3_bind_text(stmt_, index, text.data(), text.size(), SQLITE_STATIC);
if (rc != SQLITE_OK) {
throw Error{rc, fmt::format("bind text parameter failed, err: {}", sqlite3_errmsg(db_))};
}
}
auto Statement::Step() -> bool {
auto rc = sqlite3_step(stmt_);
switch (rc) {
case SQLITE_DONE:
return true;
case SQLITE_ROW:
return false;
default:
throw Error{rc, fmt::format("executing statement failed, err: {}", sqlite3_errmsg(db_))};
}
}
auto Statement::GetColumnBlob(int index) -> bytes::View {
auto size = sqlite3_column_bytes(stmt_, index);
auto blob = sqlite3_column_blob(stmt_, index);
return bytes::View{blob, static_cast<size_t>(size)};
}
auto Statement::GetColumnText(int index) -> std::string_view {
std::size_t size = sqlite3_column_bytes(stmt_, index);
auto blob = sqlite3_column_text(stmt_, index);
return std::string_view{reinterpret_cast<const char*>(blob), size};
}
void Statement::ResetAndClearBindings() {
auto rc = sqlite3_clear_bindings(stmt_);
if (rc != SQLITE_OK) {
throw Error{rc, fmt::format("statement reset bindings failed, err: {}", sqlite3_errmsg(db_))};
}
Reset();
}
void Statement::Reset() {
auto rc = sqlite3_reset(stmt_);
if (rc != SQLITE_OK) {
throw Error{rc, fmt::format("statement reset failed, err: {}", sqlite3_errmsg(db_))};
}
}
} // namespace vereign::sqlite