FR [ 1984834 ] Add POResultSet.hasNext convenient method
FR [ 1985134 ] POResultSet improvements
This commit is contained in:
parent
eb83609f3a
commit
626546f3f2
|
@ -13,6 +13,7 @@
|
||||||
*
|
*
|
||||||
* Copyright (C) 2007 Low Heng Sin hengsin@avantz.com
|
* Copyright (C) 2007 Low Heng Sin hengsin@avantz.com
|
||||||
* Contributor(s):
|
* Contributor(s):
|
||||||
|
* Teo Sarca, SC ARHIPAC SERVICE SRL
|
||||||
* __________________________________________
|
* __________________________________________
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.compiere.model;
|
package org.compiere.model;
|
||||||
|
@ -21,10 +22,15 @@ import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.compiere.util.DB;
|
||||||
|
import org.compiere.util.DBException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple wrapper over jdbc resultset
|
* Simple wrapper over jdbc resultset
|
||||||
* @author Low Heng Sin
|
* @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> {
|
public class POResultSet<T extends PO> {
|
||||||
|
|
||||||
|
@ -32,9 +38,14 @@ public class POResultSet<T extends PO> {
|
||||||
private ResultSet resultSet;
|
private ResultSet resultSet;
|
||||||
private MTable table;
|
private MTable table;
|
||||||
private PreparedStatement statement;
|
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 = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Constructs the POResultSet.
|
||||||
|
* By default, closeOnError option is false. You need to set it explicitly.
|
||||||
* @param table
|
* @param table
|
||||||
* @param ps
|
* @param ps
|
||||||
* @param rs
|
* @param rs
|
||||||
|
@ -45,34 +56,78 @@ public class POResultSet<T extends PO> {
|
||||||
this.statement = ps;
|
this.statement = ps;
|
||||||
this.resultSet = rs;
|
this.resultSet = rs;
|
||||||
this.trxName = trxName;
|
this.trxName = trxName;
|
||||||
|
this.closeOnError = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return PO or null if reach the end of resultset
|
* @return PO or null if reach the end of resultset
|
||||||
* @throws SQLException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
public T next() throws SQLException {
|
public T next() throws DBException {
|
||||||
|
if (currentPO != null) {
|
||||||
|
T po = currentPO;
|
||||||
|
currentPO = null;
|
||||||
|
return po;
|
||||||
|
}
|
||||||
|
try {
|
||||||
if ( resultSet.next() ) {
|
if ( resultSet.next() ) {
|
||||||
return (T) table.getPO(resultSet, trxName);
|
return (T) table.getPO(resultSet, trxName);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
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 be 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.
|
* Release database resources.
|
||||||
*/
|
*/
|
||||||
public void close() {
|
public void close() {
|
||||||
if (statement != null) {
|
DB.close(this.resultSet, this.statement);
|
||||||
try {
|
this.resultSet = null;
|
||||||
statement.close();
|
this.statement = null;
|
||||||
} catch (SQLException e) {
|
currentPO = null;
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
resultSet.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -250,17 +250,29 @@ public class Query {
|
||||||
public <T extends PO> POResultSet<T> scroll() throws DBException {
|
public <T extends PO> POResultSet<T> scroll() throws DBException {
|
||||||
String sql = buildSQL(null);
|
String sql = buildSQL(null);
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
POResultSet<T> rsPO = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
pstmt = DB.prepareStatement (sql, trxName);
|
pstmt = DB.prepareStatement (sql, trxName);
|
||||||
ResultSet rs = createResultSet(pstmt);
|
rs = createResultSet(pstmt);
|
||||||
return new POResultSet<T>(table, pstmt, rs, trxName);
|
rsPO = new POResultSet<T>(table, pstmt, rs, trxName);
|
||||||
|
rsPO.setCloseOnError(true);
|
||||||
|
return rsPO;
|
||||||
}
|
}
|
||||||
catch (SQLException e)
|
catch (SQLException e)
|
||||||
{
|
{
|
||||||
log.log(Level.SEVERE, sql, e);
|
log.log(Level.SEVERE, sql, e);
|
||||||
throw new DBException(e);
|
throw new DBException(e);
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
// If there was an error, then close the statement and resultset
|
||||||
|
if (rsPO == null) {
|
||||||
|
DB.close(rs, pstmt);
|
||||||
|
rs = null; pstmt = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue