-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlite.c++.h
309 lines (250 loc) · 7.56 KB
/
sqlite.c++.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
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
301
302
303
304
305
306
307
308
309
#ifndef SQLITE_CPP_H_
#define SQLITE_CPP_H_
#include <exception>
#include <memory>
#include <vector>
#include <type_traits>
#include <iostream>
#include "sqlite3.h"
inline void closeDB(sqlite3 *h) {
if (h != nullptr)
sqlite3_close(h);
}
class SQLiteQuery;
class SQLiteTransactionGuard;
class SQLiteDB {
friend class SQLiteQuery;
public:
SQLiteDB(const std::string &filename) :
filename(filename),
dbHandle(nullptr, closeDB) {
sqlite3 *handle;
auto res = sqlite3_open(this->filename.c_str(), &handle);
if (res != SQLITE_OK)
throw std::runtime_error(sqlite3_errstr(res));
dbHandle.reset(handle);
};
template<class ... Types>
SQLiteQuery createQuery(const std::string &queryString, const Types& ... values);
SQLiteQuery createQuery(const std::string &queryString);
SQLiteTransactionGuard startTransaction();
sqlite3_int64 lastInsertRowID() {
return sqlite3_last_insert_rowid(dbHandle.get());
};
void setVerbose(bool v) {
verbose = v;
};
bool getVerbose() {
return verbose;
};
private:
bool verbose = false;
std::string filename;
std::unique_ptr<sqlite3, decltype(&closeDB)> dbHandle;
};
inline int bindValue(sqlite3_stmt *stmt, int c, const int &value) {
return sqlite3_bind_int(stmt, c, value);
}
inline int bindValue(sqlite3_stmt *stmt, int c, const sqlite_int64 &value) {
return sqlite3_bind_int64(stmt, c, value);
}
inline int bindValue(sqlite3_stmt *stmt, int c, const double &value) {
return sqlite3_bind_double(stmt, c, value);
}
inline int bindValue(sqlite3_stmt *stmt, int pos, const std::string &value) {
return sqlite3_bind_text(stmt, pos, value.c_str(), -1, SQLITE_TRANSIENT);
}
inline int bindValue(sqlite3_stmt *stmt, int pos, char * const &value) {
return sqlite3_bind_text(stmt, pos, value, -1, SQLITE_TRANSIENT);
}
inline int bindValue(sqlite3_stmt *stmt, int pos, const std::vector<char> &value) {
return sqlite3_bind_blob(stmt, pos, (const void *)&value[0], value.size(), SQLITE_TRANSIENT);
}
template<class T>
void _bindValues(sqlite3_stmt *stmt, int k, const T& first) {
auto res = bindValue(stmt, k, first);
if (res != SQLITE_OK)
throw std::runtime_error(sqlite3_errstr(res));
}
template<class T, class ...Types>
void _bindValues(sqlite3_stmt *stmt, int k, const T& first, const Types&...args) {
auto res = bindValue(stmt, k, first);
if (res != SQLITE_OK)
throw std::runtime_error(sqlite3_errstr(res));
_bindValues(stmt, k+1, args...);
}
inline void finalizeStmt(sqlite3_stmt *stmt) {
if (stmt != nullptr)
sqlite3_finalize(stmt);
}
class SQLiteResult;
class SQLiteQuery {
friend SQLiteResult;
public:
SQLiteQuery(SQLiteDB &db) :
db(db),
stmt(nullptr, finalizeStmt) {
};
SQLiteQuery(SQLiteDB &db, const std::string &query) :
db(db),
stmt(nullptr, finalizeStmt),
query(query) {
prepare();
};
SQLiteQuery &operator=(const std::string &query) {
this->query = query;
prepare();
return *this;
}
template<class ... Types>
void bindValues(const Types&... args) {
_bindValues(stmt.get(), 1, args...);
}
template<class T>
void bind(int pos, const T& value) {
auto res = bindValue(stmt.get(), pos, value);
if (res != SQLITE_OK)
throw std::runtime_error(sqlite3_errstr(res));
}
void bind(int pos, const char *value) {
std::string str(value);
auto res = bindValue(stmt.get(), pos, str);
if (res != SQLITE_OK)
throw std::runtime_error(sqlite3_errstr(res));
}
void execute() {
if (db.getVerbose())
std::cerr << sqlite3_expanded_sql(stmt.get()) << std::endl << std::endl;
auto res = sqlite3_step(stmt.get());
if (res != SQLITE_OK && res != SQLITE_DONE)
throw std::runtime_error(sqlite3_errstr(res));
};
void reset() {
auto res = sqlite3_reset(stmt.get());
if (res != SQLITE_OK)
throw std::runtime_error(sqlite3_errstr(res));
};
SQLiteResult getResult();
private:
SQLiteDB &db;
std::shared_ptr<sqlite3_stmt> stmt;
std::string query;
void prepare() {
sqlite3_stmt *stmtHandle;
auto res = sqlite3_prepare(
db.dbHandle.get(),
query.c_str(),
query.size(),
&stmtHandle,
nullptr);
if (res != SQLITE_OK)
throw std::runtime_error(sqlite3_errstr(res));
stmt.reset(stmtHandle, finalizeStmt);
}
};
template<class ... Types>
SQLiteQuery SQLiteDB::createQuery(const std::string &queryString, const Types& ... values) {
SQLiteQuery query(*this, queryString);
query.bindValues(values...);
return query;
}
inline SQLiteQuery SQLiteDB::createQuery(const std::string &queryString) {
return SQLiteQuery(*this, queryString);
}
class SQLiteTransactionGuard {
public:
SQLiteTransactionGuard(SQLiteDB &db) : db(db) {
db.createQuery("BEGIN TRANSACTION;").execute();
};
SQLiteTransactionGuard(SQLiteTransactionGuard &&rhs) : finished(rhs.finished), db(rhs.db) {
rhs.finished = true; // to prevent rollback when destroying moved object
};
void commit() { // must be called explicitly, otherwise rolls back when destroying transaction obj
db.createQuery("COMMIT;").execute();
finished = true;
};
void rollback() {
db.createQuery("ROLLBACK;").execute();
finished = true;
};
~SQLiteTransactionGuard() {
if (!finished)
rollback();
}
private:
bool finished = false;
SQLiteDB &db;
SQLiteTransactionGuard(SQLiteTransactionGuard &) = delete;
};
inline SQLiteTransactionGuard SQLiteDB::startTransaction() {
return SQLiteTransactionGuard(*this);
};
template<class T>
T getColumn(sqlite3_stmt *stmt, int c) {
T result;
throw std::runtime_error("invalid or unhandled data type - column");
return result;
};
template<>
inline int getColumn<int>(sqlite3_stmt *stmt, int c) {
return sqlite3_column_int(stmt, c);
}
template<>
inline sqlite_int64 getColumn<sqlite_int64>(sqlite3_stmt *stmt, int c) {
return sqlite3_column_int64(stmt, c);
}
template<>
inline double getColumn<double>(sqlite3_stmt *stmt, int c) {
return sqlite3_column_double(stmt, c);
}
template<>
inline std::string getColumn<std::string>(sqlite3_stmt *stmt, int c) {
auto pStr = (const char *)sqlite3_column_text(stmt, c);
auto len = sqlite3_column_bytes(stmt, c);
if (pStr == nullptr || len <= 0)
return std::string();
return std::string((const char *)pStr, len);
}
template<>
inline std::vector<char> getColumn<std::vector<char>>(sqlite3_stmt *stmt, int c) {
auto pBlob = (const char *)sqlite3_column_blob(stmt, c);
auto len = sqlite3_column_bytes(stmt, c);
if (pBlob == nullptr || len <= 0)
return std::vector<char>();
std::vector<char> result(len);
std::copy(pBlob, pBlob + len, &result[0]);
return result;
}
template<class T>
void getRecord(sqlite3_stmt *stmt, int k, T &first) {
first = getColumn<T>(stmt, k);
}
template<class T, class ...Types>
void getRecord(sqlite3_stmt *stmt, int k, T &first, Types&...args) {
first = getColumn<T>(stmt, k);
getRecord(stmt, k+1, args...);
}
class SQLiteResult {
public:
SQLiteResult(SQLiteQuery &query) : stmt(query.stmt) {};
template<class ... Types>
void fetch(Types&...args) {
getRecord(stmt.get(), 0, args...);
}
bool next() {
auto res = sqlite3_step(stmt.get());
if (res == SQLITE_ROW)
return true;
if (res == SQLITE_DONE)
return false;
throw std::runtime_error(sqlite3_errstr(res));
};
private:
std::shared_ptr<sqlite3_stmt> stmt;
};
inline SQLiteResult SQLiteQuery::getResult() {
if (db.getVerbose())
std::cerr << sqlite3_expanded_sql(stmt.get()) << std::endl << std::endl;
return SQLiteResult(*this);
};
#endif // SQLITE_CPP_H_