summaryrefslogtreecommitdiff
path: root/include/boost/sqlite/detail/scalar_function.hpp
blob: ee16a23afd594f5f458f46d9edf350c26ad54256 (plain)
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
// Copyright (c) 2023 Klemens D. Morgenstern
//
// 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_DETAIL_SCALAR_FUNCTION_HPP
#define BOOST_SQLITE_DETAIL_SCALAR_FUNCTION_HPP

#include <boost/sqlite/detail/config.hpp>
#include <boost/sqlite/cstring_ref.hpp>
#include <boost/sqlite/memory.hpp>
#include <boost/sqlite/result.hpp>
#include <boost/sqlite/value.hpp>

#include <boost/callable_traits/args.hpp>
#include <boost/callable_traits/return_type.hpp>
#include <boost/callable_traits/has_void_return.hpp>
#include <boost/core/span.hpp>


BOOST_SQLITE_BEGIN_NAMESPACE

template<typename ... Args>
struct context;

namespace detail
{

template<typename Func, typename ... Args, std::size_t Extent>
auto create_scalar_function_impl(sqlite3 * db,
                                 cstring_ref name,
                                 Func && func,
                                 int flags,
                                 std::tuple<context<Args...>, boost::span<value, Extent>> * ,
                                 std::false_type /* void return */,
                                 std::false_type /* is pointer */) -> int
{
  using func_type = typename std::decay<Func>::type;
  return sqlite3_create_function_v2(
      db, name.c_str(),
      Extent == boost::dynamic_extent ? -1 : static_cast<int>(Extent),
      SQLITE_UTF8 | flags,
      new (memory_tag{}) func_type(std::forward<Func>(func)),
      +[](sqlite3_context* ctx, int len, sqlite3_value** args)
      {
        auto cc = context<Args...>(ctx);
        auto aa =  reinterpret_cast<value*>(args);
        auto &f = *reinterpret_cast<func_type*>(sqlite3_user_data(ctx));
        boost::span<value, Extent> vals{aa, static_cast<std::size_t>(len)};

        execute_context_function(ctx, f, cc, vals);
      }, nullptr, nullptr,
      +[](void * ptr) noexcept {delete_(static_cast<func_type*>(ptr));}
  );
}

template<typename Func, typename ... Args, std::size_t Extent>
auto create_scalar_function_impl(sqlite3 * db,
                                 cstring_ref name,
                                 Func && func, int flags,
                                 std::tuple<context<Args...>, boost::span<value, Extent>> * ,
                                 std::true_type /* void return */,
                                 std::false_type /* is pointer */) -> int
{
  using func_type = typename std::decay<Func>::type;
  return sqlite3_create_function_v2(
      db,
      name.c_str(),
      (Extent == boost::dynamic_extent) ? -1 : static_cast<int>(Extent),
      SQLITE_UTF8 | flags,
      new (memory_tag{}) func_type(std::forward<Func>(func)),
      +[](sqlite3_context* ctx, int len, sqlite3_value** args)
      {
        auto cc = context<Args...>(ctx);
        auto aa =  reinterpret_cast<value*>(args);
        auto &f = *reinterpret_cast<func_type*>(sqlite3_user_data(ctx));
        boost::span<value, Extent> vals{aa, static_cast<std::size_t>(len)};

        execute_context_function(
            ctx,
            [&]()
            {
              f(cc, vals);
              return result<void>();
            });

      }, nullptr, nullptr,
      +[](void * ptr){delete_(static_cast<func_type*>(ptr));}
  );
}


template<typename Func, typename ... Args, std::size_t Extent>
auto create_scalar_function_impl(sqlite3 * db,
                                 cstring_ref name,
                                 Func * func, int flags,
                                 std::tuple<context<Args...>, boost::span<value, Extent>> * ,
                                 std::false_type /* void return */,
                                 std::true_type /* is pointer */) -> int
{
  return sqlite3_create_function_v2(
      db, name.c_str(),
      Extent == boost::dynamic_extent ? -1 : static_cast<int>(Extent),
      SQLITE_UTF8 | flags,
      reinterpret_cast<void*>(func),
      +[](sqlite3_context* ctx, int len, sqlite3_value** args)
      {
        auto cc = context<Args...>(ctx);
        auto aa = reinterpret_cast<value*>(args);
        auto  f = reinterpret_cast<Func*>(sqlite3_user_data(ctx));

        boost::span<value, Extent> vals{aa, static_cast<std::size_t>(len)};

        execute_context_function(
            ctx, f, cc, vals);
      }, nullptr, nullptr, nullptr);
}

template<typename Func, typename ... Args, std::size_t Extent>
auto create_scalar_function_impl(sqlite3 * db,
                                 cstring_ref name,
                                 Func * func, int flags,
                                 std::tuple<context<Args...>, boost::span<value, Extent>> * ,
                                 std::true_type /* void return */,
                                 std::true_type /* is pointer */) -> int
{
  return sqlite3_create_function_v2(
      db,
      name.c_str(),
      (Extent == boost::dynamic_extent) ? -1 : static_cast<int>(Extent),
      SQLITE_UTF8 | flags,
      reinterpret_cast<void*>(func),
      +[](sqlite3_context* ctx, int len, sqlite3_value** args)
      {
        auto cc = context<Args...>(ctx);
        auto aa =  reinterpret_cast<value*>(args);
        auto  f = *reinterpret_cast<Func*>(sqlite3_user_data(ctx));
        boost::span<value, Extent> vals{aa, static_cast<std::size_t>(len)};
        execute_context_function(
            ctx,
            [&]()
            {
              f(cc, vals);
              return result<void>();
            });

      }, nullptr, nullptr, nullptr);
}

template<typename Func>
auto create_scalar_function(sqlite3 * db,
                     cstring_ref name,
                     Func && func,
                     int flags)
                     -> decltype(create_scalar_function_impl(
                         db, name, std::forward<Func>(func), flags,
                         static_cast<callable_traits::args_t<Func>*>(nullptr),
                         callable_traits::has_void_return<Func>{},
                         std::is_pointer<typename std::decay<Func>::type>{}
                         ))
{
  return create_scalar_function_impl(db, name, std::forward<Func>(func), flags,
                                     static_cast<callable_traits::args_t<Func>*>(nullptr),
                                     callable_traits::has_void_return<Func>{},
                                     std::is_pointer<typename std::decay<Func>::type>{});
}


}

BOOST_SQLITE_END_NAMESPACE

#endif //BOOST_SQLITE_DETAIL_SCALAR_FUNCTION_HPP