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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
#include "backend/Exception.h"
#include "DB.h"
using namespace std;
DB::DB(const string & path) {
sqlite3 * db = NULL;
int ret = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE, NULL);
this->db = {
db,
[] (sqlite3 * db) {
sqlite3_close_v2(db);
},
};
if (ret != SQLITE_OK)
throw Exception("sqlite3_open_v2: %d", ret);
}
DBStatement DB::prepare(const string & query) const {
return DBStatement(*this, query);
}
DBStatement::DBStatement(const DB & parent, const string & query) : parent(parent) {
sqlite3_stmt * stmt = NULL;
int ret = sqlite3_prepare_v2(this->parent.db.get(), query.c_str(), query.size(), &stmt, NULL);
this->stmt = {
stmt,
[] (sqlite3_stmt * stmt) {
sqlite3_finalize(stmt);
},
};
if (ret != SQLITE_OK)
throw Exception("sqlite3_prepare_v2: %d", ret);
}
DBStatement & DBStatement::bind(const string & text) {
int ret = sqlite3_bind_text(this->stmt.get(), this->param_index, text.data(), text.size(), NULL);
if (ret != SQLITE_OK)
throw Exception("sqlite3_bind_text: %d", ret);
this->param_index++;
return *this;
}
DBStatement & DBStatement::bind(const int & number) {
int ret = sqlite3_bind_int(this->stmt.get(), this->param_index, number);
if (ret != SQLITE_OK)
throw Exception("sqlite3_bind_int: %d", ret);
this->param_index++;
return *this;
}
DBStatement & DBStatement::reset() {
int ret = sqlite3_reset(this->stmt.get());
if (ret != SQLITE_OK)
throw Exception("sqlite3_reset: %d", ret);
this->param_index = 1;
return *this;
}
void DBStatement::execute() {
int ret = sqlite3_step(this->stmt.get());
if (ret != SQLITE_DONE)
throw Exception("sqlite3_step: %d", ret);
}
DBQueryRow DBStatement::row() {
int ret = sqlite3_step(this->stmt.get());
if (ret != SQLITE_ROW && ret != SQLITE_DONE)
throw Exception("sqlite3_step: %d", ret);
return { *this };
}
DBQueryRow::DBQueryRow(DBStatement & parent) : parent(parent) { }
template <>
const char * DBQueryRow::col<const char*>(int index, const char * const & default_value) const {
int type = sqlite3_column_type(this->parent.stmt.get(), index);
if (type == SQLITE_NULL) return default_value;
return reinterpret_cast<const char *>(sqlite3_column_text(this->parent.stmt.get(), index));
}
template <>
const char * DBQueryRow::col<const char*>(int index) const {
return this->col<const char *>(index, "");
}
template <>
int DBQueryRow::col<int>(int index, const int & default_value) const {
int type = sqlite3_column_type(this->parent.stmt.get(), index);
if (type == SQLITE_NULL) return default_value;
return sqlite3_column_int(this->parent.stmt.get(), index);
}
template <>
int DBQueryRow::col<int>(int index) const {
return this->col<int>(index, 0);
}
template <>
unsigned int DBQueryRow::col<unsigned int>(int index, const unsigned int & default_value) const {
return this->col<int>(index, default_value);
}
template <>
unsigned int DBQueryRow::col<unsigned int>(int index) const {
return this->col<unsigned int>(index, 0);
}
DBQueryRowRange DBStatement::rows() {
return { *this };
}
DBQueryRowRange::DBQueryRowIterator & DBQueryRowRange::DBQueryRowIterator::operator ++ () {
int ret = sqlite3_step(this->parent.parent.stmt.get());
if (ret == SQLITE_DONE) {
this->end = true;
return *this;
}
if (ret != SQLITE_ROW)
throw Exception("sqlite3_step: %d", ret);
return *this;
}
bool DBQueryRowRange::DBQueryRowIterator::operator != (const DBQueryRowIterator &) const {
return !this->end;
}
DBQueryRow & DBQueryRowRange::DBQueryRowIterator::operator * () const {
return this->parent.row;
}
DBQueryRowRange::DBQueryRowIterator DBQueryRowRange::begin() {
DBQueryRowRange::DBQueryRowIterator it = { *this };
return ++it;
}
DBQueryRowRange::DBQueryRowIterator DBQueryRowRange::end() {
return { *this };
}
DBQueryRowRange::DBQueryRowRange(DBStatement & parent) : parent(parent), row(parent) { }
DBQueryRowRange::DBQueryRowIterator::DBQueryRowIterator(DBQueryRowRange & parent) : parent(parent) { }
|