forked from treefrogframework/treefrog-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcriteria.cpp
More file actions
373 lines (327 loc) · 10.1 KB
/
Copy pathtcriteria.cpp
File metadata and controls
373 lines (327 loc) · 10.1 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
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
/* Copyright (c) 2010-2019, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include <QSqlDatabase>
#include <QSqlField>
#include <TCriteria>
#include <TCriteriaConverter>
/*!
\class TCriteria
\brief The TCriteria class represents a WHERE clause without SQL for
the sake of database abstraction.
\sa TSqlObject
*/
/*!
Constructor.
*/
TCriteria::TCriteria() :
logiOp(None)
{
}
/*!
Copy constructor.
*/
TCriteria::TCriteria(const TCriteria &other) :
cri1(other.cri1),
cri2(other.cri2),
logiOp(other.logiOp)
{
}
/*!
Constructs a criteria initialized with a WHERE clause to
which a property of ORM object with the index \a property is
NULL value or NOT NULL value.
@sa TCriteria &TCriteria::add(int property, TSql::ComparisonOperator op)
*/
TCriteria::TCriteria(int property, TSql::ComparisonOperator op) :
logiOp(None)
{
cri1 = QVariant::fromValue(TCriteriaData(property, op));
}
/*!
Constructs a criteria initialized with a WHERE clause to which
a property of ORM object with the index \a property equals the
value \a val.
@sa TCriteria &TCriteria::add(int property, const QVariant &val)
*/
TCriteria::TCriteria(int property, const QVariant &val) :
logiOp(None)
{
cri1 = QVariant::fromValue(TCriteriaData(property, TSql::Equal, val));
}
/*!
Constructs a criteria initialized with a WHERE clause to which
a property of ORM object with the index \a property is compared
with the \a val value by a means of the \a op parameter.
@sa TCriteria &TCriteria::add(int property, TSql::ComparisonOperator op, const QVariant &val)
*/
TCriteria::TCriteria(int property, TSql::ComparisonOperator op, const QVariant &val) :
logiOp(None)
{
cri1 = QVariant::fromValue(TCriteriaData(property, op, val));
}
/*!
Constructs a criteria initialized with
a WHERE clause to which a property of ORM object with
the index \a property is compared with the \a val1 value and
the \a val2 value by a means of the \a op parameter.
@sa TCriteria &TCriteria::add(int property, TSql::ComparisonOperator op, const QVariant &val1, const QVariant &val2)
*/
TCriteria::TCriteria(int property, TSql::ComparisonOperator op, const QVariant &val1, const QVariant &val2) :
logiOp(None)
{
cri1 = QVariant::fromValue(TCriteriaData(property, op, val1, val2));
}
/*!
Constructs a criteria initialized with
a WHERE clause to which a property of ORM object with
the index \a property is compared with the \a val value by
a means of the \a op1 and \a op2 parameter.
@sa TCriteria &TCriteria::add(int property, TSql::ComparisonOperator op1, TSql::ComparisonOperator op2, const QVariant &val)
*/
TCriteria::TCriteria(int property, TSql::ComparisonOperator op1, TSql::ComparisonOperator op2, const QVariant &val) :
logiOp(None)
{
cri1 = QVariant::fromValue(TCriteriaData(property, op1, op2, val));
}
TCriteria::TCriteria(int property, TMongo::ComparisonOperator op) :
logiOp(None)
{
cri1 = QVariant::fromValue(TCriteriaData(property, op));
}
TCriteria::TCriteria(int property, TMongo::ComparisonOperator op, const QVariant &val) :
logiOp(None)
{
cri1 = QVariant::fromValue(TCriteriaData(property, op, val));
}
/*!
Adds a WHERE clause to which a property of ORM object with
the index \a property is NULL value or NOT NULL value.
The \a op parameter must be TSql::IsNull or TSql::IsNotNull.
*/
TCriteria &TCriteria::add(int property, TSql::ComparisonOperator op)
{
return add(And, TCriteria(property, op));
}
/*!
Adds a WHERE clause to which a property of ORM object with
the index \a property equals the value \a val.
*/
TCriteria &TCriteria::add(int property, const QVariant &val)
{
return add(And, TCriteria(property, val));
}
/*!
Adds a WHERE clause to which a property of ORM object with
the index \a property is compared with the \a val value by
a means of the \a op parameter.
The \a op parameter must be one of the following constants:\n
TSql::Equal, TSql::NotEqual, TSql::LessThan, TSql::GreaterThan,
TSql::LessEqual, TSql::GreaterEqual, TSql::Like, TSql::NotLike,
TSql::ILike, TSql::NotILike.
*/
TCriteria &TCriteria::add(int property, TSql::ComparisonOperator op, const QVariant &val)
{
return add(And, TCriteria(property, op, val));
}
/*!
Adds a WHERE clause to which a property of ORM object with
the index \a property is compared with the \a val1 value and
the \a val2 value by a means of the \a op parameter.
The \a op parameter must be one of the following constants:\n
TSql::LikeEscape, TSql::NotLikeEscape, TSql::ILikeEscape,
TSql::NotILikeEscape, TSql::Between, TSql::NotBetween.
*/
TCriteria &TCriteria::add(int property, TSql::ComparisonOperator op, const QVariant &val1, const QVariant &val2)
{
return add(And, TCriteria(property, op, val1, val2));
}
/*!
Adds a WHERE clause to which a property of ORM object with
the index \a property is compared with the \a val value by
a means of the \a op1 and \a op2 parameter. This function is
used with \a TSql::Any or \a TSql::All constant as the
\a op2 parameter to generate a WHERE clause such as
"column >= any (10, 20, 50)".
*/
TCriteria &TCriteria::add(int property, TSql::ComparisonOperator op1, TSql::ComparisonOperator op2, const QVariant &val)
{
return add(And, TCriteria(property, op1, op2, val));
}
TCriteria &TCriteria::add(int property, TMongo::ComparisonOperator op)
{
return add(And, TCriteria(property, op));
}
TCriteria &TCriteria::add(int property, TMongo::ComparisonOperator op, const QVariant &val)
{
return add(And, TCriteria(property, op, val));
}
/*!
Adds a WHERE clause of the \a criteria parameter with the
AND operator.
*/
TCriteria &TCriteria::add(const TCriteria &criteria)
{
return add(And, criteria);
}
/*!
Adds a WHERE clause with OR operator to which a property of
ORM object with the index \a property is NULL value or NOT
NULL value. The \a op parameter must be TSql::IsNull or
TSql::IsNotNull.
*/
TCriteria &TCriteria::addOr(int property, TSql::ComparisonOperator op)
{
return add(Or, TCriteria(property, op));
}
/*!
Adds a WHERE clause with OR operator to which a property
of ORM object with the index \a property equals the value
\a val.
*/
TCriteria &TCriteria::addOr(int property, const QVariant &val)
{
return add(Or, TCriteria(property, val));
}
/*!
Adds a WHERE clause with OR operator to which a property
of ORM object with the index \a property is compared with
the \a val value by a means of the \a op parameter.
*/
TCriteria &TCriteria::addOr(int property, TSql::ComparisonOperator op, const QVariant &val)
{
return add(Or, TCriteria(property, op, val));
}
/*!
Adds a WHERE clause with OR operator to which a property
of ORM object with the index \a property is compared with
the \a val1 value and the \a val2 value by a means of the
\a op parameter.
The \a op parameter must be one of the following constants:\n
TSql::LikeEscape, TSql::NotLikeEscape, TSql::ILikeEscape,
TSql::NotILikeEscape, TSql::Between, TSql::NotBetween.
*/
TCriteria &TCriteria::addOr(int property, TSql::ComparisonOperator op, const QVariant &val1, const QVariant &val2)
{
return add(Or, TCriteria(property, op, val1, val2));
}
/*!
Adds a WHERE clause with OR operator to which a property
of ORM object with the index \a property is compared with
the \a val value by a means of the \a op1 and \a op2
parameter. This function is used with \a TSql::Any or
\a TSql::All constant as the \a op2 parameter to generate
a WHERE clause such as
"column >= any (10, 20, 50)".
*/
TCriteria &TCriteria::addOr(int property, TSql::ComparisonOperator op1, TSql::ComparisonOperator op2, const QVariant &val)
{
return add(Or, TCriteria(property, op1, op2, val));
}
TCriteria &TCriteria::addOr(int property, TMongo::ComparisonOperator op)
{
return add(Or, TCriteria(property, op));
}
TCriteria &TCriteria::addOr(int property, TMongo::ComparisonOperator op, const QVariant &val)
{
return add(Or, TCriteria(property, op, val));
}
/*!
Adds a WHERE clause of the \a criteria parameter with the
OR operator.
*/
TCriteria &TCriteria::addOr(const TCriteria &criteria)
{
return add(Or, criteria);
}
/*!
Adds a WHERE clause of the \a criteria parameter with the
\a op operator.
*/
TCriteria &TCriteria::add(LogicalOperator op, const TCriteria &criteria)
{
if (cri1.isNull()) {
cri1 = QVariant::fromValue(criteria);
logiOp = None;
cri2.clear();
} else {
if (logiOp != None) {
cri1 = QVariant::fromValue(*this);
}
logiOp = op;
cri2 = QVariant::fromValue(criteria);
}
return *this;
}
/*!
Adds a WHERE clause of the \a criteria parameter with the
AND operator.
@sa TCriteria &TCriteria::add(const TCriteria &criteria)
*/
const TCriteria TCriteria::operator&&(const TCriteria &criteria) const
{
TCriteria res(*this);
res.add(criteria);
return res;
}
/*!
Adds a WHERE clause of the \a criteria parameter with the
OR operator.
@sa TCriteria &TCriteria::addOr(const TCriteria &criteria)
*/
const TCriteria TCriteria::operator||(const TCriteria &criteria) const
{
TCriteria res(*this);
res.addOr(criteria);
return res;
}
/*!
Returns a WHERE clause that negated this criteria.
*/
const TCriteria TCriteria::operator!() const
{
TCriteria cri(*this);
cri.logiOp = Not;
return cri;
}
/*!
Assignment operator.
*/
TCriteria &TCriteria::operator=(const TCriteria &other)
{
cri1 = other.cri1;
cri2 = other.cri2;
logiOp = other.logiOp;
return *this;
}
/*!
Returns true if the criteria has no data; otherwise returns false.
*/
bool TCriteria::isEmpty() const
{
return cri1.isNull();
}
/*!
Clears the criteria and makes it empty.
*/
void TCriteria::clear()
{
cri1.clear();
logiOp = None;
cri2.clear();
}
/*!
\fn const QVariant &TCriteria::first() const
This function is for internal use only.
*/
/*!
\fn const QVariant &TCriteria::second() const
This function is for internal use only.
*/
/*!
\fn LogicalOperator TCriteria::logicalOperator() const
This function is for internal use only.
*/