ORM C++
Loading...
Searching...
No Matches
aggregate.cppm
Go to the documentation of this file.
1module;
2
3#include <concepts>
4#include <memory>
5#include <optional>
6#include <tuple>
7#include <type_traits>
8#include <utility>
9#include <variant>
10
11export module orm:query_aggregate;
12
13import :foundation;
14import :query_value;
15import :query_runtime_predicate;
16import :query_parameters;
17import :query_expression;
18import :query_columns;
19
20// query/Aggregate.hpp
21namespace orm::query::detail
22{
23class AggregatePredicate;
24struct AggregatePredicateNode;
25
26using AggregatePredicateNodePtr = std::shared_ptr<const AggregatePredicateNode>;
27
28enum class AggregateFunction
29{
30 Count,
31 CountAll,
32 Sum,
33 Avg,
34 Min,
35 Max,
36};
37
38struct AggregateExpression
39{
40 AggregateFunction function;
41 std::optional<Column> column = std::nullopt;
42
43 template <typename T>
44 auto operator==(T value) const -> AggregatePredicate;
45
46 template <typename T>
47 auto operator!=(T value) const -> AggregatePredicate;
48
49 template <typename T>
50 auto operator>(T value) const -> AggregatePredicate;
51
52 template <typename T>
53 auto operator>=(T value) const -> AggregatePredicate;
54
55 template <typename T>
56 auto operator<(T value) const -> AggregatePredicate;
57
58 template <typename T>
59 auto operator<=(T value) const -> AggregatePredicate;
60
61private:
62 auto compare(ComparisonOperator comparisonOperator, QueryValue value) const -> AggregatePredicate;
63};
64
65struct AggregateComparisonExpression
66{
67 AggregateExpression aggregate;
68 ComparisonOperator comparisonOperator;
69 QueryValue value;
70};
71
72struct AggregateLogicalExpression
73{
74 AggregatePredicateNodePtr left;
75 LogicalOperator logicalOperator;
76 AggregatePredicateNodePtr right;
77};
78
79struct AggregateNotExpression
80{
81 AggregatePredicateNodePtr predicate;
82};
83
84struct AggregatePredicateNode
85{
86 using Expression = std::variant<AggregateComparisonExpression, AggregateLogicalExpression, AggregateNotExpression>;
87
88 Expression expression;
89};
90
91class AggregatePredicate
92{
93public:
94 explicit AggregatePredicate(AggregatePredicateNode predicateNode)
95 : node{std::make_shared<AggregatePredicateNode>(std::move(predicateNode))}
96 {
97 }
98
99 [[nodiscard]] auto getNode() const -> const AggregatePredicateNode&
100 {
101 return *node;
102 }
103
104private:
105 AggregatePredicateNodePtr node;
106
107 friend auto operator&&(const AggregatePredicate& left, const AggregatePredicate& right) -> AggregatePredicate;
108 friend auto operator||(const AggregatePredicate& left, const AggregatePredicate& right) -> AggregatePredicate;
109 friend auto operator!(const AggregatePredicate& predicate) -> AggregatePredicate;
110};
111
112inline auto count(Column sourceColumn) -> AggregateExpression
113{
114 return AggregateExpression{.function = AggregateFunction::Count, .column = std::move(sourceColumn)};
115}
116
117inline auto countAll() -> AggregateExpression
118{
119 return AggregateExpression{.function = AggregateFunction::CountAll};
120}
121
122inline auto sum(Column sourceColumn) -> AggregateExpression
123{
124 return AggregateExpression{.function = AggregateFunction::Sum, .column = std::move(sourceColumn)};
125}
126
127inline auto avg(Column sourceColumn) -> AggregateExpression
128{
129 return AggregateExpression{.function = AggregateFunction::Avg, .column = std::move(sourceColumn)};
130}
131
132inline auto min(Column sourceColumn) -> AggregateExpression
133{
134 return AggregateExpression{.function = AggregateFunction::Min, .column = std::move(sourceColumn)};
135}
136
137inline auto max(Column sourceColumn) -> AggregateExpression
138{
139 return AggregateExpression{.function = AggregateFunction::Max, .column = std::move(sourceColumn)};
140}
141
142template <typename T>
143auto AggregateExpression::operator==(T value) const -> AggregatePredicate
144{
145 return compare(ComparisonOperator::Equal, QueryValue{std::move(value)});
146}
147
148template <typename T>
149auto AggregateExpression::operator!=(T value) const -> AggregatePredicate
150{
151 return compare(ComparisonOperator::NotEqual, QueryValue{std::move(value)});
152}
153
154template <typename T>
155auto AggregateExpression::operator>(T value) const -> AggregatePredicate
156{
157 return compare(ComparisonOperator::Greater, QueryValue{std::move(value)});
158}
159
160template <typename T>
161auto AggregateExpression::operator>=(T value) const -> AggregatePredicate
162{
163 return compare(ComparisonOperator::GreaterOrEqual, QueryValue{std::move(value)});
164}
165
166template <typename T>
167auto AggregateExpression::operator<(T value) const -> AggregatePredicate
168{
169 return compare(ComparisonOperator::Less, QueryValue{std::move(value)});
170}
171
172template <typename T>
173auto AggregateExpression::operator<=(T value) const -> AggregatePredicate
174{
175 return compare(ComparisonOperator::LessOrEqual, QueryValue{std::move(value)});
176}
177
178inline auto AggregateExpression::compare(ComparisonOperator comparisonOperator,
179 QueryValue value) const -> AggregatePredicate
180{
181 return AggregatePredicate{AggregatePredicateNode{
182 AggregateComparisonExpression{.aggregate = *this, .comparisonOperator = comparisonOperator, .value = value}}};
183}
184
185inline auto operator&&(const AggregatePredicate& left, const AggregatePredicate& right) -> AggregatePredicate
186{
187 return AggregatePredicate{AggregatePredicateNode{
188 AggregateLogicalExpression{.left = left.node, .logicalOperator = LogicalOperator::And, .right = right.node}}};
189}
190
191inline auto operator||(const AggregatePredicate& left, const AggregatePredicate& right) -> AggregatePredicate
192{
193 return AggregatePredicate{AggregatePredicateNode{
194 AggregateLogicalExpression{.left = left.node, .logicalOperator = LogicalOperator::Or, .right = right.node}}};
195}
196
197inline auto operator!(const AggregatePredicate& predicate) -> AggregatePredicate
198{
199 return AggregatePredicate{AggregatePredicateNode{AggregateNotExpression{.predicate = predicate.node}}};
200}
201} // namespace orm::query::detail
202
203namespace orm::query
204{
205export
206{
207
208 template <typename Owner>
210 {
211 public:
212 using Model = Owner;
213 using ParameterTypes = std::tuple<>;
215 inline static constexpr auto kind = ExprKind::DynamicAggregatePredicate;
216 inline static constexpr bool isPredicate = false;
217 inline static constexpr bool isAggregatePredicate = true;
218 inline static constexpr bool writeSafe = false;
219 inline static constexpr bool containsCollection = false;
220 inline static constexpr bool staticSqlEligible = false;
221 template <typename E>
222// requires detail::isExpression<E> && E::isAggregatePredicate && detail::ORM_QUERY_MODEL<E, Owner> &&
223// detail::ORM_QUERY_UNBOUND_PARAMETER<E>
224 TypedAggregatePredicate(const E& expression) : data{detail::erase(expression)}
225 {
226 }
227 template <typename E>
228// requires detail::isExpression<E> && E::isAggregatePredicate && detail::ORM_QUERY_MODEL<E, Owner> &&
229// detail::ORM_QUERY_UNBOUND_PARAMETER<E>
230 auto operator=(const E& expression) -> TypedAggregatePredicate&
231 {
232 data = detail::erase(expression);
233 return *this;
235 auto dynamic() const -> TypedAggregatePredicate
236 {
237 return *this;
238 }
239
240 private:
241 friend struct detail::TypedAccess;
242 explicit TypedAggregatePredicate(detail::AggregatePredicate value) : data{std::move(value)} {}
243 auto runtime() const -> detail::AggregatePredicate
244 {
245 return data;
246 }
247 detail::AggregatePredicate data;
248 };
249 template <typename Owner, typename ResultValue, bool Nullable>
250 class TypedAggregate
251 {
252 public:
253 using Model = Owner;
254 using Value = ResultValue;
255 using ParameterTypes = std::tuple<>;
256 inline static constexpr auto kind = ExprKind::DynamicAggregate;
257 inline static constexpr bool nullable = Nullable;
258 inline static constexpr bool isAggregate = true;
259 inline static constexpr bool writeSafe = false;
260 inline static constexpr bool containsCollection = false;
261 inline static constexpr bool staticSqlEligible = false;
262 template <typename E>
263// requires detail::isExpression<E> && E::isAggregate && detail::ORM_QUERY_MODEL<E, Owner> &&
264// std::same_as<typename E::Value, Value> &&
265// (E::nullable == Nullable) && detail::ORM_QUERY_UNBOUND_PARAMETER<E>
266 TypedAggregate(const E& expression) : data{detail::erase(expression)}
267 {
268 }
269 template <typename E>
270// requires detail::isExpression<E> && E::isAggregate && detail::ORM_QUERY_MODEL<E, Owner> &&
271// std::same_as<typename E::Value, Value> &&
272// (E::nullable == Nullable) && detail::ORM_QUERY_UNBOUND_PARAMETER<E>
273 auto operator=(const E& expression) -> TypedAggregate&
274 {
275 data = detail::erase(expression);
276 return *this;
277 }
278 template <typename T>
279// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T>
280 constexpr auto operator==(T&& value) const
281 {
282 return detail::comparison<detail::ComparisonOperator::Equal>(*this, std::forward<T>(value));
283 }
284
285 template <typename T>
286// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T>
287 constexpr auto operator!=(T&& value) const
288 {
289 return detail::comparison<detail::ComparisonOperator::NotEqual>(*this, std::forward<T>(value));
290 }
291
292 template <typename T>
293// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_ORDERABLE<Value>
294 constexpr auto operator>(T&& value) const
295 {
296 return detail::comparison<detail::ComparisonOperator::Greater>(*this, std::forward<T>(value));
297 }
298
299 template <typename T>
300// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_ORDERABLE<Value>
301 constexpr auto operator>=(T&& value) const
302 {
303 return detail::comparison<detail::ComparisonOperator::GreaterOrEqual>(*this, std::forward<T>(value));
304 }
305
306 template <typename T>
307// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_ORDERABLE<Value>
308 constexpr auto operator<(T&& value) const
309 {
310 return detail::comparison<detail::ComparisonOperator::Less>(*this, std::forward<T>(value));
311 }
312
313 template <typename T>
314// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_ORDERABLE<Value>
315 constexpr auto operator<=(T&& value) const
316 {
317 return detail::comparison<detail::ComparisonOperator::LessOrEqual>(*this, std::forward<T>(value));
319 auto dynamic() const -> TypedAggregate
320 {
321 return *this;
322 }
323
324 private:
325 friend struct detail::TypedAccess;
326 explicit TypedAggregate(detail::AggregateExpression value) : data{std::move(value)} {}
327 auto runtime() const -> detail::AggregateExpression
328 {
329 return data;
330 }
331 detail::AggregateExpression data;
332 };
333}
334} // namespace orm::query
335
336namespace orm::query::detail
337{
338template <AggregateFunction Function, typename C, typename M, typename V, bool N>
339struct AggregateMeta : ExpressionMeta<M, false>
340{
341 using Source = C;
342 using Value = V;
343 using Dynamic = TypedAggregate<M, V, N>;
344 inline static constexpr auto function = Function;
345 inline static constexpr bool nullable = N;
346 inline static constexpr bool isAggregate = true;
347 inline static constexpr bool staticSqlEligible = true;
348 template <typename Children, typename Args>
349 static auto render(const Children& children, const Args& args)
350 {
351 if constexpr (Function == AggregateFunction::CountAll)
352 return AggregateExpression{.function = Function};
353 else
354 return AggregateExpression{.function = Function, .column = TypedAccess::erase(std::get<0>(children), args)};
355 }
356 template <typename Children, typename Args, typename Callback>
357 static auto visit(const Children&, const Args&, Callback&) -> void
358 {
359 }
360};
361template <typename M>
362struct IsTypedAggregatePredicate<TypedAggregatePredicate<M>> : std::true_type
363{
364};
365template <typename E>
366struct IsTypedAggregate : std::false_type
367{
368};
369template <typename M, typename V, bool N>
370struct IsTypedAggregate<TypedAggregate<M, V, N>> : std::true_type
371{
372};
373template <typename E>
374concept AggregateExpressionType =
375 (IsTypedAggregate<std::remove_cvref_t<E>>::value || isExpression<E>) && std::remove_cvref_t<E>::isAggregate;
376template <typename E, typename M>
377concept AnyAggregatePredicateFor =
378 (IsTypedAggregatePredicate<std::remove_cvref_t<E>>::value || isExpression<E>) &&
379 std::remove_cvref_t<E>::isAggregatePredicate && ORM_QUERY_MODEL<std::remove_cvref_t<E>, M>;
380template <typename E, typename M>
381concept AggregatePredicateFor = AnyAggregatePredicateFor<E, M> && ORM_QUERY_UNBOUND_PARAMETER<E>;
382template <typename E, typename M>
383concept PlanAggregatePredicateFor = AnyAggregatePredicateFor<E, M>;
384} // namespace orm::query::detail
385
386namespace orm::query
387{
388export
389{
390
391 template <auto... Members>
392 constexpr auto count(TypedColumn<Members...> column)
393 {
394 using C = TypedColumn<Members...>;
395 return detail::makeExpression<ExprKind::Aggregate, detail::AggregateMeta<detail::AggregateFunction::Count, C,
396 typename C::Model, long long, false>>(
397 column);
398 }
399 template <typename Model>
400 constexpr auto countAll()
401 {
402 return detail::makeExpression<ExprKind::Aggregate, detail::AggregateMeta<detail::AggregateFunction::CountAll,
403 void, Model, long long, false>>();
404 }
405 template <auto... Members>
406// requires detail::ORM_QUERY_NUMERIC<typename TypedColumn<Members...>::Value>
407 constexpr auto sum(TypedColumn<Members...> column)
408 {
409 using C = TypedColumn<Members...>;
410 using V = std::conditional_t<std::is_integral_v<typename C::Value>, long long, double>;
411 return detail::makeExpression<
412 ExprKind::Aggregate, detail::AggregateMeta<detail::AggregateFunction::Sum, C, typename C::Model, V, true>>(
413 column);
414 }
415 template <auto... Members>
416// requires detail::ORM_QUERY_NUMERIC<typename TypedColumn<Members...>::Value>
417 constexpr auto avg(TypedColumn<Members...> column)
418 {
419 using C = TypedColumn<Members...>;
420 return detail::makeExpression<ExprKind::Aggregate, detail::AggregateMeta<detail::AggregateFunction::Avg, C,
421 typename C::Model, double, true>>(
422 column);
423 }
424 template <auto... Members>
425// requires detail::ORM_QUERY_ORDERABLE<typename TypedColumn<Members...>::Value>
426 constexpr auto min(TypedColumn<Members...> column)
427 {
428 using C = TypedColumn<Members...>;
429 return detail::makeExpression<
431 detail::AggregateMeta<detail::AggregateFunction::Min, C, typename C::Model, typename C::Value, true>>(
432 column);
433 }
434 template <auto... Members>
435// requires detail::ORM_QUERY_ORDERABLE<typename TypedColumn<Members...>::Value>
436 constexpr auto max(TypedColumn<Members...> column)
437 {
438 using C = TypedColumn<Members...>;
439 return detail::makeExpression<
441 detail::AggregateMeta<detail::AggregateFunction::Max, C, typename C::Model, typename C::Value, true>>(
442 column);
443 }
444}
445} // namespace orm::query
A value that can be bound as a query parameter.
Definition value.cppm:33
TypedAggregatePredicate(const E &expression)
static constexpr bool isPredicate
auto operator=(const E &expression) -> TypedAggregatePredicate &
static constexpr bool staticSqlEligible
auto dynamic() const -> TypedAggregatePredicate
static constexpr bool isAggregatePredicate
static constexpr bool containsCollection
constexpr auto operator<=(T &&value) const
constexpr auto operator!=(T &&value) const
auto dynamic() const -> TypedAggregate
auto operator=(const E &expression) -> TypedAggregate &
static constexpr bool staticSqlEligible
TypedAggregate(const E &expression)
constexpr auto operator<(T &&value) const
static constexpr bool containsCollection
constexpr auto operator>(T &&value) const
constexpr auto operator==(T &&value) const
constexpr auto operator>=(T &&value) const
orm::query::detail::AggregateExpression AggregateExpression
constexpr auto operator!(const E &value)
Definition columns.cppm:439
constexpr auto min(TypedColumn< Members... > column)
constexpr auto countAll()
constexpr auto avg(TypedColumn< Members... > column)
constexpr auto sum(TypedColumn< Members... > column)
constexpr auto max(TypedColumn< Members... > column)
constexpr auto operator&&(const L &left, const R &right)
Definition columns.cppm:423
constexpr auto count(TypedColumn< Members... > column)
constexpr auto operator||(const L &left, const R &right)
Definition columns.cppm:432