21namespace orm::query::detail
23enum class PlanOperation
35 inline static constexpr auto member = Member;
40 template <
typename P,
typename S>
41 static constexpr auto make(S&& state) -> P
43 return P{std::forward<S>(state)};
46 static constexpr auto get(
const P& plan) ->
const typename P::State&
52template <
typename Column,
typename ValueStorage>
56 using Stored = ValueStorage;
57 using ParameterTypes = detail::ParameterTypes<ValueStorage>;
62inline constexpr bool isStaticPlan =
false;
63template <
typename C,
typename V>
64inline constexpr bool setCompatible = []
66 using T = parameter_value_t<V>;
67 if constexpr (std::same_as<T, std::nullopt_t> || std::same_as<T, std::nullptr_t>)
70 return C::nullable && compatibleValue<
typename C::Value, scalar_t<T>>;
72 return compatibleValue<typename C::Value, T>;
74template <
typename C,
typename V>
75concept ORM_QUERY_SET_VALUE = setCompatible<C, V>;
77template <
typename C,
typename V,
typename Args>
78auto applyAssignment(orm::Update<typename C::Model>& result,
const Assignment<C, V>& assignment,
79 const Args& args) ->
void
81 auto value = resolveValue(assignment.value, args);
82 if constexpr (std::same_as<
decltype(value), std::nullptr_t>)
83 result.set(TypedAccess::make<C>(), std::nullopt);
85 result.set(TypedAccess::make<C>(), std::move(value));
95 template <detail::PlanOperation Operation,
typename Owner,
typename ResultType,
typename StateTuple,
96 bool Distinct =
false,
bool Join =
true>
105 using Orders = std::tuple_element_t<1, State>;
106 using Groups = std::tuple_element_t<2, State>;
107 using Having = std::tuple_element_t<3, State>;
110 using Limit = std::tuple_element_t<6, State>;
111 using Offset = std::tuple_element_t<7, State>;
116 inline static constexpr bool isProjection = !std::same_as<Projections, std::tuple<>>;
117 inline static constexpr bool hasPredicate = !std::same_as<Predicate, detail::NoClause>;
118 inline static constexpr bool hasHaving = !std::same_as<Having, detail::NoClause>;
119 inline static constexpr bool hasLimit = !std::same_as<Limit, detail::NoClause>;
120 inline static constexpr bool hasOffset = !std::same_as<Offset, detail::NoClause>;
122 template <
typename P>
125 constexpr auto where(P predicate)
const
127 return replace<0>(std::move(predicate));
129 template <
typename P>
132 constexpr auto andWhere(P predicate)
const
135 return where(std::get<0>(state) && std::move(predicate));
137 return where(std::move(predicate));
139 template <
typename P>
142 constexpr auto orWhere(P predicate)
const
145 return where(std::get<0>(state) || std::move(predicate));
147 return where(std::move(predicate));
149 template <
typename... O>
151 constexpr auto orderBy(O... orders)
const
153 return replace<1>(std::tuple{std::move(orders)...});
155 template <
typename... C>
157 constexpr auto groupBy(C... columns)
const
159 return replace<2>(std::tuple{std::move(columns)...});
161 template <
typename P>
163 constexpr auto having(P predicate)
const
165 return replace<3>(std::move(predicate));
167 template <
typename P>
169 constexpr auto andHaving(P predicate)
const
172 return having(std::get<3>(state) && std::move(predicate));
174 return having(std::move(predicate));
176 template <
typename P>
178 constexpr auto orHaving(P predicate)
const
181 return having(std::get<3>(state) || std::move(predicate));
183 return having(std::move(predicate));
185 template <
typename... P>
189 constexpr auto project(P... projections)
const
191 static_assert(
sizeof...(P) > 0,
"ORM_QUERY_PROJECTION: a projection plan requires projected fields");
192 return replace<4>(std::tuple{std::move(projections)...});
197 return detail::PlanAccess::make<StaticPlan<Operation, Model, Result, State, true, Join>>(state);
202 return detail::PlanAccess::make<StaticPlan<Operation, Model, Result, State, Distinct, false>>(state);
204 constexpr auto limit(std::size_t value)
const
207 return replace<6>(value);
209 constexpr auto offset(std::size_t value)
const
212 return replace<7>(value);
214 template <std::
size_t I>
218 return replace<6>(value);
220 template <std::
size_t I>
224 return replace<7>(value);
226 template <auto Member>
232 constexpr bool alreadyIncluded = []<std::size_t... I>(std::index_sequence<I...>)
consteval {
233 return (std::same_as<std::tuple_element_t<I, Includes>, detail::IncludeTag<Member>> || ...);
234 }(std::make_index_sequence<std::tuple_size_v<Includes>>{});
235 if constexpr (alreadyIncluded)
238 return replace<8>(std::tuple_cat(std::get<8>(state), std::tuple{detail::IncludeTag<Member>{}}));
240 template <
auto... Members,
typename V>
246 auto stored = detail::captureValue(std::forward<V>(value));
247 using A = detail::Assignment<
TypedColumn<Members...>,
decltype(stored)>;
248 return replace<5>(std::tuple_cat(std::get<5>(state), std::tuple{A{std::move(stored)}}));
252 if constexpr (Operation != detail::PlanOperation::Select)
253 static_assert(
hasPredicate,
"ORM_QUERY_REQUIRED_WHERE: a mutation plan requires WHERE");
254 if constexpr (Operation == detail::PlanOperation::Update)
255 static_assert(std::tuple_size_v<Assignments> != 0,
256 "ORM_QUERY_UPDATE_ASSIGNMENTS: an update plan requires SET");
258 template <
typename... Args>
262 detail::validateParameters<StaticPlan, Args...>();
263 const auto args = std::forward_as_tuple(values...);
264 if constexpr (Operation == detail::PlanOperation::Remove)
265 return detail::bindExpression(std::get<0>(state), args);
266 else if constexpr (Operation == detail::PlanOperation::Update)
269 std::apply([&](
const auto&... assignment) { (detail::applyAssignment(result, assignment, args), ...); },
271 result.
where(detail::bindExpression(std::get<0>(state), args));
281 std::apply([&](
const auto&... projection) { output.
project(projection...); },
289 result.where(detail::bindExpression(std::get<0>(state), args));
290 std::apply([&](
const auto&... order) { result.orderBy(order...); }, std::get<1>(state));
291 std::apply([&](
const auto&... group) { result.groupBy(group...); }, std::get<2>(state));
293 result.having(detail::bindExpression(std::get<3>(state), args));
294 if constexpr (Distinct)
297 result.disableJoining();
299 result.limit(paginationValue(std::get<6>(state), args));
301 result.offset(paginationValue(std::get<7>(state), args));
303 std::apply([&](
const auto&...
include)
304 { (result.template
include<std::remove_cvref_t<
decltype(
include)>::member>(), ...); },
311 friend struct detail::PlanAccess;
312 constexpr explicit StaticPlan(
State data) : state{std::move(data)} {}
313 template <std::
size_t Index,
typename V>
314 constexpr auto replace(V value)
const
316 auto newState = [&]<std::size_t... I>(std::index_sequence<I...>)
318 auto element = [&]<std::size_t N>()
320 if constexpr (N == Index)
323 return std::get<N>(state);
325 return std::tuple { element.template operator()<I>()... };
326 }(std::make_index_sequence<std::tuple_size_v<State>>{});
327 return detail::PlanAccess::make<StaticPlan<Operation,
Model,
Result,
decltype(newState), Distinct, Join>>(
328 std::move(newState));
330 template <
typename V,
typename A>
331 static auto paginationValue(
const V& value,
const A& args) -> std::size_t
333 const auto result = detail::resolveValue(value, args);
334 if (result >
static_cast<std::size_t
>(std::numeric_limits<long long>::max()))
335 throw std::out_of_range{
"Pagination exceeds the supported signed 64-bit range"};
343using EmptyPlanState = std::tuple<NoClause, std::tuple<>, std::tuple<>, NoClause, std::tuple<>, std::tuple<>, NoClause,
344 NoClause, std::tuple<>>;
345template <PlanOperation O,
typename M,
typename R,
typename S,
bool D,
bool J>
346inline constexpr bool isStaticPlan<StaticPlan<O, M, R, S, D, J>> =
true;
348concept StaticPlanType = isStaticPlan<std::remove_cvref_t<P>>;
354 template <
typename Model>
357 return detail::PlanAccess::make<
360 template <
typename Source,
typename Result,
typename... P>
363 constexpr auto selectAs(P... projections)
365 static_assert(
sizeof...(P) > 0,
"ORM_QUERY_PROJECTION: a projection plan requires projected fields");
366 using State = std::tuple<detail::NoClause, std::tuple<>, std::tuple<>, detail::NoClause, std::tuple<P...>,
367 std::tuple<>, detail::NoClause, detail::NoClause, std::tuple<>>;
368 return detail::PlanAccess::make<StaticPlan<detail::PlanOperation::Select, Source, Result, State>>(
369 State{{}, {}, {}, {}, std::tuple{std::move(projections)...}, {}, {}, {}, {}});
371 template <
typename Model>
374 return detail::PlanAccess::make<
375 StaticPlan<detail::PlanOperation::Update, Model, Model, detail::EmptyPlanState>>(detail::EmptyPlanState{});
377 template <
typename Model>
380 return detail::PlanAccess::make<
381 StaticPlan<detail::PlanOperation::Remove, Model, Model, detail::EmptyPlanState>>(detail::EmptyPlanState{});
A select query that returns a user-defined DTO instead of the full source model.
auto project(Projections... projections) -> ProjectionQuery< Source, Result > &
A template class representing a select query in the ORM framework.
auto where(const P &predicate) -> Update< T > &
static consteval auto validateShape() -> void
std::tuple_element_t< 4, State > Projections
constexpr auto having(P predicate) const
std::tuple_element_t< 7, State > Offset
constexpr auto disableJoining() const
std::tuple_element_t< 6, State > Limit
constexpr auto project(P... projections) const
constexpr auto offset(std::size_t value) const
constexpr auto where(P predicate) const
constexpr auto orderBy(O... orders) const
constexpr auto orHaving(P predicate) const
std::tuple_element_t< 3, State > Having
constexpr auto andWhere(P predicate) const
static constexpr bool isDistinct
static constexpr bool shouldJoin
static constexpr bool hasPredicate
constexpr auto groupBy(C... columns) const
std::tuple_element_t< 5, State > Assignments
std::tuple_element_t< 1, State > Orders
detail::ParameterTypes< State > ParameterTypes
std::tuple_element_t< 8, State > Includes
constexpr auto distinct() const
static constexpr bool hasLimit
std::tuple_element_t< 0, State > Predicate
constexpr auto limit(std::size_t value) const
static constexpr bool hasOffset
constexpr auto include() const
static constexpr bool hasHaving
constexpr auto set(TypedColumn< Members... >, V &&value) const
auto toDynamic(Args &&... values) const
std::tuple_element_t< 2, State > Groups
static constexpr auto operation
static constexpr bool isProjection
constexpr auto orWhere(P predicate) const
constexpr auto andHaving(P predicate) const
orm::query::detail::Column Column
constexpr bool isNullable
constexpr auto selectAs(P... projections)