-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatement.h
268 lines (223 loc) · 7.4 KB
/
Statement.h
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
#ifndef STATEMENT_H
#define STATEMENT_H
#include "Exception.h"
#include "sqlite3.h"
#include <cstdint>
#include <string>
#include <tuple>
struct sqlite3_value;
struct sqlite3_stmt;
struct sqlite3;
namespace sqlite {
class Statement
{
public:
enum State { BUSY, ROW, DONE, MISUSE };
enum DataType { INTEGER = 1, FLOAT, TEXT, BLOB, NUL };
enum DestructorType { STATIC, TRANSIENT };
Statement(sqlite3 *db, const std::string &sql)
: _db(db), _sql(sql)
{
if (sqlite3_prepare_v2(db, sql.c_str(), sql.size(), &_stmt, NULL) != SQLITE_OK)
throw PrepareException(db, std::string("cannot prepare ") + sql);
}
Statement(Statement &&other)
:_db(other._db), _stmt(other._stmt), _sql(std::move(other._sql))
{
other._db = NULL;
other._stmt = NULL;
}
~Statement()
{
finalize();
}
void finalize()
{
sqlite3_finalize(_stmt);
_stmt = NULL;
}
void bind(int idx, const void *blob, int n, DestructorType dtor)
{
auto d = dtor == STATIC ? SQLITE_STATIC : SQLITE_TRANSIENT;
if (sqlite3_bind_blob(_stmt, idx, blob, n, d) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx, const void *blob, uint64_t n, DestructorType dtor)
{
auto d = dtor == STATIC ? SQLITE_STATIC : SQLITE_TRANSIENT;
if (sqlite3_bind_blob64(_stmt, idx, blob, n, d) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx, double value)
{
if (sqlite3_bind_double(_stmt, idx, value) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx, int value)
{
if (sqlite3_bind_int(_stmt, idx, value) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx, int64_t value)
{
if (sqlite3_bind_int64(_stmt, idx, value) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx)
{
if (sqlite3_bind_null(_stmt, idx) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx, const std::string &text)
{
if (sqlite3_bind_text(_stmt, idx, text.c_str(), text.size(), SQLITE_TRANSIENT) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx, const char *text, int n, DestructorType dtor)
{
auto d = dtor == STATIC ? SQLITE_STATIC : SQLITE_TRANSIENT;
if (sqlite3_bind_text(_stmt, idx, text, n, d) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx, const char *text, uint64_t n, DestructorType dtor, unsigned char encoding)
{
auto d = dtor == STATIC ? SQLITE_STATIC : SQLITE_TRANSIENT;
if (sqlite3_bind_text64(_stmt, idx, text, n, d, encoding) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx, const sqlite3_value *value)
{
if (sqlite3_bind_value(_stmt, idx, value) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind(int idx, void *ptr, const char *type, DestructorType dtor)
{
auto d = dtor == STATIC ? SQLITE_STATIC : SQLITE_TRANSIENT;
if (sqlite3_bind_pointer(_stmt, idx, ptr, type, d) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind_zeroblob(int idx, int n)
{
if (sqlite3_bind_zeroblob(_stmt, idx, n) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
void bind_zeroblob64(int idx, uint64_t n)
{
if (sqlite3_bind_zeroblob64(_stmt, idx, n) != SQLITE_OK)
throw BindException(_db, "cannot bind");
}
template <typename ...Args>
void bind_all(Args...args)
{
_bind(1, args...);
}
State step()
{
auto ret = sqlite3_step(_stmt);
switch (ret)
{
case SQLITE_ROW:
return ROW;
case SQLITE_DONE:
return DONE;
case SQLITE_BUSY:
return BUSY;
case SQLITE_MISUSE:
return MISUSE;
default:
throw StepException(_db, "cannot step");
}
}
void reset()
{
if (sqlite3_reset(_stmt) != SQLITE_OK)
throw ResetException(_db, "cannot reset");
}
const void* column_blob(int iCol)
{
return sqlite3_column_blob(_stmt, iCol);
}
double column_double(int iCol)
{
return sqlite3_column_double(_stmt, iCol);
}
int column_int(int iCol)
{
return sqlite3_column_int(_stmt, iCol);
}
int64_t column_int64(int iCol)
{
return sqlite3_column_int64(_stmt, iCol);
}
const unsigned char* column_text(int iCol)
{
return sqlite3_column_text(_stmt, iCol);
}
std::string column_string(int iCol)
{
return std::string(reinterpret_cast<const char*>(sqlite3_column_text(_stmt, iCol)));
}
const void *column_text16(int iCol)
{
return sqlite3_column_text16(_stmt, iCol);
}
sqlite3_value* column_value(int iCol)
{
return sqlite3_column_value(_stmt, iCol);
}
// template <typename ...Args>
// std::tuple<Args...> column_all()
// {
// std::tuple<Args...> values;
// _column<0>(values);
// return values;
// }
const char* column_name(int N)
{
return sqlite3_column_name(_stmt, N);
}
DataType column_type(int iCol)
{
return (DataType)sqlite3_column_type(_stmt, iCol);
}
int column_bytes(int iCol)
{
return sqlite3_column_bytes(_stmt, iCol);
}
int column_bytes16(int iCol)
{
return sqlite3_column_bytes16(_stmt, iCol);
}
int column_count()
{
return sqlite3_column_count(_stmt);
}
private:
sqlite3 *_db;
sqlite3_stmt *_stmt;
const std::string _sql;
void _bind(int) {}
template <typename T, typename ...Args>
void _bind(int idx, T value, Args...args)
{
bind(idx, value);
_bind(idx + 1, args...);
}
// template <size_t index, typename ...Args>
// typename std::enable_if<index == sizeof...(Args)>::type
// _column(std::tuple<Args...> &) {}
// template <size_t index, typename ...Args>
// typename std::enable_if<(index < sizeof...(Args))>::type
// _column(std::tuple<Args...> &values)
// {
// _column_value(index, std::get<index>(values));
// _column<index + 1>(values);
// }
// template <typename T>
// void _column_value(int iCol, T &value)
// {
// value = std::move(column<T>(iCol));
// }
};
}
#endif // STATEMENT_H