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
|
//
// 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_FUNCTION_HPP
#define BOOST_SQLITE_FUNCTION_HPP
#include <boost/sqlite/detail/config.hpp>
#include <boost/sqlite/detail/aggregate_function.hpp>
#include <boost/sqlite/detail/scalar_function.hpp>
#include <boost/sqlite/detail/window_function.hpp>
#include <boost/sqlite/connection.hpp>
#include <boost/sqlite/result.hpp>
#include <boost/sqlite/value.hpp>
#include <boost/sqlite/detail/exception.hpp>
#include <boost/sqlite/cstring_ref.hpp>
#include <boost/core/span.hpp>
#include <boost/callable_traits/args.hpp>
#include <boost/callable_traits/has_void_return.hpp>
#include <boost/callable_traits/return_type.hpp>
BOOST_SQLITE_BEGIN_NAMESPACE
enum function_flags
{
deterministic = SQLITE_DETERMINISTIC,
directonly = SQLITE_DIRECTONLY,
subtype = SQLITE_SUBTYPE,
innocuous = SQLITE_INNOCUOUS,
result_subtype = SQLITE_RESULT_SUBTYPE,
#if defined(SQLITE_SELFORDER1)
selforder1 = SQLITE_SELFORDER1,
#else
selforder1 = 0
#endif
};
/** @brief A context that can be passed into scalar functions.
@ingroup reference
@tparam Args The argument that can be stored in the context.
@code{.cpp}
extern sqlite::connection conn;
sqlite::create_scalar_function(
conn, "my_sum",
[](sqlite::context<std::size_t> ctx,
boost::span<sqlite::value, 1u> args) -> std::size_t
{
auto value = args[0].get_int();
auto p = ctx.get_if<0>();
if (p != nullptr) // increment the counter
return (*p) += value;
else // set the initial value
ctx.set<0>(value);
return value;
});
@endcode
*/
template<typename ... Args>
struct context
{
template<std::size_t N>
using element = mp11::mp_take_c<mp11::mp_list<Args...>, N>;
/// Set the value in the context at position `Idx`
template<std::size_t Idx>
void set(element<Idx> value)
{
sqlite3_set_auxdata(ctx_, Idx, *static_cast<void**>(&value),
new (memory_tag{}) element<Idx>(std::move(value)),
+[](void * ptr)
{
delete_(static_cast<element<Idx> *>(ptr));
});
}
/// Returns the value in the context at position `Idx`. Throws if the value isn't set.
template<std::size_t Idx>
auto get() -> element<Idx> &
{
using type = element<Idx> ;
auto p = static_cast<type*>(sqlite3_get_auxdata(ctx_, Idx));
if (p == nullptr)
detail::throw_invalid_argument("argument not set",
BOOST_CURRENT_LOCATION);
return *p;
}
/// Returns the value in the context at position `Idx`. Returns nullptr .value isn't set.
template<std::size_t Idx>
auto get_if() -> element<Idx> *
{
using type = element<Idx> ;
return static_cast<type*>(sqlite3_get_auxdata(ctx_, Idx));
}
explicit context(sqlite3_context * ctx) noexcept : ctx_(ctx) {}
/// Set the result through the context, instead of returning it.
template<typename T>
auto set_result(T && val)
#if !defined(BOOST_SQLITE_GENERATING_DOCS)
-> decltype(detail::set_result(static_cast<sqlite3_context*>(nullptr), std::forward<T>(val)))
#endif
{
detail::set_result(ctx_, std::forward<T>(val));
}
/// Set the an error through the context, instead of throwing it.
void set_error(cstring_ref message, int code = SQLITE_ERROR)
{
sqlite3_result_error(ctx_, message.c_str(), -1);
sqlite3_result_error_code(ctx_, code);
}
/// Returns the connection of the context.
connection get_connection() const
{
return connection{sqlite3_context_db_handle(ctx_), false};
}
private:
sqlite3_context * ctx_;
};
///@{
/** @brief create a scalar function
@ingroup reference
@param conn The connection to add the function to.
@param name The name of the function
@param func The function to be added
@param ec The system::error_code
@throws `system::system_error` when the overload without `ec` is used.
`func` must take `context<Args...>` as the first and a `span<value, N>` as the second value.
If `N` is not `dynamic_extent` it will be used to deduce the number of arguments for the function.
@par Example
@code{.cpp}
extern sqlite::connection conn;
sqlite::create_function(
conn, "my_sum",
[](sqlite::context<std::size_t> ctx,
boost::span<sqlite::value, 1u> args) -> std::size_t
{
auto value = args[0].get_int();
auto p = ctx.get_if<0>();
if (p != nullptr) // increment the counter
return (*p) += value;
else // set the initial value
ctx.set<0>(value);
return value;
});
@endcode
*/
template<typename Func>
auto create_scalar_function(
connection & conn,
cstring_ref name,
Func && func,
function_flags flags,
system::error_code & ec,
error_info & ei)
#if !defined(BOOST_SQLITE_GENERATING_DOCS)
-> typename std::enable_if<
std::is_same<
decltype(
detail::create_scalar_function(
static_cast<sqlite3*>(nullptr), name,
std::declval<Func>(), flags)
), int>::value>::type
#endif
{
auto res = detail::create_scalar_function(conn.handle(), name,
std::forward<Func>(func), static_cast<int>(flags));
if (res != 0)
{
BOOST_SQLITE_ASSIGN_EC(ec, res);
ei.set_message(sqlite3_errmsg(conn.handle()));
}
}
///@{
/** @brief create a scalar function
@ingroup reference
@param conn The connection to add the function to.
@param name The name of the function
@param func The function to be added
@param ec The system::error_code
@throws `system::system_error` when the overload without `ec` is used.
`func` must take `context<Args...>` as the first and a `span<value, N>` as the second value.
If `N` is not `dynamic_extent` it will be used to deduce the number of arguments for the function.
@par Example
@code{.cpp}
extern sqlite::connection conn;
sqlite::create_function(
conn, "to_upper",
[](sqlite::context<> ctx,
boost::span<sqlite::value, 1u> args) -> std::string
{
std::string res;
auto txt = val[0].get_text();
res.resize(txt.size());
std::transform(txt.begin(), txt.end(), res.begin(),
[](char c){return std::toupper(c);});
return value;
});
@endcode
*/
template<typename Func>
auto create_scalar_function(
connection & conn,
cstring_ref name,
Func && func,
function_flags flags = {})
-> typename std::enable_if<
std::is_same<
decltype(
detail::create_scalar_function(
static_cast<sqlite3*>(nullptr), name,
std::declval<Func>(), flags)
), int>::value>::type
{
system::error_code ec;
error_info ei;
create_scalar_function(conn, name, std::forward<Func>(func), flags, ec, ei);
if (ec)
detail::throw_error_code(ec, ei);
}
///@}
///@{
/** @brief create a aggregate function
@ingroup reference
@param conn The connection to add the function to.
@param name The name of the function
@param args
@param ec The system::error_code
@tparam Func The function to be added
@throws `system::system_error` when the overload without `ec` is used.
`func` needs to be an object with two functions:
@code{.cpp}
void step(boost::span<sqlite::value, N> args);
T final();
@endcode
An aggregrate function will create a new `Func` for a new `aggregate` from the args tuple and call `step` for every step.
When the aggregation is done `final` is called and the result is returned to sqlite.
@par Example
@code{.cpp}
extern sqlite::connection conn;
struct aggregate_func
{
aggregate_func(std::size_t init) : counter(init) {}
std::size_t counter;
void step(, boost::span<sqlite::value, 1u> val)
{
counter += val[0].get_text().size();
}
std::size_t final()
{
return counter;
}
};
sqlite::create_function<aggregate_func>(
conn, "char_counter", std::make_tuple(42));
@endcode
*/
template<typename Func, typename Args = std::tuple<>>
void create_aggregate_function(
connection & conn,
cstring_ref name,
Args && args,
function_flags flags,
system::error_code & ec,
error_info & ei)
{
using func_type = typename std::decay<Func>::type;
auto res = detail::create_aggregate_function<Func>(
conn.handle(), name, std::forward<Args>(args), static_cast<int>(flags),
callable_traits::has_void_return<decltype(&func_type::step)>{}
);
if (res != 0)
{
BOOST_SQLITE_ASSIGN_EC(ec, res);
ei.set_message(sqlite3_errmsg(conn.handle()));
}
}
template<typename Func, typename Args = std::tuple<>>
void create_aggregate_function(
connection & conn,
cstring_ref name,
Args && args= {},
function_flags flags = {})
{
system::error_code ec;
error_info ei;
create_aggregate_function<Func>(conn, name, std::forward<Args>(args), flags, ec, ei);
if (ec)
detail::throw_error_code(ec, ei);
}
///@}
#if SQLITE_VERSION_NUMBER >= 3025000
///@{
/** @brief create a aggregate window function
@ingroup reference
@param conn The connection to add the function to.
@param name The name of the function
@param args The arguments to construct Func from.
@param ec The system::error_code
@tparam Func The function to be added
@throws `system::system_error` when the overload without `ec` is used.
`func` needs to be an object with three functions:
@code{.cpp}
void step(boost::span<sqlite::value, N> args);
void inverse(boost::span<sqlite::value, N> args);
T final();
@endcode
`State` can be any type and will get deduced together with `N`.
An aggregrate function will create a new `State` for a new `aggregate` and call `step` for every step.
When an element is removed from the window `inverse` is called.
When the aggregation is done `final` is called and the result is returned to sqlite.
@par Example
@code{.cpp}
extern sqlite::connection conn;
struct window_func
{
std::size_t counter;
void step(boost::span<sqlite::value, 1u> val)
{
counter += val[0].get_text().size();
}
void inverse(boost::span<sqlite::value, 1u> val)
{
counter -= val[0].get_text().size();
}
std::size_t final()
{
return counter;
}
};
sqlite::create_function(
conn, "win_char_counter",
aggregate_func{});
@endcode
*/
template<typename Func, typename Args = std::tuple<>>
void create_window_function(
connection & conn,
cstring_ref name,
Args && args,
function_flags flags,
system::error_code & ec)
{
using func_type = typename std::decay<Func>::type;
auto res = detail::create_window_function<Func>(
conn.handle(), name, std::forward<Args>(args), static_cast<int>(flags),
callable_traits::has_void_return<decltype(&func_type::step)>{});
if (res != 0)
BOOST_SQLITE_ASSIGN_EC(ec, res);
}
template<typename Func, typename Args = std::tuple<>>
void create_window_function(
connection & conn,
cstring_ref name,
Args && args = {},
function_flags flags = {})
{
system::error_code ec;
create_window_function<Func>(conn, name, std::forward<Args>(args), flags, ec);
if (ec)
detail::throw_error_code(ec);
}
///@}
///@{
/// Delete function
inline void delete_function(connection & conn, cstring_ref name, int argc, system::error_code &ec)
{
auto res = sqlite3_create_function_v2(conn.handle(), name.c_str(), argc, 0, nullptr, nullptr, nullptr, nullptr, nullptr);
if (res != 0)
BOOST_SQLITE_ASSIGN_EC(ec, res);
}
inline void delete_function(connection & conn, cstring_ref name, int argc = -1)
{
system::error_code ec;
delete_function(conn, name, argc, ec);
if (ec)
detail::throw_error_code(ec);
}
///@}
#endif
BOOST_SQLITE_END_NAMESPACE
#endif //BOOST_SQLITE_FUNCTION_HPP
|