-
Notifications
You must be signed in to change notification settings - Fork 20
/
config.cpp
193 lines (162 loc) · 4.58 KB
/
config.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
#include "config.hpp"
#include "error.hpp"
#include <memory>
#include <sqlite3.h>
namespace sqxx {
namespace detail {
struct config_callback_table {
std::unique_ptr<log_handler_t> log_handler;
std::unique_ptr<sqllog_handler_t> sqllog_handler;
};
}
detail::config_callback_table config_callbacks;
void config_singlethread() {
int rv = sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_multithread() {
int rv = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_serialized() {
int rv = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_malloc(mem_methods*);
void config_getmalloc(mem_methods*);
void config_memstatus(bool enable) {
int rv = sqlite3_config(SQLITE_CONFIG_MEMSTATUS, enable);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_scratch(void *buf, int sz, int n) {
int rv = sqlite3_config(SQLITE_CONFIG_SCRATCH, buf, sz, n);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_pagecache(void *buf, int sz, int n) {
int rv = sqlite3_config(SQLITE_CONFIG_PAGECACHE, buf, sz, n);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_heap(void *buf, int bytes, int minalloc) {
int rv = sqlite3_config(SQLITE_CONFIG_HEAP, buf, bytes, minalloc);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_mutex(mutex_methods*);
void config_getmutex(mutex_methods*);
void config_lookaside(int sz, int n) {
int rv = sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_pcache2(pcache_methods2*);
void config_getpcache2(pcache_methods2*);
extern "C"
void sqxx_call_config_log_handler(void *data, int err, const char *msg) {
log_handler_t *fn = reinterpret_cast<log_handler_t*>(data);
try {
//(*fn)(static_error(err), msg);
(*fn)(err, msg);
}
catch (...) {
handle_callback_exception("log handler");
}
}
void config_log(const log_handler_t &fun) {
if (fun) {
std::unique_ptr<log_handler_t> cb = std::make_unique<log_handler_t>(fun);
int rv = sqlite3_config(SQLITE_CONFIG_LOG, sqxx_call_config_log_handler, cb.get());
if (rv != SQLITE_OK)
throw static_error(rv);
config_callbacks.log_handler = std::move(cb);
}
else {
config_log();
}
}
void config_log() {
int rv = sqlite3_config(SQLITE_CONFIG_LOG, nullptr, nullptr);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_uri(bool enable) {
int rv = sqlite3_config(SQLITE_CONFIG_URI, enable);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_covering_index_scan(bool enable) {
int rv = sqlite3_config(SQLITE_CONFIG_COVERING_INDEX_SCAN, enable);
if (rv != SQLITE_OK)
throw static_error(rv);
}
extern "C"
void sqxx_call_config_sqllog_handler(void *data, sqlite3* /*conn*/, const char *msg, int kind) {
// TODO: `conn` might be useful to have here, but we'd need a
// non-owning version.
sqllog_handler_t *fn = reinterpret_cast<sqllog_handler_t*>(data);
try {
(*fn)(/*conn,*/ msg, kind);
}
catch (...) {
handle_callback_exception("sqllog handler");
}
}
void config_sqllog(const sqllog_handler_t &fun) {
if (fun) {
std::unique_ptr<sqllog_handler_t> cb = std::make_unique<sqllog_handler_t>(fun);
int rv = sqlite3_config(SQLITE_CONFIG_SQLLOG, sqxx_call_config_sqllog_handler,
cb.get());
if (rv != SQLITE_OK)
throw static_error(rv);
config_callbacks.sqllog_handler = std::move(cb);
}
else {
config_sqllog();
}
}
void config_sqllog() {
int rv = sqlite3_config(SQLITE_CONFIG_SQLLOG, nullptr, nullptr);
if (rv != SQLITE_OK)
throw static_error(rv);
}
void config_mmap_size(int64_t defaultlimit, int64_t maxlimit) {
int rv = sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, defaultlimit, maxlimit);
if (rv != SQLITE_OK)
throw static_error(rv);
}
#if defined(SQLITE_WIN32_MALLOC)
void config_win32_heapsize(uint32_t maxsize) {
int rv = sqlite3_config(SQLITE_CONFIG_WIN32_HEAPSIZE, maxsize);
if (rv != SQLITE_OK)
throw static_error(rv);
}
#endif
#if SQLITE_VERSION_NUMBER >= 3008008
int config_pcache_hdrsz() {
int result;
int rv = sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &result);
if (rv != SQLITE_OK)
throw static_error(rv);
return result;
}
#endif
#if SQLITE_VERSION_NUMBER >= 3008008
void config_pmasz(unsigned int minsize) {
int rv = sqlite3_config(SQLITE_CONFIG_PMASZ, minsize);
if (rv != SQLITE_OK)
throw static_error(rv);
}
#endif
#if SQLITE_VERSION_NUMBER >= 3012000
void config_stmtjrnl_spill(int threshold) {
int rv = sqlite3_config(SQLITE_CONFIG_PMASZ, threshold);
if (rv != SQLITE_OK)
throw static_error(rv);
}
#endif
} // namespace sqxx