-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Query.java
1101 lines (1016 loc) · 30.8 KB
/
Query.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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 1999-2006 Adempiere, Inc. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA *
* 02111-1307 USA. *
* *
* Copyright (C) 2007 Low Heng Sin hengsin@avantz.com *
* Contributor(s): *
* Teo Sarca, www.arhipac.ro *
* __________________________________________ *
******************************************************************************/
package org.compiere.model;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.adempiere.exceptions.DBException;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.compiere.util.Util;
/**
* Query model
* @author Low Heng Sin
* @author Teo Sarca, www.arhipac.ro
* <li>FR [ 1981760 ] Improve Query class
* <li>BF [ 2030280 ] org.compiere.model.Query apply access filter issue
* <li>FR [ 2041894 ] Add Query.match() method
* <li>FR [ 2107068 ] Query.setOrderBy should be more error tolerant
* <li>FR [ 2107109 ] Add method Query.setOnlyActiveRecords
* <li>FR [ 2421313 ] Introduce Query.firstOnly convenient method
* <li>FR [ 2546052 ] Introduce Query aggregate methods
* <li>FR [ 2726447 ] Query aggregate methods for all return types
* <li>FR [ 2818547 ] Implement Query.setOnlySelection
* https://sourceforge.net/p/adempiere/feature-requests/759/
* <li>FR [ 2818646 ] Implement Query.firstId/firstIdOnly
* https://sourceforge.net/p/adempiere/feature-requests/760/
* @author Redhuan D. Oon
* <li>FR: [ 2214883 ] Remove SQL code and Replace for Query // introducing SQL String prompt in log.info
* <li>FR: [ 2214883 ] - to introduce .setClient_ID
*/
public class Query
{
public static final String AGGREGATE_COUNT = "COUNT";
public static final String AGGREGATE_SUM = "SUM";
public static final String AGGREGATE_AVG = "AVG";
public static final String AGGREGATE_MIN = "MIN";
public static final String AGGREGATE_MAX = "MAX";
private static CLogger log = CLogger.getCLogger (Query.class);
private Properties ctx = null;
private MTable table = null;
private String whereClause = null;
private String orderBy = null;
private String trxName = null;
private Object[] parameters = null;
/**
* Name of virtual columns to be included in the query
*/
private String[] virtualColumns = null;
private boolean applyAccessFilter = false;
private boolean applyAccessFilterRW = false;
private boolean applyAccessFilterFullyQualified = true;
private boolean onlyActiveRecords = false;
private boolean onlyClient_ID = false;
private int onlySelection_ID = -1;
private boolean forUpdate = false;
/**
* Whether to load (<code>false</code> value) all declared virtual columns at once or use
* lazy loading (<code>true</code> value).
*/
private boolean noVirtualColumn = true;
private int queryTimeout = 0;
private List<String> joinClauseList = new ArrayList<String>();
/**
* Limit current query rows return.
*/
private int pageSize;
/**
* Number of records will be skipped on query run.
*/
private int recordsToSkip;
/** list of columns to include in select statement (optional) */
private String[] selectColumns;
/**
* @param table
* @param whereClause
* @param trxName
* @deprecated Use {@link #Query(Properties, MTable, String, String)} instead because this method is security error prone
*/
public Query(MTable table, String whereClause, String trxName)
{
this.ctx = table.getCtx();
this.table = table;
this.whereClause = whereClause;
this.trxName = trxName;
}
/**
* @param ctx context
* @param table
* @param whereClause
* @param trxName
*/
public Query(Properties ctx, MTable table, String whereClause, String trxName)
{
this.ctx = ctx;
this.table = table;
this.whereClause = whereClause;
this.trxName = trxName;
}
/**
* @param ctx
* @param tableName
* @param whereClause
* @param trxName
*/
public Query(Properties ctx, String tableName, String whereClause, String trxName)
{
this(ctx, MTable.get(ctx, tableName), whereClause, trxName);
if (this.table == null)
throw new IllegalArgumentException("Table Name Not Found - "+tableName);
}
/**
* Set query parameters
* @param parameters
*/
public Query setParameters(Object ...parameters)
{
this.parameters = parameters;
return this;
}
/**
* Set query parameters
* @param parameters collection of parameters
*/
public Query setParameters(List<Object> parameters)
{
if (parameters == null) {
this.parameters = null;
return this;
}
this.parameters = new Object[parameters.size()];
parameters.toArray(this.parameters);
return this;
}
/**
* Set order by clause.<br/>
* If the string starts with "ORDER BY" then "ORDER BY" keywords will be discarded.
* @param orderBy SQL ORDER BY clause
*/
public Query setOrderBy(String orderBy)
{
this.orderBy = orderBy != null ? orderBy.trim() : null;
if (this.orderBy != null && this.orderBy.toUpperCase().startsWith("ORDER BY"))
{
this.orderBy = this.orderBy.substring(8);
}
return this;
}
/**
* Turn on/off the addition of data access filter
* @param flag
*/
public Query setApplyAccessFilter(boolean flag)
{
this.applyAccessFilter = flag;
return this;
}
/**
* Turn on data access filter with controls
* @param fullyQualified
* @param RW
*/
public Query setApplyAccessFilter(boolean fullyQualified, boolean RW)
{
this.applyAccessFilter = true;
this.applyAccessFilterFullyQualified = fullyQualified;
this.applyAccessFilterRW = RW;
return this;
}
/**
* Select only active records (i.e. IsActive='Y')
* @param onlyActiveRecords
*/
public Query setOnlyActiveRecords(boolean onlyActiveRecords)
{
this.onlyActiveRecords = onlyActiveRecords;
return this;
}
/**
* Set Client_ID true for WhereClause to auto include AD_Client_ID
*/
public Query setClient_ID()
{
return setClient_ID (true);
}
/**
* Set include or not include AD_Client_ID in where clause
* @param isIncludeClient
*/
public Query setClient_ID(boolean isIncludeClient)
{
this.onlyClient_ID = isIncludeClient;
return this;
}
/**
* Only records that are in T_Selection with AD_PInstance_ID.
* @param AD_PInstance_ID
*/
public Query setOnlySelection(int AD_PInstance_ID)
{
this.onlySelection_ID = AD_PInstance_ID;
return this;
}
/**
* Add FOR UPDATE clause
* @param forUpdate
*/
public Query setForUpdate(boolean forUpdate)
{
this.forUpdate = forUpdate;
return this;
}
/**
* Virtual columns are lazy loaded by default. In case lazy loading is not desired use this method with
* the <code>false</code> value.
* @param noVirtualColumn Whether to load (<code>false</code> value) all declared virtual columns at once or use lazy loading (<code>true</code> value).
* @see #setVirtualColumns(String...)
*/
public Query setNoVirtualColumn(boolean noVirtualColumn)
{
this.noVirtualColumn = noVirtualColumn;
return this;
}
/**
* Set query statement timeout in seconds
* @param seconds
*/
public Query setQueryTimeout(int seconds)
{
this.queryTimeout = seconds;
return this;
}
/**
* Add join clause (with JOIN keyword)
* @param joinClause
*/
public Query addJoinClause(String joinClause)
{
joinClauseList.add(joinClause);
return this;
}
/**
* Convenient method to add table direct type of joint.<br/>
* For e.g, if foreignTableName is C_BPartner and TableName for Query is AD_User,<br/>
* this will add join clause of <br/>
* "INNER JOIN C_BPartner ON (AD_User.C_BPartner_ID=C_BPartner.C_BParner_ID)".
* @param foreignTableName
*/
public void addTableDirectJoin(String foreignTableName) {
String foreignId = foreignTableName + "_ID";
addJoinClause("INNER JOIN " + foreignTableName + " ON (" + table.getTableName() + "." + foreignId
+ "=" + foreignTableName + "." + foreignId + ")");
}
/**
* Get a list of POs that match the query criteria.
* @return PO List
* @throws DBException
*/
public <T extends PO> List<T> list() throws DBException
{
List<T> list = new ArrayList<T>();
String sql = buildSQL(null, true);
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement (sql, trxName);
rs = createResultSet(pstmt);
while (rs.next ())
{
T po = getPO(rs);
list.add(po);
}
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
return list;
}
/**
* Get first PO that match query criteria
* @return first PO
* @throws DBException
*/
public <T extends PO> T first() throws DBException
{
T po = null;
int oldPageSize = this.pageSize;
if(DB.getDatabase().isPagingSupported())
setPageSize(1); // Limit to One record
String sql = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
sql = buildSQL(null, true);
pstmt = DB.prepareStatement (sql, trxName);
rs = createResultSet(pstmt);
if (rs.next ())
{
po = getPO(rs);
}
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null; pstmt = null;
setPageSize(oldPageSize);
}
return po;
}
/**
* Get partial or full PO
* @param <T>
* @param rs
* @return partial or full PO.
*/
@SuppressWarnings("unchecked")
private <T extends PO> T getPO(ResultSet rs) {
T po;
if (selectColumns != null && selectColumns.length > 0)
po = (T)table.getPartialPO(rs, selectColumns, trxName);
else
po = (T)table.getPO(rs, trxName);
return po;
}
/**
* Get first PO that match query criteria.<br/>
* If there are more PO that match query criteria, an exception will be thrown.
* @return first PO
* @throws DBException
* @see {@link #first()}
*/
public <T extends PO> T firstOnly() throws DBException
{
T po = null;
int oldPageSize = this.pageSize;
if(DB.getDatabase().isPagingSupported())
setPageSize(2); // Limit to 2 Records
String sql = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
sql = buildSQL(null, true);
pstmt = DB.prepareStatement (sql, trxName);
rs = createResultSet(pstmt);
if (rs.next())
{
po = getPO(rs);
}
if (rs.next())
{
throw new DBException(Msg.getMsg(Env.getCtx(), "QueryMoreThanOneRecordsFound"));
}
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
setPageSize(oldPageSize);
}
return po;
}
/**
* Get first matching record id
* @return first record ID or -1 if not found
* @throws DBException
*/
public int firstId() throws DBException
{
return firstId(false);
}
/**
* Get first matching record id.<br/>
* If there are more than 1 matching records, an exception is thrown.
* @return first record ID
* @throws DBException
*/
public int firstIdOnly() throws DBException
{
return firstId(true);
}
/**
* Get first matching record id.<br/>
* If there are more than 1 matching records and assumeOnlyOneResult is true, an exception is thrown.
* @param assumeOnlyOneResult
* @return first record ID or -1 if not found
* @throws DBException
*/
private int firstId(boolean assumeOnlyOneResult) throws DBException
{
String[] keys = table.getKeyColumns();
if (keys.length != 1)
{
throw new DBException("Table "+table+" has 0 or more than 1 key columns");
}
StringBuilder selectClause = new StringBuilder("SELECT ");
if (!joinClauseList.isEmpty())
selectClause.append(table.getTableName()).append(".");
selectClause.append(keys[0]);
selectClause.append(" FROM ").append(table.getTableName());
int oldPageSize = this.pageSize;
if(DB.getDatabase().isPagingSupported())
setPageSize(assumeOnlyOneResult ? 2 : 1);
String sql = null;
int id = -1;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
sql = buildSQL(selectClause, true);
pstmt = DB.prepareStatement(sql, trxName);
rs = createResultSet(pstmt);
if (rs.next())
{
id = rs.getInt(1);
}
if (assumeOnlyOneResult && rs.next())
{
throw new DBException(Msg.getMsg(Env.getCtx(), "QueryMoreThanOneRecordsFound"));
}
}
catch (SQLException e)
{
throw new DBException(e, sql);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
setPageSize(oldPageSize);
}
//
return id;
}
/**
* Get full SQL select statement
* @return SQL select statement
*
*/
public String getSQL() throws DBException
{
return buildSQL(null, true);
}
/**
* Get aggregate for given expression on this query criteria
* @param sqlExpression can be null if sqlFunction is COUNT, otherwise mandatory
* @param sqlFunction aggregate function
* @return aggregated value
* @throws DBException
*/
public BigDecimal aggregate(String sqlExpression, String sqlFunction) throws DBException
{
return aggregate(sqlExpression, sqlFunction, BigDecimal.class);
}
/**
* Get aggregate for given expression on this query criteria
* @param <T>
* @param sqlExpression can be null if sqlFunction is COUNT, otherwise mandatory
* @param sqlFunction aggregate function
* @param returnType
* @return aggregated value
* @throws DBException
*/
@SuppressWarnings("unchecked")
public <T> T aggregate(String sqlExpression, String sqlFunction, Class<T> returnType) throws DBException
{
if (Util.isEmpty(sqlFunction, true))
{
throw new DBException("No Aggregate Function defined");
}
if (Util.isEmpty(sqlExpression, true))
{
if (AGGREGATE_COUNT == sqlFunction)
{
sqlExpression = "*";
}
else
{
throw new DBException("No Expression defined");
}
}
StringBuilder sqlSelect = new StringBuilder("SELECT ").append(sqlFunction).append("(")
.append(sqlExpression).append(")")
.append(" FROM ").append(table.getTableName());
T value = null;
T defaultValue = null;
String sql = buildSQL(sqlSelect, false);
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, this.trxName);
rs = createResultSet(pstmt);
if (rs.next())
{
if (returnType.isAssignableFrom(BigDecimal.class))
{
value = (T)rs.getBigDecimal(1);
defaultValue = (T)Env.ZERO;
}
else if (returnType.isAssignableFrom(Double.class))
{
value = (T)Double.valueOf(rs.getDouble(1));
defaultValue = (T)Double.valueOf(0.00);
}
else if (returnType.isAssignableFrom(Integer.class))
{
value = (T)Integer.valueOf(rs.getInt(1));
defaultValue = (T)Integer.valueOf(0);
}
else if (returnType.isAssignableFrom(Timestamp.class))
{
value = (T)rs.getTimestamp(1);
}
else if (returnType.isAssignableFrom(Boolean.class))
{
value = (T) Boolean.valueOf("Y".equals(rs.getString(1)));
defaultValue = (T) Boolean.FALSE;
}
else
{
value = (T)rs.getObject(1);
}
}
if (rs.next())
{
throw new DBException(Msg.getMsg(Env.getCtx(), "QueryMoreThanOneRecordsFound"));
}
}
catch (SQLException e)
{
throw new DBException(e, sql);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
//
if (value == null)
{
value = defaultValue;
}
return value;
}
/**
* Count records that match query criteria
* @return count
* @throws DBException
*/
public int count() throws DBException
{
return aggregate("*", AGGREGATE_COUNT).intValue();
}
/**
* SUM sqlExpression for records that match query criteria
* @param sqlExpression
* @return sum
*/
public BigDecimal sum(String sqlExpression)
{
return aggregate(sqlExpression, AGGREGATE_SUM);
}
/**
* Check if there are any matching records for this query criteria
* @return true if exists, false otherwise
* @throws DBException
*/
public boolean match() throws DBException
{
String sql = buildSQL(new StringBuilder("SELECT 1 FROM ").append(table.getTableName()), false);
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, this.trxName);
rs = createResultSet(pstmt);
if (rs.next())
return true;
}
catch (SQLException e) {
throw new DBException(e, sql);
}
finally {
DB.close(rs, pstmt);
}
return false;
}
/**
* Get an Stream implementation to fetch one PO at a time. This method will only create POs on-demand and
* they will become eligible for garbage collection once they have been consumed by the stream, so unlike
* {@link #list()} it doesn't have to hold a copy of all the POs in the result set in memory at one time.
* <p>
* And unlike {#link #iterate()}, it only creates one ResultSet and iterates over it, creating a PO for each
* row. ({@link #iterate()}, on the other hand, has to re-run the query for each element).<br/>
* <p>
* For situations where you need to iterate over a result set and operate on the results one-at-a-time rather
* than operate on the group as a whole, this method is likely to give better performance than <code>list()</code>
* or <code>iterate()</code>.
* <p>
* <strong>However</strong>, because it keeps the underlying {@code ResultSet} open, you need to make sure that the
* stream is properly disposed of using {@code close()} or else you will get resource leaks. As {@link Stream}
* extends {@link AutoCloseable}, you can use it in a try-with-resources statement to automatically close it when
* you are done.
*
* @return Stream of POs.
* @throws DBException
*/
public <T extends PO> Stream<T> stream() throws DBException
{
String sql = buildSQL(null, true);
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement (sql, trxName);
final PreparedStatement finalPstmt = pstmt;
rs = createResultSet(pstmt);
final ResultSet finalRS = rs;
return StreamSupport.stream(new Spliterators.AbstractSpliterator<T>(
Long.MAX_VALUE,Spliterator.ORDERED) {
@Override
public boolean tryAdvance(Consumer<? super T> action) {
try {
if(!finalRS.next()) return false;
final T newRec = getPO(finalRS);
action.accept(newRec);
return true;
} catch(SQLException ex) {
log.log(Level.SEVERE, sql, ex);
throw new DBException(ex, sql);
}
}
}, false).onClose(() -> DB.close(finalRS, finalPstmt));
}
catch (SQLException e)
{
DB.close(rs, pstmt);
log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
}
}
/**
* Get an Iterator implementation to fetch one PO at a time.<br/>
* The implementation first retrieve
* all IDS that match the query criteria and issue SQL query to fetch each PO when caller want to
* fetch the next PO.<br/>
* This minimize memory usage (at both application and DB server end) but it is slower than the list, stream and scroll method.
* @return Iterator
* @throws DBException
*/
public <T extends PO> Iterator<T> iterate() throws DBException
{
String[] keys = table.getKeyColumns();
StringBuilder sqlBuffer = new StringBuilder(" SELECT ");
for (int i = 0; i < keys.length; i++) {
if (i > 0)
sqlBuffer.append(", ");
if (!joinClauseList.isEmpty())
sqlBuffer.append(table.getTableName()).append(".");
sqlBuffer.append(keys[i]);
}
sqlBuffer.append(" FROM ").append(table.getTableName());
String sql = buildSQL(sqlBuffer, true);
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Object[]> idList = new ArrayList<Object[]>();
try
{
pstmt = DB.prepareStatement (sql, trxName);
rs = createResultSet(pstmt);
while (rs.next ())
{
Object[] ids = new Object[keys.length];
for (int i = 0; i < ids.length; i++) {
ids[i] = rs.getObject(i+1);
}
idList.add(ids);
}
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
return new POIterator<T>(table, idList, trxName);
}
/**
* Get a simple wrapper over a jdbc resultset.<br/>
* It is the caller responsibility to call the close method to release the underlying database resources.<br/>
* Since POResultSet implements the AutoCloseable interface, it is recommended to use it in a try-with-resources
* statement to automatically close it when you are done.
* @return POResultSet
* @throws DBException
*/
public <T extends PO> POResultSet<T> scroll() throws DBException
{
String sql = buildSQL(null, true);
PreparedStatement pstmt = null;
ResultSet rs = null;
POResultSet<T> rsPO = null;
try
{
pstmt = DB.prepareStatement (sql, trxName);
rs = createResultSet(pstmt);
rsPO = new POResultSet<T>(table, pstmt, rs, trxName);
rsPO.setCloseOnError(true);
if (selectColumns != null && selectColumns.length > 0)
{
rsPO.setSelectColumns(selectColumns);
}
return rsPO;
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
}
finally
{
// If there was an error, then close the statement and resultset
if (rsPO == null) {
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
}
}
/**
* Build SQL SELECT statement.
* @param selectClause optional; if null the select statement will be built by {@link POInfo}
* @param useOrderByClause
* @return final SQL
*/
private final String buildSQL(StringBuilder selectClause, boolean useOrderByClause)
{
if (selectClause == null)
{
POInfo info = POInfo.getPOInfo(this.ctx, table.getAD_Table_ID(), trxName);
if (info == null)
{
throw new IllegalStateException("No POInfo found for AD_Table_ID="+table.getAD_Table_ID());
}
boolean isFullyQualified = !joinClauseList.isEmpty();
if (selectColumns != null && selectColumns.length > 0)
{
selectClause = info.buildSelectForColumns(isFullyQualified, selectColumns);
}
else
{
if(virtualColumns == null)
selectClause = info.buildSelect(isFullyQualified, noVirtualColumn);
else
selectClause = info.buildSelect(isFullyQualified, virtualColumns);
}
}
if (!joinClauseList.isEmpty())
{
for(String joinClause : joinClauseList)
{
selectClause.append(" ").append(joinClause);
}
}
StringBuilder whereBuffer = new StringBuilder();
if (!Util.isEmpty(this.whereClause, true))
{
if (whereBuffer.length() > 0)
whereBuffer.append(" AND ");
whereBuffer.append("(").append(this.whereClause).append(")");
}
if (this.onlyActiveRecords)
{
if (whereBuffer.length() > 0)
whereBuffer.append(" AND ");
if (!joinClauseList.isEmpty())
whereBuffer.append(table.getTableName()).append(".");
whereBuffer.append("IsActive=?");
}
if (this.onlyClient_ID) //red1
{
if (whereBuffer.length() > 0)
whereBuffer.append(" AND ");
if (!joinClauseList.isEmpty())
whereBuffer.append(table.getTableName()).append(".");
whereBuffer.append("AD_Client_ID=?");
}
if (this.onlySelection_ID > 0)
{
String[] keys = table.getKeyColumns();
if (keys.length != 1)
{
throw new DBException("Table "+table+" has 0 or more than 1 key columns");
}
//
if (whereBuffer.length() > 0)
whereBuffer.append(" AND ");
whereBuffer.append(" EXISTS (SELECT 1 FROM T_Selection s WHERE s.AD_PInstance_ID=? AND s.");
if (table.isUUIDKeyTable())
whereBuffer.append("T_Selection_UU=");
else
whereBuffer.append("T_Selection_ID=");
whereBuffer.append(table.getTableName()).append(".").append(keys[0]).append(")");
}
StringBuilder sqlBuffer = new StringBuilder(selectClause);
if (whereBuffer.length() > 0)
{
sqlBuffer.append(" WHERE ").append(whereBuffer);
}
if (useOrderByClause && !Util.isEmpty(orderBy, true))
{
sqlBuffer.append(" ORDER BY ").append(orderBy);
}
String sql = sqlBuffer.toString();
if (applyAccessFilter)
{
MRole role = MRole.getDefault(this.ctx, false);
sql = role.addAccessSQL(sql, table.getTableName(), applyAccessFilterFullyQualified, applyAccessFilterRW);
}
if (forUpdate) {
sql = sql + " FOR UPDATE";
if (DB.isPostgreSQL())
sql = sql + " OF " + table.getTableName();
}
// If have pagination
if (pageSize > 0 || recordsToSkip > 0) {
sql = appendPagination(sql);
}
if (log.isLoggable(Level.FINEST))
log.finest("TableName = " + table.getTableName() + "... SQL = " + sql); // red1 - to assist in debugging SQL
return sql;
}
/**
* Set the pagination of the query.
*
* @param pPageSize Limit number of query rows to return.
*/
public Query setPageSize(int pPageSize) {
this.pageSize = pPageSize;
return this;
}
/**
* Set the pagination of the query.
*
* @param pPageSize Limit number of rows to return.
* @param pPagesToSkip Number of pages to skipped on query run. ZERO for first page.
*/
public Query setPage(int pPageSize, int pPagesToSkip) {
if (pPageSize > 0) {
this.pageSize = pPageSize;
this.recordsToSkip = pPagesToSkip * pageSize;
} else {
log.warning("Wrong PageSize <= 0");
}
return this;
}
/**
* Set the number of records to skip (a.k.a. OFFSET)
*
* @param pRecordsToSkip Number of records to skip
*/
public Query setRecordstoSkip(int pRecordsToSkip) {
this.recordsToSkip = pRecordsToSkip;
return this;
}
/**
* Append pagination clause to pQuery
* @param pQuery SQL query statement
*/
private String appendPagination(String pQuery) {
String query = pQuery;
if (pageSize > 0 || recordsToSkip > 0) {
if (DB.getDatabase().isPagingSupported()) {
query = DB.getDatabase().addPagingSQL(query, recordsToSkip+1, pageSize <= 0 ? 0 : recordsToSkip + pageSize);
} else {
throw new IllegalArgumentException("Pagination not supported by database");
}
}