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 pathoperators.h
278 lines (227 loc) · 7.78 KB
/
operators.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
#pragma once
#include <type_traits> // std::false_type, std::true_type
#include <utility> // std::move
#include "functional/cxx_optional.h"
#include "tags.h"
#include "serialize_result_type.h"
namespace sqlite_orm {
namespace internal {
/**
* Inherit this class to support arithmetic types overloading
*/
struct arithmetic_t {};
template<class L, class R, class... Ds>
struct binary_operator : Ds... {
using left_type = L;
using right_type = R;
left_type lhs;
right_type rhs;
binary_operator(left_type lhs_, right_type rhs_) : lhs(std::move(lhs_)), rhs(std::move(rhs_)) {}
};
struct conc_string {
serialize_result_type serialize() const {
return "||";
}
};
/**
* Result of concatenation || operator
*/
template<class L, class R>
using conc_t = binary_operator<L, R, conc_string>;
struct add_string {
serialize_result_type serialize() const {
return "+";
}
};
/**
* Result of addition + operator
*/
template<class L, class R>
using add_t = binary_operator<L, R, add_string, arithmetic_t, negatable_t>;
struct sub_string {
serialize_result_type serialize() const {
return "-";
}
};
/**
* Result of substitute - operator
*/
template<class L, class R>
using sub_t = binary_operator<L, R, sub_string, arithmetic_t, negatable_t>;
struct mul_string {
serialize_result_type serialize() const {
return "*";
}
};
/**
* Result of multiply * operator
*/
template<class L, class R>
using mul_t = binary_operator<L, R, mul_string, arithmetic_t, negatable_t>;
struct div_string {
serialize_result_type serialize() const {
return "/";
}
};
/**
* Result of divide / operator
*/
template<class L, class R>
using div_t = binary_operator<L, R, div_string, arithmetic_t, negatable_t>;
struct mod_operator_string {
serialize_result_type serialize() const {
return "%";
}
};
/**
* Result of mod % operator
*/
template<class L, class R>
using mod_t = binary_operator<L, R, mod_operator_string, arithmetic_t, negatable_t>;
struct bitwise_shift_left_string {
serialize_result_type serialize() const {
return "<<";
}
};
/**
* Result of bitwise shift left << operator
*/
template<class L, class R>
using bitwise_shift_left_t = binary_operator<L, R, bitwise_shift_left_string, arithmetic_t, negatable_t>;
struct bitwise_shift_right_string {
serialize_result_type serialize() const {
return ">>";
}
};
/**
* Result of bitwise shift right >> operator
*/
template<class L, class R>
using bitwise_shift_right_t = binary_operator<L, R, bitwise_shift_right_string, arithmetic_t, negatable_t>;
struct bitwise_and_string {
serialize_result_type serialize() const {
return "&";
}
};
/**
* Result of bitwise and & operator
*/
template<class L, class R>
using bitwise_and_t = binary_operator<L, R, bitwise_and_string, arithmetic_t, negatable_t>;
struct bitwise_or_string {
serialize_result_type serialize() const {
return "|";
}
};
/**
* Result of bitwise or | operator
*/
template<class L, class R>
using bitwise_or_t = binary_operator<L, R, bitwise_or_string, arithmetic_t, negatable_t>;
struct bitwise_not_string {
serialize_result_type serialize() const {
return "~";
}
};
/**
* Result of bitwise not ~ operator
*/
template<class T>
struct bitwise_not_t : bitwise_not_string, arithmetic_t, negatable_t {
using argument_type = T;
argument_type argument;
bitwise_not_t(argument_type argument_) : argument(std::move(argument_)) {}
};
struct assign_string {
serialize_result_type serialize() const {
return "=";
}
};
/**
* Result of assign = operator
*/
template<class L, class R>
using assign_t = binary_operator<L, R, assign_string>;
/**
* Assign operator traits. Common case
*/
template<class T>
struct is_assign_t : public std::false_type {};
/**
* Assign operator traits. Specialized case
*/
template<class L, class R>
struct is_assign_t<assign_t<L, R>> : public std::true_type {};
template<class L, class... Args>
struct in_t;
}
/**
* Public interface for || concatenation operator. Example: `select(conc(&User::name, "@gmail.com"));` => SELECT
* name || '@gmail.com' FROM users
*/
template<class L, class R>
internal::conc_t<L, R> conc(L l, R r) {
return {std::move(l), std::move(r)};
}
/**
* Public interface for + operator. Example: `select(add(&User::age, 100));` => SELECT age + 100 FROM users
*/
template<class L, class R>
internal::add_t<L, R> add(L l, R r) {
return {std::move(l), std::move(r)};
}
/**
* Public interface for - operator. Example: `select(sub(&User::age, 1));` => SELECT age - 1 FROM users
*/
template<class L, class R>
internal::sub_t<L, R> sub(L l, R r) {
return {std::move(l), std::move(r)};
}
/**
* Public interface for * operator. Example: `select(mul(&User::salary, 2));` => SELECT salary * 2 FROM users
*/
template<class L, class R>
internal::mul_t<L, R> mul(L l, R r) {
return {std::move(l), std::move(r)};
}
/**
* Public interface for / operator. Example: `select(div(&User::salary, 3));` => SELECT salary / 3 FROM users
* @note Please notice that ::div function already exists in pure C standard library inside <cstdlib> header.
* If you use `using namespace sqlite_orm` directive you an specify which `div` you call explicitly using `::div` or `sqlite_orm::div` statements.
*/
template<class L, class R>
internal::div_t<L, R> div(L l, R r) {
return {std::move(l), std::move(r)};
}
/**
* Public interface for % operator. Example: `select(mod(&User::age, 5));` => SELECT age % 5 FROM users
*/
template<class L, class R>
internal::mod_t<L, R> mod(L l, R r) {
return {std::move(l), std::move(r)};
}
template<class L, class R>
internal::bitwise_shift_left_t<L, R> bitwise_shift_left(L l, R r) {
return {std::move(l), std::move(r)};
}
template<class L, class R>
internal::bitwise_shift_right_t<L, R> bitwise_shift_right(L l, R r) {
return {std::move(l), std::move(r)};
}
template<class L, class R>
internal::bitwise_and_t<L, R> bitwise_and(L l, R r) {
return {std::move(l), std::move(r)};
}
template<class L, class R>
internal::bitwise_or_t<L, R> bitwise_or(L l, R r) {
return {std::move(l), std::move(r)};
}
template<class T>
internal::bitwise_not_t<T> bitwise_not(T t) {
return {std::move(t)};
}
template<class L, class R>
internal::assign_t<L, R> assign(L l, R r) {
return {std::move(l), std::move(r)};
}
}