MCPcopy Create free account
hub / github.com/WiseLibs/better-sqlite3 / CustomAggregate

Class CustomAggregate

src/util/custom-aggregate.cpp:1–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class CustomAggregate : public CustomFunction {
2public:
3
4 explicit CustomAggregate(
5 Napi::Env env,
6 Database* db,
7 const char* name,
8 Napi::Value start,
9 Napi::Function step,
10 Napi::Value inverse,
11 Napi::Value result,
12 bool safe_ints
13 ) :
14 CustomFunction(env, db, name, step, safe_ints),
15 invoke_result(result.IsFunction()),
16 invoke_start(start.IsFunction()),
17 inverse(inverse.IsFunction() ? Napi::Persistent(inverse.As<Napi::Function>()) : Napi::FunctionReference()),
18 result(result.IsFunction() ? Napi::Persistent(result.As<Napi::Function>()) : Napi::FunctionReference()),
19 start(Napi::Persistent(start)) {}
20
21 static void xStep(sqlite3_context* invocation, int argc, sqlite3_value** argv) {
22 xStepBase(invocation, argc, argv, &CustomAggregate::fn);
23 }
24
25 static void xInverse(sqlite3_context* invocation, int argc, sqlite3_value** argv) {
26 xStepBase(invocation, argc, argv, &CustomAggregate::inverse);
27 }
28
29 static void xValue(sqlite3_context* invocation) {
30 xValueBase(invocation, false);
31 }
32
33 static void xFinal(sqlite3_context* invocation) {
34 xValueBase(invocation, true);
35 }
36
37private:
38
39 static inline void xStepBase(sqlite3_context* invocation, int argc, sqlite3_value** argv, const Napi::FunctionReference CustomAggregate::*ptrtm) {
40 AGGREGATE_START();
41
42 napi_value args_fast[5];
43 napi_value* args = argc <= 4 ? args_fast : ALLOC_ARRAY<napi_value>(argc + 1);
44 args[0] = acc->value.Value();
45 if (argc != 0) Data::GetArgumentsJS(env, args + 1, argv, argc, self->safe_ints);
46
47 Napi::Value returnValue = SafeCall(env, (self->*ptrtm).Value(), env.Undefined(), argc + 1, args);
48 if (args != args_fast) delete[] args;
49
50 if (env.IsExceptionPending()) {
51 self->PropagateJSError(invocation);
52 } else {
53 if (!returnValue.IsUndefined()) acc->value.Reset(returnValue, 1);
54 }
55 }
56
57 static inline void xValueBase(sqlite3_context* invocation, bool is_final) {
58 AGGREGATE_START();
59
60 if (!is_final) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected