/
AbstractSQLQuery.java
802 lines (701 loc) · 24.7 KB
/
AbstractSQLQuery.java
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
* Copyright 2011, Mysema Ltd
*
* 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.mysema.query.sql;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableList;
import com.mysema.commons.lang.CloseableIterator;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinFlag;
import com.mysema.query.Query;
import com.mysema.query.QueryException;
import com.mysema.query.QueryFlag;
import com.mysema.query.QueryFlag.Position;
import com.mysema.query.QueryMetadata;
import com.mysema.query.QueryModifiers;
import com.mysema.query.SearchResults;
import com.mysema.query.Tuple;
import com.mysema.query.support.Expressions;
import com.mysema.query.support.ProjectableQuery;
import com.mysema.query.support.QueryMixin;
import com.mysema.query.types.Expression;
import com.mysema.query.types.ExpressionUtils;
import com.mysema.query.types.FactoryExpression;
import com.mysema.query.types.OperationImpl;
import com.mysema.query.types.ParamExpression;
import com.mysema.query.types.ParamNotSetException;
import com.mysema.query.types.Path;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.QTuple;
import com.mysema.query.types.SubQueryExpression;
import com.mysema.query.types.query.ListSubQuery;
import com.mysema.query.types.template.NumberTemplate;
import com.mysema.query.types.template.SimpleTemplate;
import com.mysema.util.ResultSetAdapter;
/**
*
/**
* AbstractSQLQuery is the base type for SQL query implementations
*
* @author tiwe
*
* @param <Q> concrete subtype
*/
public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q> & Query<Q>> extends
ProjectableQuery<Q> implements SQLCommonQuery<Q> {
private static final Logger logger = LoggerFactory.getLogger(AbstractSQLQuery.class);
@Nullable
private final Connection conn;
@Nullable
private List<Object> constants;
@Nullable
private List<Path<?>> constantPaths;
@Nullable
protected Expression<?> union;
private final Configuration configuration;
private final SQLListeners listeners;
protected final QueryMixin<Q> queryMixin;
protected boolean unionAll;
public AbstractSQLQuery(@Nullable Connection conn, Configuration configuration) {
this(conn, configuration, new DefaultQueryMetadata().noValidate());
}
@SuppressWarnings("unchecked")
public AbstractSQLQuery(@Nullable Connection conn, Configuration configuration, QueryMetadata metadata) {
super(new QueryMixin<Q>(metadata, false));
this.queryMixin = super.queryMixin;
this.queryMixin.setSelf((Q) this);
this.conn = conn;
this.configuration = configuration;
this.listeners = new SQLListeners(configuration.getListeners());
}
/**
* @param listener
*/
public void addListener(SQLListener listener) {
listeners.add(listener);
}
/**
* Add the given String literal as a join flag to the last added join with the position
* BEFORE_TARGET
*
* @param flag
* @return
*/
@Override
public Q addJoinFlag(String flag) {
return addJoinFlag(flag, JoinFlag.Position.BEFORE_TARGET);
}
/**
* Add the given String literal as a join flag to the last added join
*
* @param flag
* @param position
* @return
*/
@Override
@SuppressWarnings("unchecked")
public Q addJoinFlag(String flag, JoinFlag.Position position) {
queryMixin.addJoinFlag(new JoinFlag(flag, position));
return (Q)this;
}
/**
* Add the given prefix and expression as a general query flag
*
* @param position position of the flag
* @param prefix prefix for the flag
* @param expr expression of the flag
* @return
*/
@Override
public Q addFlag(Position position, String prefix, Expression<?> expr) {
Expression<?> flag = SimpleTemplate.create(expr.getType(), prefix + "{0}", expr);
return queryMixin.addFlag(new QueryFlag(position, flag));
}
/**
* Add the given query flag
*
* @param flag
* @return
*/
public Q addFlag(QueryFlag flag) {
return queryMixin.addFlag(flag);
}
/**
* Add the given String literal as query flag
*
* @param position
* @param flag
* @return
*/
@Override
public Q addFlag(Position position, String flag) {
return queryMixin.addFlag(new QueryFlag(position, flag));
}
/**
* Add the given Expression as a query flag
*
* @param position
* @param flag
* @return
*/
@Override
public Q addFlag(Position position, Expression<?> flag) {
return queryMixin.addFlag(new QueryFlag(position, flag));
}
protected String buildQueryString(boolean forCountRow) {
SQLSerializer serializer = createSerializer();
if (union != null) {
serializer.serializeUnion(union, queryMixin.getMetadata(), unionAll);
} else {
serializer.serialize(queryMixin.getMetadata(), forCountRow);
}
constants = serializer.getConstants();
constantPaths = serializer.getConstantPaths();
return serializer.toString();
}
@Override
public long count() {
try {
return unsafeCount();
} catch (SQLException e) {
String error = "Caught " + e.getClass().getName();
logger.error(error, e);
throw new QueryException(e.getMessage(), e);
}
}
@Override
public boolean exists() {
return limit(1).singleResult(NumberTemplate.ONE) != null;
}
/**
* If you use forUpdate() with a backend that uses page or row locks, rows examined by the
* query are write-locked until the end of the current transaction.
*
* Not supported for SQLite and CUBRID
*
* @return
*/
public Q forUpdate() {
return addFlag(SQLTemplates.FOR_UPDATE_FLAG);
}
protected SQLSerializer createSerializer() {
return new SQLSerializer(configuration);
}
public Q from(Expression<?> arg) {
return queryMixin.from(arg);
}
@Override
public Q from(Expression<?>... args) {
return queryMixin.from(args);
}
@Override
@SuppressWarnings("unchecked")
public Q from(SubQueryExpression<?> subQuery, Path<?> alias) {
return queryMixin.from(ExpressionUtils.as((Expression)subQuery, alias));
}
@Override
public Q fullJoin(RelationalPath<?> target) {
return queryMixin.fullJoin(target);
}
@Override
public <E> Q fullJoin(RelationalFunctionCall<E> target, Path<E> alias) {
return queryMixin.fullJoin(target, alias);
}
@Override
public Q fullJoin(SubQueryExpression<?> target, Path<?> alias) {
return queryMixin.fullJoin(target, alias);
}
@Override
public <E> Q fullJoin(ForeignKey<E> key, RelationalPath<E> entity) {
return queryMixin.fullJoin(entity).on(key.on(entity));
}
@Override
public Q innerJoin(RelationalPath<?> target) {
return queryMixin.innerJoin(target);
}
@Override
public <E> Q innerJoin(RelationalFunctionCall<E> target, Path<E> alias) {
return queryMixin.innerJoin(target, alias);
}
@Override
public Q innerJoin(SubQueryExpression<?> target, Path<?> alias) {
return queryMixin.innerJoin(target, alias);
}
@Override
public <E> Q innerJoin(ForeignKey<E> key, RelationalPath<E> entity) {
return queryMixin.innerJoin(entity).on(key.on(entity));
}
@Override
public Q join(RelationalPath<?> target) {
return queryMixin.join(target);
}
@Override
public <E> Q join(RelationalFunctionCall<E> target, Path<E> alias) {
return queryMixin.join(target, alias);
}
@Override
public Q join(SubQueryExpression<?> target, Path<?> alias) {
return queryMixin.join(target, alias);
}
@Override
public <E> Q join(ForeignKey<E> key, RelationalPath<E> entity) {
return queryMixin.join(entity).on(key.on(entity));
}
@Override
public Q leftJoin(RelationalPath<?> target) {
return queryMixin.leftJoin(target);
}
@Override
public <E> Q leftJoin(RelationalFunctionCall<E> target, Path<E> alias) {
return queryMixin.leftJoin(target, alias);
}
@Override
public Q leftJoin(SubQueryExpression<?> target, Path<?> alias) {
return queryMixin.leftJoin(target, alias);
}
@Override
public <E> Q leftJoin(ForeignKey<E> key, RelationalPath<E> entity) {
return queryMixin.leftJoin(entity).on(key.on(entity));
}
@Override
public Q rightJoin(RelationalPath<?> target) {
return queryMixin.rightJoin(target);
}
@Override
public <E> Q rightJoin(RelationalFunctionCall<E> target, Path<E> alias) {
return queryMixin.rightJoin(target, alias);
}
@Override
public Q rightJoin(SubQueryExpression<?> target, Path<?> alias) {
return queryMixin.rightJoin(target, alias);
}
@Override
public <E> Q rightJoin(ForeignKey<E> key, RelationalPath<E> entity) {
return queryMixin.rightJoin(entity).on(key.on(entity));
}
@Nullable
private <T> T get(ResultSet rs, Expression<?> expr, int i, Class<T> type) throws SQLException {
return configuration.get(rs, expr instanceof Path ? (Path)expr : null, i, type);
}
private void set(PreparedStatement stmt, Path<?> path, int i, Object value) throws SQLException{
configuration.set(stmt, path, i, value);
}
public QueryMetadata getMetadata() {
return queryMixin.getMetadata();
}
/**
* Get the query as an SQL query string and bindings
*
* @param exprs
* @return
*/
public SQLBindings getSQL(Expression<?>... exprs) {
queryMixin.addProjection(exprs);
String queryString = buildQueryString(false);
ImmutableList.Builder<Object> args = ImmutableList.builder();
Map<ParamExpression<?>, Object> params = getMetadata().getParams();
for (Object o : constants) {
if (o instanceof ParamExpression) {
if (!params.containsKey(o)) {
throw new ParamNotSetException((ParamExpression<?>) o);
}
}
args.add(o);
}
return new SQLBindings(queryString, args.build());
}
/**
* Get the results as an JDBC result set
*
* @param args
* @return
*/
public ResultSet getResults(Expression<?>... exprs) {
queryMixin.addProjection(exprs);
String queryString = buildQueryString(false);
if (logger.isDebugEnabled()) {
logger.debug("query : {}", queryString);
}
listeners.notifyQuery(queryMixin.getMetadata());
try {
final PreparedStatement stmt = conn.prepareStatement(queryString);
setParameters(stmt, constants, constantPaths, getMetadata().getParams());
final ResultSet rs = stmt.executeQuery();
return new ResultSetAdapter(rs) {
@Override
public void close() throws SQLException {
try {
super.close();
} finally {
stmt.close();
}
}
};
} catch (SQLException e) {
throw new QueryException(e);
} finally {
reset();
}
}
private <RT> Union<RT> innerUnion(SubQueryExpression<?>... sq) {
queryMixin.getMetadata().setValidate(false);
if (!queryMixin.getMetadata().getJoins().isEmpty()) {
throw new IllegalArgumentException("Don't mix union and from");
}
this.union = UnionUtils.union(sq, unionAll);
return new UnionImpl<Q ,RT>((Q)this, sq[0].getMetadata().getProjection());
}
protected Configuration getConfiguration() {
return configuration;
}
@Override
public CloseableIterator<Tuple> iterate(Expression<?>... args) {
return iterate(new QTuple(args));
}
@Override
public <RT> CloseableIterator<RT> iterate(Expression<RT> expr) {
expr = queryMixin.addProjection(expr);
return iterateSingle(queryMixin.getMetadata(), expr);
}
@SuppressWarnings("unchecked")
private <RT> CloseableIterator<RT> iterateSingle(QueryMetadata metadata, @Nullable final Expression<RT> expr) {
final String queryString = buildQueryString(false);
if (logger.isDebugEnabled()) {
logger.debug("query : {}", queryString);
}
listeners.notifyQuery(queryMixin.getMetadata());
try {
final PreparedStatement stmt = conn.prepareStatement(queryString);
setParameters(stmt, constants, constantPaths, metadata.getParams());
final ResultSet rs = stmt.executeQuery();
if (expr == null) {
return new SQLResultIterator<RT>(stmt, rs) {
@Override
public RT produceNext(ResultSet rs) throws Exception {
return (RT) rs.getObject(1);
}
};
} else if (expr instanceof FactoryExpression) {
return new SQLResultIterator<RT>(stmt, rs) {
@Override
public RT produceNext(ResultSet rs) throws Exception {
return newInstance((FactoryExpression<RT>) expr, rs, 0);
}
};
} else if (expr.getType().isArray()) {
return new SQLResultIterator<RT>(stmt, rs) {
@Override
public RT produceNext(ResultSet rs) throws Exception {
Object[] rv = new Object[rs.getMetaData().getColumnCount()];
for (int i = 0; i < rv.length; i++) {
rv[i] = rs.getObject(i+1);
}
return (RT) rv;
}
};
} else {
return new SQLResultIterator<RT>(stmt, rs) {
@Override
public RT produceNext(ResultSet rs) throws Exception {
return get(rs, expr, 1, expr.getType());
}
};
}
} catch (SQLException e) {
throw new QueryException("Caught " + e.getClass().getSimpleName() + " for " + queryString, e);
} finally {
reset();
}
}
@Override
public List<Tuple> list(Expression<?>... args) {
return list(new QTuple(args));
}
@Override
public <RT> List<RT> list(Expression<RT> expr) {
expr = queryMixin.addProjection(expr);
final String queryString = buildQueryString(false);
if (logger.isDebugEnabled()) {
logger.debug("query : {}", queryString);
}
listeners.notifyQuery(queryMixin.getMetadata());
try {
final PreparedStatement stmt = conn.prepareStatement(queryString);
try {
setParameters(stmt, constants, constantPaths, queryMixin.getMetadata().getParams());
final ResultSet rs = stmt.executeQuery();
try {
final List<RT> rv = new ArrayList<RT>();
if (expr instanceof FactoryExpression) {
FactoryExpression<RT> fe = (FactoryExpression<RT>)expr;
while (rs.next()) {
rv.add(newInstance(fe, rs, 0));
}
} else if (expr.getType().isArray()) {
while (rs.next()) {
Object[] row = new Object[rs.getMetaData().getColumnCount()];
for (int i = 0; i < row.length; i++) {
row[i] = rs.getObject(i+1);
}
rv.add((RT)row);
}
} else {
while (rs.next()) {
rv.add(get(rs, expr, 1, expr.getType()));
}
}
return rv;
} catch (IllegalAccessException e) {
throw new QueryException(e);
} catch (InvocationTargetException e) {
throw new QueryException(e);
} catch (InstantiationException e) {
throw new QueryException(e);
} catch (SQLException e) {
throw new QueryException(e);
} finally {
rs.close();
}
} finally {
stmt.close();
}
} catch (SQLException e) {
throw new QueryException("Caught " + e.getClass().getSimpleName() + " for " + queryString, e);
} finally {
reset();
}
}
@Override
public SearchResults<Tuple> listResults(Expression<?>... args) {
return listResults(new QTuple(args));
}
@Override
public <RT> SearchResults<RT> listResults(Expression<RT> expr) {
queryMixin.addProjection(expr);
long total = count();
try {
if (total > 0) {
queryMixin.getMetadata().clearProjection();
QueryModifiers modifiers = queryMixin.getMetadata().getModifiers();
return new SearchResults<RT>(list(expr), modifiers, total);
} else {
return SearchResults.emptyResults();
}
} finally {
reset();
}
}
private <RT> RT newInstance(FactoryExpression<RT> c, ResultSet rs, int offset)
throws InstantiationException, IllegalAccessException, InvocationTargetException, SQLException{
Object[] args = new Object[c.getArgs().size()];
for (int i = 0; i < args.length; i++) {
args[i] = get(rs, c.getArgs().get(i), offset + i + 1, c.getArgs().get(i).getType());
}
return c.newInstance(args);
}
public Q on(Predicate condition) {
return queryMixin.on(condition);
}
@Override
public Q on(Predicate... conditions) {
return queryMixin.on(conditions);
}
private void reset() {
queryMixin.getMetadata().reset();
constants = null;
}
protected void setParameters(PreparedStatement stmt, List<?> objects, List<Path<?>> constantPaths,
Map<ParamExpression<?>, ?> params) {
if (objects.size() != constantPaths.size()) {
throw new IllegalArgumentException("Expected " + objects.size() +
" paths, but got " + constantPaths.size());
}
for (int i = 0; i < objects.size(); i++) {
Object o = objects.get(i);
try {
if (o instanceof ParamExpression) {
if (!params.containsKey(o)) {
throw new ParamNotSetException((ParamExpression<?>) o);
}
o = params.get(o);
}
set(stmt, constantPaths.get(i), i+1, o);
} catch (SQLException e) {
throw new IllegalArgumentException(e);
}
}
}
@Override
public String toString() {
return buildQueryString(false).trim();
}
/**
* Creates an union expression for the given subqueries
*
* @param <RT>
* @param sq
* @return
*/
public <RT> Union<RT> union(ListSubQuery<RT>... sq) {
return innerUnion(sq);
}
/**
* Creates an union expression for the given subqueries
*
* @param <RT>
* @param sq
* @return
*/
public <RT> Q union(Path<?> alias, ListSubQuery<RT>... sq) {
return from(UnionUtils.union(sq, alias, false));
}
/**
* Creates an union expression for the given subqueries
*
* @param <RT>
* @param sq
* @return
*/
public <RT> Union<RT> union(SubQueryExpression<RT>... sq) {
return innerUnion(sq);
}
/**
* Creates an union expression for the given subqueries
*
* @param <RT>
* @param sq
* @return
*/
public <RT> Q union(Path<?> alias, SubQueryExpression<RT>... sq) {
return from(UnionUtils.union(sq, alias, false));
}
/**
* Creates an union expression for the given subqueries
*
* @param <RT>
* @param sq
* @return
*/
public <RT> Union<RT> unionAll(ListSubQuery<RT>... sq) {
unionAll = true;
return innerUnion(sq);
}
/**
* Creates an union expression for the given subqueries
*
* @param <RT>
* @param sq
* @return
*/
public <RT> Q unionAll(Path<?> alias, ListSubQuery<RT>... sq) {
return from(UnionUtils.union(sq, alias, true));
}
/**
* Creates an union expression for the given subqueries
*
* @param <RT>
* @param sq
* @return
*/
public <RT> Union<RT> unionAll(SubQueryExpression<RT>... sq) {
unionAll = true;
return innerUnion(sq);
}
/**
* Creates an union expression for the given subqueries
*
* @param <RT>
* @param sq
* @return
*/
public <RT> Q unionAll(Path<?> alias, SubQueryExpression<RT>... sq) {
return from(UnionUtils.union(sq, alias, true));
}
@Override
public Tuple uniqueResult(Expression<?>... expr) {
return uniqueResult(new QTuple(expr));
}
@Override
public <RT> RT uniqueResult(Expression<RT> expr) {
if (getMetadata().getModifiers().getLimit() == null
&& !expr.toString().contains("count(")) {
limit(2);
}
CloseableIterator<RT> iterator = iterate(expr);
return uniqueResult(iterator);
}
@Override
public Q withRecursive(Path<?> alias, SubQueryExpression<?> query) {
queryMixin.addFlag(new QueryFlag(QueryFlag.Position.WITH, SQLTemplates.RECURSIVE));
return with(alias, query);
}
public Q withRecursive(Path<?> alias, Expression<?> query) {
queryMixin.addFlag(new QueryFlag(QueryFlag.Position.WITH, SQLTemplates.RECURSIVE));
return with(alias, query);
}
public WithBuilder<Q> withRecursive(Path<?> alias, Path<?>... columns) {
queryMixin.addFlag(new QueryFlag(QueryFlag.Position.WITH, SQLTemplates.RECURSIVE));
return with(alias, columns);
}
@Override
public Q with(Path<?> alias, SubQueryExpression<?> query) {
Expression<?> expr = OperationImpl.create(alias.getType(), SQLTemplates.WITH_ALIAS, alias, query);
return queryMixin.addFlag(new QueryFlag(QueryFlag.Position.WITH, expr));
}
public Q with(Path<?> alias, Expression<?> query) {
Expression<?> expr = OperationImpl.create(alias.getType(), SQLTemplates.WITH_ALIAS, alias, query);
return queryMixin.addFlag(new QueryFlag(QueryFlag.Position.WITH, expr));
}
public WithBuilder<Q> with(Path<?> alias, Path<?>... columns) {
Expression<?> columnsCombined = ExpressionUtils.list(Object.class, columns);
Expression<?> aliasCombined = Expressions.operation(alias.getType(), SQLTemplates.WITH_COLUMNS, alias, columnsCombined);
return new WithBuilder<Q>(queryMixin, aliasCombined);
}
private long unsafeCount() throws SQLException {
final String queryString = buildQueryString(true);
if (logger.isDebugEnabled()) {
logger.debug("query : {}", queryString);
}
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = conn.prepareStatement(queryString);
setParameters(stmt, constants, constantPaths, getMetadata().getParams());
rs = stmt.executeQuery();
rs.next();
return rs.getLong(1);
} catch (SQLException e) {
throw new QueryException(e.getMessage(), e);
} finally {
try {
if (rs != null) {
rs.close();
}
} finally {
if (stmt != null) {
stmt.close();
}
}
}
}
}