12template <
class T,
class... Args>
14 { t.update(args...) };
17template <
class T,
class... Args>
22template <
class T,
class... Args>
24 { t.serialize() } -> std::same_as<bytea>;
25 { T(ba) } -> std::same_as<T>;
28template <
class T,
class... Args>
30 { T(t, t1) } -> std::same_as<T>;
33template <
class Agg,
typename... Args>
35 auto *state = ctx.template alloc<Agg>();
36 std::construct_at(state, std::forward<Args>(args)...);
37 if constexpr (!std::is_trivially_destructible_v<Agg>) {
38 ctx.register_reset_callback([](
void *arg) { std::destroy_at(
static_cast<Agg *
>(arg)); }, state);
43template <
class Agg,
typename... Args>
44Agg *construct_aggregate_state(abstract_memory_context &&ctx, Args &&...args) {
45 return construct_aggregate_state<Agg>(ctx, std::forward<Args>(args)...);
48template <
class Agg,
typename... InTs> datum aggregate_sfunc(value state, InTs... args) {
51 if (!ffi_guard{::AggCheckCallContext}(current_postgres_function::call_info().operator*(),
53 report(ERROR,
"not aggregate context");
56 if constexpr (!convertible_into_datum<Agg> && finalizable_aggregate<Agg, InTs...>) {
58 if (state.get_nullable_datum().is_null()) {
59 state0 = construct_aggregate_state<Agg>(memory_context(aggctx));
61 state0 =
reinterpret_cast<Agg *
>(
62 from_nullable_datum<void *>(state.get_nullable_datum(), state.get_type().oid));
65 state0->update(args...);
68 }
else if constexpr (convertible_into_datum<Agg>) {
70 state0.update(args...);
73 report(ERROR,
"not supported");
74 __builtin_unreachable();
77template <
class Agg,
typename... InTs> nullable_datum aggregate_ffunc(value state) {
78 if constexpr (finalizable_aggregate<Agg, InTs...>) {
80 if (state.get_nullable_datum().is_null()) {
81 state0 = construct_aggregate_state<Agg>(memory_context());
83 state0 =
reinterpret_cast<Agg *
>(
84 from_nullable_datum<void *>(state.get_nullable_datum(), state.get_type().oid));
86 return into_nullable_datum(state0->finalize());
88 report(ERROR,
"this aggregate does not support final function");
89 __builtin_unreachable();
93template <
class Agg,
typename... InTs> bytea aggregate_serial(value state) {
94 if constexpr (serializable_aggregate<Agg, InTs...>) {
95 if (state.get_type().oid == INTERNALOID) {
97 state0 =
reinterpret_cast<Agg *
>(
98 from_nullable_datum<void *>(state.get_nullable_datum(), state.get_type().oid));
99 bytea ba = state0->serialize();
103 report(ERROR,
"this aggregate does not support serialize");
104 __builtin_unreachable();
107template <
class Agg,
typename... InTs> datum aggregate_deserial(bytea ba, value) {
108 if constexpr (serializable_aggregate<Agg, InTs...>) {
109 MemoryContext aggctx;
110 if (!ffi_guard{::AggCheckCallContext}(current_postgres_function::call_info().operator*(),
112 report(ERROR,
"not aggregate context");
114 Agg *state0 = construct_aggregate_state<Agg>(memory_context(aggctx), ba);
117 report(ERROR,
"this aggregate does not support serialize");
118 __builtin_unreachable();
121template <
class Agg,
typename... InTs> datum aggregate_combine(value state, value other) {
122 MemoryContext aggctx;
123 if (!ffi_guard{::AggCheckCallContext}(current_postgres_function::call_info().operator*(),
125 report(ERROR,
"not aggregate context");
127 if constexpr (combinable_aggregate<Agg, InTs...>) {
128 if constexpr (!convertible_into_datum<Agg> && finalizable_aggregate<Agg, InTs...>) {
130 if (state.get_nullable_datum().is_null()) {
131 state0 = construct_aggregate_state<Agg>(memory_context(aggctx));
133 state0 =
reinterpret_cast<Agg *
>(
134 from_nullable_datum<void *>(state.get_nullable_datum(), state.get_type().oid));
138 if (other.get_nullable_datum().is_null()) {
139 state1 = construct_aggregate_state<Agg>(memory_context(aggctx));
141 state1 =
reinterpret_cast<Agg *
>(
142 from_nullable_datum<void *>(other.get_nullable_datum(), other.get_type().oid));
145 Agg *newstate = construct_aggregate_state<Agg>(memory_context(aggctx), *state0, *state1);
148 }
else if constexpr (convertible_into_datum<Agg>) {
154 report(ERROR,
"not supported");
155 __builtin_unreachable();
160#define declare_aggregate(name, typname, ...) \
161 static_assert(::cppgres::aggregate<typname, ##__VA_ARGS__>); \
162 static_assert(::cppgres::convertible_into_datum<typname> || \
163 ::cppgres::finalizable_aggregate<typname, ##__VA_ARGS__>, \
164 "must be convertible to datum or have finalize()"); \
165 postgres_function(name##_sfunc, (cppgres::aggregate_sfunc<typname, ##__VA_ARGS__>)); \
166 postgres_function(name##_ffunc, (cppgres::aggregate_ffunc<typname, ##__VA_ARGS__>)); \
167 postgres_function(name##_serial, (cppgres::aggregate_serial<typname, ##__VA_ARGS__>)); \
168 postgres_function(name##_deserial, (cppgres::aggregate_deserial<typname, ##__VA_ARGS__>)); \
169 postgres_function(name##_combine, (cppgres::aggregate_combine<typname, ##__VA_ARGS__>));
Definition: aggregate.hpp:13
Definition: aggregate.hpp:29
Definition: datum.hpp:171
Definition: aggregate.hpp:18
Definition: aggregate.hpp:23
Definition: memory.hpp:14
static T from_nullable_datum(const nullable_datum &d, const oid oid, std::optional< memory_context > context=std::nullopt)=delete
Convert from a nullable datum.
static datum into_datum(const T &d)=delete
Convert datum into a type.