10template <
class T,
class... Args>
12 { t.update(args...) };
15template <
class T,
class... Args>
20template <
class T,
class... Args>
22 { t.serialize() } -> std::same_as<bytea>;
23 { T(ba) } -> std::same_as<T>;
26template <
class T,
class... Args>
28 { T(t, t1) } -> std::same_as<T>;
31template <
class Agg,
typename... InTs>
datum aggregate_sfunc(
value state, InTs... args) {
34 if (!
ffi_guard{::AggCheckCallContext}(current_postgres_function::call_info().operator*(),
36 report(ERROR,
"not aggregate context");
39 if constexpr (!convertible_into_datum<Agg> && finalizable_aggregate<Agg, InTs...>) {
41 if (state.get_nullable_datum().is_null()) {
42 state0 = memory_context(aggctx).alloc<Agg>();
43 std::construct_at(state0);
45 state0 =
reinterpret_cast<Agg *
>(
46 from_nullable_datum<void *>(state.get_nullable_datum(), state.get_type().oid));
49 state0->update(args...);
52 }
else if constexpr (convertible_into_datum<Agg>) {
54 state0.update(args...);
57 report(ERROR,
"not supported");
58 __builtin_unreachable();
61template <
class Agg,
typename... InTs> nullable_datum aggregate_ffunc(value state) {
62 if constexpr (finalizable_aggregate<Agg, InTs...>) {
64 if (state.get_nullable_datum().is_null()) {
65 state0 = memory_context(memory_context()).alloc<Agg>();
66 std::construct_at(state0);
68 state0 =
reinterpret_cast<Agg *
>(
69 from_nullable_datum<void *>(state.get_nullable_datum(), state.get_type().oid));
71 return into_nullable_datum(state0->finalize());
73 report(ERROR,
"this aggregate does not support final function");
74 __builtin_unreachable();
78template <
class Agg,
typename... InTs> bytea aggregate_serial(value state) {
79 if constexpr (serializable_aggregate<Agg, InTs...>) {
80 if (state.get_type().oid == INTERNALOID) {
82 state0 =
reinterpret_cast<Agg *
>(
83 from_nullable_datum<void *>(state.get_nullable_datum(), state.get_type().oid));
84 bytea ba = state0->serialize();
88 report(ERROR,
"this aggregate does not support serialize");
89 __builtin_unreachable();
92template <
class Agg,
typename... InTs> datum aggregate_deserial(bytea ba, value) {
93 if constexpr (serializable_aggregate<Agg, InTs...>) {
95 if (!ffi_guard{::AggCheckCallContext}(current_postgres_function::call_info().operator*(),
97 report(ERROR,
"not aggregate context");
99 Agg *state0 = memory_context(aggctx).alloc<Agg>();
100 std::construct_at(state0, ba);
103 report(ERROR,
"this aggregate does not support serialize");
104 __builtin_unreachable();
107template <
class Agg,
typename... InTs> datum aggregate_combine(value state, value other) {
108 MemoryContext aggctx;
109 if (!ffi_guard{::AggCheckCallContext}(current_postgres_function::call_info().operator*(),
111 report(ERROR,
"not aggregate context");
113 if constexpr (combinable_aggregate<Agg, InTs...>) {
114 if constexpr (!convertible_into_datum<Agg> && finalizable_aggregate<Agg, InTs...>) {
116 if (state.get_nullable_datum().is_null()) {
117 state0 = memory_context(aggctx).alloc<Agg>();
118 std::construct_at(state0);
120 state0 =
reinterpret_cast<Agg *
>(
121 from_nullable_datum<void *>(state.get_nullable_datum(), state.get_type().oid));
125 if (other.get_nullable_datum().is_null()) {
126 state1 = memory_context(aggctx).alloc<Agg>();
127 std::construct_at(state1);
129 state1 =
reinterpret_cast<Agg *
>(
130 from_nullable_datum<void *>(other.get_nullable_datum(), other.get_type().oid));
133 Agg *newstate = memory_context(aggctx).alloc<Agg>();
134 std::construct_at(newstate, *state0, *state1);
137 }
else if constexpr (convertible_into_datum<Agg>) {
143 report(ERROR,
"not supported");
144 __builtin_unreachable();
149#define declare_aggregate(name, typname, ...) \
150 static_assert(::cppgres::aggregate<typname, ##__VA_ARGS__>); \
151 static_assert(::cppgres::convertible_into_datum<typname> || \
152 ::cppgres::finalizable_aggregate<typname, ##__VA_ARGS__>, \
153 "must be convertible to datum or have finalize()"); \
154 postgres_function(name##_sfunc, (cppgres::aggregate_sfunc<typname, ##__VA_ARGS__>)); \
155 postgres_function(name##_ffunc, (cppgres::aggregate_ffunc<typname, ##__VA_ARGS__>)); \
156 postgres_function(name##_serial, (cppgres::aggregate_serial<typname, ##__VA_ARGS__>)); \
157 postgres_function(name##_deserial, (cppgres::aggregate_deserial<typname, ##__VA_ARGS__>)); \
158 postgres_function(name##_combine, (cppgres::aggregate_combine<typname, ##__VA_ARGS__>));
Definition: aggregate.hpp:11
Definition: aggregate.hpp:27
Definition: datum.hpp:171
Definition: aggregate.hpp:16
Definition: aggregate.hpp:21
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.