ORM C++
Loading...
Searching...
No Matches
columns.cppm
Go to the documentation of this file.
1module;
2
3#include <array>
4#include <concepts>
5#include <cstddef>
6#include <initializer_list>
7#include <optional>
8#include <string>
9#include <tuple>
10#include <type_traits>
11#include <utility>
12
13export module orm:query_columns;
14
15import orm.reflection;
16import :foundation;
17import :model_mapping;
18import :model_columns;
19import :query_value;
20import :query_runtime_predicate;
21import :query_parameters;
22import :query_expression;
23
24// query/TypedColumn.hpp
25namespace orm::query::detail
26{
27template <auto... Members>
28struct ColumnTraits;
29template <auto Member>
30struct ColumnTraits<Member>
31{
32 static_assert(std::is_member_object_pointer_v<decltype(Member)>,
33 "ORM_QUERY_MEMBER: expected a data member pointer");
34 using Model = model::detail::member_owner_t<Member>;
35 using Field = model::detail::member_value_t<Member>;
36 using Value = scalar_t<Field>;
37 static_assert(Member != nullptr && model::detail::memberExists<Model, Member>(),
38 "ORM_QUERY_MEMBER: expected a reflected aggregate member");
39 static_assert(supportedScalar<Value>, "ORM_QUERY_COLUMN: terminal field must be a supported scalar");
40 inline static constexpr bool nullable = model::isNullable<Field>;
41 inline static constexpr bool writeSafe = true;
42 inline static constexpr std::array pathParts{model::detail::reflectedMemberNameStorage<Member>.view()};
43 static auto path() -> std::string
44 {
45 return std::string{pathParts[0]};
46 }
47};
48template <auto Relation, auto Member>
49struct ColumnTraits<Relation, Member>
50{
51 static_assert(std::is_member_object_pointer_v<decltype(Relation)> &&
52 std::is_member_object_pointer_v<decltype(Member)>,
53 "ORM_QUERY_MEMBER: expected data member pointers");
54 using Model = model::detail::member_owner_t<Relation>;
55 using RelationField = model::detail::member_value_t<Relation>;
56 using Target = scalar_t<RelationField>;
57 using Terminal = ColumnTraits<Member>;
58 using Value = typename Terminal::Value;
59 static_assert(Relation != nullptr && model::detail::memberExists<Model, Relation>(),
60 "ORM_QUERY_MEMBER: expected a reflected relation member");
61 static_assert(std::same_as<Target, typename Terminal::Model> && std::is_aggregate_v<Target> &&
63 "ORM_QUERY_PATH: adjacent owners must follow a to-one relation");
64 inline static constexpr bool nullable = model::isNullable<RelationField> || Terminal::nullable;
65 inline static constexpr bool writeSafe =
66 model::detail::isPrimaryKey<Target>(model::detail::reflectedMemberNameStorage<Member>.view());
67 inline static constexpr std::array pathParts{model::detail::reflectedMemberNameStorage<Relation>.view(),
68 model::detail::reflectedMemberNameStorage<Member>.view()};
69 static auto path() -> std::string
70 {
71 return std::string{pathParts[0]} + "." + std::string{pathParts[1]};
72 }
73};
74template <auto Member>
75consteval auto collectionMember() -> bool
76{
77 if constexpr (!std::is_member_object_pointer_v<decltype(Member)>)
78 return false;
79 else
81}
82template <auto Member>
83concept ORM_QUERY_COLLECTION = collectionMember<Member>();
84template <bool C>
85concept ORM_QUERY_NO_NESTED_COLLECTION = !C;
86template <auto Member>
87struct CollectionTraits
88{
89 using Model = model::detail::member_owner_t<Member>;
91 static_assert(model::detail::memberExists<Model, Member>(), "ORM_QUERY_MEMBER: expected a reflected collection");
92 static auto name() -> std::string
93 {
94 return std::string{model::detail::reflectedMemberNameStorage<Member>.view()};
95 }
96};
97} // namespace orm::query::detail
98
99namespace orm::query
100{
101export
102{
103
104 template <typename Owner, bool WriteSafe = true, bool ContainsCollection = false>
105 class TypedPredicate
106 {
107 public:
108 using Model = Owner;
109 using ParameterTypes = std::tuple<>;
110 inline static constexpr auto kind = ExprKind::DynamicPredicate;
111 inline static constexpr bool isPredicate = true;
112 inline static constexpr bool isAggregatePredicate = false;
113 inline static constexpr bool isAggregate = false;
114 inline static constexpr bool writeSafe = WriteSafe;
115 inline static constexpr bool containsCollection = ContainsCollection;
116 inline static constexpr bool staticSqlEligible = false;
117 template <typename E>
118// requires detail::isExpression<E> && E::isPredicate && detail::ORM_QUERY_MODEL<E, Owner> &&
119// (E::writeSafe == WriteSafe) &&
120// (E::containsCollection == ContainsCollection) && detail::ORM_QUERY_UNBOUND_PARAMETER<E>
121 TypedPredicate(const E& expression) : data{detail::erase(expression)}
122 {
123 }
124 template <typename E>
125// requires detail::isExpression<E> && E::isPredicate && detail::ORM_QUERY_MODEL<E, Owner> &&
126// (E::writeSafe == WriteSafe) &&
127// (E::containsCollection == ContainsCollection) && detail::ORM_QUERY_UNBOUND_PARAMETER<E>
128 auto operator=(const E& expression) -> TypedPredicate&
129 {
130 data = detail::erase(expression);
131 return *this;
133 auto dynamic() const -> TypedPredicate
134 {
135 return *this;
136 }
137
138 private:
139 friend struct detail::TypedAccess;
140 explicit TypedPredicate(detail::Predicate value) : data{std::move(value)} {}
141 auto runtime() const -> detail::Predicate
142 {
143 return data;
144 }
145 detail::Predicate data;
146 };
147
148 template <auto... Members>
149 class TypedColumn
150 {
151 static_assert(sizeof...(Members) >= 1 && sizeof...(Members) <= 2,
152 "ORM_QUERY_PATH: columns support a direct member or one to-one relation level");
153 using Traits = detail::ColumnTraits<Members...>;
154
155 public:
156 using Model = typename Traits::Model;
157 using Value = typename Traits::Value;
158 using ParameterTypes = std::tuple<>;
159 inline static constexpr auto kind = ExprKind::Column;
160 inline static constexpr auto pathParts = Traits::pathParts;
161 inline static constexpr bool isColumn = true;
162 inline static constexpr bool isAggregate = false;
163 inline static constexpr bool nullable = Traits::nullable;
164 inline static constexpr bool writeSafe = Traits::writeSafe;
165 inline static constexpr bool containsCollection = false;
166 inline static constexpr bool staticSqlEligible = true;
167 template <typename T>
168// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T>
169 constexpr auto operator==(T&& value) const
170 {
171 return detail::comparison<detail::ComparisonOperator::Equal>(*this, std::forward<T>(value));
172 }
173
174 template <typename T>
175// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T>
176 constexpr auto operator!=(T&& value) const
177 {
178 return detail::comparison<detail::ComparisonOperator::NotEqual>(*this, std::forward<T>(value));
179 }
180
181 template <typename T>
182// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_ORDERABLE<Value>
183 constexpr auto operator>(T&& value) const
184 {
185 return detail::comparison<detail::ComparisonOperator::Greater>(*this, std::forward<T>(value));
186 }
187
188 template <typename T>
189// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_ORDERABLE<Value>
190 constexpr auto operator>=(T&& value) const
191 {
192 return detail::comparison<detail::ComparisonOperator::GreaterOrEqual>(*this, std::forward<T>(value));
193 }
194
195 template <typename T>
196// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_ORDERABLE<Value>
197 constexpr auto operator<(T&& value) const
198 {
199 return detail::comparison<detail::ComparisonOperator::Less>(*this, std::forward<T>(value));
200 }
201
202 template <typename T>
203// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_ORDERABLE<Value>
204 constexpr auto operator<=(T&& value) const
205 {
206 return detail::comparison<detail::ComparisonOperator::LessOrEqual>(*this, std::forward<T>(value));
207 }
208 template <typename T>
209// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_STRING<Value>
210 constexpr auto like(T&& value) const
211 {
212 return detail::comparison<detail::ComparisonOperator::Like>(*this, std::forward<T>(value));
213 }
214 template <typename T>
215// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && detail::ORM_QUERY_STRING<Value>
216 constexpr auto notLike(T&& value) const
217 {
218 return detail::comparison<detail::ComparisonOperator::NotLike>(*this, std::forward<T>(value));
220 constexpr auto isNull() const
221// requires detail::ORM_QUERY_NULLABLE<nullable>
222 {
223 return detail::makeExpression<ExprKind::Null, detail::NullMeta<TypedColumn, detail::NullOperator::IsNull>>(
224 *this);
226 constexpr auto isNotNull() const
227// requires detail::ORM_QUERY_NULLABLE<nullable>
228 {
229 return detail::makeExpression<ExprKind::Null,
230 detail::NullMeta<TypedColumn, detail::NullOperator::IsNotNull>>(*this);
232 constexpr auto operator==(std::nullptr_t) const
233// requires detail::ORM_QUERY_NULLABLE<nullable>
234 {
235 return isNull();
237 constexpr auto operator!=(std::nullptr_t) const
238// requires detail::ORM_QUERY_NULLABLE<nullable>
239 {
240 return isNotNull();
242 constexpr auto operator==(std::nullopt_t) const
243// requires detail::ORM_QUERY_NULLABLE<nullable>
244 {
245 return isNull();
247 constexpr auto operator!=(std::nullopt_t) const
248// requires detail::ORM_QUERY_NULLABLE<nullable>
249 {
250 return isNotNull();
251 }
252 template <typename T>
253// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T>
254 auto in(std::initializer_list<T> values) const
255 {
256 return detail::containerList<detail::ListOperator::In>(*this, values);
257 }
258 template <typename T>
259// requires detail::ContainerTraits<detail::parameter_value_t<T>>::isContainer &&
260// detail::ORM_QUERY_VALUE_OR_PARAMETER<
261// Value, typename detail::ContainerTraits<detail::parameter_value_t<T>>::Value>
262 constexpr auto in(const T& values) const
263 {
264 return detail::containerList<detail::ListOperator::In>(*this, values);
265 }
266 template <typename T, std::size_t N>
267// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T>
268 constexpr auto in(const T (&values)[N]) const
269 {
270 return [&]<std::size_t... I>(std::index_sequence<I...>)
271 { return detail::fixedList<detail::ListOperator::In>(*this, values[I]...); }(std::make_index_sequence<N>{});
272 }
273 template <typename... T>
274// requires(sizeof...(T) > 0) && (detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && ...)
275 constexpr auto in(T&&... values) const
276 {
277 return detail::fixedList<detail::ListOperator::In>(*this, std::forward<T>(values)...);
278 }
279
280 template <typename T>
281// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T>
282 auto notIn(std::initializer_list<T> values) const
283 {
284 return detail::containerList<detail::ListOperator::NotIn>(*this, values);
285 }
286 template <typename T>
287// requires detail::ContainerTraits<detail::parameter_value_t<T>>::isContainer &&
288// detail::ORM_QUERY_VALUE_OR_PARAMETER<
289// Value, typename detail::ContainerTraits<detail::parameter_value_t<T>>::Value>
290 constexpr auto notIn(const T& values) const
291 {
292 return detail::containerList<detail::ListOperator::NotIn>(*this, values);
293 }
294 template <typename T, std::size_t N>
295// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T>
296 constexpr auto notIn(const T (&values)[N]) const
297 {
298 return [&]<std::size_t... I>(std::index_sequence<I...>) {
299 return detail::fixedList<detail::ListOperator::NotIn>(*this, values[I]...);
300 }(std::make_index_sequence<N>{});
301 }
302 template <typename... T>
303// requires(sizeof...(T) > 0) && (detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, T> && ...)
304 constexpr auto notIn(T&&... values) const
305 {
306 return detail::fixedList<detail::ListOperator::NotIn>(*this, std::forward<T>(values)...);
307 }
308 template <typename L, typename U>
309// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, L> && detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, U> &&
310// detail::ORM_QUERY_ORDERABLE<Value>
311 constexpr auto between(L&& lower, U&& upper) const
312 {
313 return detail::makeExpression<ExprKind::Between,
314 detail::BetweenMeta<TypedColumn, detail::BetweenOperator::Between>>(
315 *this, detail::captureValue(std::forward<L>(lower)), detail::captureValue(std::forward<U>(upper)));
316 }
317 template <typename L, typename U>
318// requires detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, L> && detail::ORM_QUERY_VALUE_OR_PARAMETER<Value, U> &&
319// detail::ORM_QUERY_ORDERABLE<Value>
320 constexpr auto notBetween(L&& lower, U&& upper) const
321 {
322 return detail::makeExpression<ExprKind::Between,
323 detail::BetweenMeta<TypedColumn, detail::BetweenOperator::NotBetween>>(
324 *this, detail::captureValue(std::forward<L>(lower)), detail::captureValue(std::forward<U>(upper)));
325 }
326
327 private:
328 friend struct detail::TypedAccess;
329 constexpr TypedColumn() = default;
330 auto runtime() const -> detail::Column
331 {
332 return detail::col(Traits::path());
333 }
334 };
335 template <auto... Members>
336// requires detail::ORM_QUERY_MEMBER<Members...>
337 constexpr auto col() -> TypedColumn<Members...>
338 {
339 return detail::TypedAccess::make<TypedColumn<Members...>>();
340 }
341}
342} // namespace orm::query
343
344namespace orm::query::detail
345{
346template <typename E>
347concept ORM_QUERY_WRITE_SAFE = E::writeSafe;
348template <typename E>
349struct IsTypedPredicate : std::false_type
350{
351};
352template <typename M, bool W, bool C>
353struct IsTypedPredicate<TypedPredicate<M, W, C>> : std::true_type
354{
355};
356template <typename E>
357struct IsTypedColumn : std::false_type
358{
359};
360template <auto... Members>
361struct IsTypedColumn<TypedColumn<Members...>> : std::true_type
362{
363};
364template <typename E>
365concept PredicateExpression =
366 (IsTypedPredicate<std::remove_cvref_t<E>>::value || isExpression<E>) && std::remove_cvref_t<E>::isPredicate;
367template <typename E, typename M>
368concept AnyPredicateFor = PredicateExpression<E> && ORM_QUERY_MODEL<std::remove_cvref_t<E>, M>;
369template <typename E, typename M>
370concept PredicateFor = AnyPredicateFor<E, M> && ORM_QUERY_UNBOUND_PARAMETER<E>;
371template <typename E, typename M>
372concept PlanPredicateFor = AnyPredicateFor<E, M>;
373template <typename E, typename M>
374concept ColumnFor = IsTypedColumn<std::remove_cvref_t<E>>::value && ORM_QUERY_MODEL<std::remove_cvref_t<E>, M>;
375template <typename M, typename... Columns>
376concept ORM_QUERY_MODEL_COLUMNS = (ColumnFor<Columns, M> && ...);
377template <typename E>
378struct IsTypedAggregatePredicate : std::false_type
379{
380};
381template <typename E>
382concept LogicalPredicate =
383 PredicateExpression<E> || ((isExpression<E> || IsTypedAggregatePredicate<std::remove_cvref_t<E>>::value) &&
384 std::remove_cvref_t<E>::isAggregatePredicate);
385template <auto Member, CollectionOperator Op, typename C>
386struct CollectionMeta : ExpressionMeta<typename CollectionTraits<Member>::Model, true, true>
387{
388 using Child = C;
389 using Target = typename CollectionTraits<Member>::Target;
390 using Dynamic = TypedPredicate<typename CollectionTraits<Member>::Model, true, true>;
391 inline static constexpr auto member = Member;
392 inline static constexpr auto operation = Op;
393 inline static constexpr bool isPredicate = true;
394 inline static constexpr bool staticSqlEligible = std::same_as<C, void> || detail::staticSqlEligible<C>;
395 inline static constexpr auto relationName = model::detail::reflectedMemberNameStorage<Member>.view();
396 template <typename Children, typename Args>
397 static auto render(const Children& children, const Args& args)
398 {
399 if constexpr (Op == CollectionOperator::Exists)
400 return detail::exists(std::string{relationName});
401 else if constexpr (Op == CollectionOperator::Any)
402 return detail::any(std::string{relationName}, TypedAccess::erase(std::get<0>(children), args));
403 else
404 return detail::none(std::string{relationName}, TypedAccess::erase(std::get<0>(children), args));
405 }
406 template <typename Children, typename Args, typename Callback>
407 static auto visit(const Children& children, const Args& args, Callback& callback) -> void
408 {
409 if constexpr (!std::same_as<C, void>)
410 visitValues(std::get<0>(children), args, callback);
411 }
412};
413} // namespace orm::query::detail
414
415namespace orm::query
416{
417export
418{
419
420 template <typename L, typename R>
421// requires detail::LogicalPredicate<L> && detail::LogicalPredicate<R> &&
422// detail::ORM_QUERY_MODEL<std::remove_cvref_t<R>, typename std::remove_cvref_t<L>::Model> &&
423// (std::remove_cvref_t<L>::isAggregatePredicate == std::remove_cvref_t<R>::isAggregatePredicate)
424 constexpr auto operator&&(const L& left, const R& right)
425 {
426 return detail::makeExpression<ExprKind::Logical, detail::LogicalMeta<L, R, detail::LogicalOperator::And>>(
427 left, right);
428 }
429 template <typename L, typename R>
430// requires detail::LogicalPredicate<L> && detail::LogicalPredicate<R> &&
431// detail::ORM_QUERY_MODEL<std::remove_cvref_t<R>, typename std::remove_cvref_t<L>::Model> &&
432// (std::remove_cvref_t<L>::isAggregatePredicate == std::remove_cvref_t<R>::isAggregatePredicate)
433 constexpr auto operator||(const L& left, const R& right)
434 {
435 return detail::makeExpression<ExprKind::Logical, detail::LogicalMeta<L, R, detail::LogicalOperator::Or>>(left,
436 right);
437 }
438 template <typename E>
439// requires detail::LogicalPredicate<E>
440 constexpr auto operator!(const E& value)
441 {
442 return detail::makeExpression<ExprKind::Not, detail::NotMeta<E>>(value);
443 }
444 template <auto Member, typename E>
445// requires detail::ORM_QUERY_COLLECTION<Member> &&
446// detail::AnyPredicateFor<E, typename detail::CollectionTraits<Member>::Target> &&
447// detail::ORM_QUERY_NO_NESTED_COLLECTION<E::containsCollection>
448 constexpr auto any(const E& predicate)
449 {
450 return detail::makeExpression<ExprKind::Collection,
451 detail::CollectionMeta<Member, detail::CollectionOperator::Any, E>>(predicate);
452 }
453 template <auto Member, typename E>
454// requires detail::ORM_QUERY_COLLECTION<Member> &&
455// detail::AnyPredicateFor<E, typename detail::CollectionTraits<Member>::Target> &&
456// detail::ORM_QUERY_NO_NESTED_COLLECTION<E::containsCollection>
457 constexpr auto none(const E& predicate)
458 {
459 return detail::makeExpression<ExprKind::Collection,
460 detail::CollectionMeta<Member, detail::CollectionOperator::None, E>>(predicate);
461 }
462 template <auto Member>
463// requires detail::ORM_QUERY_COLLECTION<Member>
464 constexpr auto exists()
465 {
466 return detail::makeExpression<ExprKind::Collection,
467 detail::CollectionMeta<Member, detail::CollectionOperator::Exists, void>>();
468 }
469 template <typename M, typename... P>
470// requires(std::same_as<std::remove_cvref_t<P>, QueryParameter> && ...)
471 auto raw(std::string sql, P... params) -> TypedPredicate<M>
472 {
473 return detail::TypedAccess::make<TypedPredicate<M>>(detail::raw(std::move(sql), std::move(params)...));
474 }
475}
476} // namespace orm::query
477
478// query/Predicate.hpp
static constexpr auto pathParts
Definition columns.cppm:159
static constexpr auto kind
Definition columns.cppm:158
static constexpr bool writeSafe
Definition columns.cppm:163
static constexpr bool staticSqlEligible
Definition columns.cppm:165
static constexpr bool nullable
Definition columns.cppm:162
constexpr auto isNotNull() const
Definition columns.cppm:225
constexpr auto operator<(T &&value) const
Definition columns.cppm:196
constexpr auto operator!=(T &&value) const
Definition columns.cppm:175
constexpr auto operator>=(T &&value) const
Definition columns.cppm:189
std::tuple<> ParameterTypes
Definition columns.cppm:157
static constexpr bool isColumn
Definition columns.cppm:160
typename Traits::Value Value
Definition columns.cppm:156
constexpr auto like(T &&value) const
Definition columns.cppm:209
static constexpr bool containsCollection
Definition columns.cppm:164
constexpr auto notLike(T &&value) const
Definition columns.cppm:215
typename Traits::Model Model
Definition columns.cppm:155
static constexpr bool isAggregate
Definition columns.cppm:161
auto notIn(std::initializer_list< T > values) const
Definition columns.cppm:281
constexpr auto operator>(T &&value) const
Definition columns.cppm:182
auto in(std::initializer_list< T > values) const
Definition columns.cppm:253
constexpr auto isNull() const
Definition columns.cppm:219
constexpr auto operator<=(T &&value) const
Definition columns.cppm:203
constexpr auto operator==(T &&value) const
Definition columns.cppm:168
constexpr auto between(L &&lower, U &&upper) const
Definition columns.cppm:310
constexpr auto notBetween(L &&lower, U &&upper) const
Definition columns.cppm:319
auto dynamic() const -> TypedPredicate
Definition columns.cppm:132
TypedPredicate(const E &expression)
Definition columns.cppm:120
constexpr bool isNullable
Definition mapping.cppm:552
constexpr auto operator!(const E &value)
Definition columns.cppm:439
constexpr auto operator&&(const L &left, const R &right)
Definition columns.cppm:423
auto raw(std::string sql, P... params) -> TypedPredicate< M >
Definition columns.cppm:470
constexpr auto any(const E &predicate)
Definition columns.cppm:447
constexpr auto operator||(const L &left, const R &right)
Definition columns.cppm:432
constexpr auto col() -> TypedColumn< Members... >
Definition columns.cppm:336
constexpr auto exists()
Definition columns.cppm:463
constexpr auto none(const E &predicate)
Definition columns.cppm:456
typename detail::RelationCollectionTraits< std::remove_cvref_t< T > >::Target relation_target_t
constexpr bool is_relation_collection_v