-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconnection_create_aggregate.impl.hpp
196 lines (168 loc) · 6.03 KB
/
connection_create_aggregate.impl.hpp
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
// Implementation of connection::create_aggregate()
#if !defined(SQXX_CONNECTION_CREATE_AGGREGATE_IMPL_HPP_INCLUDED)
#define SQXX_CONNECTION_CREATE_AGGREGATE_IMPL_HPP_INCLUDED
// Function types with C calling convention for callbacks
// https://stackoverflow.com/a/5590050/
extern "C" typedef void sqxx_aggregate_step_type(sqlite3_context*, int, sqlite3_value**);
extern "C" typedef void sqxx_aggregate_final_type(sqlite3_context*);
namespace sqxx {
namespace detail {
// Data associated with an aggregate
template<typename StateD>
struct aggregate_invocation_data {
bool initialized;
StateD state;
aggregate_invocation_data(const StateD &zero) : initialized(true), state(zero) {
}
};
template<typename StateD, typename StepD, typename FinalD>
class aggregate_data {
private:
const StateD state_zero;
StepD stepfun;
FinalD finalfun;
static constexpr int NArgs = callable_traits<StepD>::argc - 1;
public:
template<typename State, typename StepFun, typename FinalFun>
aggregate_data(State &&state_zero_arg, StepFun &&stepfun_arg, FinalFun &&finalfun_arg)
: state_zero(std::forward<State>(state_zero_arg)), stepfun(std::forward<StepFun>(stepfun_arg)), finalfun(std::forward<FinalFun>(finalfun_arg)) {
}
private:
typedef aggregate_invocation_data<StateD> invocation_data;
// Returns the aggregation state, creating it if it doesn't exist yet.
invocation_data* get_state_create(context &ctx) {
invocation_data *as = reinterpret_cast<invocation_data*>(
ctx.aggregate_context(sizeof(invocation_data))
);
if (!as) {
throw std::bad_alloc();
}
// Sqlite zeros freshly allocated memory, so `as->initialized` will
// start out as `false` if we haven't called the constructor yet.
if (!as->initialized) {
// Construct object, with a state copied from `state_zero`
new(as) invocation_data(state_zero);
}
return as;
}
// Returns the aggregation state, if there is any
invocation_data* get_state_existing(context &ctx) {
// Calling with size 0. We'll get any existing data, or a nullptr if
// there isn't any.
invocation_data *as = reinterpret_cast<invocation_data*>(
ctx.aggregate_context(0)
);
return as;
}
public:
void step_call(context &ctx, int argc, sqlite3_value **values) {
if (argc != NArgs) {
ctx.result_error_misuse();
return;
}
invocation_data *as = get_state_create(ctx);
apply_value_array<NArgs, void>(stepfun, values, as->state);
//std::vector<value> vs(values, values+nargs);
}
void final_call(context &ctx) {
// Take ownership of state (if any), deleting it on return
ownedobj_ptr<invocation_data> as(get_state_existing(ctx));
if (as) {
// We have state
ctx.result(finalfun(as->state));
}
else {
// There was no allocated state
ctx.result(finalfun(state_zero));
}
}
};
template<typename StateD, typename StepD, typename FinalD>
sqxx_aggregate_step_type aggregate_call_step;
template<typename StateD, typename StepD, typename FinalD>
void aggregate_call_step(sqlite3_context *handle, int argc, sqlite3_value** argv) {
typedef aggregate_data<StateD, StepD, FinalD> data_t;
data_t *data = reinterpret_cast<data_t*>(sqlite3_user_data(handle));
context ctx(handle);
try {
data->step_call(ctx, argc, argv);
}
catch (const error &e) {
ctx.result_error_code(e.code);
}
catch (const std::bad_alloc &) {
ctx.result_error_nomem();
}
catch (const std::exception &e) {
ctx.result_error(e.what());
}
catch (...) {
ctx.result_error_misuse();
}
}
template<typename StateD, typename StepD, typename FinalD>
sqxx_aggregate_final_type aggregate_call_final;
template<typename StateD, typename StepD, typename FinalD>
void aggregate_call_final(sqlite3_context *handle) {
typedef aggregate_data<StateD, StepD, FinalD> data_t;
data_t *data = reinterpret_cast<data_t*>(sqlite3_user_data(handle));
context ctx(handle);
try {
data->final_call(ctx);
}
catch (const error &e) {
ctx.result_error_code(e.code);
}
catch (const std::bad_alloc &) {
ctx.result_error_nomem();
}
catch (const std::exception &e) {
ctx.result_error(e.what());
}
catch (...) {
ctx.result_error_misuse();
}
}
void create_aggregate_register(sqlite3 *handle, const char *name, int nargs, void *data,
sqxx_aggregate_step_type *stepfun, sqxx_aggregate_final_type *finalfun,
sqxx_appdata_destroy_type *destroy);
} // namespace detail
template<typename State, typename StepCallable, typename FinalCallable>
void connection::create_aggregate(const char *name,
State &&zero,
StepCallable &&stepfun,
FinalCallable &&finalfun) {
typedef std::decay_t<State> StateD;
typedef std::decay_t<StepCallable> StepD;
typedef std::decay_t<FinalCallable> FinalD;
typedef callable_traits<StepD> traits;
typedef detail::aggregate_data<StateD, StepD, FinalD> data_t;
data_t *adat = new data_t(
std::forward<State>(zero),
std::forward<StepCallable>(stepfun),
std::forward<FinalCallable>(finalfun)
);
detail::create_aggregate_register(handle, name, traits::argc-1, reinterpret_cast<void*>(adat),
detail::aggregate_call_step<StateD, StepD, FinalD>,
detail::aggregate_call_final<StateD, StepD, FinalD>,
detail::appdata_destroy_object<data_t>);
}
template<typename State, typename StepCallable, typename FinalCallable>
void connection::create_aggregate(const std::string &name, State &&zero,
StepCallable &&step_fun, FinalCallable &&final_fun) {
create_aggregate(name.c_str(), std::forward<State>(zero), std::forward<StepCallable>(step_fun),
std::forward<FinalCallable>(final_fun));
}
template<typename State, typename StepCallable>
void connection::create_aggregate(const char *name, State &&zero, StepCallable &&step_fun) {
create_aggregate<State, StepCallable>(name, std::forward<State>(zero),
std::forward<StepCallable>(step_fun),
[](const State &state) -> State { return state; }
);
}
template<typename State, typename StepCallable>
void connection::create_aggregate(const std::string &name, State &&zero, StepCallable &&step_fun) {
create_aggregate(name.c_str(), std::forward<State>(zero), std::forward<StepCallable>(step_fun));
}
} // namespace sqxx
#endif // SQXX_CONNECTION_CREATE_AGGREGATE_IMPL_HPP_INCLUDED