Package org.compiere.model
Class Query
- java.lang.Object
-
- org.compiere.model.Query
-
public class Query extends Object
- Author:
- Low Heng Sin, Teo Sarca, www.arhipac.ro
- FR [ 1981760 ] Improve Query class
- BF [ 2030280 ] org.compiere.model.Query apply access filter issue
- FR [ 2041894 ] Add Query.match() method
- FR [ 2107068 ] Query.setOrderBy should be more error tolerant
- FR [ 2107109 ] Add method Query.setOnlyActiveRecords
- FR [ 2421313 ] Introduce Query.firstOnly convenient method
- FR [ 2546052 ] Introduce Query aggregate methods
- FR [ 2726447 ] Query aggregate methods for all return types
- FR [ 2818547 ] Implement Query.setOnlySelection https://sourceforge.net/p/adempiere/feature-requests/759/
- FR [ 2818646 ] Implement Query.firstId/firstIdOnly https://sourceforge.net/p/adempiere/feature-requests/760/, Redhuan D. Oon
- FR: [ 2214883 ] Remove SQL code and Replace for Query // introducing SQL String prompt in log.info
- FR: [ 2214883 ] - to introduce .setClient_ID
-
-
Field Summary
Fields Modifier and Type Field Description static String
AGGREGATE_AVG
static String
AGGREGATE_COUNT
static String
AGGREGATE_MAX
static String
AGGREGATE_MIN
static String
AGGREGATE_SUM
-
Constructor Summary
Constructors Constructor Description Query(Properties ctx, String tableName, String whereClause, String trxName)
Query(Properties ctx, MTable table, String whereClause, String trxName)
Query(MTable table, String whereClause, String trxName)
Deprecated.UseQuery(Properties, MTable, String, String)
instead because this method is security error prone
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Query
addJoinClause(String joinClause)
BigDecimal
aggregate(String sqlExpression, String sqlFunction)
Aggregate given expression on this criteria<T> T
aggregate(String sqlExpression, String sqlFunction, Class<T> returnType)
Aggregate given expression on this criteriaint
count()
Count items that match query criteria<T extends PO>
Tfirst()
Return first PO that match query criteriaint
firstId()
Return first IDint
firstIdOnly()
Return first ID.<T extends PO>
TfirstOnly()
Return first PO that match query criteria.int[]
getIDs()
Get a Array with the IDs for this QueryString
getSQL()
red1 - returns full SQL string - for caller needs<T extends PO>
Iterator<T>iterate()
Return an Iterator implementation to fetch one PO at a time.<T extends PO>
List<T>list()
Return a list of all po that match the query criteria.boolean
match()
Check if there items for query criteria<T extends PO>
POResultSet<T>scroll()
Return a simple wrapper over a jdbc resultset.Query
setApplyAccessFilter(boolean flag)
Turn on/off the addition of data access filterQuery
setApplyAccessFilter(boolean fullyQualified, boolean RW)
Turn on data access filter with controlsQuery
setClient_ID()
Set Client_ID true for WhereClause routine to include AD_Client_IDQuery
setClient_ID(boolean isIncludeClient)
Set include or not include AD_Client_ID in where clauseQuery
setForUpdate(boolean forUpdate)
Add FOR UPDATE clauseQuery
setNoVirtualColumn(boolean noVirtualColumn)
Query
setOnlyActiveRecords(boolean onlyActiveRecords)
Select only active records (i.e.Query
setOnlySelection(int AD_PInstance_ID)
Only records that are in T_Selection with AD_PInstance_ID.Query
setOrderBy(String orderBy)
Set order by clause.Query
setPage(int pPageSize, int pPagesToSkip)
Set the pagination of the query.Query
setPageSize(int pPageSize)
Set the pagination of the query.Query
setParameters(Object... parameters)
Set query parametersQuery
setParameters(List<Object> parameters)
Set query parametersQuery
setQueryTimeout(int seconds)
Query
setRecordstoSkip(int pRecordsToSkip)
Set the number of records to skip (a.k.a.BigDecimal
sum(String sqlExpression)
SUM sqlExpression for items that match query criteria
-
-
-
Field Detail
-
AGGREGATE_COUNT
public static final String AGGREGATE_COUNT
- See Also:
- Constant Field Values
-
AGGREGATE_SUM
public static final String AGGREGATE_SUM
- See Also:
- Constant Field Values
-
AGGREGATE_AVG
public static final String AGGREGATE_AVG
- See Also:
- Constant Field Values
-
AGGREGATE_MIN
public static final String AGGREGATE_MIN
- See Also:
- Constant Field Values
-
AGGREGATE_MAX
public static final String AGGREGATE_MAX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Query
public Query(MTable table, String whereClause, String trxName)
Deprecated.UseQuery(Properties, MTable, String, String)
instead because this method is security error prone- Parameters:
table
-whereClause
-trxName
-
-
Query
public Query(Properties ctx, MTable table, String whereClause, String trxName)
- Parameters:
ctx
- contexttable
-whereClause
-trxName
-
-
Query
public Query(Properties ctx, String tableName, String whereClause, String trxName)
- Parameters:
ctx
-tableName
-whereClause
-trxName
-
-
-
Method Detail
-
setParameters
public Query setParameters(Object... parameters)
Set query parameters- Parameters:
parameters
-
-
setParameters
public Query setParameters(List<Object> parameters)
Set query parameters- Parameters:
parameters
- collection of parameters
-
setOrderBy
public Query setOrderBy(String orderBy)
Set order by clause. If the string starts with "ORDER BY" then "ORDER BY" keywords will be discarded.- Parameters:
orderBy
- SQL ORDER BY clause
-
setApplyAccessFilter
public Query setApplyAccessFilter(boolean flag)
Turn on/off the addition of data access filter- Parameters:
flag
-
-
setApplyAccessFilter
public Query setApplyAccessFilter(boolean fullyQualified, boolean RW)
Turn on data access filter with controls- Parameters:
fullyQualified
-RW
-- Returns:
-
setOnlyActiveRecords
public Query setOnlyActiveRecords(boolean onlyActiveRecords)
Select only active records (i.e. IsActive='Y')- Parameters:
onlyActiveRecords
-
-
setClient_ID
public Query setClient_ID()
Set Client_ID true for WhereClause routine to include AD_Client_ID
-
setClient_ID
public Query setClient_ID(boolean isIncludeClient)
Set include or not include AD_Client_ID in where clause
-
setOnlySelection
public Query setOnlySelection(int AD_PInstance_ID)
Only records that are in T_Selection with AD_PInstance_ID.- Parameters:
AD_PInstance_ID
-
-
setForUpdate
public Query setForUpdate(boolean forUpdate)
Add FOR UPDATE clause- Parameters:
forUpdate
-
-
setNoVirtualColumn
public Query setNoVirtualColumn(boolean noVirtualColumn)
-
setQueryTimeout
public Query setQueryTimeout(int seconds)
-
list
public <T extends PO> List<T> list() throws DBException
Return a list of all po that match the query criteria.- Returns:
- List
- Throws:
DBException
-
first
public <T extends PO> T first() throws DBException
Return first PO that match query criteria- Returns:
- first PO
- Throws:
DBException
-
firstOnly
public <T extends PO> T firstOnly() throws DBException
Return first PO that match query criteria. If there are more records that match criteria an exception will be throwed- Returns:
- first PO
- Throws:
DBException
-
firstId
public int firstId() throws DBException
Return first ID- Returns:
- first ID or -1 if not found
- Throws:
DBException
-
firstIdOnly
public int firstIdOnly() throws DBException
Return first ID. If there are more results and exception is thrown.- Returns:
- first ID or -1 if not found
- Throws:
DBException
-
getSQL
public String getSQL() throws DBException
red1 - returns full SQL string - for caller needs- Returns:
- buildSQL(null,true)
- Throws:
DBException
-
aggregate
public BigDecimal aggregate(String sqlExpression, String sqlFunction) throws DBException
Aggregate given expression on this criteria- Parameters:
sqlExpression
-sqlFunction
-- Returns:
- aggregated value
- Throws:
DBException
-
aggregate
public <T> T aggregate(String sqlExpression, String sqlFunction, Class<T> returnType) throws DBException
Aggregate given expression on this criteria- Type Parameters:
T
-- Parameters:
sqlExpression
-sqlFunction
-returnType
-- Returns:
- aggregated value
- Throws:
DBException
-
count
public int count() throws DBException
Count items that match query criteria- Returns:
- count
- Throws:
DBException
-
sum
public BigDecimal sum(String sqlExpression)
SUM sqlExpression for items that match query criteria- Parameters:
sqlExpression
-- Returns:
- sum
-
match
public boolean match() throws DBException
Check if there items for query criteria- Returns:
- true if exists, false otherwise
- Throws:
DBException
-
iterate
public <T extends PO> Iterator<T> iterate() throws DBException
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 fetch the next PO. This minimize memory usage but it is slower than the list method.- Returns:
- Iterator
- Throws:
DBException
-
scroll
public <T extends PO> POResultSet<T> scroll() throws DBException
Return a simple wrapper over a jdbc resultset. It is the caller responsibility to call the close method to release the underlying database resources.- Returns:
- POResultSet
- Throws:
DBException
-
setPageSize
public Query setPageSize(int pPageSize)
Set the pagination of the query.- Parameters:
pPageSize
- Limit current query rows return.- Returns:
- current Query
-
setPage
public Query setPage(int pPageSize, int pPagesToSkip)
Set the pagination of the query.- Parameters:
pPageSize
- Limit current query rows return.pPagesToSkip
- Number of pages will be skipped on query run. ZERO for first page- Returns:
- current Query
-
setRecordstoSkip
public Query setRecordstoSkip(int pRecordsToSkip)
Set the number of records to skip (a.k.a. OFFSET)- Parameters:
pRecordsToSkip
- Limit current query rows return.- Returns:
- current Query
-
getIDs
public int[] getIDs()
Get a Array with the IDs for this Query- Returns:
- Get a Array with the IDs
-
-