From 65eaf44dfa50b4471cc60262d98d591d83ad4d7b Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Mon, 2 Jun 2008 07:27:35 +0000 Subject: [PATCH] FR [ 1981760 ] Improve Query class --- base/src/org/compiere/model/POIterator.java | 10 ++--- base/src/org/compiere/model/POResultSet.java | 6 +-- base/src/org/compiere/model/Query.java | 39 +++++++++++++++----- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/base/src/org/compiere/model/POIterator.java b/base/src/org/compiere/model/POIterator.java index 26eed6c70c..a117097488 100644 --- a/base/src/org/compiere/model/POIterator.java +++ b/base/src/org/compiere/model/POIterator.java @@ -28,7 +28,7 @@ import org.compiere.util.Env; * @author Low Heng Sin * */ -public class POIterator implements Iterator { +public class POIterator implements Iterator { private MTable table; private List idList; @@ -59,7 +59,7 @@ public class POIterator implements Iterator { /** * @see java.util.Iterator#next() */ - public PO next() { + public T next() { if ( iteratorIndex < (idList.size() - 1)) { iteratorIndex ++; return get(iteratorIndex); @@ -86,11 +86,11 @@ public class POIterator implements Iterator { * @param index * @return PO or null if index is invalid */ - public PO get(int index) { + public T get(int index) { if (index <= (idList.size() - 1)) { Object[] ids = idList.get(index); if (ids.length == 1 && ids[0] instanceof Integer) { - return table.getPO((Integer)ids[0], trxName); + return (T) table.getPO((Integer)ids[0], trxName); } else { if (keyWhereClause == null) { String[] keys = table.getKeyColumns(); @@ -105,7 +105,7 @@ public class POIterator implements Iterator { } keyWhereClause = sqlBuffer.toString(); } - return table.getPO(keyWhereClause, ids, trxName); + return (T) table.getPO(keyWhereClause, ids, trxName); } } else { return null; diff --git a/base/src/org/compiere/model/POResultSet.java b/base/src/org/compiere/model/POResultSet.java index 9cea15b059..0fabe909cc 100644 --- a/base/src/org/compiere/model/POResultSet.java +++ b/base/src/org/compiere/model/POResultSet.java @@ -26,7 +26,7 @@ import java.sql.SQLException; * @author Low Heng Sin * */ -public class POResultSet { +public class POResultSet { private String trxName; private ResultSet resultSet; @@ -52,9 +52,9 @@ public class POResultSet { * @return PO or null if reach the end of resultset * @throws SQLException */ - public PO next() throws SQLException { + public T next() throws SQLException { if ( resultSet.next() ) { - return table.getPO(resultSet, trxName); + return (T) table.getPO(resultSet, trxName); } else { return null; } diff --git a/base/src/org/compiere/model/Query.java b/base/src/org/compiere/model/Query.java index 601eb0b730..20c2fa7461 100644 --- a/base/src/org/compiere/model/Query.java +++ b/base/src/org/compiere/model/Query.java @@ -21,6 +21,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Iterator; import java.util.logging.Level; @@ -33,7 +34,8 @@ import org.compiere.util.Env; /** * * @author Low Heng Sin - * + * @author Teo Sarca, SC ARHIPAC SERVICE SRL + *
  • FR [ 1981760 ] Improve Query class */ public class Query { @@ -62,24 +64,41 @@ public class Query { * Set query parameters * @param parameters */ - public void setParameters(Object[] parameters) { + public Query setParameters(Object[] parameters) { this.parameters = parameters; + return this; + } + + /** + * Set query parameters + * @param parameters collection of parameters + */ + public Query setParameters(Collection 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 ). * @param orderBy */ - public void setOrderBy(String orderBy) { + public Query setOrderBy(String orderBy) { this.orderBy = orderBy; + return this; } /** * Turn on/off the addition of data access filter * @param flag */ - public void setApplyAccessFilter(boolean flag) { + public Query setApplyAccessFilter(boolean flag) { applyAccessFilter = flag; + return this; } /** @@ -140,7 +159,7 @@ public class Query { * @return Iterator * @throws SQLException */ - public Iterator iterate() throws SQLException { + public Iterator iterate() throws DBException { String[] keys = table.getKeyColumns(); StringBuffer sqlBuffer = new StringBuffer(" SELECT "); for (int i = 0; i < keys.length; i++) { @@ -184,12 +203,12 @@ public class Query { catch (SQLException e) { log.log(Level.SEVERE, sql, e); - throw e; + throw new DBException(e); } finally { DB.close(rs, pstmt); rs = null; pstmt = null; } - return new POIterator(table, idList, trxName); + return new POIterator(table, idList, trxName); } /** @@ -198,7 +217,7 @@ public class Query { * @return POResultSet * @throws SQLException */ - public POResultSet scroll() throws SQLException { + public POResultSet scroll() throws DBException { POInfo info = POInfo.getPOInfo(Env.getCtx(), table.getAD_Table_ID(), trxName); if (info == null) return null; StringBuffer sqlBuffer = info.buildSelect(); @@ -224,12 +243,12 @@ public class Query { } } ResultSet rs = pstmt.executeQuery (); - return new POResultSet(table, pstmt, rs, trxName); + return new POResultSet(table, pstmt, rs, trxName); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); - throw e; + throw new DBException(e); } } }