-
Notifications
You must be signed in to change notification settings - Fork 6
/
sqlite3.hpp
executable file
·276 lines (255 loc) · 8.52 KB
/
sqlite3.hpp
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
#include <vector>
#include <string>
#include <algorithm>
#include <sqlite3.h>
#ifdef DEBUG_LOCKS
#include <sys/syscall.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
extern pthread_mutex_t db_mutex;
#define db_locked (db_mutex.__data.__owner == syscall(SYS_gettid))
#else
#define db_locked true
#endif
extern sqlite3 *db;
extern std::vector<class SqlStmtBase *> sql_stmts;
struct Path {
const char * str;
Path(const char * str) : str(str) {}
};
struct CharBuf {
char * str;
size_t sz;
};
struct SqlError {
int retcode;
int code;
std::string msg;
SqlError(int retcode, sqlite3 * db) : retcode(retcode)
{
code = sqlite3_errcode(db);
msg = sqlite3_errmsg(db);
}
SqlError(const char * msg) : retcode(), code(), msg(msg) {}
SqlError(std::string && msg) : retcode(), code(), msg(std::move(msg)) {}
SqlError & w_query(std::string && q) {msg = '"' + q + "\": " + msg; return *this;}
};
struct SqlStmtInfo {
const char * const sql;
sqlite3_stmt * stmt;
int inline_bound;
};
class SqlStmtBase {
public:
SqlStmtBase(const char * sql) : inf(new SqlStmtInfo{sql, NULL}) {}
SqlStmtBase(SqlStmtInfo * i) : inf(i) {}
void prepare() {
assert(db_locked);
if (inf->stmt == NULL) {
int res = sqlite3_prepare_v2(db, inf->sql, -1, &inf->stmt, NULL);
if (res != 0) throw SqlError(res, db).w_query(inf->sql);
}
}
SqlStmtInfo * const inf;
protected:
int bind(int & idx, int val) {return sqlite3_bind_int(inf->stmt, idx++, val);}
int bind(int & idx, long int val) {return sqlite3_bind_int64(inf->stmt, idx++, val);}
int bind(int & idx, long long int val) {return sqlite3_bind_int64(inf->stmt, idx++, val);}
int bind(int & idx, const char * val) {return sqlite3_bind_text(inf->stmt, idx++, val, -1, SQLITE_STATIC);};
int bind(int & idx, Path val) {
const char * pos = strrchr(val.str, '/');
assert(pos);
pos++;
int res = sqlite3_bind_text(inf->stmt, idx++, val.str, pos - val.str, SQLITE_STATIC);
if (res != 0) return res;
return sqlite3_bind_text(inf->stmt, idx++, pos, -1, SQLITE_STATIC);
}
int bind(int & idx, const string & val) {return sqlite3_bind_text(inf->stmt, idx++, val.c_str(), -1, SQLITE_TRANSIENT);};
};
class SqlStmt : public SqlStmtBase {
public:
SqlStmt(const char * sql) : SqlStmtBase(sql) {}
SqlStmt(SqlStmtInfo * i) : SqlStmtBase(i) {}
void exec0(int idx) {
assert(db_locked);
if (idx - 1 != sqlite3_bind_parameter_count(inf->stmt) - inf->inline_bound)
throw SqlError(std::string("Wrong number of binding parameters for query: ") + inf->sql);
int res = sqlite3_step(inf->stmt);
if (res != SQLITE_DONE) throw SqlError(res, db);
sqlite3_reset(inf->stmt);
sqlite3_clear_bindings(inf->stmt);
}
template<typename T, typename... Ts> void exec0(int idx, T arg, Ts... args) {
int res = bind(idx, arg);
if (res != 0) throw SqlError(res, db);
exec0(idx, args...);
}
};
class SqlInsert : public SqlStmt {
public:
SqlInsert(const char * sql) : SqlStmt(sql) {}
SqlInsert(SqlStmtInfo * i) : SqlStmt(i) {}
// execute statement
template<typename... Ts> int64_t exec(Ts... args) {
prepare();
exec0(1, args...);
return sqlite3_last_insert_rowid(db);
}
// execite statement, and check that exactly one row was changed
template<typename... Ts> int64_t exec1(Ts... args) {
exec(args...);
if (sqlite3_changes(db) > 1)
throw SqlError(std::string("More than one row modifed while executing: ") + inf->sql);
return sqlite3_last_insert_rowid(db);
}
};
class SqlOther : public SqlStmt {
public:
SqlOther(const char * sql) : SqlStmt(sql) {}
SqlOther(SqlStmtInfo * i) : SqlStmt(i) {}
// execute statement
template<typename... Ts> void exec_nocheck(Ts... args) {
prepare();
exec0(1, args...);
}
// execute statement, and check that it did something
template<typename... Ts> void exec(Ts... args) {
exec_nocheck(args...);
if (sqlite3_changes(db) <= 0)
throw SqlError(std::string("No rows modifed while executing: ") + inf->sql);
}
// execite statement, and check that exactly one row was changed
template<typename... Ts> void exec1(Ts... args) {
exec(args...);
if (sqlite3_changes(db) > 1)
throw SqlError(std::string("More than one row modifed while executing: ") + inf->sql);
}
};
class SqlResult {
public:
SqlResult() : db(), stmt(), step_called(false) {}
SqlResult(sqlite3 * db, sqlite3_stmt * stmt) : db(db), stmt(stmt), step_called(false) {}
SqlResult(SqlResult && other)
: db(other.db), stmt(other.stmt), step_called(other.step_called) {other.stmt = NULL;}
SqlResult & operator=(SqlResult && other) {
reset();
db = other.db;
stmt = other.stmt;
step_called = other.step_called;
other.stmt = NULL;
return *this;
}
bool step() {
assert(db_locked);
int res = sqlite3_step(stmt);
step_called = 2;
if (res == SQLITE_DONE) return false;
if (res == SQLITE_ROW) return true;
else throw SqlError(res, db);
}
void get0(int idx) {}
template<typename T, typename... Ts> void get0(int idx, T & arg, Ts & ... args) {
get_column(idx, arg);
get0(idx + 1, args...);
}
template<typename... Ts> void get(Ts & ... args) {
if (!step_called)
if (!step()) throw SqlError("Query did not return any result.");
get0(0, args...);
}
void reset() {
if (stmt) {
assert(db_locked);
sqlite3_reset(stmt);
sqlite3_clear_bindings(stmt);
stmt = NULL;
}
}
~SqlResult() {
reset();
}
protected:
sqlite3 * db;
sqlite3_stmt * stmt;
int8_t step_called; // 0 = step never called, 1 = step called and we got the data, 2 = step called and we didn't get the data
void get_column(int idx, const char * & val) {val = (const char *)sqlite3_column_text(stmt, idx);};
void get_column(int idx, std::string & val) {
auto val0 = (const char *)sqlite3_column_text(stmt, idx);
if (val0 == 0) val.clear();
else val = val0;
}
void get_column(int idx, bool & val) {val = (bool)sqlite3_column_int(stmt, idx);};
void get_column(int idx, int & val) {val = sqlite3_column_int(stmt, idx);};
void get_column(int idx, unsigned int & val) {val = sqlite3_column_int(stmt, idx);};
void get_column(int idx, long int & val) {val = (long int)sqlite3_column_int64(stmt, idx);};
void get_column(int idx, long long int & val) {val = (long int)sqlite3_column_int64(stmt, idx);};
};
template <typename Res = SqlResult>
class SqlSelect : public SqlStmtBase {
public:
SqlSelect(const char * sql) : SqlStmtBase(sql) {}
SqlSelect(SqlStmtInfo * i) : SqlStmtBase(i) {}
Res exec0(int) {
return Res(db, inf->stmt);
}
template<typename T, typename... Ts> Res exec0(int idx, T arg, Ts... args) {
int res = bind(idx, arg);
if (res != 0) throw SqlError(res, db);
return exec0(idx, args...);
}
template<typename... Ts> Res operator() (Ts... args) {prepare(); return exec0(1, args...);}
template<typename... Ts> void get(Ts & ... args) {
operator()();
auto res = Res(db, inf->stmt);
if (!res.step()) throw SqlError(std::string("Query did not return any result: ") + inf->sql);
res.get(args...);
}
};
void sql_exec(const char * sql) {
assert(db_locked);
auto ret = sqlite3_exec(db, sql, NULL, NULL, NULL);
if (ret != 0) throw SqlError(ret, db).w_query(sql);
}
class SqlTrans {
public:
bool trans_open;
SqlTrans() {
assert(db_locked);
auto res = sqlite3_exec(db, "BEGIN", 0, 0, 0);
if (res != 0) throw SqlError(res, db);
trans_open = true;
}
void commit() {
assert(db_locked);
auto res = sqlite3_exec(db, "COMMIT", 0, 0, 0);
if (res != 0) throw SqlError(res, db);
trans_open = false;
}
void rollback() {
assert(db_locked);
auto res = sqlite3_exec(db, "ROLLBACK", 0, 0, 0);
if (res != 0) throw SqlError(res, db);
trans_open = false;
}
~SqlTrans() {
assert(db_locked);
if (trans_open)
sqlite3_exec(db, "ROLLBACK", 0, 0, 0);
}
};
template <typename T>
struct SqLIter {
T * val;
typedef typename T::Value value_type;
SqLIter(T * v) : val(v) {if (val) operator++();}
const value_type & operator*() const {return val->res;}
const value_type * operator->() const {return &val->res;}
SqLIter & operator++() {bool res = val->step(); if (!res) val = NULL; else val->populate(); return *this;}
void operator++(int) {bool res = val->step(); if (!res) val = NULL; else val->populate();}
};
template <typename T>
static inline bool operator==(SqLIter<T> x, SqLIter<T> y) {return x.val == y.val;}
template <typename T>
static inline bool operator!=(SqLIter<T> x, SqLIter<T> y) {return x.val != y.val;}