ORM C++
Loading...
Searching...
No Matches
expression.cppm
Go to the documentation of this file.
1module;
2
3#include <array>
4#include <cstddef>
5#include <span>
6#include <stdexcept>
7#include <tuple>
8#include <type_traits>
9#include <utility>
10#include <vector>
11
12export module orm:query_expression;
13
14import :query_value;
15import :query_runtime_predicate;
16import :query_parameters;
17
18// query/Expression.hpp
19namespace orm::query::detail
20{
21template <ComparisonOperator Op, typename S, typename T>
22constexpr auto comparison(const S& source, T&& value);
23}
24
25namespace orm::query
26{
27export
28{
29
30 template <typename Owner, bool WriteSafe, bool ContainsCollection>
31 class TypedPredicate;
32 template <typename Owner>
53 };
54
55 template <ExprKind Kind, typename Meta, typename... Children>
56 class Expression : public Meta
57 {
58 public:
59 inline static constexpr ExprKind kind = Kind;
60 using ParameterTypes = detail::ConcatTuples<detail::ParameterTypes<Children>...>;
61 inline static constexpr auto parameterSlots = detail::SlotIndices<ParameterTypes>::value;
63 auto dynamic() const
64// requires detail::ORM_QUERY_UNBOUND_PARAMETER<Expression>
65 {
66 return detail::TypedAccess::make<typename Meta::Dynamic>(runtime());
67 }
68
69 template <typename T>
70// requires(Kind == ExprKind::Aggregate) && detail::ORM_QUERY_VALUE_OR_PARAMETER<typename Meta::Value, T>
71 constexpr auto operator==(T&& value) const
72 {
73 return detail::comparison<detail::ComparisonOperator::Equal>(*this, std::forward<T>(value));
74 }
75
76 template <typename T>
77// requires(Kind == ExprKind::Aggregate) && detail::ORM_QUERY_VALUE_OR_PARAMETER<typename Meta::Value, T>
78 constexpr auto operator!=(T&& value) const
79 {
80 return detail::comparison<detail::ComparisonOperator::NotEqual>(*this, std::forward<T>(value));
81 }
82
83 template <typename T>
84// requires(Kind == ExprKind::Aggregate) && detail::ORM_QUERY_VALUE_OR_PARAMETER<typename Meta::Value, T> &&
85// detail::ORM_QUERY_ORDERABLE<typename Meta::Value>
86 constexpr auto operator>(T&& value) const
87 {
88 return detail::comparison<detail::ComparisonOperator::Greater>(*this, std::forward<T>(value));
89 }
90
91 template <typename T>
92// requires(Kind == ExprKind::Aggregate) && detail::ORM_QUERY_VALUE_OR_PARAMETER<typename Meta::Value, T> &&
93// detail::ORM_QUERY_ORDERABLE<typename Meta::Value>
94 constexpr auto operator>=(T&& value) const
95 {
96 return detail::comparison<detail::ComparisonOperator::GreaterOrEqual>(*this, std::forward<T>(value));
97 }
98
99 template <typename T>
100// requires(Kind == ExprKind::Aggregate) && detail::ORM_QUERY_VALUE_OR_PARAMETER<typename Meta::Value, T> &&
101// detail::ORM_QUERY_ORDERABLE<typename Meta::Value>
102 constexpr auto operator<(T&& value) const
103 {
104 return detail::comparison<detail::ComparisonOperator::Less>(*this, std::forward<T>(value));
105 }
106
107 template <typename T>
108// requires(Kind == ExprKind::Aggregate) && detail::ORM_QUERY_VALUE_OR_PARAMETER<typename Meta::Value, T> &&
109// detail::ORM_QUERY_ORDERABLE<typename Meta::Value>
110 constexpr auto operator<=(T&& value) const
111 {
112 return detail::comparison<detail::ComparisonOperator::LessOrEqual>(*this, std::forward<T>(value));
113 }
114
115 private:
116 friend struct detail::TypedAccess;
117 constexpr explicit Expression(Children... childrenInit) : children{std::move(childrenInit)...} {}
118 auto runtime() const
119// requires detail::ORM_QUERY_UNBOUND_PARAMETER<Expression>
120 {
121 return runtime(std::tuple<>{});
122 }
123 template <typename Args>
124 auto runtime(const Args& args) const
125 {
126 return Meta::render(children, args);
127 }
128 std::tuple<Children...> children;
129 };
130}
131} // namespace orm::query
132
133namespace orm::query::detail
134{
135template <typename E>
136struct IsExpression : std::false_type
137{
138};
139template <ExprKind K, typename M, typename... C>
140struct IsExpression<Expression<K, M, C...>> : std::true_type
141{
142};
143template <typename E>
144inline constexpr bool isExpression = IsExpression<std::remove_cvref_t<E>>::value;
145template <typename E>
146inline constexpr bool staticSqlEligible = []
147{
148 if constexpr (requires { E::staticSqlEligible; })
149 return E::staticSqlEligible;
150 else
151 return false;
152}();
153template <ExprKind K, typename Meta, typename... Children>
154constexpr auto makeExpression(Children&&... children)
155{
156 using E = Expression<K, Meta, std::remove_cvref_t<Children>...>;
157 return TypedAccess::make<E>(std::forward<Children>(children)...);
158}
159template <typename E, typename Args>
160auto bindExpression(const E& expression, const Args& args)
161{
162 if constexpr (isExpression<E>)
163 return TypedAccess::make<typename E::Dynamic>(TypedAccess::erase(expression, args));
164 else
165 return expression;
166}
167template <typename E, typename Args, typename Callback>
168auto visitValues(const E& expression, const Args& args, Callback&& callback) -> void
169{
170 if constexpr (isExpression<E>)
171 E::visit(TypedAccess::children(expression), args, callback);
172}
173template <typename M, bool W = true, bool C = false>
174struct ExpressionMeta
175{
176 using Model = M;
177 using Value = void;
178 inline static constexpr bool writeSafe = W;
179 inline static constexpr bool containsCollection = C;
180 inline static constexpr bool nullable = false;
181 inline static constexpr bool isPredicate = false;
182 inline static constexpr bool isAggregatePredicate = false;
183 inline static constexpr bool isAggregate = false;
184 inline static constexpr bool isColumn = false;
185 inline static constexpr bool isOrder = false;
186 inline static constexpr bool isProjection = false;
187};
188template <typename S, ComparisonOperator Op>
189struct ComparisonMeta : ExpressionMeta<typename S::Model, S::writeSafe, S::containsCollection>
190{
191 using Source = S;
192 using Dynamic = std::conditional_t<S::isAggregate, TypedAggregatePredicate<typename S::Model>,
193 TypedPredicate<typename S::Model, S::writeSafe, S::containsCollection>>;
194 inline static constexpr auto operation = Op;
195 inline static constexpr bool isPredicate = !S::isAggregate;
196 inline static constexpr bool isAggregatePredicate = S::isAggregate;
197 inline static constexpr bool staticSqlEligible = detail::staticSqlEligible<S>;
198 template <typename Children, typename Args>
199 static auto render(const Children& children, const Args& args)
200 {
201 const auto source = TypedAccess::erase(std::get<0>(children), args);
202 const auto value = typedValue<typename S::Value>(resolveValue(std::get<1>(children), args));
203 if constexpr (S::isAggregate)
204 {
205 if constexpr (Op == ComparisonOperator::Equal)
206 return source == value;
207 else if constexpr (Op == ComparisonOperator::NotEqual)
208 return source != value;
209 else if constexpr (Op == ComparisonOperator::Greater)
210 return source > value;
211 else if constexpr (Op == ComparisonOperator::GreaterOrEqual)
212 return source >= value;
213 else if constexpr (Op == ComparisonOperator::Less)
214 return source < value;
215 else
216 return source <= value;
217 }
218 else
219 return Predicate{PredicateNode{ComparisonExpression{source, Op, value}}};
220 }
221 template <typename Children, typename Args, typename Callback>
222 static auto visit(const Children& children, const Args& args, Callback& callback) -> void
223 {
224 callback(typedValue<typename S::Value>(resolveValue(std::get<1>(children), args)));
225 }
226};
227template <ComparisonOperator Op, typename S, typename T>
228constexpr auto comparison(const S& source, T&& value)
229{
230 return makeExpression<ExprKind::Comparison, ComparisonMeta<S, Op>>(source, captureValue(std::forward<T>(value)));
231}
232template <typename S, NullOperator Op>
233struct NullMeta : ExpressionMeta<typename S::Model, S::writeSafe, S::containsCollection>
234{
235 using Source = S;
236 using Dynamic = TypedPredicate<typename S::Model, S::writeSafe, S::containsCollection>;
237 inline static constexpr auto operation = Op;
238 inline static constexpr bool isPredicate = true;
239 inline static constexpr bool staticSqlEligible = detail::staticSqlEligible<S>;
240 template <typename Children, typename Args>
241 static auto render(const Children& children, const Args& args)
242 {
243 return Predicate{PredicateNode{NullExpression{TypedAccess::erase(std::get<0>(children), args), Op}}};
244 }
245 template <typename Children, typename Args, typename Callback>
246 static auto visit(const Children&, const Args&, Callback&) -> void
247 {
248 }
249};
250template <typename S, BetweenOperator Op>
251struct BetweenMeta : ExpressionMeta<typename S::Model, S::writeSafe, S::containsCollection>
252{
253 using Source = S;
254 using Dynamic = TypedPredicate<typename S::Model, S::writeSafe, S::containsCollection>;
255 inline static constexpr auto operation = Op;
256 inline static constexpr bool isPredicate = true;
257 inline static constexpr bool staticSqlEligible = detail::staticSqlEligible<S>;
258 template <typename Children, typename Args>
259 static auto render(const Children& children, const Args& args)
260 {
261 return Predicate{
262 PredicateNode{BetweenExpression{TypedAccess::erase(std::get<0>(children), args), Op,
263 typedValue<typename S::Value>(resolveValue(std::get<1>(children), args)),
264 typedValue<typename S::Value>(resolveValue(std::get<2>(children), args))}}};
265 }
266 template <typename Children, typename Args, typename Callback>
267 static auto visit(const Children& children, const Args& args, Callback& callback) -> void
268 {
269 callback(typedValue<typename S::Value>(resolveValue(std::get<1>(children), args)));
270 callback(typedValue<typename S::Value>(resolveValue(std::get<2>(children), args)));
271 }
272};
273template <typename... V>
274struct FixedValues
275{
276 std::tuple<V...> values;
277 using ParameterTypes = ConcatTuples<detail::ParameterTypes<V>...>;
278};
279template <typename T>
280struct ListTraits
281{
282 inline static constexpr bool fixed = false;
283 inline static constexpr auto size = std::dynamic_extent;
284};
285template <typename... V>
286struct ListTraits<FixedValues<V...>>
287{
288 inline static constexpr bool fixed = true;
289 inline static constexpr std::size_t size = sizeof...(V);
290};
291template <typename V, std::size_t N, std::size_t I>
292struct ListTraits<Parameter<std::array<V, N>, I>>
293{
294 inline static constexpr bool fixed = true;
295 inline static constexpr std::size_t size = N;
296};
297template <typename S, ListOperator Op, typename Stored>
298struct ListMeta : ExpressionMeta<typename S::Model, S::writeSafe, S::containsCollection>
299{
300 using Source = S;
301 using Values = Stored;
302 using Dynamic = TypedPredicate<typename S::Model, S::writeSafe, S::containsCollection>;
303 inline static constexpr auto operation = Op;
304 inline static constexpr bool isFixedList = ListTraits<Stored>::fixed;
305 inline static constexpr auto arity = ListTraits<Stored>::size;
306 inline static constexpr bool isPredicate = true;
307 inline static constexpr bool staticSqlEligible = detail::staticSqlEligible<S> && isFixedList;
308 static_assert(!isFixedList || arity != 0, "ORM_QUERY_EMPTY_IN: IN requires at least one value");
309 template <typename Children, typename Args, typename Callback>
310 static auto visit(const Children& children, const Args& args, Callback& callback) -> void
311 {
312 const auto& stored = std::get<1>(children);
313 if constexpr (requires { stored.values; })
314 std::apply([&](const auto&... value)
315 { (callback(typedValue<typename S::Value>(resolveValue(value, args))), ...); }, stored.values);
316 else
317 {
318 const auto& values = resolveValue(stored, args);
319 if (values.empty())
320 throw std::invalid_argument{"IN predicate requires at least one value"};
321 for (const auto& value : values)
322 callback(typedValue<typename S::Value>(
323 static_cast<typename std::remove_cvref_t<decltype(values)>::value_type>(value)));
324 }
325 }
326 template <typename Children, typename Args>
327 static auto render(const Children& children, const Args& args)
328 {
329 std::vector<QueryValue> values;
330 auto append = [&](QueryValue value) { values.push_back(std::move(value)); };
331 visit(children, args, append);
332 return Predicate{
333 PredicateNode{ListExpression{TypedAccess::erase(std::get<0>(children), args), Op, std::move(values)}}};
334 }
335};
336template <ListOperator Op, typename S, typename... V>
337constexpr auto fixedList(const S& source, V&&... value)
338{
339 auto values = FixedValues<decltype(captureValue(std::forward<V>(value)))...>{
340 std::tuple{captureValue(std::forward<V>(value))...}};
341 return makeExpression<ExprKind::List, ListMeta<S, Op, decltype(values)>>(source, std::move(values));
342}
343template <ListOperator Op, typename S, typename Container>
344constexpr auto containerList(const S& source, const Container& values)
345{
346 using C = std::remove_cvref_t<Container>;
347 if constexpr (isParameter<C>)
348 return makeExpression<ExprKind::List, ListMeta<S, Op, C>>(source, values);
349 else if constexpr (ContainerTraits<C>::fixed)
350 return [&]<std::size_t... I>(std::index_sequence<I...>)
351 { return fixedList<Op>(source, values[I]...); }(std::make_index_sequence<ContainerTraits<C>::size>{});
352 else
353 {
354 if (values.size() == 0)
355 throw std::invalid_argument{"IN predicate requires at least one value"};
356 using V = decltype(captureValue(std::declval<typename C::value_type>()));
357 std::vector<V> owned;
358 owned.reserve(values.size());
359 for (const auto& value : values)
360 owned.push_back(captureValue(static_cast<typename C::value_type>(value)));
361 return makeExpression<ExprKind::List, ListMeta<S, Op, decltype(owned)>>(source, std::move(owned));
362 }
363}
364template <typename L, typename R, LogicalOperator Op>
365struct LogicalMeta
366 : ExpressionMeta<typename L::Model, L::writeSafe && R::writeSafe, L::containsCollection || R::containsCollection>
367{
368 using Left = L;
369 using Right = R;
370 using Dynamic = std::conditional_t<L::isAggregatePredicate, TypedAggregatePredicate<typename L::Model>,
371 TypedPredicate<typename L::Model, L::writeSafe && R::writeSafe,
372 L::containsCollection || R::containsCollection>>;
373 inline static constexpr auto operation = Op;
374 inline static constexpr bool isPredicate = L::isPredicate;
375 inline static constexpr bool isAggregatePredicate = L::isAggregatePredicate;
376 inline static constexpr bool staticSqlEligible = detail::staticSqlEligible<L> && detail::staticSqlEligible<R>;
377 template <typename Children, typename Args>
378 static auto render(const Children& children, const Args& args)
379 {
380 const auto left = TypedAccess::erase(std::get<0>(children), args);
381 const auto right = TypedAccess::erase(std::get<1>(children), args);
382 if constexpr (Op == LogicalOperator::And)
383 return left && right;
384 else
385 return left || right;
386 }
387 template <typename Children, typename Args, typename Callback>
388 static auto visit(const Children& children, const Args& args, Callback& callback) -> void
389 {
390 visitValues(std::get<0>(children), args, callback);
391 visitValues(std::get<1>(children), args, callback);
392 }
393};
394template <typename E>
395struct NotMeta : ExpressionMeta<typename E::Model, E::writeSafe, E::containsCollection>
396{
397 using Child = E;
398 using Dynamic = std::conditional_t<E::isAggregatePredicate, TypedAggregatePredicate<typename E::Model>,
399 TypedPredicate<typename E::Model, E::writeSafe, E::containsCollection>>;
400 inline static constexpr bool isPredicate = E::isPredicate;
401 inline static constexpr bool isAggregatePredicate = E::isAggregatePredicate;
402 inline static constexpr bool staticSqlEligible = detail::staticSqlEligible<E>;
403 template <typename Children, typename Args>
404 static auto render(const Children& children, const Args& args)
405 {
406 return !TypedAccess::erase(std::get<0>(children), args);
407 }
408 template <typename Children, typename Args, typename Callback>
409 static auto visit(const Children& children, const Args& args, Callback& callback) -> void
410 {
411 visitValues(std::get<0>(children), args, callback);
412 }
413};
414} // namespace orm::query::detail
static constexpr ExprKind kind
constexpr auto operator>(T &&value) const
constexpr auto operator<=(T &&value) const
constexpr auto operator!=(T &&value) const
detail::ConcatTuples< detail::ParameterTypes< Children >... > ParameterTypes
constexpr auto operator<(T &&value) const
constexpr auto operator==(T &&value) const
static constexpr auto parameterSlots
constexpr auto operator>=(T &&value) const
orm::query::detail::PredicateNode PredicateNode
orm::query::detail::BetweenExpression BetweenExpression
orm::query::detail::NullExpression NullExpression
orm::query::detail::ListExpression ListExpression
orm::query::detail::ComparisonExpression ComparisonExpression
query::detail::Predicate Predicate