8#ifndef BOOST_SQLITE_HOOKS_HPP
9#define BOOST_SQLITE_HOOKS_HPP
11#include <boost/sqlite/detail/config.hpp>
12#include <boost/sqlite/function.hpp>
13#include <boost/system/result.hpp>
15BOOST_SQLITE_BEGIN_NAMESPACE
18#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
27 system::result<value>
old(
int column)
const
30 int res = sqlite3_preupdate_old(db_, column, &val);
32 BOOST_SQLITE_RETURN_EC(res);
36 int count()
const {
return sqlite3_preupdate_count(db_); }
38 int depth()
const {
return sqlite3_preupdate_depth(db_); }
40 system::result<value>
new_(
int column)
const
43 int res = sqlite3_preupdate_new(db_, column, &val);
45 BOOST_SQLITE_RETURN_EC(res);
51 int blob_write()
const {
return sqlite3_preupdate_blobwrite(db_); }
64template<
typename Func>
65bool commit_hook_impl(sqlite3 * db,
69 static_assert(
noexcept(func()),
"hook must be noexcept");
70 return sqlite3_commit_hook( db, func, [](
void * data) {
return (*
static_cast<Func *
>(data))() ? 1 : 0; },
nullptr) !=
nullptr;
74template<
typename Func>
75bool commit_hook_impl(sqlite3 * db,
79 static_assert(
noexcept(func()),
"hook must be noexcept");
80 return sqlite3_commit_hook(
82 [](
void * data) {
return (*
static_cast<Func *
>(data))() ? 1 : 0; },
86inline bool commit_hook(sqlite3 * db, std::nullptr_t, std::false_type)
88 return sqlite3_commit_hook(db,
nullptr,
nullptr);
93template<
typename Func>
94bool commit_hook(sqlite3 * db,
97 using func_type =
typename std::decay<Func>::type;
98 return commit_hook_impl(db, std::forward<Func>(func), std::is_pointer<func_type>{});
103template<
typename Func>
104bool rollback_hook_impl(sqlite3 * db,
108 static_assert(
noexcept(func()),
"hook must be noexcept");
109 return sqlite3_rollback_hook( db, func, [](
void * data) { (*
static_cast<Func *
>(data))(); },
nullptr) !=
nullptr;
113template<
typename Func>
114bool rollback_hook_impl(sqlite3 * db,
118 static_assert(
noexcept(func()),
"hook must be noexcept");
119 return sqlite3_rollback_hook(
121 [](
void * data) { (*
static_cast<Func *
>(data))(); },
125inline bool rollback_hook_impl(sqlite3 * db, std::nullptr_t, std::false_type)
127 return sqlite3_rollback_hook(db,
nullptr,
nullptr);
130template<
typename Func>
131bool rollback_hook(sqlite3 * db,
134 using func_type =
typename std::decay<Func>::type;
135 return rollback_hook_impl(db, std::forward<Func>(func), std::is_pointer<func_type>{});
138#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
140template<
typename Func>
141bool preupdate_hook_impl(sqlite3 * db,
145 static_assert(
noexcept(func(preupdate_context(
nullptr), SQLITE_SELECT,
"",
"", 0, 0)),
"hook must be noexcept");
146 return sqlite3_preupdate_hook(
151 const char * db_name,
152 const char * table_name,
156 (*
static_cast<Func *
>(data))(preupdate_context(db), op, db_name, table_name, key1, key2);
157 },
nullptr) !=
nullptr;
161template<
typename Func>
162bool preupdate_hook_impl(sqlite3 * db,
166 static_assert(
noexcept(func(preupdate_context(
nullptr), SQLITE_SELECT,
"",
"", 0, 0)),
167 "hooks but be noexcept");
168 using func_type =
typename std::decay<Func>::type;
170 return sqlite3_preupdate_hook(
175 const char * db_name,
176 const char * table_name,
180 (*
static_cast<Func *
>(data))(preupdate_context(db), op, db_name, table_name, key1, key2);
181 }, &func) !=
nullptr;
184inline bool preupdate_hook_impl(sqlite3 * db, std::nullptr_t, std::false_type)
186 return sqlite3_preupdate_hook(db,
nullptr,
nullptr);
189template<
typename Func>
190bool preupdate_hook(sqlite3 * db,
193 using func_type =
typename std::decay<Func>::type;
194 return preupdate_hook_impl(db, std::forward<Func>(func), std::is_pointer<func_type>{});
199template<
typename Func>
200bool update_hook_impl(sqlite3 * db,
204 static_assert(
noexcept(func(SQLITE_SELECT,
"",
"", 0)),
"hook must be noexcept");
205 return sqlite3_update_hook(
214 (*
static_cast<Func *
>(data))(op, db, name, key);
215 },
nullptr) !=
nullptr;
219template<
typename Func>
220bool update_hook_impl(sqlite3 * db,
224 static_assert(
noexcept(func(SQLITE_SELECT,
"",
"", 0)),
"hook must be noexcept");
225 using func_type =
typename std::decay<Func>::type;
227 return sqlite3_update_hook(
235 (*
static_cast<func_type*
>(data))(op, db, name, key);
236 }, &func) !=
nullptr;
240bool update_hook_impl(sqlite3 * db,
244 return sqlite3_update_hook(db,
nullptr,
nullptr);
247template<
typename Func>
248bool update_hook(sqlite3 * db,
251 using func_type =
typename std::decay<Func>::type;
252 return update_hook_impl(db, std::forward<Func>(func), std::is_pointer<func_type>{});
275template<
typename Func>
278 return detail::commit_hook(conn.handle(), std::forward<Func>(func));
297template<
typename Func>
300 return detail::rollback_hook(conn.handle(), std::forward<Func>(func));
303#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
335template<
typename Func>
338 return detail::preupdate_hook(conn.handle(), std::forward<Func>(func));
362template<
typename Func>
365 return detail::update_hook(conn.handle(), std::forward<Func>(func));
368BOOST_SQLITE_END_NAMESPACE
main object for a connection to a database.
int depth() const
The nesting depth of the update.
int blob_write() const
Query the status of blob access, e.g. when using blob_handle.
system::result< value > old(int column) const
Returns the old value, i.e. the value before the update.
system::result< value > new_(int column) const
The new value to be written to column.
int count() const
The count of colums to be updated.
A holder for a sqlite values used for internal APIs.