-
Notifications
You must be signed in to change notification settings - Fork 20
/
statement.cpp
301 lines (246 loc) · 6.72 KB
/
statement.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// (c) 2013 Stephan Hohe
#include "statement.hpp"
#include "column.hpp"
#include "parameter.hpp"
#include "error.hpp"
#include <sqlite3.h>
#include <cstring>
namespace sqxx {
statement::statement(connection &conn_arg, sqlite3_stmt *handle_arg)
: handle(handle_arg), conn(conn_arg), completed(true) {
}
statement::~statement() {
if (handle) {
sqlite3_finalize(handle);
}
}
const char* statement::sql() {
return sqlite3_sql(handle);
}
int statement::status(int op, bool reset) {
return sqlite3_stmt_status(handle, op, static_cast<int>(reset));
}
int statement::status_fullscan_step(bool reset) {
return status(SQLITE_STMTSTATUS_FULLSCAN_STEP, reset);
}
int statement::status_sort(bool reset) {
return status(SQLITE_STMTSTATUS_SORT, reset);
}
int statement::status_autoindex(bool reset) {
return status(SQLITE_STMTSTATUS_AUTOINDEX, reset);
}
#if SQLITE_VERSION_NUMBER >= 3008000
int statement::status_vm_step(bool reset) {
return status(SQLITE_STMTSTATUS_VM_STEP, reset);
}
#endif
bool statement::readonly() const {
return sqlite3_stmt_readonly(handle);
}
bool statement::busy() const {
return sqlite3_stmt_busy(handle);
}
int statement::param_index(const char *name) const {
int idx = sqlite3_bind_parameter_index(handle, name);
if (idx == 0)
throw error(SQLITE_RANGE, std::string("cannot find parameter \"") + name + "\"");
return idx-1;
}
int statement::param_index(const std::string &name) const {
return param_index(name.c_str());
}
int statement::param_count() const {
return sqlite3_bind_parameter_count(handle);
}
parameter statement::param(int idx) {
return parameter(*this, idx);
}
parameter statement::param(const char *name) {
return param(param_index(name));
}
parameter statement::param(const std::string &name) {
return param(name.c_str());
}
void statement::bind(int idx) {
int rv = sqlite3_bind_null(handle, idx+1);
if (rv != SQLITE_OK)
throw static_error(rv);
}
template<>
void statement::bind<int>(int idx, int value) {
int rv = sqlite3_bind_int(handle, idx+1, value);
if (rv != SQLITE_OK)
throw static_error(rv);
}
template<>
void statement::bind<int64_t>(int idx, int64_t value) {
int rv = sqlite3_bind_int64(handle, idx+1, value);
if (rv != SQLITE_OK)
throw static_error(rv);
}
template<>
void statement::bind<double>(int idx, double value) {
int rv = sqlite3_bind_double(handle, idx+1, value);
if (rv != SQLITE_OK)
throw static_error(rv);
}
template<>
void statement::bind<const char*>(int idx, const char *value, bool copy) {
if (value) {
int rv =
#if SQLITE_VERSION_NUMBER >= 3008007
sqlite3_bind_text64(handle, idx+1, value, std::strlen(value), (copy ? SQLITE_TRANSIENT : SQLITE_STATIC), SQLITE_UTF8);
#else
sqlite3_bind_text(handle, idx+1, value, -1, (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
#endif
if (rv != SQLITE_OK)
throw static_error(rv);
}
else {
bind(idx);
}
}
template<>
void statement::bind<std::string>(int idx, const std::string &value, bool copy) {
int rv =
#if SQLITE_VERSION_NUMBER >= 3008007
sqlite3_bind_text64(handle, idx+1, value.c_str(), value.length(), (copy ? SQLITE_TRANSIENT : SQLITE_STATIC), SQLITE_UTF8);
#else
sqlite3_bind_text(handle, idx+1, value.c_str(), value.length(), (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
#endif
if (rv != SQLITE_OK)
throw static_error(rv);
}
template<>
void statement::bind<blob>(int idx, const blob &value, bool copy) {
if (value.data) {
int rv =
#if SQLITE_VERSION_NUMBER >= 3008007
sqlite3_bind_blob64(handle, idx+1, value.data, value.length, (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
#else
sqlite3_bind_blob(handle, idx+1, value.data, value.length, (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
#endif
if (rv != SQLITE_OK)
throw static_error(rv);
}
else {
int rv =
#if SQLITE_VERSION_NUMBER >= 3008011
sqlite3_bind_zeroblob64(handle, idx+1, value.length);
#else
sqlite3_bind_zeroblob(handle, idx+1, value.length);
#endif
if (rv != SQLITE_OK)
throw static_error(rv);
}
}
int statement::col_count() const {
return sqlite3_column_count(handle);
}
int statement::col_index(const char *name) const {
if (!col_index_table_built) {
for (int i = 0, n = col_count(); i < n; ++i) {
col_index_table[col(i).name()] = i;
}
col_index_table_built = true;
}
return col_index_table[name];
}
int statement::col_index(const std::string &name) const {
return col_index(name.c_str());
}
column statement::col(int idx) const {
return column(*this, idx);
}
column statement::col(const char *name) const {
return col(col_index(name));
}
column statement::col(const std::string &name) const {
return col(name.c_str());
}
template<>
int statement::val<int>(int idx) const {
return sqlite3_column_int(handle, idx);
}
template<>
int64_t statement::val<int64_t>(int idx) const {
return sqlite3_column_int64(handle, idx);
}
template<>
double statement::val<double>(int idx) const {
return sqlite3_column_double(handle, idx);
}
template<>
const char* statement::val<const char*>(int idx) const {
const unsigned char *text = sqlite3_column_text(handle, idx);
return reinterpret_cast<const char*>(text);
}
template<>
std::string statement::val<std::string>(int idx) const {
// Correct order to call functions according to http://www.sqlite.org/c3ref/column_blob.html
const unsigned char* text = sqlite3_column_text(handle, idx);
int bytes = sqlite3_column_bytes(handle, idx);
return std::string(reinterpret_cast<const char*>(text), bytes);
}
template<>
blob statement::val<blob>(int idx) const {
// Correct order to call functions according to http://www.sqlite.org/c3ref/column_blob.html
const void *data = sqlite3_column_blob(handle, idx);
int bytes = sqlite3_column_bytes(handle, idx);
return blob(data, bytes);
}
void statement::step() {
int rv;
rv = sqlite3_step(handle);
if (rv == SQLITE_ROW) {
completed = false;
}
else if (rv == SQLITE_DONE) {
completed = true;
}
else {
throw static_error(rv);
}
}
void statement::run() {
// TODO: Check that statement was not yet run(), maybe reset() if it was
step();
}
void statement::query() {
run();
}
void statement::next_row() {
// TODO: Add check that stmt was run()
step();
}
void statement::reset() {
int rv;
rv = sqlite3_reset(handle);
if (rv != SQLITE_OK) {
// last step() gave error. Does this mean reset() is also invalid?
// TODO
}
col_index_table.clear();
col_index_table_built = false;
}
void statement::clear_bindings() {
int rv;
rv = sqlite3_clear_bindings(handle);
if (rv != SQLITE_OK)
throw static_error(rv);
}
statement::row_iterator::row_iterator(statement *s_arg) : s(s_arg), rowidx(0) {
check_complete();
}
void statement::row_iterator::check_complete() {
if (s && s->completed) {
s = nullptr;
}
}
statement::row_iterator& statement::row_iterator::operator++() {
s->step();
check_complete();
++rowidx;
return *this;
}
} // namespace sqxx