-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLite.h
449 lines (365 loc) · 11 KB
/
SQLite.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#pragma once
#include "Handle.h"
#include "sqlite3.h"
#include <string>
#ifdef _DEBUG
#define VERIFY ASSERT
#define VERIFY_(result, expression) ASSERT(result == expression)
#else
#define VERIFY(expression) (expression)
#define VERIFY_(result, expression) (expression)
#endif
enum class Type
{
Integer = SQLITE_INTEGER,
Float = SQLITE_FLOAT,
Blob = SQLITE_BLOB,
Null = SQLITE_NULL,
Text = SQLITE_TEXT,
};
inline char const * TypeName(Type const type) noexcept
{
switch (type)
{
case Type::Integer: return "Integer";
case Type::Float: return "Float";
case Type::Blob: return "Blob";
case Type::Null: return "Null";
case Type::Text: return "Text";
}
ASSERT(false);
return "Invalid";
}
struct Exception
{
int Result = 0;
std::string Message;
explicit Exception(sqlite3 * const connection) :
Result(sqlite3_extended_errcode(connection)),
Message(sqlite3_errmsg(connection))
{}
};
class Connection
{
struct ConnectionHandleTraits : HandleTraits<sqlite3 *>
{
static void Close(Type value) noexcept
{
VERIFY_(SQLITE_OK, sqlite3_close(value));
}
};
using ConnectionHandle = Handle<ConnectionHandleTraits>;
ConnectionHandle m_handle;
template <typename F, typename C>
void InternalOpen(F open, C const * const filename)
{
Connection temp;
if (SQLITE_OK != open(filename, temp.m_handle.Set()))
{
temp.ThrowLastError();
}
swap(m_handle, temp.m_handle);
}
public:
Connection() noexcept = default;
template <typename C>
explicit Connection(C const * const filename)
{
Open(filename);
}
static Connection Memory()
{
return Connection(":memory:");
}
static Connection WideMemory()
{
return Connection(L":memory:");
}
__declspec(noreturn) void ThrowLastError() const
{
throw Exception(GetAbi());
}
explicit operator bool() const noexcept
{
return static_cast<bool>(m_handle);
}
sqlite3 * GetAbi() const noexcept
{
return m_handle.Get();
}
void Open(char const * const filename)
{
InternalOpen(sqlite3_open, filename);
}
void Open(wchar_t const * const filename)
{
InternalOpen(sqlite3_open16, filename);
}
long long RowId() const noexcept
{
return sqlite3_last_insert_rowid(GetAbi());
}
template <typename F>
void Profile(F callback, void * const context = nullptr)
{
sqlite3_profile(GetAbi(), callback, context);
}
};
class Backup
{
struct BackupHandleTraits : HandleTraits<sqlite3_backup *>
{
static void Close(Type value) noexcept
{
sqlite3_backup_finish(value);
}
};
using BackupHandle = Handle<BackupHandleTraits>;
BackupHandle m_handle;
Connection const * m_destination = nullptr;
public:
Backup(Connection const & destination,
Connection const & source,
char const * const destinationName = "main",
char const * const sourceName = "main") :
m_handle(sqlite3_backup_init(destination.GetAbi(),
destinationName,
source.GetAbi(),
sourceName)),
m_destination(&destination)
{
if (!m_handle)
{
destination.ThrowLastError();
}
}
sqlite3_backup * GetAbi() const noexcept
{
return m_handle.Get();
}
bool Step(int const pages = -1)
{
int const result = sqlite3_backup_step(GetAbi(), pages);
if (result == SQLITE_OK) return true;
if (result == SQLITE_DONE) return false;
// Reset() calls sqlite3_backup_finish() so that error information
// will be made available through the destination connection.
m_handle.Reset();
m_destination->ThrowLastError();
//throw Exception(result);
}
};
template <typename T>
struct Reader
{
int GetInt(int const column = 0) const noexcept
{
return sqlite3_column_int(static_cast<T const *>(this)->GetAbi(), column);
}
char const * GetString(int const column = 0) const noexcept
{
return reinterpret_cast<char const *>(sqlite3_column_text(static_cast<T const *>(this)->GetAbi(), column));
}
wchar_t const * GetWideString(int const column = 0) const noexcept
{
return static_cast<wchar_t const *>(sqlite3_column_text16(static_cast<T const *>(this)->GetAbi(), column));
}
int GetStringLength(int const column = 0) const noexcept
{
return sqlite3_column_bytes(static_cast<T const *>(this)->GetAbi(), column);
}
int GetWideStringLength(int const column = 0) const noexcept
{
return sqlite3_column_bytes16(static_cast<T const *>(this)->GetAbi(), column) / sizeof(wchar_t);
}
Type GetType(int const column = 0) const noexcept
{
return static_cast<Type>(sqlite3_column_type(static_cast<T const *>(this)->GetAbi(), column));
}
};
class Statement : public Reader<Statement>
{
struct StatementHandleTraits : HandleTraits<sqlite3_stmt *>
{
static void Close(Type value) noexcept
{
sqlite3_finalize(value);
}
};
using StatementHandle = Handle<StatementHandleTraits>;
StatementHandle m_handle;
template <typename F, typename C, typename ... Values>
void InternalPrepare(Connection const & connection, F prepare, C const * const text, Values && ... values)
{
ASSERT(connection);
if (SQLITE_OK != prepare(connection.GetAbi(), text, -1, m_handle.Set(), nullptr))
{
connection.ThrowLastError();
}
BindAll(std::forward<Values>(values) ...);
}
void InternalBind(int) const noexcept
{}
template <typename First, typename ... Rest>
void InternalBind(int const index, First && first, Rest && ... rest) const
{
Bind(index, std::forward<First>(first));
InternalBind(index + 1, std::forward<Rest>(rest) ...);
}
public:
Statement() noexcept = default;
template <typename C, typename ... Values>
Statement(Connection const & connection, C const * const text, Values && ... values)
{
Prepare(connection, text, std::forward<Values>(values) ...);
}
explicit operator bool() const noexcept
{
return static_cast<bool>(m_handle);
}
sqlite3_stmt * GetAbi() const noexcept
{
return m_handle.Get();
}
__declspec(noreturn) void ThrowLastError() const
{
throw Exception(sqlite3_db_handle(GetAbi()));
}
template <typename ... Values>
void Prepare(Connection const & connection, char const * const text, Values && ... values)
{
InternalPrepare(connection, sqlite3_prepare_v2, text, std::forward<Values>(values) ...);
}
template <typename ... Values>
void Prepare(Connection const & connection, wchar_t const * const text, Values && ... values)
{
InternalPrepare(connection, sqlite3_prepare16_v2, text, std::forward<Values>(values) ...);
}
bool Step() const
{
int const result = sqlite3_step(GetAbi());
if (result == SQLITE_ROW) return true;
if (result == SQLITE_DONE) return false;
ThrowLastError();
}
void Execute() const
{
VERIFY(!Step());
}
void Reset() const
{
if (SQLITE_OK != sqlite3_reset(GetAbi()))
{
ThrowLastError();
}
}
void Bind(int const index, int const value) const
{
if (SQLITE_OK != sqlite3_bind_int(GetAbi(), index, value))
{
ThrowLastError();
}
}
void Bind(int const index, double const value) const
{
if (SQLITE_OK != sqlite3_bind_double(GetAbi(), index, value))
{
ThrowLastError();
}
}
void Bind(int const index, char const * const value, int const size = -1) const
{
if (SQLITE_OK != sqlite3_bind_text(GetAbi(), index, value, size, SQLITE_STATIC))
{
ThrowLastError();
}
}
void Bind(int const index, wchar_t const * const value, int const size = -1) const
{
if (SQLITE_OK != sqlite3_bind_text16(GetAbi(), index, value, size, SQLITE_STATIC))
{
ThrowLastError();
}
}
void Bind(int const index, std::string const & value) const
{
Bind(index, value.c_str(), value.size());
}
void Bind(int const index, std::wstring const & value) const
{
Bind(index, value.c_str(), value.size() * sizeof(wchar_t));
}
void Bind(int const index, std::string && value) const
{
if (SQLITE_OK != sqlite3_bind_text(GetAbi(), index, value.c_str(), value.size(), SQLITE_TRANSIENT))
{
ThrowLastError();
}
}
void Bind(int const index, std::wstring && value) const
{
if (SQLITE_OK != sqlite3_bind_text16(GetAbi(), index, value.c_str(), value.size() * sizeof(wchar_t), SQLITE_TRANSIENT))
{
ThrowLastError();
}
}
template <typename ... Values>
void BindAll(Values && ... values)
{
InternalBind(1, std::forward<Values>(values) ...);
}
};
class Row : public Reader<Row>
{
sqlite3_stmt * m_statement = nullptr;
public:
sqlite3_stmt * GetAbi() const noexcept
{
return m_statement;
}
Row(sqlite3_stmt * const statement) noexcept :
m_statement(statement)
{}
};
class RowIterator
{
Statement const * m_statement = nullptr;
public:
RowIterator() noexcept = default;
RowIterator(Statement const & statement) noexcept
{
if (statement.Step())
{
m_statement = &statement;
}
}
RowIterator & operator++() noexcept
{
if (!m_statement->Step())
{
m_statement = nullptr;
}
return *this;
}
bool operator !=(RowIterator const & other) const noexcept
{
return m_statement != other.m_statement;
}
Row operator *() const noexcept
{
return Row(m_statement->GetAbi());
}
};
inline RowIterator begin(Statement const & statement) noexcept
{
return RowIterator(statement);
}
inline RowIterator end(Statement const &) noexcept
{
return RowIterator();
}
template <typename C, typename ... Values>
void Execute(Connection const & connection, C const * const text, Values && ... values)
{
Statement(connection, text, std::forward<Values>(values) ...).Execute();
}