[ 1777787 ] Add more query method to MTable

- Implemented Trifon's suggestion.
This commit is contained in:
Heng Sin Low 2007-08-23 07:44:14 +00:00
parent 95cb260ac1
commit 9a86f89870
1 changed files with 47 additions and 29 deletions

View File

@ -19,6 +19,7 @@ package org.compiere.model;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Iterator; import java.util.Iterator;
@ -42,6 +43,7 @@ public class Query {
private String orderBy = null; private String orderBy = null;
private String trxName = null; private String trxName = null;
private Object[] parameters = null; private Object[] parameters = null;
private boolean applyAccessFilter = false;
/** /**
* *
@ -71,11 +73,20 @@ public class Query {
this.orderBy = orderBy; this.orderBy = orderBy;
} }
/**
* Turn on/off the addition of data access filter
* @param flag
*/
public void setApplyAccessFilter(boolean flag) {
applyAccessFilter = flag;
}
/** /**
* Return a list of all po that match the query criteria. * Return a list of all po that match the query criteria.
* @return List * @return List
* @throws SQLException
*/ */
public List<PO> list() { public List<PO> list() throws SQLException {
List<PO> list = new ArrayList<PO>(); List<PO> list = new ArrayList<PO>();
POInfo info = POInfo.getPOInfo(Env.getCtx(), table.getAD_Table_ID()); POInfo info = POInfo.getPOInfo(Env.getCtx(), table.getAD_Table_ID());
@ -86,6 +97,10 @@ public class Query {
if (orderBy != null && orderBy.trim().length() > 0) if (orderBy != null && orderBy.trim().length() > 0)
sqlBuffer.append(" Order By ").append(orderBy); sqlBuffer.append(" Order By ").append(orderBy);
String sql = sqlBuffer.toString(); String sql = sqlBuffer.toString();
if (applyAccessFilter) {
MRole role = MRole.getDefault();
sql = role.addAccessSQL(sql, table.get_TableName(), true, false);
}
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try try
@ -108,19 +123,16 @@ public class Query {
pstmt.close (); pstmt.close ();
pstmt = null; pstmt = null;
} }
catch (Exception e) catch (SQLException e)
{ {
log.log(Level.SEVERE, sql, e); log.log(Level.SEVERE, sql, e);
log.saveError("Error", e); throw e;
} } finally {
try try {
{ if (pstmt != null)
if (pstmt != null) pstmt.close ();
pstmt.close (); }
pstmt = null; catch (Exception e){}
}
catch (Exception e)
{
pstmt = null; pstmt = null;
} }
return list; return list;
@ -131,8 +143,9 @@ public class Query {
* all IDS that match the query criteria and issue sql query to fetch the PO when caller want to * all IDS that match the query criteria and issue sql query to fetch the PO when caller want to
* fetch the next PO. This minimize memory usage but it is slower than the list method. * fetch the next PO. This minimize memory usage but it is slower than the list method.
* @return Iterator * @return Iterator
* @throws SQLException
*/ */
public Iterator iterate() { public Iterator iterate() throws SQLException {
String[] keys = table.get_KeyColumns(); String[] keys = table.get_KeyColumns();
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++) {
@ -145,7 +158,11 @@ public class Query {
sqlBuffer.append(" WHERE ").append(whereClause); sqlBuffer.append(" WHERE ").append(whereClause);
if (orderBy != null && orderBy.trim().length() > 0) if (orderBy != null && orderBy.trim().length() > 0)
sqlBuffer.append(" Order By ").append(orderBy); sqlBuffer.append(" Order By ").append(orderBy);
String sql = sqlBuffer.toString(); String sql = sqlBuffer.toString();
if (applyAccessFilter) {
MRole role = MRole.getDefault();
sql = role.addAccessSQL(sql, table.get_TableName(), true, false);
}
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
List<Object[]> idList = new ArrayList<Object[]>(); List<Object[]> idList = new ArrayList<Object[]>();
try try
@ -171,19 +188,16 @@ public class Query {
pstmt.close (); pstmt.close ();
pstmt = null; pstmt = null;
} }
catch (Exception e) catch (SQLException e)
{ {
log.log(Level.SEVERE, sql, e); log.log(Level.SEVERE, sql, e);
log.saveError("Error", e); throw e;
} } finally {
try try {
{ if (pstmt != null)
if (pstmt != null) pstmt.close ();
pstmt.close (); }
pstmt = null; catch (Exception e) {}
}
catch (Exception e)
{
pstmt = null; pstmt = null;
} }
return new POIterator(table, idList, trxName); return new POIterator(table, idList, trxName);
@ -193,8 +207,9 @@ public class Query {
* Return a simple wrapper over a jdbc resultset. It is the caller responsibility to * Return a simple wrapper over a jdbc resultset. It is the caller responsibility to
* call the close method to release the underlying database resources. * call the close method to release the underlying database resources.
* @return POResultSet * @return POResultSet
* @throws SQLException
*/ */
public POResultSet scroll() { public POResultSet scroll() throws SQLException {
POInfo info = POInfo.getPOInfo(Env.getCtx(), table.getAD_Table_ID()); POInfo info = POInfo.getPOInfo(Env.getCtx(), table.getAD_Table_ID());
if (info == null) return null; if (info == null) return null;
StringBuffer sqlBuffer = info.buildSelect(); StringBuffer sqlBuffer = info.buildSelect();
@ -203,6 +218,10 @@ public class Query {
if (orderBy != null && orderBy.trim().length() > 0) if (orderBy != null && orderBy.trim().length() > 0)
sqlBuffer.append(" Order By ").append(orderBy); sqlBuffer.append(" Order By ").append(orderBy);
String sql = sqlBuffer.toString(); String sql = sqlBuffer.toString();
if (applyAccessFilter) {
MRole role = MRole.getDefault();
sql = role.addAccessSQL(sql, table.get_TableName(), true, false);
}
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try try
@ -218,11 +237,10 @@ public class Query {
ResultSet rs = pstmt.executeQuery (); ResultSet rs = pstmt.executeQuery ();
return new POResultSet(table, pstmt, rs, trxName); return new POResultSet(table, pstmt, rs, trxName);
} }
catch (Exception e) catch (SQLException e)
{ {
log.log(Level.SEVERE, sql, e); log.log(Level.SEVERE, sql, e);
log.saveError("Error", e); throw e;
} }
return null;
} }
} }