15import :query_runtime_predicate;
16import :query_parameters;
19namespace orm::query::detail
21template <ComparisonOperator Op,
typename S,
typename T>
22constexpr auto comparison(
const S& source, T&& value);
30 template <
typename Owner,
bool WriteSafe,
bool ContainsCollection>
32 template <
typename Owner>
55 template <
ExprKind Kind,
typename Meta,
typename... Children>
56 class Expression :
public Meta
60 using ParameterTypes = detail::ConcatTuples<detail::ParameterTypes<Children>...>;
61 inline static constexpr auto parameterSlots = detail::SlotIndices<ParameterTypes>::value;
66 return detail::TypedAccess::make<typename Meta::Dynamic>(runtime());
73 return detail::comparison<detail::ComparisonOperator::Equal>(*
this, std::forward<T>(value));
80 return detail::comparison<detail::ComparisonOperator::NotEqual>(*
this, std::forward<T>(value));
88 return detail::comparison<detail::ComparisonOperator::Greater>(*
this, std::forward<T>(value));
96 return detail::comparison<detail::ComparisonOperator::GreaterOrEqual>(*
this, std::forward<T>(value));
102 constexpr auto operator<(T&& value)
const
104 return detail::comparison<detail::ComparisonOperator::Less>(*
this, std::forward<T>(value));
107 template <
typename T>
112 return detail::comparison<detail::ComparisonOperator::LessOrEqual>(*
this, std::forward<T>(value));
116 friend struct detail::TypedAccess;
117 constexpr explicit Expression(Children... childrenInit) : children{std::move(childrenInit)...} {}
121 return runtime(std::tuple<>{});
123 template <
typename Args>
124 auto runtime(
const Args& args)
const
126 return Meta::render(children, args);
128 std::tuple<Children...> children;
133namespace orm::query::detail
136struct IsExpression : std::false_type
139template <
ExprKind K,
typename M,
typename... C>
140struct IsExpression<Expression<K, M, C...>> : std::true_type
144inline constexpr bool isExpression = IsExpression<std::remove_cvref_t<E>>::value;
146inline constexpr bool staticSqlEligible = []
148 if constexpr (
requires { E::staticSqlEligible; })
149 return E::staticSqlEligible;
153template <
ExprKind K,
typename Meta,
typename... Children>
154constexpr auto makeExpression(Children&&... children)
156 using E = Expression<K, Meta, std::remove_cvref_t<Children>...>;
157 return TypedAccess::make<E>(std::forward<Children>(children)...);
159template <
typename E,
typename Args>
160auto bindExpression(
const E& expression,
const Args& args)
162 if constexpr (isExpression<E>)
163 return TypedAccess::make<typename E::Dynamic>(TypedAccess::erase(expression, args));
167template <
typename E,
typename Args,
typename Callback>
168auto visitValues(
const E& expression,
const Args& args, Callback&& callback) ->
void
170 if constexpr (isExpression<E>)
171 E::visit(TypedAccess::children(expression), args, callback);
173template <
typename M,
bool W = true,
bool C = false>
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;
188template <
typename S, ComparisonOperator Op>
189struct ComparisonMeta : ExpressionMeta<typename S::Model, S::writeSafe, S::containsCollection>
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)
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)
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;
216 return source <= value;
221 template <
typename Children,
typename Args,
typename Callback>
222 static auto visit(
const Children& children,
const Args& args, Callback& callback) ->
void
224 callback(typedValue<typename S::Value>(resolveValue(std::get<1>(children), args)));
227template <ComparisonOperator Op,
typename S,
typename T>
228constexpr auto comparison(
const S& source, T&& value)
230 return makeExpression<ExprKind::Comparison, ComparisonMeta<S, Op>>(source, captureValue(std::forward<T>(value)));
232template <
typename S, NullOperator Op>
233struct NullMeta : ExpressionMeta<typename S::Model, S::writeSafe, S::containsCollection>
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)
245 template <
typename Children,
typename Args,
typename Callback>
246 static auto visit(
const Children&,
const Args&, Callback&) ->
void
250template <
typename S, BetweenOperator Op>
251struct BetweenMeta : ExpressionMeta<typename S::Model, S::writeSafe, S::containsCollection>
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)
263 typedValue<typename S::Value>(resolveValue(std::get<1>(children), args)),
264 typedValue<typename S::Value>(resolveValue(std::get<2>(children), args))}}};
266 template <
typename Children,
typename Args,
typename Callback>
267 static auto visit(
const Children& children,
const Args& args, Callback& callback) ->
void
269 callback(typedValue<typename S::Value>(resolveValue(std::get<1>(children), args)));
270 callback(typedValue<typename S::Value>(resolveValue(std::get<2>(children), args)));
273template <
typename... V>
276 std::tuple<V...> values;
277 using ParameterTypes = ConcatTuples<detail::ParameterTypes<V>...>;
282 inline static constexpr bool fixed =
false;
283 inline static constexpr auto size = std::dynamic_extent;
285template <
typename... V>
286struct ListTraits<FixedValues<V...>>
288 inline static constexpr bool fixed =
true;
289 inline static constexpr std::size_t size =
sizeof...(V);
291template <
typename V, std::
size_t N, std::
size_t I>
292struct ListTraits<Parameter<std::array<V, N>, I>>
294 inline static constexpr bool fixed =
true;
295 inline static constexpr std::size_t size = N;
297template <
typename S, ListOperator Op,
typename Stored>
298struct ListMeta : ExpressionMeta<typename S::Model, S::writeSafe, S::containsCollection>
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
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);
318 const auto& values = resolveValue(stored, args);
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)));
326 template <
typename Children,
typename Args>
327 static auto render(
const Children& children,
const Args& args)
329 std::vector<QueryValue> values;
330 auto append = [&](QueryValue value) { values.push_back(std::move(value)); };
331 visit(children, args, append);
336template <ListOperator Op,
typename S,
typename... V>
337constexpr auto fixedList(
const S& source, V&&... value)
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));
343template <ListOperator Op,
typename S,
typename Container>
344constexpr auto containerList(
const S& source,
const Container& values)
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>{});
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));
364template <
typename L,
typename R, LogicalOperator Op>
366 : ExpressionMeta<typename L::Model, L::writeSafe && R::writeSafe, L::containsCollection || R::containsCollection>
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)
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;
385 return left || right;
387 template <
typename Children,
typename Args,
typename Callback>
388 static auto visit(
const Children& children,
const Args& args, Callback& callback) ->
void
390 visitValues(std::get<0>(children), args, callback);
391 visitValues(std::get<1>(children), args, callback);
395struct NotMeta : ExpressionMeta<typename E::Model, E::writeSafe, E::containsCollection>
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)
406 return !TypedAccess::erase(std::get<0>(children), args);
408 template <
typename Children,
typename Args,
typename Callback>
409 static auto visit(
const Children& children,
const Args& args, Callback& callback) ->
void
411 visitValues(std::get<0>(children), args, callback);
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
@ DynamicAggregatePredicate