21import :query_runtime_predicate;
22import :query_parameters;
23import :query_expression;
25import :query_aggregate;
28namespace orm::query::detail
30enum class OrderDirection
42 OrderDirection direction{OrderDirection::Asc};
50inline auto asc(Column column) -> OrderBy
52 return OrderBy{.column = std::move(column), .direction = OrderDirection::Asc};
58inline auto desc(Column column) -> OrderBy
60 return OrderBy{.column = std::move(column), .direction = OrderDirection::Desc};
66inline auto rawOrder(std::string sql) -> OrderBy
68 return OrderBy{.rawSql = std::move(sql), .isRaw =
true};
77 template <
typename Owner>
84 inline static constexpr bool isOrder =
true;
89 TypedOrderBy(
const E& expression) : data{detail::erase(expression)}
97 data = detail::erase(expression);
106 friend struct detail::TypedAccess;
107 explicit TypedOrderBy(detail::OrderBy value) : data{std::move(value)} {}
108 auto runtime() const -> detail::OrderBy
112 detail::OrderBy data;
117namespace orm::query::detail
119template <
typename C, OrderDirection Direction>
120struct OrderMeta : ExpressionMeta<typename C::Model>
123 using Dynamic = TypedOrderBy<typename C::Model>;
124 inline static constexpr auto direction =
Direction;
125 inline static constexpr bool isOrder =
true;
126 inline static constexpr bool staticSqlEligible =
true;
127 template <
typename Children,
typename Args>
128 static auto render(
const Children& children,
const Args& args)
130 return OrderBy{.column = TypedAccess::erase(std::get<0>(children), args), .direction =
Direction};
132 template <
typename Children,
typename Args,
typename Callback>
133 static auto visit(
const Children&,
const Args&, Callback&) ->
void
138struct IsTypedOrderBy : std::false_type
142struct IsTypedOrderBy<TypedOrderBy<M>> : std::true_type
145template <
typename E,
typename M>
147 (IsTypedOrderBy<std::remove_cvref_t<E>>::value || isExpression<E>) && std::remove_cvref_t<E>::isOrder &&
148 ORM_QUERY_MODEL<std::remove_cvref_t<E>, M> && ORM_QUERY_UNBOUND_PARAMETER<E>;
149template <
typename M,
typename... Orders>
150concept ORM_QUERY_MODEL_ORDERS = (OrderFor<Orders, M> && ...);
158 template <
auto... Members>
163 detail::OrderMeta<
TypedColumn<Members...>, detail::OrderDirection::Asc>>(column);
165 template <
auto... Members>
170 detail::OrderMeta<
TypedColumn<Members...>, detail::OrderDirection::Desc>>(column);
172 template <
typename M>
175 return detail::TypedAccess::make<TypedOrderBy<M>>(detail::rawOrder(std::move(sql)));
181namespace orm::query::detail
183using ProjectionSource = std::variant<Column, AggregateExpression>;
190 std::string resultField;
191 ProjectionSource source;
192 std::optional<model::TypeId> sourceType{};
193 bool sourceUuidAdapted =
false;
199inline auto as(std::string resultField, Column sourceColumn) -> Projection
201 return Projection{.resultField = std::move(resultField), .source = std::move(sourceColumn)};
207inline auto as(std::string resultField, AggregateExpression aggregate) -> Projection
209 return Projection{.resultField = std::move(resultField), .source = std::move(aggregate)};
219 template <
typename Owner>
228 template <
typename E>
234 template <
typename E>
239 data = detail::erase(expression);
248 friend struct detail::TypedAccess;
249 explicit TypedProjection(detail::Projection value) : data{std::move(value)} {}
250 auto runtime() const -> detail::Projection
254 detail::Projection data;
259namespace orm::query::detail
261template <
typename S, reflection::FixedString Alias>
262struct ProjectionMeta : ExpressionMeta<typename S::Model>
265 using Dynamic = TypedProjection<typename S::Model>;
266 inline static constexpr auto alias = Alias;
267 inline static constexpr bool isProjection =
true;
268 inline static constexpr bool staticSqlEligible = detail::staticSqlEligible<S>;
269 template <
typename Children,
typename Args>
270 static auto render(
const Children& children,
const Args& args)
272 auto projection = detail::as(std::string{Alias.view()}, TypedAccess::erase(std::get<0>(children), args));
274 projection.sourceUuidAdapted = UuidDomainValue<typename S::Value>;
277 template <
typename Children,
typename Args,
typename Callback>
278 static auto visit(
const Children&,
const Args&, Callback&) ->
void
283struct RuntimeProjectionMeta : ExpressionMeta<typename S::Model>
286 using Dynamic = TypedProjection<typename S::Model>;
287 inline static constexpr bool isProjection =
true;
288 inline static constexpr bool staticSqlEligible =
false;
289 template <
typename Children,
typename Args>
290 static auto render(
const Children& children,
const Args& args)
292 auto projection = detail::as(std::get<1>(children), TypedAccess::erase(std::get<0>(children), args));
294 projection.sourceUuidAdapted = UuidDomainValue<typename S::Value>;
297 template <
typename Children,
typename Args,
typename Callback>
298 static auto visit(
const Children&,
const Args&, Callback&) ->
void
303struct IsTypedProjection : std::false_type
307struct IsTypedProjection<TypedProjection<M>> : std::true_type
310template <
typename E,
typename M>
311concept ProjectionFor =
312 (IsTypedProjection<std::remove_cvref_t<E>>::value || isExpression<E>) && std::remove_cvref_t<E>::isProjection &&
313 ORM_QUERY_MODEL<std::remove_cvref_t<E>, M> && ORM_QUERY_UNBOUND_PARAMETER<E>;
314template <
typename M,
typename... Projections>
315concept ORM_QUERY_MODEL_PROJECTIONS = (ProjectionFor<Projections, M> && ...);
320template <
typename Result,
typename Projection>
321consteval auto uuidProjectionDomainCompatible() ->
bool
323 using P = std::remove_cvref_t<Projection>;
324 if constexpr (
requires {
329 using source_t =
typename P::Source::Value;
331 bool compatible =
true;
332 [&]<std::size_t... I>(std::index_sequence<I...>)
consteval
334 auto check = [&]<std::size_t Index>()
consteval
336 using target_t = scalar_t<reflection::field_type_t<Result, Index>>;
337 if constexpr (UuidDomainValue<source_t> || UuidDomainValue<target_t>)
338 if (fields[Index].name == P::alias.view())
339 compatible = std::same_as<source_t, target_t>;
341 (check.template operator()<I>(), ...);
342 }(std::make_index_sequence<reflection::fieldCount<Result>>{});
348template <
typename Result,
typename... Projections>
349concept ORM_QUERY_UUID_PROJECTION_DOMAINS = (uuidProjectionDomainCompatible<Result, Projections>() && ...);
352concept ProjectionSourceExpression = IsTypedColumn<std::remove_cvref_t<S>>::value || AggregateExpressionType<S>;
360 template <
typename S>
362 constexpr auto as(std::string alias, S source)
364 return detail::makeExpression<ExprKind::Projection, detail::RuntimeProjectionMeta<S>>(source, std::move(alias));
366 template <reflection::FixedString Alias,
typename S>
368 constexpr auto as(S source)
370 return detail::makeExpression<ExprKind::Projection, detail::ProjectionMeta<S, Alias>>(source);
376namespace orm::query::detail
383 std::optional<std::size_t> offset = std::nullopt;
384 std::optional<std::size_t> limit = std::nullopt;
385 std::optional<Predicate> predicate = std::nullopt;
386 std::vector<OrderBy> orderBy;
387 std::vector<Projection> projections;
388 std::vector<Column> groupBy;
389 std::optional<AggregatePredicate> having = std::nullopt;
390 std::vector<std::string> includes;
391 bool isDistinct =
false;
392 bool shouldJoin =
true;
397namespace orm::query::detail
401 std::optional<QueryValue> value;
404struct UpdateAssignment
415 std::vector<UpdateAssignment> assignments;
416 std::optional<Predicate> predicate = std::nullopt;
static constexpr bool staticSqlEligible
static constexpr bool isOrder
static constexpr auto kind
auto dynamic() const -> TypedOrderBy
std::tuple<> ParameterTypes
auto operator=(const E &expression) -> TypedOrderBy &
TypedOrderBy(const E &expression)
TypedProjection(const E &expression)
auto operator=(const E &expression) -> TypedProjection &
std::tuple<> ParameterTypes
static constexpr auto kind
auto dynamic() const -> TypedProjection
static constexpr bool staticSqlEligible
static constexpr bool isProjection
orm::query::detail::Column Column
orm::query::detail::UpdateValue UpdateValue
orm::query::detail::OrderBy OrderBy
consteval auto typeId() noexcept -> TypeId
constexpr auto asc(TypedColumn< Members... > column)
constexpr auto desc(TypedColumn< Members... > column)
constexpr auto as(std::string alias, S source)
auto rawOrder(std::string sql) -> TypedOrderBy< M >
consteval auto fields() noexcept