This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from fnc12/sqlite_orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_constraints.h
433 lines (361 loc) · 13.8 KB
/
select_constraints.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#pragma once
#include <type_traits> // std::remove_const
#include <string> // std::string
#include <utility> // std::move
#include <tuple> // std::tuple, std::get, std::tuple_size
#include "functional/cxx_optional.h"
#include "functional/cxx_universal.h"
#include "functional/cxx_type_traits_polyfill.h"
#include "is_base_of_template.h"
#include "tuple_helper/tuple_filter.h"
#include "optional_container.h"
#include "ast/where.h"
#include "ast/group_by.h"
#include "core_functions.h"
#include "alias_traits.h"
#include "column_pointer.h"
namespace sqlite_orm {
namespace internal {
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
template<class T>
struct as_optional_t {
using value_type = T;
value_type value;
};
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
struct distinct_string {
operator std::string() const {
return "DISTINCT";
}
};
/**
* DISCTINCT generic container.
*/
template<class T>
struct distinct_t : distinct_string {
using value_type = T;
value_type value;
distinct_t(value_type value_) : value(std::move(value_)) {}
};
struct all_string {
operator std::string() const {
return "ALL";
}
};
/**
* ALL generic container.
*/
template<class T>
struct all_t : all_string {
T value;
all_t(T value_) : value(std::move(value_)) {}
};
template<class... Args>
struct columns_t {
using columns_type = std::tuple<Args...>;
columns_type columns;
bool distinct = false;
static constexpr int count = std::tuple_size<columns_type>::value;
#ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
columns_t(columns_type columns) : columns{std::move(columns)} {}
#endif
};
template<class T>
SQLITE_ORM_INLINE_VAR constexpr bool is_columns_v = polyfill::is_specialization_of_v<T, columns_t>;
template<class T>
using is_columns = polyfill::bool_constant<is_columns_v<T>>;
/**
* Subselect object type.
*/
template<class T, class... Args>
struct select_t {
using return_type = T;
using conditions_type = std::tuple<Args...>;
return_type col;
conditions_type conditions;
bool highest_level = false;
#ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
select_t(return_type col, conditions_type conditions) :
col{std::move(col)}, conditions{std::move(conditions)} {}
#endif
};
template<class T>
SQLITE_ORM_INLINE_VAR constexpr bool is_select_v = polyfill::is_specialization_of_v<T, select_t>;
template<class T>
using is_select = polyfill::bool_constant<is_select_v<T>>;
/**
* Base for UNION, UNION ALL, EXCEPT and INTERSECT
*/
template<class L, class R>
struct compound_operator {
using left_type = L;
using right_type = R;
left_type left;
right_type right;
compound_operator(left_type l, right_type r) : left(std::move(l)), right(std::move(r)) {
this->left.highest_level = true;
this->right.highest_level = true;
}
};
template<class T>
SQLITE_ORM_INLINE_VAR constexpr bool is_compound_operator_v = is_base_of_template_v<T, compound_operator>;
template<class T>
using is_compound_operator = polyfill::bool_constant<is_compound_operator_v<T>>;
struct union_base {
bool all = false;
#ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
union_base(bool all) : all{all} {}
#endif
operator std::string() const {
if(!this->all) {
return "UNION";
} else {
return "UNION ALL";
}
}
};
/**
* UNION object type.
*/
template<class L, class R>
struct union_t : public compound_operator<L, R>, union_base {
using left_type = typename compound_operator<L, R>::left_type;
using right_type = typename compound_operator<L, R>::right_type;
union_t(left_type l, right_type r, bool all_) :
compound_operator<L, R>{std::move(l), std::move(r)}, union_base{all_} {}
union_t(left_type l, right_type r) : union_t{std::move(l), std::move(r), false} {}
};
struct except_string {
operator std::string() const {
return "EXCEPT";
}
};
/**
* EXCEPT object type.
*/
template<class L, class R>
struct except_t : compound_operator<L, R>, except_string {
using super = compound_operator<L, R>;
using left_type = typename super::left_type;
using right_type = typename super::right_type;
using super::super;
};
struct intersect_string {
operator std::string() const {
return "INTERSECT";
}
};
/**
* INTERSECT object type.
*/
template<class L, class R>
struct intersect_t : compound_operator<L, R>, intersect_string {
using super = compound_operator<L, R>;
using left_type = typename super::left_type;
using right_type = typename super::right_type;
using super::super;
};
/**
* Generic way to get DISTINCT value from any type.
*/
template<class T>
bool get_distinct(const T&) {
return false;
}
template<class... Args>
bool get_distinct(const columns_t<Args...>& cols) {
return cols.distinct;
}
template<class T>
struct asterisk_t {
using type = T;
bool defined_order = false;
#ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
asterisk_t(bool definedOrder) : defined_order{definedOrder} {}
#endif
};
template<class T>
struct object_t {
using type = T;
bool defined_order = false;
#ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
object_t(bool definedOrder) : defined_order{definedOrder} {}
#endif
};
template<class T>
struct then_t {
using expression_type = T;
expression_type expression;
};
template<class R, class T, class E, class... Args>
struct simple_case_t {
using return_type = R;
using case_expression_type = T;
using args_type = std::tuple<Args...>;
using else_expression_type = E;
optional_container<case_expression_type> case_expression;
args_type args;
optional_container<else_expression_type> else_expression;
};
/**
* T is a case expression type
* E is else type (void is ELSE is omitted)
* Args... is a pack of WHEN expressions
*/
template<class R, class T, class E, class... Args>
struct simple_case_builder {
using return_type = R;
using case_expression_type = T;
using args_type = std::tuple<Args...>;
using else_expression_type = E;
optional_container<case_expression_type> case_expression;
args_type args;
optional_container<else_expression_type> else_expression;
template<class W, class Th>
simple_case_builder<R, T, E, Args..., std::pair<W, Th>> when(W w, then_t<Th> t) {
using result_args_type = std::tuple<Args..., std::pair<W, Th>>;
std::pair<W, Th> newPair{std::move(w), std::move(t.expression)};
result_args_type result_args = std::tuple_cat(std::move(this->args), std::make_tuple(newPair));
std::get<std::tuple_size<result_args_type>::value - 1>(result_args) = std::move(newPair);
return {std::move(this->case_expression), std::move(result_args), std::move(this->else_expression)};
}
simple_case_t<R, T, E, Args...> end() {
return {std::move(this->case_expression), std::move(args), std::move(this->else_expression)};
}
template<class El>
simple_case_builder<R, T, El, Args...> else_(El el) {
return {{std::move(this->case_expression)}, std::move(args), {std::move(el)}};
}
};
template<class T>
void validate_conditions() {
static_assert(count_tuple<T, is_where>::value <= 1, "a single query cannot contain > 1 WHERE blocks");
static_assert(count_tuple<T, is_group_by>::value <= 1, "a single query cannot contain > 1 GROUP BY blocks");
static_assert(count_tuple<T, is_order_by>::value <= 1, "a single query cannot contain > 1 ORDER BY blocks");
static_assert(count_tuple<T, is_limit>::value <= 1, "a single query cannot contain > 1 LIMIT blocks");
static_assert(count_tuple<T, is_from>::value <= 1, "a single query cannot contain > 1 FROM blocks");
}
}
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
template<class T>
internal::as_optional_t<T> as_optional(T value) {
return {std::move(value)};
}
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
template<class T>
internal::then_t<T> then(T t) {
return {std::move(t)};
}
template<class R, class T>
internal::simple_case_builder<R, T, void> case_(T t) {
return {{std::move(t)}};
}
template<class R>
internal::simple_case_builder<R, void, void> case_() {
return {};
}
template<class T>
internal::distinct_t<T> distinct(T t) {
return {std::move(t)};
}
template<class T>
internal::all_t<T> all(T t) {
return {std::move(t)};
}
template<class... Args>
internal::columns_t<Args...> distinct(internal::columns_t<Args...> cols) {
cols.distinct = true;
return cols;
}
template<class... Args>
internal::columns_t<Args...> columns(Args... args) {
return {std::make_tuple<Args...>(std::forward<Args>(args)...)};
}
/**
* Use it like this:
* struct MyType : BaseType { ... };
* storage.select(column<MyType>(&BaseType::id));
*/
template<class T, class F>
internal::column_pointer<T, F> column(F f) {
return {std::move(f)};
}
/**
* Public function for subselect query. Is useful in UNION queries.
*/
template<class T, class... Args>
internal::select_t<T, Args...> select(T t, Args... args) {
using args_tuple = std::tuple<Args...>;
internal::validate_conditions<args_tuple>();
return {std::move(t), std::make_tuple(std::forward<Args>(args)...)};
}
/**
* Public function for UNION operator.
* lhs and rhs are subselect objects.
* Look through example in examples/union.cpp
*/
template<class L, class R>
internal::union_t<L, R> union_(L lhs, R rhs) {
return {std::move(lhs), std::move(rhs)};
}
/**
* Public function for EXCEPT operator.
* lhs and rhs are subselect objects.
* Look through example in examples/except.cpp
*/
template<class L, class R>
internal::except_t<L, R> except(L lhs, R rhs) {
return {std::move(lhs), std::move(rhs)};
}
template<class L, class R>
internal::intersect_t<L, R> intersect(L lhs, R rhs) {
return {std::move(lhs), std::move(rhs)};
}
/**
* Public function for UNION ALL operator.
* lhs and rhs are subselect objects.
* Look through example in examples/union.cpp
*/
template<class L, class R>
internal::union_t<L, R> union_all(L lhs, R rhs) {
return {std::move(lhs), std::move(rhs), true};
}
/**
* `SELECT * FROM T` expression that fetches results as tuples.
* T is a type mapped to a storage, or an alias of it.
* The `definedOrder` parameter denotes the expected order of result columns.
* The default is the implicit order as returned by SQLite, which may differ from the defined order
* if the schema of a table has been changed.
* By specifying the defined order, the columns are written out in the resulting select SQL string.
*
* In pseudo code:
* select(asterisk<User>(false)) -> SELECT * from User
* select(asterisk<User>(true)) -> SELECT id, name from User
*
* Example: auto rows = storage.select(asterisk<User>());
* // decltype(rows) is std::vector<std::tuple<...all columns in implicitly stored order...>>
* Example: auto rows = storage.select(asterisk<User>(true));
* // decltype(rows) is std::vector<std::tuple<...all columns in declared make_table order...>>
*
* If you need to fetch results as objects instead of tuples please use `object<T>()`.
*/
template<class T>
internal::asterisk_t<T> asterisk(bool definedOrder = false) {
return {definedOrder};
}
/**
* `SELECT * FROM T` expression that fetches results as objects of type T.
* T is a type mapped to a storage, or an alias of it.
*
* Example: auto rows = storage.select(object<User>());
* // decltype(rows) is std::vector<User>, where the User objects are constructed from columns in implicitly stored order
* Example: auto rows = storage.select(object<User>(true));
* // decltype(rows) is std::vector<User>, where the User objects are constructed from columns in declared make_table order
*
* If you need to fetch results as tuples instead of objects please use `asterisk<T>()`.
*/
template<class T>
internal::object_t<T> object(bool definedOrder = false) {
return {definedOrder};
}
}