-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3pp.cpp
/
sqlite3pp.cpp
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// sqlite3pp.cpp
//
// 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 Updated (std::string query::rows::get) to gracefully handle NULL string
#include <cstring>
#include <memory>
#ifdef SQLITE3PP_LOADABLE_EXTENSION
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#endif
#include "sqlite3pp.h"
namespace sqlite3pp
{
null_type ignore;
namespace
{
int busy_handler_impl(void* p, int cnt)
{
auto h = static_cast<database::busy_handler*>(p);
return (*h)(cnt);
}
int commit_hook_impl(void* p)
{
auto h = static_cast<database::commit_handler*>(p);
return (*h)();
}
void rollback_hook_impl(void* p)
{
auto h = static_cast<database::rollback_handler*>(p);
(*h)();
}
void update_hook_impl(void* p, int opcode, char const* dbname, char const* tablename, long long int rowid)
{
auto h = static_cast<database::update_handler*>(p);
(*h)(opcode, dbname, tablename, rowid);
}
int authorizer_impl(void* p, int evcode, char const* p1, char const* p2, char const* dbname, char const* tvname)
{
auto h = static_cast<database::authorize_handler*>(p);
return (*h)(evcode, p1, p2, dbname, tvname);
}
} // namespace
database::database( char const* dbname, int flags, char const* vfs ) : db_( nullptr ), borrowing_( false )
{
if (dbname) {
auto rc = connect( dbname, flags, vfs );
if ( rc != SQLITE_OK )
throw database_error( "can't connect database" );
}
}
database::database(sqlite3* pdb) : db_(pdb), borrowing_(true)
{
}
database::database(database&& db) : db_(std::move(db.db_)),
borrowing_(std::move(db.borrowing_)),
bh_(std::move(db.bh_)),
ch_(std::move(db.ch_)),
rh_(std::move(db.rh_)),
uh_(std::move(db.uh_)),
ah_(std::move(db.ah_))
{
db.db_ = nullptr;
}
database& database::operator=(database&& db)
{
db_ = std::move(db.db_);
db.db_ = nullptr;
borrowing_ = std::move(db.borrowing_);
bh_ = std::move(db.bh_);
ch_ = std::move(db.ch_);
rh_ = std::move(db.rh_);
uh_ = std::move(db.uh_);
ah_ = std::move(db.ah_);
return *this;
}
database::~database()
{
if (!borrowing_) {
disconnect();
}
}
int database::connect(char const* dbname, int flags, char const* vfs)
{
if (!borrowing_) {
disconnect();
}
return sqlite3_open_v2( dbname, &db_, flags, vfs );
}
int database::disconnect()
{
auto rc = SQLITE_OK;
if (db_) {
rc = sqlite3_close(db_);
if (rc == SQLITE_OK) {
db_ = nullptr;
}
}
return rc;
}
int database::attach(char const* dbname, char const* name)
{
return executef("ATTACH '%q' AS '%q'", dbname, name);
}
int database::detach(char const* name)
{
return executef("DETACH '%q'", name);
}
int database::backup(database& destdb, backup_handler h)
{
return backup("main", destdb, "main", h);
}
int database::backup(char const* dbname, database& destdb, char const* destdbname, backup_handler h, int step_page)
{
sqlite3_backup* bkup = sqlite3_backup_init(destdb.db_, destdbname, db_, dbname);
if (!bkup) {
return error_code();
}
auto rc = SQLITE_OK;
do {
rc = sqlite3_backup_step(bkup, step_page);
if (h) {
h(sqlite3_backup_remaining(bkup), sqlite3_backup_pagecount(bkup), rc);
}
} while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
sqlite3_backup_finish(bkup);
return rc;
}
void database::set_busy_handler(busy_handler h)
{
bh_ = h;
sqlite3_busy_handler(db_, bh_ ? busy_handler_impl : 0, &bh_);
}
void database::set_commit_handler(commit_handler h)
{
ch_ = h;
sqlite3_commit_hook(db_, ch_ ? commit_hook_impl : 0, &ch_);
}
void database::set_rollback_handler(rollback_handler h)
{
rh_ = h;
sqlite3_rollback_hook(db_, rh_ ? rollback_hook_impl : 0, &rh_);
}
void database::set_update_handler(update_handler h)
{
uh_ = h;
sqlite3_update_hook(db_, uh_ ? update_hook_impl : 0, &uh_);
}
void database::set_authorize_handler(authorize_handler h)
{
ah_ = h;
sqlite3_set_authorizer(db_, ah_ ? authorizer_impl : 0, &ah_);
}
long long int database::last_insert_rowid() const
{
return sqlite3_last_insert_rowid(db_);
}
int database::enable_foreign_keys(bool enable)
{
return sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_FKEY, enable ? 1 : 0, nullptr);
}
int database::enable_triggers(bool enable)
{
return sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_TRIGGER, enable ? 1 : 0, nullptr);
}
int database::enable_extended_result_codes(bool enable)
{
return sqlite3_extended_result_codes(db_, enable ? 1 : 0);
}
int database::changes() const
{
return sqlite3_changes(db_);
}
int database::error_code() const
{
return sqlite3_errcode(db_);
}
int database::extended_error_code() const
{
return sqlite3_extended_errcode(db_);
}
char const* database::error_msg() const
{
return sqlite3_errmsg(db_);
}
int database::execute(char const* sql)
{
return sqlite3_exec(db_, sql, 0, 0, 0);
}
int database::executef(char const* sql, ...)
{
va_list ap;
va_start(ap, sql);
std::shared_ptr<char> msql(sqlite3_vmprintf(sql, ap), sqlite3_free);
va_end(ap);
return execute(msql.get());
}
int database::set_busy_timeout(int ms)
{
return sqlite3_busy_timeout(db_, ms);
}
statement::statement(database& db, char const* stmt) : db_(db), stmt_(0), tail_(0)
{
if (stmt) {
auto rc = prepare(stmt);
if (rc != SQLITE_OK)
throw database_error(db_);
}
}
statement::~statement()
{
// finish() can return error. If you want to check the error, call
// finish() explicitly before this object is destructed.
finish();
}
int statement::prepare(char const* stmt)
{
auto rc = finish();
if (rc != SQLITE_OK)
return rc;
return prepare_impl(stmt);
}
int statement::prepare_impl(char const* stmt)
{
return sqlite3_prepare_v2(db_.db_, stmt, static_cast<int>(std::strlen(stmt)), &stmt_, &tail_);
}
int statement::finish()
{
auto rc = SQLITE_OK;
if (stmt_) {
rc = finish_impl(stmt_);
stmt_ = nullptr;
}
tail_ = nullptr;
return rc;
}
int statement::finish_impl(sqlite3_stmt* stmt)
{
return sqlite3_finalize(stmt);
}
int statement::step()
{
return sqlite3_step(stmt_);
}
int statement::reset()
{
return sqlite3_reset(stmt_);
}
int statement::bind(int idx, int value)
{
return sqlite3_bind_int(stmt_, idx, value);
}
int statement::bind(int idx, double value)
{
return sqlite3_bind_double(stmt_, idx, value);
}
int statement::bind(int idx, long long int value)
{
return sqlite3_bind_int64(stmt_, idx, value);
}
int statement::bind(int idx, char const* value, copy_semantic fcopy)
{
return sqlite3_bind_text(stmt_, idx, value, static_cast<int>(std::strlen(value)), fcopy == copy ? SQLITE_TRANSIENT : SQLITE_STATIC );
}
int statement::bind(int idx, void const* value, int n, copy_semantic fcopy)
{
return sqlite3_bind_blob(stmt_, idx, value, n, fcopy == copy ? SQLITE_TRANSIENT : SQLITE_STATIC );
}
int statement::bind(int idx, std::string const& value, copy_semantic fcopy)
{
return sqlite3_bind_text(stmt_, idx, value.c_str(), static_cast<int>(value.size()), fcopy == copy ? SQLITE_TRANSIENT : SQLITE_STATIC );
}
int statement::bind(int idx)
{
return sqlite3_bind_null(stmt_, idx);
}
int statement::bind(int idx, null_type)
{
return bind(idx);
}
int statement::bind(char const* name, int value)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value);
}
int statement::bind(char const* name, double value)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value);
}
int statement::bind(char const* name, long long int value)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value);
}
int statement::bind(char const* name, char const* value, copy_semantic fcopy)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value, fcopy);
}
int statement::bind(char const* name, void const* value, int n, copy_semantic fcopy)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value, n, fcopy);
}
int statement::bind(char const* name, std::string const& value, copy_semantic fcopy)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value, fcopy);
}
int statement::bind(char const* name)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx);
}
int statement::bind(char const* name, null_type)
{
return bind(name);
}
command::bindstream::bindstream(command& cmd, int idx) : cmd_(cmd), idx_(idx)
{
}
command::command(database& db, char const* stmt) : statement(db, stmt)
{
}
command::bindstream command::binder(int idx)
{
return bindstream(*this, idx);
}
int command::execute()
{
auto rc = step();
if (rc == SQLITE_DONE) rc = SQLITE_OK;
return rc;
}
int command::execute_all()
{
auto rc = execute();
if (rc != SQLITE_OK) return rc;
char const* sql = tail_;
while (std::strlen(sql) > 0) { // sqlite3_complete() is broken.
sqlite3_stmt* old_stmt = stmt_;
if ((rc = prepare_impl(sql)) != SQLITE_OK) return rc;
if ((rc = sqlite3_transfer_bindings(old_stmt, stmt_)) != SQLITE_OK) return rc;
finish_impl(old_stmt);
if ((rc = execute()) != SQLITE_OK) return rc;
sql = tail_;
}
return rc;
}
query::rows::getstream::getstream(rows* rws, int idx) : rws_(rws), idx_(idx)
{
}
query::rows::rows(sqlite3_stmt* stmt) : stmt_(stmt)
{
}
int query::rows::data_count() const
{
return sqlite3_data_count(stmt_);
}
int query::rows::column_type(int idx) const
{
return sqlite3_column_type(stmt_, idx);
}
int query::rows::column_bytes(int idx) const
{
return sqlite3_column_bytes(stmt_, idx);
}
int query::rows::get(int idx, int) const
{
return sqlite3_column_int(stmt_, idx);
}
double query::rows::get(int idx, double) const
{
return sqlite3_column_double(stmt_, idx);
}
long long int query::rows::get(int idx, long long int) const
{
return sqlite3_column_int64(stmt_, idx);
}
char const* query::rows::get( int idx, char const* ) const
{
return reinterpret_cast<char const*>(sqlite3_column_text( stmt_, idx ));
}
std::string query::rows::get( int idx, std::string ) const
{
char const* Val = get( idx, (char const*)0 );
#ifndef SQLITE3PP_ALLOW_NULL_STRING_RETURN
if ( !Val )
return "";
#endif // !SQLITE3PP_ALLOW_NULL_STRING_RETURN
return Val;
}
void const* query::rows::get(int idx, void const*) const
{
return sqlite3_column_blob(stmt_, idx);
}
null_type query::rows::get(int /*idx*/, null_type) const
{
return ignore;
}
query::rows::getstream query::rows::getter(int idx)
{
return getstream(this, idx);
}
query::query_iterator::query_iterator() : cmd_(0)
{
rc_ = SQLITE_DONE;
}
query::query_iterator::query_iterator(query* cmd) : cmd_(cmd)
{
rc_ = cmd_->step();
if (rc_ != SQLITE_ROW && rc_ != SQLITE_DONE)
throw database_error(cmd_->db_);
}
bool query::query_iterator::operator==(query::query_iterator const& other) const
{
return rc_ == other.rc_;
}
bool query::query_iterator::operator!=(query::query_iterator const& other) const
{
return rc_ != other.rc_;
}
query::query_iterator& query::query_iterator::operator++()
{
rc_ = cmd_->step();
if (rc_ != SQLITE_ROW && rc_ != SQLITE_DONE)
throw database_error(cmd_->db_);
return *this;
}
query::rows query::query_iterator::operator*() const
{
return rows(cmd_->stmt_);
}
query::query( database& db, char const* stmt ) : statement( db, stmt )
{
}
int query::column_count() const
{
return sqlite3_column_count(stmt_);
}
char const* query::column_name(int idx) const
{
return sqlite3_column_name(stmt_, idx);
}
char const* query::column_decltype(int idx) const
{
return sqlite3_column_decltype(stmt_, idx);
}
query::iterator query::begin()
{
return query_iterator(this);
}
query::iterator query::end()
{
return query_iterator();
}
transaction::transaction(database& db, bool fcommit, bool freserve) : db_(&db), fcommit_(fcommit)
{
int rc = db_->execute(freserve ? "BEGIN IMMEDIATE" : "BEGIN");
if (rc != SQLITE_OK)
throw database_error(*db_);
}
transaction::~transaction()
{
if (db_) {
// execute() can return error. If you want to check the error,
// call commit() or rollback() explicitly before this object is
// destructed.
db_->execute(fcommit_ ? "COMMIT" : "ROLLBACK");
}
}
int transaction::commit()
{
auto db = db_;
db_ = nullptr;
int rc = db->execute("COMMIT");
return rc;
}
int transaction::rollback()
{
auto db = db_;
db_ = nullptr;
int rc = db->execute("ROLLBACK");
return rc;
}
database_error::database_error(char const* msg) : std::runtime_error(msg)
{
}
database_error::database_error(database& db) : std::runtime_error(sqlite3_errmsg(db.db_))
{
}
} // namespace sqlite3pp