FR [ 1981760 ] Improve Query class

This commit is contained in:
teo_sarca 2008-06-02 07:27:35 +00:00
parent 2e6a2306ca
commit 65eaf44dfa
3 changed files with 37 additions and 18 deletions

View File

@ -28,7 +28,7 @@ import org.compiere.util.Env;
* @author Low Heng Sin * @author Low Heng Sin
* *
*/ */
public class POIterator implements Iterator<PO> { public class POIterator<T extends PO> implements Iterator<T> {
private MTable table; private MTable table;
private List<Object[]> idList; private List<Object[]> idList;
@ -59,7 +59,7 @@ public class POIterator implements Iterator<PO> {
/** /**
* @see java.util.Iterator#next() * @see java.util.Iterator#next()
*/ */
public PO next() { public T next() {
if ( iteratorIndex < (idList.size() - 1)) { if ( iteratorIndex < (idList.size() - 1)) {
iteratorIndex ++; iteratorIndex ++;
return get(iteratorIndex); return get(iteratorIndex);
@ -86,11 +86,11 @@ public class POIterator implements Iterator<PO> {
* @param index * @param index
* @return PO or null if index is invalid * @return PO or null if index is invalid
*/ */
public PO get(int index) { public T get(int index) {
if (index <= (idList.size() - 1)) { if (index <= (idList.size() - 1)) {
Object[] ids = idList.get(index); Object[] ids = idList.get(index);
if (ids.length == 1 && ids[0] instanceof Integer) { if (ids.length == 1 && ids[0] instanceof Integer) {
return table.getPO((Integer)ids[0], trxName); return (T) table.getPO((Integer)ids[0], trxName);
} else { } else {
if (keyWhereClause == null) { if (keyWhereClause == null) {
String[] keys = table.getKeyColumns(); String[] keys = table.getKeyColumns();
@ -105,7 +105,7 @@ public class POIterator implements Iterator<PO> {
} }
keyWhereClause = sqlBuffer.toString(); keyWhereClause = sqlBuffer.toString();
} }
return table.getPO(keyWhereClause, ids, trxName); return (T) table.getPO(keyWhereClause, ids, trxName);
} }
} else { } else {
return null; return null;

View File

@ -26,7 +26,7 @@ import java.sql.SQLException;
* @author Low Heng Sin * @author Low Heng Sin
* *
*/ */
public class POResultSet { public class POResultSet<T extends PO> {
private String trxName; private String trxName;
private ResultSet resultSet; private ResultSet resultSet;
@ -52,9 +52,9 @@ public class POResultSet {
* @return PO or null if reach the end of resultset * @return PO or null if reach the end of resultset
* @throws SQLException * @throws SQLException
*/ */
public PO next() throws SQLException { public T next() throws SQLException {
if ( resultSet.next() ) { if ( resultSet.next() ) {
return table.getPO(resultSet, trxName); return (T) table.getPO(resultSet, trxName);
} else { } else {
return null; return null;
} }

View File

@ -21,6 +21,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Iterator; import java.util.Iterator;
import java.util.logging.Level; import java.util.logging.Level;
@ -33,7 +34,8 @@ import org.compiere.util.Env;
/** /**
* *
* @author Low Heng Sin * @author Low Heng Sin
* * @author Teo Sarca, SC ARHIPAC SERVICE SRL
* <li>FR [ 1981760 ] Improve Query class
*/ */
public class Query { public class Query {
@ -62,24 +64,41 @@ public class Query {
* Set query parameters * Set query parameters
* @param parameters * @param parameters
*/ */
public void setParameters(Object[] parameters) { public Query setParameters(Object[] parameters) {
this.parameters = parameters; this.parameters = parameters;
return this;
}
/**
* Set query parameters
* @param parameters collection of parameters
*/
public Query setParameters(Collection<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 ( without the order by sql keyword ). * Set order by clause ( without the order by sql keyword ).
* @param orderBy * @param orderBy
*/ */
public void setOrderBy(String orderBy) { public Query setOrderBy(String orderBy) {
this.orderBy = orderBy; this.orderBy = orderBy;
return this;
} }
/** /**
* Turn on/off the addition of data access filter * Turn on/off the addition of data access filter
* @param flag * @param flag
*/ */
public void setApplyAccessFilter(boolean flag) { public Query setApplyAccessFilter(boolean flag) {
applyAccessFilter = flag; applyAccessFilter = flag;
return this;
} }
/** /**
@ -140,7 +159,7 @@ public class Query {
* @return Iterator * @return Iterator
* @throws SQLException * @throws SQLException
*/ */
public Iterator iterate() throws SQLException { public <T extends PO> Iterator<T> iterate() throws DBException {
String[] keys = table.getKeyColumns(); String[] keys = table.getKeyColumns();
StringBuffer sqlBuffer = new StringBuffer(" SELECT "); StringBuffer sqlBuffer = new StringBuffer(" SELECT ");
for (int i = 0; i < keys.length; i++) { for (int i = 0; i < keys.length; i++) {
@ -184,12 +203,12 @@ public class Query {
catch (SQLException e) catch (SQLException e)
{ {
log.log(Level.SEVERE, sql, e); log.log(Level.SEVERE, sql, e);
throw e; throw new DBException(e);
} finally { } finally {
DB.close(rs, pstmt); DB.close(rs, pstmt);
rs = null; pstmt = null; rs = null; pstmt = null;
} }
return new POIterator(table, idList, trxName); return new POIterator<T>(table, idList, trxName);
} }
/** /**
@ -198,7 +217,7 @@ public class Query {
* @return POResultSet * @return POResultSet
* @throws SQLException * @throws SQLException
*/ */
public POResultSet scroll() throws SQLException { public <T extends PO> POResultSet<T> scroll() throws DBException {
POInfo info = POInfo.getPOInfo(Env.getCtx(), table.getAD_Table_ID(), trxName); POInfo info = POInfo.getPOInfo(Env.getCtx(), table.getAD_Table_ID(), trxName);
if (info == null) return null; if (info == null) return null;
StringBuffer sqlBuffer = info.buildSelect(); StringBuffer sqlBuffer = info.buildSelect();
@ -224,12 +243,12 @@ public class Query {
} }
} }
ResultSet rs = pstmt.executeQuery (); ResultSet rs = pstmt.executeQuery ();
return new POResultSet(table, pstmt, rs, trxName); return new POResultSet<T>(table, pstmt, rs, trxName);
} }
catch (SQLException e) catch (SQLException e)
{ {
log.log(Level.SEVERE, sql, e); log.log(Level.SEVERE, sql, e);
throw e; throw new DBException(e);
} }
} }
} }