ORM C++
Loading...
Searching...
No Matches
select.cppm
Go to the documentation of this file.
1module;
2
3#include <algorithm>
4#include <cstddef>
5#include <optional>
6#include <stdexcept>
7#include <string>
8#include <string_view>
9#include <type_traits>
10#include <utility>
11#include <vector>
12
13export module orm:dynamic_select;
14
15import orm.reflection;
16import :foundation;
17import :model;
18import :expressions;
19
20// query.hpp
21namespace orm::detail
22{
23class QueryTestAccess;
24}
25
26namespace orm
27{
28export
29{
30
31 /**
32 * @brief A template class representing a select query in the ORM framework.
33 *
34 * This class provides functionality for building a select query.
35 *
36 * @tparam T The type of the query.
37 */
38 template <typename T>
39 class Query
40 {
41 public:
42 /**
43 * @brief Constructs a Query object.
44 */
45 Query() = default;
46
47 /**
48 * @brief Replaces the WHERE predicate.
49 * @param predicate
50 */
51 template <typename P>
52// requires query::detail::PredicateFor<P, T>
53 auto where(const P& predicate) -> Query<T>&
54 {
55 data.predicate = query::detail::erase(predicate);
56
57 return *this;
58 }
59
60 /**
61 * @brief Adds a predicate with AND.
62 * @param predicate
63 */
64 template <typename P>
65// requires query::detail::PredicateFor<P, T>
66 auto andWhere(const P& predicate) -> Query<T>&
67 {
68 data.predicate = data.predicate.has_value() ? data.predicate.value() && query::detail::erase(predicate) :
69 query::detail::erase(predicate);
70
71 return *this;
72 }
73
74 /**
75 * @brief Adds a predicate with OR.
76 * @param predicate
77 */
78 template <typename P>
79// requires query::detail::PredicateFor<P, T>
80 auto orWhere(const P& predicate) -> Query<T>&
81 {
82 data.predicate = data.predicate.has_value() ? data.predicate.value() || query::detail::erase(predicate) :
83 query::detail::erase(predicate);
84
85 return *this;
86 }
87
88 /**
89 * @brief Sets the ORDER BY clauses for the select query.
90 * @param orders The ORDER BY clauses.
91 *
92 * @return A reference to the QueryBuilder object.
93 */
94 template <typename... Orders>
95// requires query::detail::ORM_QUERY_MODEL_ORDERS<T, Orders...>
96 auto orderBy(Orders... orders) -> Query<T>&
97 {
98 data.orderBy = {query::detail::erase(orders)...};
99
100 return *this;
101 }
102
103 /**
104 * @brief Sets the GROUP BY columns for the select query.
105 * @param columns The model column paths to
106 * group by.
107 * @return A reference to this query.
108 */
109 template <typename... Columns>
110// requires query::detail::ORM_QUERY_MODEL_COLUMNS<T, Columns...>
111 auto groupBy(Columns... columns) -> Query<T>&
112 {
113 data.groupBy = {query::detail::erase(columns)...};
114
115 return *this;
116 }
117
118 /**
119 * @brief Replaces the HAVING predicate.
120 * @param predicate The aggregate predicate.
121 * @return A
122 * reference to this query.
123 */
124 template <typename P>
125// requires query::detail::AggregatePredicateFor<P, T>
126 auto having(const P& predicate) -> Query<T>&
127 {
128 data.having = query::detail::erase(predicate);
129
130 return *this;
131 }
132
133 /**
134 * @brief Adds an aggregate predicate with AND.
135 * @param predicate The aggregate predicate.
136 *
137 * @return A reference to this query.
138 */
139 template <typename P>
140// requires query::detail::AggregatePredicateFor<P, T>
141 auto andHaving(const P& predicate) -> Query<T>&
142 {
143 data.having = data.having.has_value() ? data.having.value() && query::detail::erase(predicate) :
144 query::detail::erase(predicate);
145
146 return *this;
147 }
148
149 /**
150 * @brief Adds an aggregate predicate with OR.
151 * @param predicate The aggregate predicate.
152 *
153 * @return A reference to this query.
154 */
155 template <typename P>
156// requires query::detail::AggregatePredicateFor<P, T>
157 auto orHaving(const P& predicate) -> Query<T>&
158 {
159 data.having = data.having.has_value() ? data.having.value() || query::detail::erase(predicate) :
160 query::detail::erase(predicate);
161
162 return *this;
163 }
164
165 /**
166 * @brief Select distinct rows.
167 * @return A reference to the QueryBuilder object.
168 */
169 inline auto distinct() -> Query<T>&
170 {
171 data.isDistinct = true;
172
173 return *this;
174 }
175
176 /**
177 * @brief Sets the OFFSET clause for the select query.
178 * @param offset The number of rows to skip.
179 * @return A reference to the QueryBuilder object.
180 */
181 inline auto offset(std::size_t offset) -> Query<T>&
182 {
183 data.offset = offset;
184
185 return *this;
186 }
187
188 /**
189 * @brief Sets the LIMIT clause for the select query.
190 * @param limit The maximum number of rows to return.
191 * @return A reference to the QueryBuilder object.
192 */
193 inline auto limit(std::size_t limit) -> Query<T>&
194 {
195 data.limit = limit;
196
197 return *this;
198 }
199
200 /**
201 * @brief Disables joining for the select query.
202 * @note Only ids fields will be set in related models.
203 * @return A reference to the QueryBuilder object.
204 */
205 inline auto disableJoining() -> Query<T>&
206 {
207 data.shouldJoin = false;
208
209 return *this;
210 }
211
212 /**
213 * @brief Explicitly loads a mapped OneToMany or ManyToMany collection.
214 *
215 * Repeating the same
216 * include is idempotent. Collection loading is performed
217 * after the root SELECT, so root pagination is
218 * preserved.
219 */
220 template <auto Member>
221// requires query::detail::ORM_QUERY_COLLECTION<Member> &&
222// query::detail::ORM_QUERY_MODEL_TYPE<typename query::detail::CollectionTraits<Member>::Model, T>
223 auto include() -> Query<T>&
224 {
225 const auto relation = query::detail::CollectionTraits<Member>::name();
226 if (std::ranges::find(data.includes, relation) == data.includes.end())
227 data.includes.push_back(relation);
228 return *this;
229 }
230
231 private:
232 /**
233 * @brief Database class is a friend class of Query for access to the query data.
234 */
235 template <typename>
236 friend class orm::OrmContext;
237 friend class orm::detail::QueryTestAccess;
238
239 /**
240 * @brief Gets the query data.
241 * @return The query data.
242 */
243 [[nodiscard]] inline auto getData() const -> const query::detail::SelectSpec&
244 {
245 return data;
246 }
247
248 query::detail::SelectSpec data; /**< Runtime query options; model metadata comes from OrmContext<Schema>. */
249 };
250}
251} // namespace orm
auto orderBy(Orders... orders) -> Query< T > &
Sets the ORDER BY clauses for the select query.
Definition select.cppm:95
auto orHaving(const P &predicate) -> Query< T > &
Adds an aggregate predicate with OR.
Definition select.cppm:156
auto groupBy(Columns... columns) -> Query< T > &
Sets the GROUP BY columns for the select query.
Definition select.cppm:110
auto where(const P &predicate) -> Query< T > &
Replaces the WHERE predicate.
Definition select.cppm:52
Query()=default
Constructs a Query object.
auto distinct() -> Query< T > &
Select distinct rows.
Definition select.cppm:168
auto limit(std::size_t limit) -> Query< T > &
Sets the LIMIT clause for the select query.
Definition select.cppm:192
auto andHaving(const P &predicate) -> Query< T > &
Adds an aggregate predicate with AND.
Definition select.cppm:140
auto having(const P &predicate) -> Query< T > &
Replaces the HAVING predicate.
Definition select.cppm:125
auto disableJoining() -> Query< T > &
Disables joining for the select query.
Definition select.cppm:204
auto offset(std::size_t offset) -> Query< T > &
Sets the OFFSET clause for the select query.
Definition select.cppm:180
auto orWhere(const P &predicate) -> Query< T > &
Adds a predicate with OR.
Definition select.cppm:79
auto andWhere(const P &predicate) -> Query< T > &
Adds a predicate with AND.
Definition select.cppm:65
auto include() -> Query< T > &
Explicitly loads a mapped OneToMany or ManyToMany collection.
Definition select.cppm:222