-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_predicates.h
More file actions
293 lines (245 loc) · 10.4 KB
/
Copy pathquery_predicates.h
File metadata and controls
293 lines (245 loc) · 10.4 KB
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
// MIT License
//
// Copyright (c) 2023 Ioannis Kaliakatsos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <functional>
#include <string>
#include <memory>
#include <vector>
#include "reflection.h"
namespace sqlite_reflection {
class AndPredicate;
class OrPredicate;
struct REFLECTION_EXPORT SqlValue
{
SqlValue();
SqliteStorageClass storage_class;
int64_t int_value;
bool bool_value;
double real_value;
std::string text_value;
};
/// The base class of all WHERE predicates used in SQLite queries
class REFLECTION_EXPORT QueryPredicateBase
{
public:
virtual ~QueryPredicateBase() = default;
/// Returns a textual representation of the predicate with placeholders for bound values
virtual std::string Evaluate() const = 0;
/// Returns values that need to be bound to the predicate placeholders
virtual std::vector<SqlValue> Bindings() const = 0;
/// Creates a clone for compounding predicates
virtual QueryPredicateBase* Clone() const = 0;
/// Returns a compound predicate, which requires that both the current
/// and the other predicate are valid at the same time
AndPredicate And(const QueryPredicateBase& other) const;
/// Returns a compound predicate, which requires that either the current
/// or the other predicate are valid
OrPredicate Or(const QueryPredicateBase& other) const;
};
/// A wrapper of a query predicate, which can be constructed from
/// a pointer-to-member function of a reflectable struct, thus enabling
/// type safety and compile-time guarantees, that the query is indeed valid
class REFLECTION_EXPORT QueryPredicate : public QueryPredicateBase
{
public:
std::string Evaluate() const override;
std::vector<SqlValue> Bindings() const override;
QueryPredicateBase* Clone() const override;
protected:
template <typename T, typename R>
QueryPredicate(R T::* fn, R value, const std::string& symbol, std::function<SqlValue(void*, SqliteStorageClass)> value_retrieval)
: symbol_(symbol)
{
auto record = GetRecordFromTypeId(typeid(T).name());
auto offset = OffsetFromStart(fn);
for (auto i = 0; i < record.member_metadata.size(); ++i) {
if (record.member_metadata[i].offset == offset) {
member_name_ = record.member_metadata[i].name;
value_ = value_retrieval((void*)&value, record.member_metadata[i].storage_class);
break;
}
}
}
template <typename T, typename R>
QueryPredicate(R T::* fn, R value, const std::string& symbol)
: QueryPredicate(fn, value, symbol, [&](void* v, SqliteStorageClass storage_class){
return GetSqlValue(v, storage_class);
}) {}
QueryPredicate(const std::string& symbol, const std::string& member_name, const SqlValue& value)
: symbol_(symbol), member_name_(member_name), value_(value) {}
/// Returns the value used for the current query, against which the struct member
/// (defined from the pointer-to-member function) will be compared. The value needs
/// to be type-erased, so that the header file is not bloated with unnecessary implementation details
virtual SqlValue GetSqlValue(void* v, SqliteStorageClass storage_class) const;
/// The symbol used for the comparison, for example "=" for equality
std::string symbol_;
/// The name of the compared member, as defined in source code, used to construct the
/// textual representation of the evaluation string
std::string member_name_;
/// The comparison value that will be bound to the generated SQL placeholder
SqlValue value_;
};
/// A wrapper for an empty predicate, used to fetch all elements of an SQLite table
class REFLECTION_EXPORT EmptyPredicate final : public QueryPredicateBase
{
public:
std::string Evaluate() const override;
std::vector<SqlValue> Bindings() const override;
QueryPredicateBase* Clone() const override;
};
/// A wrapper for an equality predicate, for which the value of the
/// struct member is required to be equal to a given control value
class REFLECTION_EXPORT Equal final : public QueryPredicate
{
public:
template <typename T, typename R>
explicit Equal(R T::* fn, R value)
: QueryPredicate(fn, value, "=") {}
template <typename T>
explicit Equal(int64_t T::* fn, int value)
: Equal(fn, (int64_t)value) {}
template <typename T>
explicit Equal(std::wstring T::* fn, const wchar_t* value)
: Equal(fn, std::wstring(value)) {}
};
/// A wrapper for an inequality predicate, for which the value of the
/// struct member is required to be unequal to a given control value
class REFLECTION_EXPORT Unequal final : public QueryPredicate
{
public:
template <typename T, typename R>
explicit Unequal(R T::* fn, R value)
: QueryPredicate(fn, value, "!=") {}
template <typename T>
explicit Unequal(int64_t T::* fn, int value)
: Unequal(fn, (int64_t)value) {}
template <typename T>
explicit Unequal(std::wstring T::* fn, const wchar_t* value)
: Unequal(fn, std::wstring(value)) {}
};
/// A wrapper for a similarity predicate, for which the value of the
/// struct member is required to be similar to a given control value
class REFLECTION_EXPORT Like final : public QueryPredicate
{
public:
template <typename T, typename R>
explicit Like(R T::* fn, R value)
: QueryPredicate(fn, value, "LIKE", [&](void* v, SqliteStorageClass storage_class){
return GetSqlValue(v, storage_class);
}) {}
template <typename T>
explicit Like(int64_t T::* fn, int value)
: Like(fn, (int64_t)value) {}
template <typename T>
explicit Like(std::wstring T::* fn, const wchar_t* value)
: Like(fn, std::wstring(value)) {}
protected:
SqlValue GetSqlValue(void* v, SqliteStorageClass storage_class) const override;
};
/// A wrapper for a comparison predicate, for which the value of the
/// struct member is required to be greater than a given control value
class REFLECTION_EXPORT GreaterThan final : public QueryPredicate
{
public:
template <typename T>
explicit GreaterThan(int64_t T::* fn, int64_t value)
: QueryPredicate(fn, value, ">") {}
template <typename T>
explicit GreaterThan(int64_t T::* fn, int value)
: QueryPredicate(fn, (int64_t)value, ">") {}
template <typename T>
explicit GreaterThan(double T::* fn, double value)
: QueryPredicate(fn, value, ">") {}
};
/// A wrapper for a comparison predicate, for which the value of the
/// struct member is required to be greater than or equal to a given control value
class REFLECTION_EXPORT GreaterThanOrEqual final : public QueryPredicate
{
public:
template <typename T>
explicit GreaterThanOrEqual(int64_t T::* fn, int64_t value)
: QueryPredicate(fn, value, ">=") {}
template <typename T>
explicit GreaterThanOrEqual(int64_t T::* fn, int value)
: QueryPredicate(fn, (int64_t)value, ">=") {}
template <typename T>
explicit GreaterThanOrEqual(double T::* fn, double value)
: QueryPredicate(fn, value, ">=") {}
};
/// A wrapper for a comparison predicate, for which the value of the
/// struct member is required to be smaller than a given control value
class REFLECTION_EXPORT SmallerThan final : public QueryPredicate
{
public:
template <typename T>
explicit SmallerThan(int64_t T::* fn, int64_t value)
: QueryPredicate(fn, value, "<") {}
template <typename T>
explicit SmallerThan(int64_t T::* fn, int value)
: QueryPredicate(fn, (int64_t)value, "<") {}
template <typename T>
explicit SmallerThan(double T::* fn, double value)
: QueryPredicate(fn, value, "<") {}
};
/// A wrapper for a comparison predicate, for which the value of the
/// struct member is required to be smaller than or equal to a given control value
class REFLECTION_EXPORT SmallerThanOrEqual final : public QueryPredicate
{
public:
template <typename T>
explicit SmallerThanOrEqual(int64_t T::* fn, int64_t value)
: QueryPredicate(fn, value, "<=") {}
template <typename T>
explicit SmallerThanOrEqual(int64_t T::* fn, int value)
: QueryPredicate(fn, (int64_t)value, "<=") {}
template <typename T>
explicit SmallerThanOrEqual(double T::* fn, double value)
: QueryPredicate(fn, value, "<=") {}
};
/// A wrapper of a compound predicate, which combines two other predicates,
/// allowing the construction of more complex predicates from elementary predicates
class REFLECTION_EXPORT BinaryPredicate : public QueryPredicateBase
{
public:
std::string Evaluate() const override;
std::vector<SqlValue> Bindings() const override;
protected:
BinaryPredicate(const QueryPredicateBase& left, const QueryPredicateBase& right, const std::string& symbol);
std::unique_ptr<QueryPredicateBase> left_;
std::unique_ptr<QueryPredicateBase> right_;
std::string symbol_;
};
/// A compound predicate, which requires that both predicates are valid at the same time
class REFLECTION_EXPORT AndPredicate final : public BinaryPredicate
{
public:
AndPredicate(const QueryPredicateBase& left, const QueryPredicateBase& right);
QueryPredicateBase* Clone() const override;
};
/// A compound predicate, which requires that either predicate is valid
class REFLECTION_EXPORT OrPredicate final : public BinaryPredicate
{
public:
OrPredicate(const QueryPredicateBase& left, const QueryPredicateBase& right);
QueryPredicateBase* Clone() const override;
};
}