ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the SQLite API.
Loading...
Searching...
No Matches
global_iface.hpp
1/*
2 Copyright 2019 Eugene Gershnik
3
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://github.com/gershnik/thinsqlitepp/blob/master/LICENSE
7*/
8
9#ifndef HEADER_SQLITEPP_GLOBAL_IFACE_INCLUDED
10#define HEADER_SQLITEPP_GLOBAL_IFACE_INCLUDED
11
12#include "exception_iface.hpp"
13
14#include <cstdint>
15
19namespace thinsqlitepp
20{
25
33 SQLITEPP_EXPORTED
34 inline void initialize()
35 {
36 int res = sqlite3_initialize();
37 if (res != SQLITE_OK)
38 throw exception(res);
39 }
40
48 SQLITEPP_EXPORTED
49 inline void shutdown() noexcept
50 {
52 }
53
55 SQLITEPP_EXPORTED
57 {
58 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 10, 0)
59 using counter_type = sqlite3_int64;
60 #else
61 using counter_type = int;
62 #endif
63
64 counter_type current;
65 counter_type highwater;
66 };
67
73 SQLITEPP_EXPORTED
74 inline status_value status(int op, bool reset = false)
75 {
76 status_value ret;
77 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 10, 0)
78 int res = sqlite3_status64(op, &ret.current, &ret.highwater, reset);
79 #else
80 int res = sqlite3_status(op, &ret.current, &ret.highwater, reset);
81 #endif
82 if (res != SQLITE_OK)
83 throw exception(res);
84 return ret;
85 }
86
88
89 namespace internal
90 {
91 template<int Code, class ...Args>
92 struct config_option
93 {
94 static void apply(Args && ...args)
95 {
96 int res = sqlite3_config(Code, std::forward<Args>(args)...);
97 if (res != SQLITE_OK)
98 throw exception(res);
99 }
100 };
101
102 template<int Code> struct config_mapping;
103
104 }
105
107
123 SQLITEPP_EXPORTED
124 template<int Code, class ...Args>
125 inline
126 auto config(Args && ...args) ->
127 #ifndef DOXYGEN
128 //void but prevents instantiation with wrong types
129 decltype(
130 internal::config_mapping<Code>::type::apply(std::forward<decltype(args)>(args)...)
131 )
132 #else
133 void
134 #endif
135 { internal::config_mapping<Code>::type::apply(std::forward<Args>(args)...); }
136
138
140
141 namespace internal
142 {
143 SQLITEPP_SUPPRESS_SILLY_VARARG_WARNING_BEGIN
144
145 #if SQLITEPP_USE_VARARG_POUND_POUND_TRICK
146
147 //Idiotic GCC in pedantic mode warns on MACRO(arg) for MARCO(x,...) in < C++20 mode
148 //with no way to disable the warning(!!!).
149 #define SQLITEPP_DEFINE_OPTION_0(code) \
150 template<> struct config_mapping<code> { using type = config_option<code>; };
151 #define SQLITEPP_DEFINE_OPTION_N(code, ...) \
152 template<> struct config_mapping<code> { using type = config_option<code, ##__VA_ARGS__>; };
153
154 #else
155
156 #define SQLITEPP_DEFINE_OPTION(code, ...) \
157 template<> struct config_mapping<code> { using type = config_option<code __VA_OPT__(,) __VA_ARGS__>; };
158
159 #define SQLITEPP_DEFINE_OPTION_N(code, ...) SQLITEPP_DEFINE_OPTION(code __VA_OPT__(,) __VA_ARGS__)
160 #define SQLITEPP_DEFINE_OPTION_0(code) SQLITEPP_DEFINE_OPTION_N(code)
161
162 #endif
163
164 //@ [Config Options]
165
166 SQLITEPP_DEFINE_OPTION_0( SQLITE_CONFIG_SINGLETHREAD );
167 SQLITEPP_DEFINE_OPTION_0( SQLITE_CONFIG_MULTITHREAD );
168 SQLITEPP_DEFINE_OPTION_0( SQLITE_CONFIG_SERIALIZED );
169 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_MALLOC, sqlite3_mem_methods *);
170 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_GETMALLOC, sqlite3_mem_methods *);
171 #ifdef SQLITE_CONFIG_SMALL_MALLOC
172 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_SMALL_MALLOC, int);
173 #endif
174 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_MEMSTATUS, int);
175 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_PAGECACHE, void *, int, int);
176 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_HEAP, void *, int, int);
177 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_MUTEX, sqlite3_mutex_methods *);
178 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_GETMUTEX, sqlite3_mutex_methods *);
179 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_LOOKASIDE, int, int);
180 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_PCACHE2, sqlite3_pcache_methods2 *);
181 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_GETPCACHE2, sqlite3_pcache_methods2 *);
182 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_LOG, void (*)(void*, int, const char *), void *);
183 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_URI, int);
184 #ifdef SQLITE_CONFIG_COVERING_INDEX_SCAN
185 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_COVERING_INDEX_SCAN, int);
186 #endif
187 #ifdef SQLITE_CONFIG_SQLLOG
188 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_SQLLOG, void (*)(void *, sqlite3 *, const char *, int), void *);
189 #endif
190 #ifdef SQLITE_CONFIG_MMAP_SIZE
191 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_MMAP_SIZE, int64_t, int64_t);
192 #endif
193 #ifdef SQLITE_CONFIG_WIN32_HEAPSIZE
194 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_WIN32_HEAPSIZE, int);
195 #endif
196 #ifdef SQLITE_CONFIG_PCACHE_HDRSZ
197 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_PCACHE_HDRSZ, int *);
198 #endif
199 #ifdef SQLITE_CONFIG_PMASZ
200 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_PMASZ, unsigned int);
201 #endif
202 #ifdef SQLITE_CONFIG_STMTJRNL_SPILL
203 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_STMTJRNL_SPILL, int);
204 #endif
205 #ifdef SQLITE_CONFIG_SORTERREF_SIZE
206 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_SORTERREF_SIZE, int);
207 #endif
208 #ifdef SQLITE_CONFIG_MEMDB_MAXSIZE
209 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_MEMDB_MAXSIZE, int64_t);
210 #endif
211 #ifdef SQLITE_CONFIG_ROWID_IN_VIEW
212 SQLITEPP_DEFINE_OPTION_N( SQLITE_CONFIG_ROWID_IN_VIEW, int);
213 #endif
214
215 //@ [Config Options]
216
217 #undef SQLITEPP_DEFINE_OPTION_0
218 #undef SQLITEPP_DEFINE_OPTION_N
219 #ifdef SQLITEPP_DEFINE_OPTION
220 #undef SQLITEPP_DEFINE_OPTION
221 #endif
222
223 SQLITEPP_SUPPRESS_SILLY_VARARG_WARNING_END
224
225 }
227}
228
229
230#endif
231
Exception used to report any SQLite errors.
Definition exception_iface.hpp:180
sqlite3_config
T forward(T... args)
void initialize()
Initialize the SQLite library.
Definition global_iface.hpp:34
auto config(Args &&...args) -> void
Configures SQLite library.
Definition global_iface.hpp:126
void shutdown() noexcept
Deinitialize the SQLite library.
Definition global_iface.hpp:49
status_value status(int op, bool reset=false)
Obtain SQLite runtime status.
Definition global_iface.hpp:74
Return type for status().
Definition global_iface.hpp:57
@ reset
Reset the statement (does not affect the bindings).
Definition statement_iface.hpp:782
sqlite3_initialize
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
#define SQLITE_OK
sqlite3_status64