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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
/*
* Copyright 2004-2011 H2 Group.
* Copyright 2011 James Moger.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.iciql;
import java.util.List;
/**
* This class represents a query with a condition.
*
* @param <T>
* the return type
*/
public class QueryWhere<T> {
Query<T> query;
QueryWhere(Query<T> query) {
this.query = query;
}
/**
* Specify an AND condition with a mapped primitive boolean.
*
* @param x
* the primitive boolean field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Boolean> and(boolean x) {
query.getFrom().getAliasDefinition().checkMultipleBooleans();
return addPrimitive(ConditionAndOr.AND, x);
}
/**
* Specify an AND condition with a mapped primitive byte.
*
* @param x
* the primitive byte field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Byte> and(byte x) {
return addPrimitive(ConditionAndOr.AND, x);
}
/**
* Specify an AND condition with a mapped primitive short.
*
* @param x
* the primitive short field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Short> and(short x) {
return addPrimitive(ConditionAndOr.AND, x);
}
/**
* Specify an AND condition with a mapped primitive int.
*
* @param x
* the primitive int field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Integer> and(int x) {
return addPrimitive(ConditionAndOr.AND, x);
}
/**
* Specify an AND condition with a mapped primitive long.
*
* @param x
* the primitive long field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Long> and(long x) {
return addPrimitive(ConditionAndOr.AND, x);
}
/**
* Specify an AND condition with a mapped primitive float.
*
* @param x
* the primitive float field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Float> and(float x) {
return addPrimitive(ConditionAndOr.AND, x);
}
/**
* Specify an AND condition with a mapped primitive double.
*
* @param x
* the primitive double field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Double> and(double x) {
return addPrimitive(ConditionAndOr.AND, x);
}
private <A> QueryCondition<T, A> addPrimitive(ConditionAndOr condition, A x) {
query.addConditionToken(condition);
A alias = query.getPrimitiveAliasByValue(x);
if (alias == null) {
// this will result in an unmapped field exception
return new QueryCondition<T, A>(query, x);
}
return new QueryCondition<T, A>(query, alias);
}
/**
* Specify an AND condition with a mapped Object field.
*
* @param x
* the Object field to query
* @return a query condition to continue building the condition
*/
public <A> QueryCondition<T, A> and(A x) {
query.getFrom().getAliasDefinition().checkMultipleEnums(x);
query.addConditionToken(ConditionAndOr.AND);
return new QueryCondition<T, A>(query, x);
}
/**
* Specify an OR condition with a mapped primitive boolean.
*
* @param x
* the primitive boolean field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Boolean> or(boolean x) {
query.getFrom().getAliasDefinition().checkMultipleBooleans();
return addPrimitive(ConditionAndOr.OR, x);
}
/**
* Specify an OR condition with a mapped primitive byte.
*
* @param x
* the primitive byte field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Byte> or(byte x) {
return addPrimitive(ConditionAndOr.OR, x);
}
/**
* Specify an OR condition with a mapped primitive short.
*
* @param x
* the primitive short field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Short> or(short x) {
return addPrimitive(ConditionAndOr.OR, x);
}
/**
* Specify an OR condition with a mapped primitive int.
*
* @param x
* the primitive int field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Integer> or(int x) {
return addPrimitive(ConditionAndOr.OR, x);
}
/**
* Specify an OR condition with a mapped primitive long.
*
* @param x
* the primitive long field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Long> or(long x) {
return addPrimitive(ConditionAndOr.OR, x);
}
/**
* Specify an OR condition with a mapped primitive float.
*
* @param x
* the primitive float field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Float> or(float x) {
return addPrimitive(ConditionAndOr.OR, x);
}
/**
* Specify an OR condition with a mapped primitive double.
*
* @param x
* the primitive double field to query
* @return a query condition to continue building the condition
*/
public QueryCondition<T, Double> or(double x) {
return addPrimitive(ConditionAndOr.OR, x);
}
/**
* Specify an OR condition with a mapped Object field.
*
* @param x
* the Object field to query
* @return a query condition to continue building the condition
*/
public <A> QueryCondition<T, A> or(A x) {
query.getFrom().getAliasDefinition().checkMultipleEnums(x);
query.addConditionToken(ConditionAndOr.OR);
return new QueryCondition<T, A>(query, x);
}
public QueryWhere<T> limit(long limit) {
query.limit(limit);
return this;
}
public QueryWhere<T> offset(long offset) {
query.offset(offset);
return this;
}
public String getSQL() {
SQLStatement stat = new SQLStatement(query.getDb());
stat.appendSQL("SELECT *");
query.appendFromWhere(stat);
return stat.getSQL().trim();
}
/**
* toSQL returns a static string version of the query with runtime variables
* properly encoded. This method is also useful when combined with the where
* clause methods like isParameter() or atLeastParameter() which allows
* iciql to generate re-usable parameterized string statements.
*
* @return the sql query as plain text
*/
public String toSQL() {
return query.toSQL(false);
}
/**
* toSQL returns a static string version of the query with runtime variables
* properly encoded. This method is also useful when combined with the where
* clause methods like isParameter() or atLeastParameter() which allows
* iciql to generate re-usable parameterized string statements.
*
* @param distinct
* if true SELECT DISTINCT is used for the query
* @return the sql query as plain text
*/
public String toSQL(boolean distinct) {
return query.toSQL(distinct);
}
/**
* toSQL returns a static string version of the query with runtime variables
* properly encoded. This method is also useful when combined with the where
* clause methods like isParameter() or atLeastParameter() which allows
* iciql to generate re-usable parameterized string statements.
*
* @param distinct
* if true SELECT DISTINCT is used for the query
* @param k
* k is used to select only the columns of the specified alias
* for an inner join statement. An example of a generated
* statement is: SELECT DISTINCT t1.* FROM sometable AS t1 INNER
* JOIN othertable AS t2 ON t1.id = t2.id WHERE t2.flag = true
* without the alias parameter the statement would start with
* SELECT DISTINCT * FROM...
* @return the sql query as plain text
*/
public <K> String toSQL(boolean distinct, K k) {
return query.toSQL(distinct, k);
}
public <Z> SubQuery<T, Z> subQuery(Z x) {
return new SubQuery<T, Z>(query, x);
}
public SubQuery<T, Boolean> subQuery(boolean x) {
return subQuery(query.getPrimitiveAliasByValue(x));
}
public SubQuery<T, Byte> subQuery(byte x) {
return subQuery(query.getPrimitiveAliasByValue(x));
}
public SubQuery<T, Short> subQuery(short x) {
return subQuery(query.getPrimitiveAliasByValue(x));
}
public SubQuery<T, Integer> subQuery(int x) {
return subQuery(query.getPrimitiveAliasByValue(x));
}
public SubQuery<T, Long> subQuery(long x) {
return subQuery(query.getPrimitiveAliasByValue(x));
}
public SubQuery<T, Float> subQuery(float x) {
return subQuery(query.getPrimitiveAliasByValue(x));
}
public SubQuery<T, Double> subQuery(double x) {
return subQuery(query.getPrimitiveAliasByValue(x));
}
public <X, Z> List<X> select(Z x) {
return query.select(x);
}
public <X, Z> List<X> selectDistinct(Z x) {
return query.selectDistinct(x);
}
public <X, Z> X selectFirst(Z x) {
List<X> list = query.select(x);
return list.isEmpty() ? null : list.get(0);
}
public List<T> select() {
return query.select();
}
public T selectFirst() {
List<T> list = select();
return list.isEmpty() ? null : list.get(0);
}
public List<T> selectDistinct() {
return query.selectDistinct();
}
public void createView(Class<?> viewClass) {
query.createView(viewClass);
}
public void replaceView(Class<?> viewClass) {
query.replaceView(viewClass);
}
/**
* Order by primitive boolean field
*
* @param field
* a primitive boolean field
* @return the query
*/
public QueryWhere<T> orderBy(boolean field) {
query.getFrom().getAliasDefinition().checkMultipleBooleans();
return orderByPrimitive(field);
}
/**
* Order by primitive byte field
*
* @param field
* a primitive byte field
* @return the query
*/
public QueryWhere<T> orderBy(byte field) {
return orderByPrimitive(field);
}
/**
* Order by primitive short field
*
* @param field
* a primitive short field
* @return the query
*/
public QueryWhere<T> orderBy(short field) {
return orderByPrimitive(field);
}
public QueryWhere<T> orderBy(int field) {
return orderByPrimitive(field);
}
/**
* Order by primitive long field
*
* @param field
* a primitive long field
* @return the query
*/
public QueryWhere<T> orderBy(long field) {
return orderByPrimitive(field);
}
/**
* Order by primitive float field
*
* @param field
* a primitive float field
* @return the query
*/
public QueryWhere<T> orderBy(float field) {
return orderByPrimitive(field);
}
/**
* Order by primitive double field
*
* @param field
* a primitive double field
* @return the query
*/
public QueryWhere<T> orderBy(double field) {
return orderByPrimitive(field);
}
private QueryWhere<T> orderByPrimitive(Object field) {
query.orderByPrimitive(field);
return this;
}
public QueryWhere<T> orderBy(Object field) {
query.getFrom().getAliasDefinition().checkMultipleEnums(field);
query.orderBy(field);
return this;
}
/**
* Order by a number of Object columns.
*
* @param expressions
* the order by expressions
* @return the query
*/
public QueryWhere<T> orderBy(Object... expressions) {
query.orderBy(expressions);
return this;
}
public QueryWhere<T> orderByNullsFirst(Object expr) {
query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(query, expr, false, true, false);
query.addOrderBy(e);
return this;
}
public QueryWhere<T> orderByNullsLast(Object expr) {
query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(query, expr, false, false, true);
query.addOrderBy(e);
return this;
}
public QueryWhere<T> orderByDesc(Object expr) {
query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(query, expr, true, false, false);
query.addOrderBy(e);
return this;
}
public QueryWhere<T> orderByDescNullsFirst(Object expr) {
query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(query, expr, true, true, false);
query.addOrderBy(e);
return this;
}
public QueryWhere<T> orderByDescNullsLast(Object expr) {
query.getFrom().getAliasDefinition().checkMultipleEnums(expr);
OrderExpression<T> e = new OrderExpression<T>(query, expr, true, false, true);
query.addOrderBy(e);
return this;
}
public int delete() {
return query.delete();
}
public int update() {
return query.update();
}
public long selectCount() {
return query.selectCount();
}
}
|