-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconnection.cpp
828 lines (698 loc) · 21.4 KB
/
connection.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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
// (c) 2013 Stephan Hohe
#include "connection.hpp"
#include "sqxx.hpp"
#include "error.hpp"
#include <sqlite3.h>
#include <cstring>
namespace sqxx {
recent_error::recent_error(sqlite3 *handle) : error(sqlite3_errcode(handle), sqlite3_errmsg(handle)) {
}
struct collation_data_t {
connection &conn;
connection::collation_handler_t fn;
collation_data_t(connection &conn_arg, const connection::collation_handler_t &fn_arg)
: conn(conn_arg), fn(fn_arg) {
}
};
namespace detail {
class connection_callback_table {
public:
std::unique_ptr<connection::commit_handler_t> commit_handler;
std::unique_ptr<connection::rollback_handler_t> rollback_handler;
std::unique_ptr<connection::update_handler_t> update_handler;
std::unique_ptr<connection::trace_handler_t> trace_handler;
std::unique_ptr<connection::profile_handler_t> profile_handler;
std::unique_ptr<connection::authorize_handler_t> authorize_handler;
std::unique_ptr<connection::busy_handler_t> busy_handler;
std::unique_ptr<connection::progress_handler_t> progress_handler;
std::unique_ptr<connection::wal_handler_t> wal_handler;
std::unique_ptr<collation_data_t> collation_data;
};
void create_function_register(sqlite3 *handle, const char *name, int argc,
void *data, sqxx_function_call_type *fun, sqxx_appdata_destroy_type *destroy) {
int rv = sqlite3_create_function_v2(handle, name, argc, SQLITE_UTF8, data,
fun, nullptr, nullptr, destroy);
if (rv != SQLITE_OK) {
// Registered destructor is called automatically, no need to delete |adat| ourselves
throw static_error(rv);
}
}
void create_aggregate_register(sqlite3 *handle, const char *name, int nargs, void *adat,
sqxx_aggregate_step_type *stepfun, sqxx_aggregate_final_type *finalfun,
sqxx_appdata_destroy_type *destroy) {
int rv = sqlite3_create_function_v2(handle, name, nargs, SQLITE_UTF8, adat,
nullptr, stepfun, finalfun, destroy);
if (rv != SQLITE_OK) {
// Registered destructor is called automatically, no need to delete |adat| ourselves
throw static_error(rv);
}
}
void create_collation_register(sqlite3 *handle, const char *name, void *data,
sqxx_collation_compare_type *comparefun, sqxx_appdata_destroy_type *destroy) {
int rv = sqlite3_create_collation_v2(handle, name, SQLITE_UTF8, data, comparefun, destroy);
if (rv != SQLITE_OK) {
if (destroy)
destroy(data);
throw static_error(rv);
}
}
} // namespace detail
// ---------------------------------------------------------------------------
// connection
connection::connection() : handle(nullptr) {
}
connection::connection(const char *filename, int flags) : handle(nullptr) {
open(filename, flags);
}
connection:: connection(const std::string &filename, int flags) : handle(nullptr) {
open(filename, flags);
}
connection::~connection() noexcept {
if (handle) {
close();
}
}
const char* connection::filename(const char *db) const {
return sqlite3_db_filename(handle, db);
}
const char* connection::filename(const std::string &db) const {
return filename(db.c_str());
}
void connection::config_lookaside(void *buf, int slotsize, int slotcount) {
int rv = sqlite3_db_config(handle, SQLITE_DBCONFIG_LOOKASIDE, buf, slotsize, slotcount);
if (rv != SQLITE_OK)
throw static_error(rv);
}
bool connection::config_enable_fkey(int fk) {
int state;
int rv = sqlite3_db_config(handle, SQLITE_DBCONFIG_ENABLE_FKEY, fk, &state);
if (rv != SQLITE_OK)
throw static_error(rv);
return state;
}
bool connection::config_enable_trigger(int trig) {
int state;
int rv = sqlite3_db_config(handle, SQLITE_DBCONFIG_ENABLE_TRIGGER, trig, &state);
if (rv != SQLITE_OK)
throw static_error(rv);
return state;
}
bool connection::readonly(const char *dbname) const {
int rw = sqlite3_db_readonly(handle, dbname);
if (rw == -1)
throw error(SQLITE_ERROR, "no such database");
return bool(rw);
}
bool connection::autocommit() const {
return sqlite3_get_autocommit(handle);
}
uint64_t connection::last_insert_rowid() const {
return sqlite3_last_insert_rowid(handle);
}
counter connection::status(int op, bool reset) {
int rv;
tcounter<int> result;
rv = sqlite3_db_status(handle, op, &result.current, &result.highwater, static_cast<int>(reset));
if (rv != SQLITE_OK)
throw static_error(rv);
return result;
}
counter connection::status_lookaside_used(bool reset) {
return status(SQLITE_DBSTATUS_LOOKASIDE_USED, reset);
}
counter connection::status_lookaside_hit(bool reset) {
return status(SQLITE_DBSTATUS_LOOKASIDE_HIT, reset);
}
counter connection::status_lookaside_miss_size(bool reset) {
return status(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, reset);
}
counter connection::status_lookaside_miss_full(bool reset) {
return status(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, reset);
}
counter connection::status_cache_used(bool reset) {
return status(SQLITE_DBSTATUS_CACHE_USED, reset);
}
#if SQLITE_VERSION_NUMBER >= 3014000
counter connection::status_cache_used_shared(bool reset) {
return status(SQLITE_DBSTATUS_CACHE_USED_SHARED, reset);
}
#endif
counter connection::status_schema_used(bool reset) {
return status(SQLITE_DBSTATUS_SCHEMA_USED, reset);
}
counter connection::status_stmt_used(bool reset) {
return status(SQLITE_DBSTATUS_STMT_USED, reset);
}
counter connection::status_cache_hit(bool reset) {
return status(SQLITE_DBSTATUS_CACHE_HIT, reset);
}
counter connection::status_cache_miss(bool reset) {
return status(SQLITE_DBSTATUS_CACHE_MISS, reset);
}
counter connection::status_cache_write(bool reset) {
return status(SQLITE_DBSTATUS_CACHE_WRITE, reset);
}
#if SQLITE_VERSION_NUMBER >= 3008000
counter connection::status_deferred_fks(bool reset) {
return status(SQLITE_DBSTATUS_DEFERRED_FKS, reset);
}
#endif
column_metadata connection::metadata(const char *db, const char *table, const char *column) const {
int rv;
int notnull, primarykey, autoinc;
column_metadata md;
rv = sqlite3_table_column_metadata(handle, db, table, column, &md.datatype, &md.collseq, ¬null, &primarykey, &autoinc);
if (rv != SQLITE_OK)
throw recent_error(handle);
md.notnull = notnull;
md.primarykey = primarykey;
md.autoinc = autoinc;
return md;
}
int connection::total_changes() const {
return sqlite3_total_changes(handle);
}
void connection::open(const char *filename, int flags) {
int rv;
if (!flags)
flags = OPEN_READWRITE;
rv = sqlite3_open_v2(filename, &handle, flags, nullptr);
if (rv != SQLITE_OK) {
if (handle)
close();
throw static_error(rv);
}
}
void connection::open(const std::string &filename, int flags) {
open(filename.c_str(), flags);
}
void connection::close_sync() {
int rv;
rv = sqlite3_close(handle);
if (rv != SQLITE_OK)
throw static_error(rv);
handle = nullptr;
}
void connection::close() noexcept {
#if (SQLITE_VERSION_NUMBER >= 3007014)
sqlite3_close_v2(handle);
#else
try {
close_sync();
}
catch (const error &e) {
std::cerr << "error on sqlite3_close(): " << e.what() << std::endl;
// Nothing we can do
}
#endif
handle = nullptr;
}
/*
void connection::create_str_collation(const char *name, const collation_str_function_t &coll) {
// TODO: in the lambda, coll is a copy of the function or only a copy of the reference?
create_collation(name, collation_function_t([coll](int llen, const char *lstr, int rlen, const char *rstr) -> int {
return coll(std::string(lstr, llen), std::string(rstr, rlen));
}));
}
*/
struct exec_context {
const connection::exec_handler_t &fun;
std::exception_ptr ex;
exec_context(const connection::exec_handler_t &fun_arg) : fun(fun_arg), ex() {
}
};
int sqxx_call_exec_handler(void *data, int colcount, char **values, char **columns) {
exec_context *ctx = reinterpret_cast<exec_context*>(data);
try {
return ctx->fun(colcount, values, columns);
}
catch (...) {
ctx->ex = std::current_exception();
return 1;
}
}
void connection::exec(const char *sql) {
char *errmsg = nullptr;
int rv = sqlite3_exec(handle, sql, nullptr, nullptr, &errmsg);
if (rv != SQLITE_OK) {
auto ex = error(rv, errmsg);
sqlite3_free(errmsg);
throw ex;
}
}
void connection::exec(const std::string &sql) {
exec(sql.c_str());
}
void connection::exec(const char *sql, const exec_handler_t &fun) {
if (!fun) {
// empty callback function
exec(sql);
return;
}
exec_context ctx(fun);
char *errmsg = nullptr;
int rv = sqlite3_exec(handle, sql, sqxx_call_exec_handler, &ctx, &errmsg);
if (ctx.ex) {
std::rethrow_exception(ctx.ex);
}
if (rv != SQLITE_OK) {
auto ex = error(rv, errmsg);
sqlite3_free(errmsg);
throw ex;
}
}
void connection::exec(const std::string &sql, const exec_handler_t &fun) {
exec(sql.c_str(), fun);
}
statement connection::query(const char *sql) {
statement st = prepare(sql);
st.run();
return st;
}
statement connection::query(const std::string &sql) {
return query(sql.c_str());
}
statement connection::run(const char *sql) {
return query(sql);
}
statement connection::run(const std::string &sql) {
return query(sql);
}
void connection::remove_collation(const char *name) {
int rv = sqlite3_create_collation_v2(handle, name, SQLITE_UTF8,
nullptr, nullptr, nullptr);
if (rv != SQLITE_OK) {
throw static_error(rv);
}
}
void connection::remove_collation(const std::string &name) {
remove_collation(name.c_str());
}
statement connection::prepare(const char *sql) {
int rv;
sqlite3_stmt *stmt = nullptr;
rv = sqlite3_prepare_v2(handle, sql, std::strlen(sql)+1, &stmt, nullptr);
if (rv != SQLITE_OK) {
throw static_error(rv);
}
return statement(*this, stmt);
}
statement connection::prepare(const std::string &sql) {
return prepare(sql.c_str());
}
void connection::interrupt() {
sqlite3_interrupt(handle);
}
int connection::limit(int id, int value) {
return sqlite3_limit(handle, id, value);
}
int connection::limit_length(int value) {
return limit(SQLITE_LIMIT_LENGTH, value);
}
int connection::limit_sql_length(int value) {
return limit(SQLITE_LIMIT_SQL_LENGTH, value);
}
int connection::limit_column(int value) {
return limit(SQLITE_LIMIT_COLUMN, value);
}
int connection::limit_expr_depth(int value) {
return limit(SQLITE_LIMIT_EXPR_DEPTH, value);
}
int connection::limit_compound_select(int value) {
return limit(SQLITE_LIMIT_COMPOUND_SELECT, value);
}
int connection::limit_vdbe_op(int value) {
return limit(SQLITE_LIMIT_VDBE_OP, value);
}
int connection::limit_function_arg(int value) {
return limit(SQLITE_LIMIT_FUNCTION_ARG, value);
}
int connection::limit_attached(int value) {
return limit(SQLITE_LIMIT_ATTACHED, value);
}
int connection::limit_like_pattern_length(int value) {
return limit(SQLITE_LIMIT_LIKE_PATTERN_LENGTH, value);
}
int connection::limit_variable_number(int value) {
return limit(SQLITE_LIMIT_VARIABLE_NUMBER, value);
}
int connection::limit_trigger_depth(int value) {
return limit(SQLITE_LIMIT_TRIGGER_DEPTH, value);
}
#if SQLITE_VERSION_NUMBER >= 3008007
int connection::limit_worker_threads(int value) {
return limit(SQLITE_LIMIT_WORKER_THREADS, value);
}
#endif
void connection::release_memory() {
sqlite3_db_release_memory(handle);
}
void connection::setup_callbacks() {
if (!callbacks)
callbacks.reset(new detail::connection_callback_table);
}
int connection::changes() const {
return sqlite3_changes(handle);
}
extern "C"
int sqxx_call_commit_handler(void *data) {
connection::commit_handler_t *fn = reinterpret_cast<connection::commit_handler_t*>(data);
try {
return (*fn)();
}
catch (...) {
handle_callback_exception("commit handler");
return 1;
}
}
void connection::set_commit_handler(const commit_handler_t &fun) {
if (fun) {
std::unique_ptr<commit_handler_t> cb(new commit_handler_t(fun));
sqlite3_commit_hook(handle, sqxx_call_commit_handler, cb.get());
setup_callbacks();
callbacks->commit_handler = std::move(cb);
}
else {
set_commit_handler();
}
}
void connection::set_commit_handler() {
sqlite3_commit_hook(handle, nullptr, nullptr);
if (callbacks)
callbacks->commit_handler.reset();
}
extern "C"
void sqxx_call_rollback_handler(void *data) {
connection::rollback_handler_t *fn = reinterpret_cast<connection::rollback_handler_t*>(data);
try {
(*fn)();
}
catch (...) {
handle_callback_exception("rollback handler");
}
}
void connection::set_rollback_handler(const rollback_handler_t &fun) {
if (fun) {
std::unique_ptr<rollback_handler_t> cb(new rollback_handler_t(fun));
sqlite3_rollback_hook(handle, sqxx_call_rollback_handler, cb.get());
setup_callbacks();
callbacks->rollback_handler = std::move(cb);
}
else {
set_rollback_handler();
}
}
void connection::set_rollback_handler() {
sqlite3_rollback_hook(handle, nullptr, nullptr);
if (callbacks)
callbacks->rollback_handler.reset();
}
extern "C"
void sqxx_call_update_handler(void *data, int op, char const *database_name, const char *table_name, sqlite3_int64 rowid) {
connection::update_handler_t *fn = reinterpret_cast<connection::update_handler_t*>(data);
try {
(*fn)(op, database_name, table_name, rowid);
}
catch (...) {
handle_callback_exception("update handler");
}
}
void connection::set_update_handler(const update_handler_t &fun) {
if (fun) {
std::unique_ptr<update_handler_t> cb(new update_handler_t(fun));
sqlite3_update_hook(handle, sqxx_call_update_handler, cb.get());
setup_callbacks();
callbacks->update_handler = std::move(cb);
}
else {
set_update_handler();
}
}
void connection::set_update_handler() {
sqlite3_update_hook(handle, nullptr, nullptr);
if (callbacks)
callbacks->update_handler.reset();
}
extern "C"
void sqxx_call_trace_handler(void *data, const char* sql) {
connection::trace_handler_t *fn = reinterpret_cast<connection::trace_handler_t*>(data);
try {
(*fn)(sql);
}
catch (...) {
handle_callback_exception("trace handler");
}
}
void connection::set_trace_handler(const trace_handler_t &fun) {
if (fun) {
std::unique_ptr<trace_handler_t> cb(new trace_handler_t(fun));
sqlite3_trace(handle, sqxx_call_trace_handler, cb.get());
setup_callbacks();
callbacks->trace_handler = std::move(cb);
}
else {
set_trace_handler();
}
}
void connection::set_trace_handler() {
sqlite3_trace(handle, nullptr, nullptr);
if (callbacks)
callbacks->trace_handler.reset();
}
extern "C"
void sqxx_call_profile_handler(void *data, const char* sql, sqlite_uint64 nsec) {
connection::profile_handler_t *fn = reinterpret_cast<connection::profile_handler_t*>(data);
try {
(*fn)(sql, static_cast<uint64_t>(nsec));
}
catch (...) {
handle_callback_exception("profile handler");
}
}
void connection::set_profile_handler(const profile_handler_t &fun) {
if (fun) {
std::unique_ptr<profile_handler_t> cb(new profile_handler_t(fun));
sqlite3_profile(handle, sqxx_call_profile_handler, cb.get());
setup_callbacks();
callbacks->profile_handler = std::move(cb);
}
else {
set_profile_handler();
}
}
void connection::set_profile_handler() {
sqlite3_profile(handle, nullptr, nullptr);
if (callbacks)
callbacks->profile_handler.reset();
}
extern "C"
int sqxx_call_authorize_handler(void *data, int action, const char* d1, const char *d2, const char *d3, const char *d4) {
connection::authorize_handler_t *fn = reinterpret_cast<connection::authorize_handler_t*>(data);
try {
return (*fn)(action, d1, d2, d3, d4);
}
catch (...) {
handle_callback_exception("authorize handler");
return SQLITE_IGNORE;
}
}
void connection::set_authorize_handler(const authorize_handler_t &fun) {
if (fun) {
std::unique_ptr<authorize_handler_t> cb(new authorize_handler_t(fun));
sqlite3_set_authorizer(handle, sqxx_call_authorize_handler, cb.get());
setup_callbacks();
callbacks->authorize_handler = std::move(cb);
}
else {
set_authorize_handler();
}
}
void connection::set_authorize_handler() {
sqlite3_set_authorizer(handle, nullptr, nullptr);
if (callbacks)
callbacks->authorize_handler.reset();
}
extern "C"
int sqxx_call_busy_handler(void *data, int count) {
connection::busy_handler_t *fn = reinterpret_cast<connection::busy_handler_t*>(data);
try {
return (*fn)(count);
}
catch (...) {
handle_callback_exception("busy handler");
return 0;
}
}
void connection::set_busy_handler(const busy_handler_t &fun) {
if (fun) {
std::unique_ptr<busy_handler_t> cb(new busy_handler_t(fun));
sqlite3_busy_handler(handle, sqxx_call_busy_handler, cb.get());
setup_callbacks();
callbacks->busy_handler = std::move(cb);
}
else {
set_busy_handler();
}
}
void connection::set_busy_handler() {
sqlite3_busy_handler(handle, nullptr, nullptr);
if (callbacks)
callbacks->busy_handler.reset();
}
void connection::busy_timeout(int ms) {
int rv = sqlite3_busy_timeout(handle, ms);
if (rv != SQLITE_OK)
throw static_error(rv);
if (callbacks)
callbacks->busy_handler.reset();
}
extern "C"
int sqxx_call_progress_handler(void *data) {
connection::progress_handler_t *fn = reinterpret_cast<connection::progress_handler_t*>(data);
try {
return (*fn)();
}
catch (...) {
handle_callback_exception("progress handler");
return 0;
}
}
void connection::set_progress_handler(int vinst, const progress_handler_t &fun) {
if (fun) {
std::unique_ptr<progress_handler_t> cb(new progress_handler_t(fun));
sqlite3_progress_handler(handle, vinst, sqxx_call_progress_handler, cb.get());
setup_callbacks();
callbacks->progress_handler = std::move(cb);
}
else {
set_progress_handler();
}
}
void connection::set_progress_handler() {
sqlite3_progress_handler(handle, 0, nullptr, nullptr);
if (callbacks)
callbacks->progress_handler.reset();
}
extern "C"
int sqxx_call_wal_handler(void *data, sqlite3* conn, const char *dbname, int pages) {
// We registered on a certain connection, and thats the connection we
// expect to get back here. So `conn` seems to be pretty useless here.
unused(conn);
connection::wal_handler_t *fn = reinterpret_cast<connection::wal_handler_t*>(data);
try {
(*fn)(dbname, pages);
return SQLITE_OK;
}
catch (...) {
// TODO: Better exception handling?
handle_callback_exception("wal handler");
return SQLITE_ERROR;
}
}
void connection::set_wal_handler(const wal_handler_t &fun) {
if (fun) {
std::unique_ptr<wal_handler_t> cb(new wal_handler_t(fun));
sqlite3_wal_hook(handle, sqxx_call_wal_handler, cb.get());
setup_callbacks();
callbacks->wal_handler = std::move(cb);
}
else {
set_wal_handler();
}
}
void connection::set_wal_handler() {
sqlite3_wal_hook(handle, nullptr, nullptr);
if (callbacks)
callbacks->wal_handler.reset();
}
void connection::wal_autocheckpoint(int frames) {
int rv = sqlite3_wal_autocheckpoint(handle, frames);
if (rv != SQLITE_OK)
throw static_error(rv);
if (callbacks)
callbacks->wal_handler.reset();
}
std::pair<int, int> connection::wal_checkpoint(const char *dbname, int emode) {
int log, ckpt;
int rv = sqlite3_wal_checkpoint_v2(handle, dbname, emode, &log, &ckpt);
if (rv != SQLITE_OK)
throw static_error(rv);
return std::make_pair(log, ckpt);
}
std::pair<int, int> connection::wal_checkpoint_passive(const char *dbname) {
return wal_checkpoint(dbname, SQLITE_CHECKPOINT_PASSIVE);
}
std::pair<int, int> connection::wal_checkpoint_passive(const std::string &dbname) {
return wal_checkpoint_passive(dbname.c_str());
}
std::pair<int, int> connection::wal_checkpoint_full(const char *dbname) {
return wal_checkpoint(dbname, SQLITE_CHECKPOINT_FULL);
}
std::pair<int, int> connection::wal_checkpoint_full(const std::string &dbname) {
return wal_checkpoint_full(dbname.c_str());
}
std::pair<int, int> connection::wal_checkpoint_restart(const char *dbname) {
return wal_checkpoint(dbname, SQLITE_CHECKPOINT_RESTART);
}
std::pair<int, int> connection::wal_checkpoint_restart(const std::string &dbname) {
return wal_checkpoint_restart(dbname.c_str());
}
extern "C"
void sqxx_call_collation_handler(void *data, sqlite3* /*conn*/, int textrep, const char *name) {
// We registered on a certain connection, and thats the connection we
// expect to get back here. So `conn` seems to be pretty useless here.
//
// We store a reference to the connection in collation_data_t and then
// could check here if you got the right connection back. But this seems
// unnecessary since we can assume we get the correct thing back.
// We could also *not* store the connection in collation_data_t and
// instead use the connection we get passed here. We would need to create
// a non-owning collection object for that to work properly.
collation_data_t *dat = reinterpret_cast<collation_data_t*>(data);
try {
// TODO:
//if (textrep != SQLITE_UTF8) {
// throw error("non-utf8 collation requested");
//}
// We pass the connection here because the callback will need
// it to register the requested collation function
(dat->fn)(dat->conn, name);
}
catch (...) {
handle_callback_exception("collation handler");
}
}
void connection::set_collation_handler(const collation_handler_t &fun) {
if (fun) {
std::unique_ptr<collation_data_t> d(new collation_data_t(*this, fun));
sqlite3_collation_needed(handle, d.get(), sqxx_call_collation_handler);
setup_callbacks();
callbacks->collation_data = std::move(d);
}
else {
set_collation_handler();
}
}
void connection::set_collation_handler() {
sqlite3_collation_needed(handle, nullptr, nullptr);
if (callbacks)
callbacks->collation_data.reset();
}
void connection::remove_function(const char *name, int argc) {
int rv = sqlite3_create_function_v2(handle, name, argc, SQLITE_UTF8, nullptr,
nullptr, nullptr, nullptr, nullptr);
if (rv != SQLITE_OK) {
throw static_error(rv);
}
}
void connection::remove_function(const std::string &name, int argc) {
remove_function(name.c_str(), argc);
}
void connection::remove_aggregate(const char *name, int argc) {
// Aggregates are really just functions for sqlite
remove_function(name, argc);
}
void connection::remove_aggregate(const std::string &name, int argc) {
remove_aggregate(name.c_str(), argc);
}
} // namespace sqxx