forked from iwongu/sqlite3pp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3pp.h
/
sqlite3pp.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
// sqlite3pp.h
//
// The MIT License
//
// Copyright (c) 2015 Wongoo Lee (iwongu at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// David Maisonave -- Nov-2021 Added UNICODE API's
#ifndef SQLITE3PP_H
#define SQLITE3PP_H
#define SQLITE3PP_VERSION "1.0.8"
#define SQLITE3PP_VERSION_MAJOR 1
#define SQLITE3PP_VERSION_MINOR 0
#define SQLITE3PP_VERSION_PATCH 8
#include <functional>
#include <iterator>
#include <stdexcept>
#include <string>
#include <tuple>
#include <vector>
#include <ctime>
#include <memory>
#include "sqlite3.h"
namespace sqlite3pp
{
#if defined(SQLITE3PP_NO_UNICODE) || !defined(_UNICODE)
using tstring = std::string;
#else
using tstring = std::wstring;
#endif // SQLITE3PP_NO_UNICODE || !_UNICODE
#ifndef SQLITE3PP_CONVERT_TO_RESULTING_AFFINITY
// SQLite3 types (Excluding string types)
using Blob = std::shared_ptr<std::vector<unsigned char> >;// Stores binary data
using Clob = std::shared_ptr< std::vector<char> >; // Stores strings that can have multiple NULL terminators
struct Date {std::time_t t;};
struct Datetime {std::tm tm_struct;};
using TEXT = tstring;
#endif //!SQLITE3PP_CONVERT_TO_RESULTING_AFFINITY
class database;
namespace ext
{
class function;
class aggregate;
database borrow(sqlite3* pdb);
}
template <class T>
struct convert {
using to_int = int;
};
class null_type {};
extern null_type ignore;
class noncopyable
{
protected:
noncopyable() = default;
~noncopyable() = default;
noncopyable(noncopyable&&) = default;
noncopyable& operator=(noncopyable&&) = default;
noncopyable(noncopyable const&) = delete;
noncopyable& operator=(noncopyable const&) = delete;
};
//struct sqlite3_api_routines;
class db_api_root
{
public:
db_api_root();
};
class database : public db_api_root, private noncopyable
{
friend class statement;
friend class database_error;
friend class ext::function;
friend class ext::aggregate;
friend database ext::borrow(sqlite3* pdb);
public:
using busy_handler = std::function<int (int)>;
using commit_handler = std::function<int ()>;
using rollback_handler = std::function<void ()>;
using update_handler = std::function<void (int, char const*, char const*, long long int)>;
using authorize_handler = std::function<int (int, char const*, char const*, char const*, char const*)>;
using backup_handler = std::function<void (int, int, int)>;
explicit database( char const* dbname = nullptr, int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, const char* vfs = nullptr );
database(database&& db);
database& operator=(database&& db);
~database();
#ifndef SQLITE3PP_NO_UNICODE
// Unicode support
explicit database( const wchar_t* dbname, int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, const wchar_t* vfs = nullptr );
int connect( const wchar_t* dbname, int flags, const wchar_t* vfs = nullptr );
int execute( const std::wstring& sql );
int execute( const std::string& sql );
int attach(const wchar_t* dbname, const wchar_t* name);
int detach(const wchar_t* name);
int backup(const wchar_t* dbname, database& destdb, const wchar_t* destdbname, backup_handler h, int step_page = 5);
#endif //!SQLITE3PP_NO_UNICODE
int connect( char const* dbname, int flags, const char* vfs = nullptr );
int disconnect();
int attach(char const* dbname, char const* name);
int detach(char const* name);
int backup(database& destdb, backup_handler h = {});
int backup(char const* dbname, database& destdb, char const* destdbname, backup_handler h, int step_page = 5);
long long int last_insert_rowid() const;
int enable_foreign_keys(bool enable = true);
int enable_triggers(bool enable = true);
int enable_extended_result_codes(bool enable = true);
int changes() const;
int error_code() const;
int extended_error_code() const;
char const* error_msg() const;
int execute( char const* sql );
int executef(char const* sql, ...);
int set_busy_timeout(int ms);
void set_busy_handler(busy_handler h);
void set_commit_handler(commit_handler h);
void set_rollback_handler(rollback_handler h);
void set_update_handler(update_handler h);
void set_authorize_handler(authorize_handler h);
private:
database(sqlite3* pdb);
private:
sqlite3* db_;
bool borrowing_;
busy_handler bh_;
commit_handler ch_;
rollback_handler rh_;
update_handler uh_;
authorize_handler ah_;
};
class database_error : public std::runtime_error
{
public:
explicit database_error(char const* msg);
explicit database_error(const std::string& msg);
explicit database_error(database& db);
};
enum copy_semantic { copy, nocopy };
class statement : public db_api_root, private noncopyable
{
public:
int prepare(char const* stmt);
int finish();
int bind(int idx, int value);
int bind(int idx, double value);
int bind(int idx, long long int value);
int bind(int idx, char const* value, copy_semantic fcopy);
int bind(int idx, void const* value, int n, copy_semantic fcopy);
int bind(int idx, std::string const& value, copy_semantic fcopy);
int bind(int idx);
int bind(int idx, null_type);
int bind(char const* name, int value);
int bind(char const* name, double value);
int bind(char const* name, long long int value);
int bind(char const* name, char const* value, copy_semantic fcopy);
int bind(char const* name, void const* value, int n, copy_semantic fcopy);
int bind(char const* name, std::string const& value, copy_semantic fcopy);
int bind(char const* name);
int bind(char const* name, null_type);
int step();
int reset();
protected:
explicit statement(database& db, char const* stmt = nullptr);
~statement();
int prepare_impl(char const* stmt);
int finish_impl(sqlite3_stmt* stmt);
protected:
database& db_;
sqlite3_stmt* stmt_;
char const* tail_;
#ifndef SQLITE3PP_NO_UNICODE
explicit statement( database& db, wchar_t const* stmt = nullptr );
int prepare_impl( wchar_t const* stmt);
public:
int prepare( wchar_t const* stmt);
#endif // !SQLITE3PP_NO_UNICODE
};
class command : public statement
{
public:
class bindstream
{
public:
bindstream(command& cmd, int idx);
template <class T>
bindstream& operator << (T value) {
auto rc = cmd_.bind(idx_, value);
if (rc != SQLITE_OK) {
throw database_error(cmd_.db_);
}
++idx_;
return *this;
}
bindstream& operator << (char const* value) {
auto rc = cmd_.bind(idx_, value, copy);
if (rc != SQLITE_OK) {
throw database_error(cmd_.db_);
}
++idx_;
return *this;
}
bindstream& operator << (std::string const& value) {
auto rc = cmd_.bind(idx_, value, copy);
if (rc != SQLITE_OK) {
throw database_error(cmd_.db_);
}
++idx_;
return *this;
}
private:
command& cmd_;
int idx_;
};
explicit command(database& db, char const* stmt = nullptr);
bindstream binder(int idx = 1);
int execute();
int execute_all();
};
class query : public statement
{
public:
class rows
{
public:
class getstream
{
public:
getstream(rows* rws, int idx);
template <class T>
getstream& operator >> (T& value) {
value = rws_->get(idx_, T());
++idx_;
return *this;
}
private:
rows* rws_;
int idx_;
};
explicit rows(sqlite3_stmt* stmt);
int data_count() const;
int column_type(int idx) const;
int column_bytes(int idx) const;
template <class T> T get(int idx) const {
return get(idx, T());
}
template <class... Ts>
std::tuple<Ts...> get_columns(typename convert<Ts>::to_int... idxs) const {
return std::make_tuple(get(idxs, Ts())...);
}
getstream getter(int idx = 0);
private:
int get(int idx, int) const;
double get(int idx, double) const;
long long int get(int idx, long long int) const;
char const* get(int idx, char const*) const;
//////////////////////////////////////////////////////////////////////////////////
// Start SQLite3pp_EZ changes
// To minimize changes in original SQLite3pp.cpp code, the implementation for this section is in SQLite3pp_EZ.cpp
#ifndef SQLITE3PP_CONVERT_TO_RESULTING_AFFINITY
#ifndef SQLITE3PP_NO_UNICODE
wchar_t const* get(int idx, wchar_t const*) const;
std::wstring get(int idx, const std::wstring&value) const;
#endif// !SQLITE3PP_NO_UNICODE
unsigned char get(int idx, const unsigned char&) const;
short int get(int idx, const short int&) const;
bool get(int idx, const bool&) const;
unsigned long long int get(int idx, const unsigned long long int&) const;
Blob get(int idx, const Blob&) const;
Clob get(int idx, const Clob&) const;
Date get(int idx, const Date&) const;
Datetime get(int idx, const Datetime&) const;
#endif// !SQLITE3PP_CONVERT_TO_RESULTING_AFFINITY
// End SQLite3pp_EZ changes
//////////////////////////////////////////////////////////////////////////////////
std::string get(int idx, std::string) const;
void const* get(int idx, void const*) const;
null_type get(int idx, null_type) const;
private:
sqlite3_stmt* stmt_;
};
class query_iterator
: public std::iterator<std::input_iterator_tag, rows>
{
public:
query_iterator();
explicit query_iterator(query* cmd);
bool operator==(query_iterator const&) const;
bool operator!=(query_iterator const&) const;
query_iterator& operator++();
//#pragma warning(disable : 4996)
query::rows operator*() const;
private:
query* cmd_;
int rc_;
};
explicit query( database& db, char const* stmt = nullptr );
#ifndef SQLITE3PP_NO_UNICODE
explicit query( database& db, wchar_t const* stmt );
#endif //!SQLITE3PP_NO_UNICODE
int column_count() const;
char const* column_name(int idx) const;
char const* column_decltype(int idx) const;
using iterator = query_iterator;
iterator begin();
iterator end();
};
class transaction : public db_api_root, private noncopyable
{
public:
explicit transaction(database& db, bool fcommit = false, bool freserve = false);
~transaction();
int commit();
int rollback();
private:
database* db_;
bool fcommit_;
};
} // namespace sqlite3pp
#endif