19import :dynamic_select;
54struct ProjectionResultField
57 std::optional<model::ColumnType> type;
58 model::TypeId cppType{};
59 bool uuidAdapted =
false;
63consteval auto projectionLogicalType() -> std::optional<model::ColumnType>
65 using field_t = std::remove_cvref_t<T>;
66 using value_t = std::remove_cv_t<model::detail::static_optional_value_t<field_t>>;
67 if constexpr (
requires { model::LogicalTypeTraits<value_t>::value; })
77template <
typename Projection>
78auto projectionAlias(
const Projection& projection) -> std::string_view
80 if constexpr (
requires { projection.resultField; })
81 return projection.resultField;
83 return projection.alias;
86template <
typename Projections>
87inline auto validateProjectionAliasNames(
const Projections& projections,
88 const std::vector<ProjectionResultField>& fields) ->
void
90 if (projections.empty())
92 throw std::invalid_argument{
"Projection query requires at least one projected field"};
95 std::vector<std::string> resultFields;
96 resultFields.reserve(
fields.size());
98 for (
const auto& field : fields)
100 if (not field.type.has_value() or not isSupportedProjectionResultType(field.type.value()))
102 throw std::invalid_argument{
"Unsupported projection result field type: " + field.name};
105 resultFields.push_back(field.name);
108 std::vector<std::string> projectedFields;
109 projectedFields.reserve(projections.size());
111 for (
const auto& projection : projections)
113 const std::string alias{projectionAlias(projection)};
116 throw std::invalid_argument{
"Projection alias must not be empty"};
119 if (std::find(resultFields.begin(), resultFields.end(), alias) == resultFields.end())
121 throw std::invalid_argument{
"Projection alias does not match a result field: " + alias};
124 if constexpr (
requires {
125 projection.sourceType;
126 projection.sourceUuidAdapted;
129 const auto field = std::ranges::find(fields, alias, &ProjectionResultField::name);
130 if (projection.sourceType && (projection.sourceUuidAdapted || field->uuidAdapted) &&
131 *projection.sourceType != field->cppType)
132 throw std::invalid_argument{
"Projection UUID domain type does not match the result field"};
135 if (std::find(projectedFields.begin(), projectedFields.end(), alias) != projectedFields.end())
137 throw std::invalid_argument{
"Duplicate projection alias: " + alias};
140 projectedFields.push_back(alias);
143 for (
const auto& resultField : resultFields)
145 if (std::find(projectedFields.begin(), projectedFields.end(), resultField) == projectedFields.end())
147 throw std::invalid_argument{
"Missing projection alias for result field: " + resultField};
152template <
typename Result,
typename Projections>
153auto validateProjectionAliases(
const Projections& projections) ->
void
155 std::vector<ProjectionResultField> resultFields;
158 [&]<std::size_t... Indices>(std::index_sequence<Indices...>)
161 auto appendField = [&]<std::size_t Index>()
164 using value_t = query::detail::scalar_t<field_t>;
165 constexpr auto type = projectionLogicalType<field_t>();
167 UuidDomainValue<value_t>);
169 (appendField.template operator()<Indices>(), ...);
170 }(std::make_index_sequence<reflection::fieldCount<Result>>{});
172 validateProjectionAliasNames(projections, resultFields);
185 template <
typename Source,
typename Result>
191 template <
typename... Projections>
196 data.projections = {query::detail::erase(projections)...};
197 detail::validateProjectionAliases<Result>(data.projections);
202 template <
typename P>
206 data.predicate = query::detail::erase(predicate);
211 template <
typename P>
215 data.predicate = data.predicate.has_value() ? data.predicate.value() && query::detail::erase(predicate) :
216 query::detail::erase(predicate);
221 template <
typename P>
225 data.predicate = data.predicate.has_value() ? data.predicate.value() || query::detail::erase(predicate) :
226 query::detail::erase(predicate);
231 template <
typename... Orders>
235 data.orderBy = {query::detail::erase(orders)...};
240 template <
typename... Columns>
244 data.groupBy = {query::detail::erase(columns)...};
249 template <
typename P>
253 data.having = query::detail::erase(predicate);
258 template <
typename P>
262 data.having = data.having.has_value() ? data.having.value() && query::detail::erase(predicate) :
263 query::detail::erase(predicate);
268 template <
typename P>
272 data.having = data.having.has_value() ? data.having.value() || query::detail::erase(predicate) :
273 query::detail::erase(predicate);
280 data.isDistinct =
true;
301 data.shouldJoin =
false;
309 friend class orm::detail::QueryTestAccess;
311 [[nodiscard]]
inline auto getData() const -> const
query::detail::SelectSpec&
313 detail::validateProjectionAliases<Result>(data.projections);
318 query::detail::SelectSpec data;
auto distinct() -> ProjectionQuery< Source, Result > &
auto limit(std::size_t limit) -> ProjectionQuery< Source, Result > &
auto having(const P &predicate) -> ProjectionQuery< Source, Result > &
ProjectionQuery()=default
auto groupBy(Columns... columns) -> ProjectionQuery< Source, Result > &
auto andHaving(const P &predicate) -> ProjectionQuery< Source, Result > &
auto orderBy(Orders... orders) -> ProjectionQuery< Source, Result > &
auto offset(std::size_t offset) -> ProjectionQuery< Source, Result > &
auto orWhere(const P &predicate) -> ProjectionQuery< Source, Result > &
auto orHaving(const P &predicate) -> ProjectionQuery< Source, Result > &
auto disableJoining() -> ProjectionQuery< Source, Result > &
auto where(const P &predicate) -> ProjectionQuery< Source, Result > &
auto andWhere(const P &predicate) -> ProjectionQuery< Source, Result > &
auto project(Projections... projections) -> ProjectionQuery< Source, Result > &
consteval auto logicalType() -> ColumnType
Resolves a C++ field type to its logical database type at compile time. Users may extend the closed s...
consteval auto typeId() noexcept -> TypeId
std::remove_reference_t< std::tuple_element_t< Index, typename detail::ReflectionTraits< detail::ReflectedType< T > >::Tuple > > field_type_t
consteval auto fields() noexcept
constexpr std::size_t fieldCount