FOSSredder 0.5.0
Loading...
Searching...
No Matches
StmtGuard.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <sqlite3.h>
8#include <string>
9
10namespace persistence {
11
12class StmtGuard {
13public:
14 StmtGuard(sqlite3* db, const char* sql) noexcept {
15 sqlite3_prepare_v2(db, sql, -1, &stmt_, nullptr);
16 }
17
18 StmtGuard(sqlite3* db, const std::string& sql) noexcept {
19 sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt_, nullptr);
20 }
21
22 ~StmtGuard() noexcept {
23 if (stmt_)
24 sqlite3_finalize(stmt_);
25 }
26
27 StmtGuard(const StmtGuard&) = delete;
28 StmtGuard& operator=(const StmtGuard&) = delete;
29
30 explicit operator bool() const noexcept {
31 return stmt_ != nullptr;
32 }
33 sqlite3_stmt* get() const noexcept {
34 return stmt_;
35 }
36
37 int step() noexcept {
38 return sqlite3_step(stmt_);
39 }
40 void reset() noexcept {
41 sqlite3_reset(stmt_);
42 }
43
44 void bindText(int col, const std::string& v) noexcept {
45 sqlite3_bind_text(stmt_, col, v.c_str(), -1, SQLITE_TRANSIENT);
46 }
47 void bindInt(int col, int v) noexcept {
48 sqlite3_bind_int(stmt_, col, v);
49 }
50 void bindInt64(int col, int64_t v) noexcept {
51 sqlite3_bind_int64(stmt_, col, v);
52 }
53 void bindDouble(int col, double v) noexcept {
54 sqlite3_bind_double(stmt_, col, v);
55 }
56
57 std::string columnText(int col) const noexcept {
58 const auto* t = sqlite3_column_text(stmt_, col);
59 return t ? reinterpret_cast<const char*>(t) : std::string{};
60 }
61 int columnInt(int col) const noexcept {
62 return sqlite3_column_int(stmt_, col);
63 }
64 int64_t columnInt64(int col) const noexcept {
65 return sqlite3_column_int64(stmt_, col);
66 }
67 double columnDouble(int col) const noexcept {
68 return sqlite3_column_double(stmt_, col);
69 }
70
71private:
72 sqlite3_stmt* stmt_ = nullptr;
73};
74
75} // namespace persistence
StmtGuard & operator=(const StmtGuard &)=delete
std::string columnText(int col) const noexcept
Definition StmtGuard.h:57
sqlite3_stmt * get() const noexcept
Definition StmtGuard.h:33
int64_t columnInt64(int col) const noexcept
Definition StmtGuard.h:64
~StmtGuard() noexcept
Definition StmtGuard.h:22
StmtGuard(const StmtGuard &)=delete
void bindInt(int col, int v) noexcept
Definition StmtGuard.h:47
double columnDouble(int col) const noexcept
Definition StmtGuard.h:67
StmtGuard(sqlite3 *db, const char *sql) noexcept
Definition StmtGuard.h:14
void bindInt64(int col, int64_t v) noexcept
Definition StmtGuard.h:50
StmtGuard(sqlite3 *db, const std::string &sql) noexcept
Definition StmtGuard.h:18
int step() noexcept
Definition StmtGuard.h:37
void reset() noexcept
Definition StmtGuard.h:40
void bindText(int col, const std::string &v) noexcept
Definition StmtGuard.h:44
int columnInt(int col) const noexcept
Definition StmtGuard.h:61
void bindDouble(int col, double v) noexcept
Definition StmtGuard.h:53