-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
POResultSet.java
147 lines (136 loc) · 4.83 KB
/
POResultSet.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
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 1999-2006 ComPiere, 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. *
* For the text or an alternative of this public license, you may reach us *
* ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
* or via info@compiere.org or http://www.compiere.org/license.html *
* Copyright (C) 2007 Low Heng Sin hengsin@avantz.com *
* Contributor(s): *
* Teo Sarca, SC ARHIPAC SERVICE SRL *
* __________________________________________ *
*****************************************************************************/
package org.compiere.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.adempiere.exceptions.DBException;
import org.compiere.util.DB;
/**
* Simple wrapper over JDBC result set
* @author Low Heng Sin
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
* <li>FR [ 1984834 ] Add POResultSet.hasNext convenient method
* <li>FR [ 1985134 ] POResultSet improvements
*/
public class POResultSet<T extends PO> implements AutoCloseable {
private String trxName;
private ResultSet resultSet;
private MTable table;
private PreparedStatement statement;
/** Current fetched PO */
private T currentPO = null;
/** Should we close the statement and resultSet on any exception that occur ? */
private boolean closeOnError = true;
private String[] selectColumns;
/**
* Constructs the POResultSet.<br/>
* By default, closeOnError option is false. You need to set it explicitly.
* @param table
* @param ps
* @param rs
* @param trxName
*/
public POResultSet(MTable table, PreparedStatement ps, ResultSet rs, String trxName) {
this.table = table;
this.statement = ps;
this.resultSet = rs;
this.trxName = trxName;
this.closeOnError = false;
}
/**
* Is result set has next record
* @return true if it has next, false otherwise
* @throws DBException
*/
public boolean hasNext() throws DBException {
if (currentPO != null)
return true;
currentPO = next();
return currentPO != null;
}
/**
* Get next record
* @return PO or null if reach the end of result set
* @throws DBException
*/
@SuppressWarnings("unchecked")
public T next() throws DBException {
if (currentPO != null) {
T po = currentPO;
currentPO = null;
return po;
}
try {
if ( resultSet.next() ) {
return (T) (selectColumns != null && selectColumns.length > 0 ? table.getPartialPO(resultSet, selectColumns, trxName) : table.getPO(resultSet, trxName));
} else {
this.close(); // close it if there is no more data to read
return null;
}
}
catch (SQLException e) {
if (this.closeOnError) {
this.close();
}
throw new DBException(e);
}
// Catching any RuntimeException, and close the resultset (if closeOnError is set)
catch (RuntimeException e) {
if (this.closeOnError) {
this.close();
}
throw e;
}
}
/**
* Should we automatically close the {@link PreparedStatement} and {@link ResultSet} in case
* we get an error.
* @param closeOnError
*/
public void setCloseOnError(boolean closeOnError) {
this.closeOnError = closeOnError;
}
/**
* Will the {@link PreparedStatement} and {@link ResultSet} closed on any database exception
* @return true if yes, false otherwise
*/
public boolean isCloseOnError() {
return this.closeOnError;
}
/**
* Release database resources.
*/
@Override
public void close() {
DB.close(this.resultSet, this.statement);
this.resultSet = null;
this.statement = null;
currentPO = null;
}
/**
* Set columns for result set. Use for loading of partial PO.
* @param selectColumns
*/
public void setSelectColumns(String[] selectColumns) {
this.selectColumns = selectColumns;
}
}