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
|
//
// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_SQLITE_HOOKS_HPP
#define BOOST_SQLITE_HOOKS_HPP
#include <boost/sqlite/detail/config.hpp>
#include <boost/sqlite/function.hpp>
#include <boost/system/result.hpp>
BOOST_SQLITE_BEGIN_NAMESPACE
#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
/** The context for pre-update events
@note This is only available if sqlite was compiled with `SQLITE_ENABLE_PREUPDATE_HOOK` enabled.
*/
struct preupdate_context
{
/// Returns the old value, i.e. the value before the update.
system::result<value> old(int column) const
{
sqlite3_value * val;
int res = sqlite3_preupdate_old(db_, column, &val);
if (res != 0)
BOOST_SQLITE_RETURN_EC(res);
return value(val);
}
/// The count of colums to be updated
int count() const { return sqlite3_preupdate_count(db_); }
/// The nesting depth of the update.
int depth() const { return sqlite3_preupdate_depth(db_); }
/// The new value to be written to column
system::result<value> new_(int column) const
{
sqlite3_value * val;
int res = sqlite3_preupdate_new(db_, column, &val);
if (res != 0)
BOOST_SQLITE_RETURN_EC(res);
return value(val);
}
/// @brief Query the status of blob access, e.g. when using @ref blob_handle
/// @see https://www.sqlite.org/c3ref/preupdate_blobwrite.html
int blob_write() const { return sqlite3_preupdate_blobwrite(db_); }
explicit preupdate_context(sqlite3 * db) noexcept : db_(db) {}
private:
sqlite3 * db_;
};
#endif
namespace detail
{
template<typename Func>
bool commit_hook_impl(sqlite3 * db,
Func * func,
std::true_type)
{
static_assert(noexcept(func()), "hook must be noexcept");
return sqlite3_commit_hook( db, func, [](void * data) { return (*static_cast<Func *>(data))() ? 1 : 0; }, nullptr) != nullptr;
}
template<typename Func>
bool commit_hook_impl(sqlite3 * db,
Func && func,
std::false_type)
{
static_assert(noexcept(func()), "hook must be noexcept");
return sqlite3_commit_hook(
db,
[](void * data) { return (*static_cast<Func *>(data))() ? 1 : 0; },
&func) != nullptr;
}
inline bool commit_hook(sqlite3 * db, std::nullptr_t, std::false_type)
{
return sqlite3_commit_hook(db, nullptr, nullptr);
}
template<typename Func>
bool commit_hook(sqlite3 * db,
Func && func)
{
using func_type = typename std::decay<Func>::type;
return commit_hook_impl(db, std::forward<Func>(func), std::is_pointer<func_type>{});
}
template<typename Func>
bool rollback_hook_impl(sqlite3 * db,
Func * func,
std::true_type)
{
static_assert(noexcept(func()), "hook must be noexcept");
return sqlite3_rollback_hook( db, func, [](void * data) { (*static_cast<Func *>(data))(); }, nullptr) != nullptr;
}
template<typename Func>
bool rollback_hook_impl(sqlite3 * db,
Func && func,
std::false_type)
{
static_assert(noexcept(func()), "hook must be noexcept");
return sqlite3_rollback_hook(
db,
[](void * data) { (*static_cast<Func *>(data))(); },
&func) != nullptr;
}
inline bool rollback_hook_impl(sqlite3 * db, std::nullptr_t, std::false_type)
{
return sqlite3_rollback_hook(db, nullptr, nullptr);
}
template<typename Func>
bool rollback_hook(sqlite3 * db,
Func && func)
{
using func_type = typename std::decay<Func>::type;
return rollback_hook_impl(db, std::forward<Func>(func), std::is_pointer<func_type>{});
}
#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
template<typename Func>
bool preupdate_hook_impl(sqlite3 * db,
Func * func,
std::true_type)
{
static_assert(noexcept(func(preupdate_context(nullptr), SQLITE_SELECT, "", "", 0, 0)), "hook must be noexcept");
return sqlite3_preupdate_hook(
db, func,
[](void * data,
sqlite3* db,
int op,
const char * db_name,
const char * table_name,
sqlite_int64 key1,
sqlite_int64 key2)
{
(*static_cast<Func *>(data))(preupdate_context(db), op, db_name, table_name, key1, key2);
}, nullptr) != nullptr;
}
template<typename Func>
bool preupdate_hook_impl(sqlite3 * db,
Func & func,
std::false_type)
{
static_assert(noexcept(func(preupdate_context(nullptr), SQLITE_SELECT, "", "", 0, 0)),
"hooks but be noexcept");
using func_type = typename std::decay<Func>::type;
return sqlite3_preupdate_hook(
db,
[](void * data,
sqlite3* db,
int op,
const char * db_name,
const char * table_name,
sqlite_int64 key1,
sqlite_int64 key2)
{
(*static_cast<Func *>(data))(preupdate_context(db), op, db_name, table_name, key1, key2);
}, &func) != nullptr;
}
inline bool preupdate_hook_impl(sqlite3 * db, std::nullptr_t, std::false_type)
{
return sqlite3_preupdate_hook(db, nullptr, nullptr);
}
template<typename Func>
bool preupdate_hook(sqlite3 * db,
Func && func)
{
using func_type = typename std::decay<Func>::type;
return preupdate_hook_impl(db, std::forward<Func>(func), std::is_pointer<func_type>{});
}
#endif
template<typename Func>
bool update_hook_impl(sqlite3 * db,
Func * func,
std::true_type)
{
static_assert(noexcept(func(SQLITE_SELECT, "", "", 0)), "hook must be noexcept");
return sqlite3_update_hook(
db, func,
[](void * data,
sqlite3*,
int op,
const char * db,
const char * name,
sqlite_int64 key)
{
(*static_cast<Func *>(data))(op, db, name, key);
}, nullptr) != nullptr;
}
template<typename Func>
bool update_hook_impl(sqlite3 * db,
Func & func,
std::false_type)
{
static_assert(noexcept(func(SQLITE_SELECT, "", "", 0)), "hook must be noexcept");
using func_type = typename std::decay<Func>::type;
return sqlite3_update_hook(
db,
[](void * data,
int op,
const char * db,
const char * name,
sqlite_int64 key)
{
(*static_cast<func_type*>(data))(op, db, name, key);
}, &func) != nullptr;
}
inline
bool update_hook_impl(sqlite3 * db,
std::nullptr_t,
std::false_type)
{
return sqlite3_update_hook(db, nullptr, nullptr);
}
template<typename Func>
bool update_hook(sqlite3 * db,
Func && func)
{
using func_type = typename std::decay<Func>::type;
return update_hook_impl(db, std::forward<Func>(func), std::is_pointer<func_type>{});
}
}
/**
@brief Install a commit hook
@ingroup reference
@see [related sqlite documentation](https://www.sqlite.org/c3ref/commit_hook.html)
The commit hook gets called before a commit gets performed.
If `func` returns true, the commit goes, otherwise it gets rolled back.
@note If the function is not a free function pointer, this function will *NOT* take ownership.
@note If `func` is a `nullptr` the hook gets reset.
@param conn The database connection to install the hook in
@param func The hook function
@return true if an hook has been replaced.
*/
template<typename Func>
bool commit_hook(connection & conn, Func && func)
{
return detail::commit_hook(conn.handle(), std::forward<Func>(func));
}
/**
@brief Install a rollback hook
@ingroup reference
@see [related sqlite documentation](https://www.sqlite.org/c3ref/commit_hook.html)
The rollback hook gets called when a rollback gets performed.
@note If the function is not a free function pointer, this function will *NOT* take ownership.
@note If `func` is a `nullptr` the hook gets reset.
@param conn The database connection to install the hook in
@param func The hook function
@return true if an hook has been replaced.
*/
template<typename Func>
bool rollback_hook(connection & conn, Func && func)
{
return detail::rollback_hook(conn.handle(), std::forward<Func>(func));
}
#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
/** A hook for pre-update events.
@note This is only available if sqlite was compiled with `SQLITE_ENABLE_PREUPDATE_HOOK` enabled.
@ingroup reference
@see [related sqlite documentation](https://sqlite.org/c3ref/preupdate_count.html)
The function will get called
@note If the function is not a free function pointer, this function will *NOT* take ownership.
@note If `func` is a `nullptr` the hook gets reset.
@param conn The database connection to install the hook in
@param func The hook function
@return true if an hook has been replaced.
The signature of the handler is as following (it must noexcept):
@code{.cpp}
void preupdate_hook(sqlite::preupdate_context ctx,
int op,
const char * db_name,
const char * table_name,
sqlite3_int64 current_key,
sqlite3_int64 new_key);
@endcode
*/
template<typename Func>
bool preupdate_hook(connection & conn, Func && func)
{
return detail::preupdate_hook(conn.handle(), std::forward<Func>(func));
}
#endif
/**
@brief Install an update hook
@ingroup reference
@see [related sqlite documentation](https://www.sqlite.org/c3ref/update_hook.html)
The update hook gets called when an update was performed.
@note If the function is not a free function pointer, this function will *NOT* take ownership.
The signature of the function is `void(int op, core::string_view db, core::string_view table, sqlite3_int64 id)`.
`op` is either `SQLITE_INSERT`, `SQLITE_DELETE` and `SQLITE_UPDATE`.
@note If `func` is a `nullptr` the hook gets reset.
@param conn The database connection to install the hook in
@param func The hook function
@return true if an hook has been replaced.
*/
template<typename Func>
bool update_hook(connection & conn, Func && func)
{
return detail::update_hook(conn.handle(), std::forward<Func>(func));
}
BOOST_SQLITE_END_NAMESPACE
#endif //BOOST_SQLITE_HOOKS_HPP
|