FR [ 2041894 ] Add Query.match() method
* optimization: don't use ORDER BY clause for "count" and "match" records
This commit is contained in:
parent
4a2dca160e
commit
c19dc4cd23
|
@ -37,6 +37,7 @@ import org.compiere.util.DB;
|
||||||
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
|
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
|
||||||
* <li>FR [ 1981760 ] Improve Query class
|
* <li>FR [ 1981760 ] Improve Query class
|
||||||
* <li>BF [ 2030280 ] org.compiere.model.Query apply access fielter issue
|
* <li>BF [ 2030280 ] org.compiere.model.Query apply access fielter issue
|
||||||
|
* <li>FR [ 2041894 ] Add Query.match() method
|
||||||
*/
|
*/
|
||||||
public class Query {
|
public class Query {
|
||||||
|
|
||||||
|
@ -138,7 +139,7 @@ public class Query {
|
||||||
*/
|
*/
|
||||||
public <T extends PO> List<T> list() throws DBException {
|
public <T extends PO> List<T> list() throws DBException {
|
||||||
List<T> list = new ArrayList<T>();
|
List<T> list = new ArrayList<T>();
|
||||||
String sql = buildSQL(null);
|
String sql = buildSQL(null, true);
|
||||||
|
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
|
@ -170,7 +171,7 @@ public class Query {
|
||||||
*/
|
*/
|
||||||
public <T extends PO> T first() throws DBException {
|
public <T extends PO> T first() throws DBException {
|
||||||
T po = null;
|
T po = null;
|
||||||
String sql = buildSQL(null);
|
String sql = buildSQL(null, true);
|
||||||
|
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
|
@ -202,7 +203,7 @@ public class Query {
|
||||||
public int count() throws DBException
|
public int count() throws DBException
|
||||||
{
|
{
|
||||||
int count = -1;
|
int count = -1;
|
||||||
String sql = buildSQL(new StringBuffer("SELECT COUNT(*) FROM ").append(table.getTableName()));
|
String sql = buildSQL(new StringBuffer("SELECT COUNT(*) FROM ").append(table.getTableName()), false);
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
|
@ -220,6 +221,30 @@ public class Query {
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if there items for query criteria
|
||||||
|
* @return true if exists, false otherwise
|
||||||
|
* @throws DBException
|
||||||
|
*/
|
||||||
|
public boolean match() {
|
||||||
|
String sql = buildSQL(new StringBuffer("SELECT 1 FROM ").append(table.getTableName()), false);
|
||||||
|
PreparedStatement pstmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
pstmt = DB.prepareStatement(sql, this.trxName);
|
||||||
|
rs = createResultSet(pstmt);
|
||||||
|
if (rs.next())
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
|
throw new DBException(e);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
DB.close(rs, pstmt);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an Iterator implementation to fetch one PO at a time. The implementation first retrieve
|
* Return an Iterator implementation to fetch one PO at a time. The implementation first retrieve
|
||||||
* 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
|
||||||
|
@ -236,7 +261,7 @@ public class Query {
|
||||||
sqlBuffer.append(keys[i]);
|
sqlBuffer.append(keys[i]);
|
||||||
}
|
}
|
||||||
sqlBuffer.append(" FROM ").append(table.getTableName());
|
sqlBuffer.append(" FROM ").append(table.getTableName());
|
||||||
String sql = buildSQL(sqlBuffer);
|
String sql = buildSQL(sqlBuffer, true);
|
||||||
|
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
|
@ -272,7 +297,7 @@ public class Query {
|
||||||
* @throws DBException
|
* @throws DBException
|
||||||
*/
|
*/
|
||||||
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, true);
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
POResultSet<T> rsPO = null;
|
POResultSet<T> rsPO = null;
|
||||||
|
@ -304,7 +329,7 @@ public class Query {
|
||||||
* @param selectClause optional; if null the select clause will be build according to POInfo
|
* @param selectClause optional; if null the select clause will be build according to POInfo
|
||||||
* @return final SQL
|
* @return final SQL
|
||||||
*/
|
*/
|
||||||
private final String buildSQL(StringBuffer selectClause) {
|
private final String buildSQL(StringBuffer selectClause, boolean useOrderByClause) {
|
||||||
if (selectClause == null) {
|
if (selectClause == null) {
|
||||||
POInfo info = POInfo.getPOInfo(this.ctx, table.getAD_Table_ID(), trxName);
|
POInfo info = POInfo.getPOInfo(this.ctx, table.getAD_Table_ID(), trxName);
|
||||||
if (info == null)
|
if (info == null)
|
||||||
|
@ -314,8 +339,8 @@ public class Query {
|
||||||
StringBuffer sqlBuffer = new StringBuffer(selectClause);
|
StringBuffer sqlBuffer = new StringBuffer(selectClause);
|
||||||
if (whereClause != null && whereClause.trim().length() > 0)
|
if (whereClause != null && whereClause.trim().length() > 0)
|
||||||
sqlBuffer.append(" WHERE ").append(whereClause);
|
sqlBuffer.append(" WHERE ").append(whereClause);
|
||||||
if (orderBy != null && orderBy.trim().length() > 0)
|
if (useOrderByClause && 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) {
|
if (applyAccessFilter) {
|
||||||
MRole role = MRole.getDefault(this.ctx, false);
|
MRole role = MRole.getDefault(this.ctx, false);
|
||||||
|
|
Loading…
Reference in New Issue