ch.elca.el4j.services.persistence.jpa.criteria
Class QueryBuilder

java.lang.Object
  extended by ch.elca.el4j.services.persistence.jpa.criteria.QueryBuilder
All Implemented Interfaces:
CountQuery, Expression, SelectQuery

public final class QueryBuilder
extends Object
implements Expression, SelectQuery, CountQuery

SQL Query Builder. TODO: adapt the following example such that it produces valid JPQL. (no ON clause, but AS instead)

Example query definition:

      QueryBuilder builder = QueryBuilder.select("dep.NAME", "emp.name");
      QueryBuilder subQueryBuilder = QueryBuilder.select("1");
      subQueryBuilder.from("Dual ").startAnd().ifNotNull("1 <> {p}", 2).end().endBuilder();

      builder.from("DEPARTMENT dep")
          .innerJoin("EMPLOYEE emp", "dep.ID = emp.DEPARTMENT_ID")
          .innerJoin("UNIVERSITY unv", "unv.ID = dep.UNV_ID")
      .startOr()
          .ifNotNull("emp.position = {p}", "pos")
          .startAnd()
              .ifNotNull("emp.SALARY > {p}", 200)
              .ifNotNull("emp.SALARY < {p}", 300)
          .end()
          .exist(subQueryBuilder)
          .ifNotNull("dep.ID in {p}", depIds)
      .end()
      .orderBy(SortOrder.ASC, "dep.ID").endBuilder();
 
The above query is equivalent to the following SQL:
     SELECT dep.NAME, emp.NAME
     FROM DEPARTMENT dep
         JOIN emp ON (dep.ID = emp.DEPARTMENT_ID)
         JOIN UNIVERSITY unv ON (unv.ID = dep.UNV_ID)
     WHERE emp.POSITION = {p}
         OR (emp.SALARY > {p} AND emp.SALARY < {p})" 
         OR EXISTS (SELECT 1 FROM DUAL WHERE 1 <> {p})
     ORDER BY DEP.ID ASC";
 

Author:
Simon Stelling (SST), Huy Hung Nguyen (HUN)
File-location:
QueryBuilder
Last check-in date:
2010-12-21 11:08:04 +0100 (Di, 21. Dez 2010) by swismer for revision 4253

Nested Class Summary
 class QueryBuilder.ConditionList<T extends Expression>
          Condition of QueryBuilder.
 
Method Summary
 QueryBuilder addSelectParameter(Object... parameters)
          TODO: what's this?
 void append(String query)
          TODO: this seems wrong. what's this for?
 CountQuery applyCount(javax.persistence.EntityManager entityManager)
          creates count query with the given EntityManager and stores the prepared query which is then executed through the getCount() method.
 SelectQuery applySelect(javax.persistence.EntityManager entityManager)
          creates select query with the given EntityManager and stores the prepared query which is then executed through the getResultList() methods.
 SelectQuery applySelect(javax.persistence.EntityManager entityManager, Class<?> resultClass)
          creates select query with the given EntityManager and stores the prepared query which is then executed through the getResultList() methods.
 QueryBuilder clearOrderBy()
          drops all ordering constraints.
 QueryBuilder endBuilder()
          Mark this query as completed.
 QueryBuilder from(Class<?>... entityClasses)
          Specifies on which classes the query is based.
 QueryBuilder from(String... from)
          Specifies on which tables the query is based.
 QueryBuilder fromIf(String fromExp, boolean cond)
          same as from(String...) except that from clause is added iff condition evaluates to true.
 List<Object> getBodyParameters()
          Get all attached parameters.
 int getCount()
          
 List<?> getRawResultList()
           
<T> List<T>
getResultList(Class<T> clazz)
          returns all entities matching the query.
<T> List<T>
getResultList(Class<T> clazz, int firstRow, int maxNumOfRows)
          gets at most maxNumOfRows results, first one being firstRow.
 List<?> getResultList(int firstRow, int maxNumOfRows)
          returns the results in the given range.
 List<Object> getSelectParameters()
          get SelectParameters.
<T> T
getSingleResult(Class<T> clazz)
           
 QueryBuilder innerJoin(String join, String on)
          Only used for native sql.
 QueryBuilder innerJoin(String join, String on, Object... params)
          Only used for native sql.
 QueryBuilder join(String join)
          Dont need 'on', used for JPQL.
 QueryBuilder joinIf(String join, boolean cond)
          Dont need 'on', used for JPQL.
 QueryBuilder joinIf(String join, String on, boolean cond, Object... params)
          like joinIf(String, String, boolean, Object...) but join is performed iff cond evaluates to true.
 QueryBuilder leftJoin(String join, String on)
           
 QueryBuilder leftJoin(String join, String on, Object... params)
          Only used for native sql.
 QueryBuilder orderBy(SortOrder sortOrder, String orderBy)
          appends the given column to the ordering restrictions iff not already ordered by the given column.
 QueryBuilder orderByIf(SortOrder sortOrder, String orderBy, boolean cond)
          Appends given column to the ordering restrictions iff cond evaluates to true.
 QueryBuilder rightJoin(String join, String on)
           
 QueryBuilder rightJoin(String join, String on, Object... params)
          Only used for native sql.
static QueryBuilder select(String... select)
          Creates a new Query with the given SELECT clause.
 void setParameters(List<Object> parameters)
          Set parameters of the query.
 QueryBuilder.ConditionList<QueryBuilder> startAnd()
          marks the beginning of a list of predicates to be ANDed.
 QueryBuilder.ConditionList<QueryBuilder> startOr()
          marks the beginning of a list of predicates to be ORed.
 String toString()
           
 QueryBuilder union(QueryBuilder builder)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getSelectParameters

public List<Object> getSelectParameters()
get SelectParameters.

Returns:
the selectParameters

select

public static QueryBuilder select(String... select)
Creates a new Query with the given SELECT clause.

Parameters:
select - select clause
Returns:
a new query

addSelectParameter

public QueryBuilder addSelectParameter(Object... parameters)
TODO: what's this?


from

public QueryBuilder from(Class<?>... entityClasses)
Specifies on which classes the query is based.

Parameters:
entityClasses - entities' classes
Returns:
this

from

public QueryBuilder from(String... from)
Specifies on which tables the query is based.

Parameters:
from - tables
Returns:
this

fromIf

public QueryBuilder fromIf(String fromExp,
                           boolean cond)
same as from(String...) except that from clause is added iff condition evaluates to true.

Parameters:
fromExp - from string to add
cond - predicate
Returns:
this

innerJoin

public QueryBuilder innerJoin(String join,
                              String on,
                              Object... params)
Only used for native sql.

Parameters:
join -
on -
params -
Returns:

innerJoin

public QueryBuilder innerJoin(String join,
                              String on)
Only used for native sql.

Parameters:
join -
on -
params -
Returns:

leftJoin

public QueryBuilder leftJoin(String join,
                             String on,
                             Object... params)
Only used for native sql.

Parameters:
join -
on -
params -
Returns:

leftJoin

public QueryBuilder leftJoin(String join,
                             String on)

union

public QueryBuilder union(QueryBuilder builder)

rightJoin

public QueryBuilder rightJoin(String join,
                              String on,
                              Object... params)
Only used for native sql.

Parameters:
join -
on -
params -
Returns:

rightJoin

public QueryBuilder rightJoin(String join,
                              String on)

joinIf

public QueryBuilder joinIf(String join,
                           String on,
                           boolean cond,
                           Object... params)
like joinIf(String, String, boolean, Object...) but join is performed iff cond evaluates to true.

Parameters:
join - join
on - on clause
cond - predicate
params - params
Returns:
this

join

public QueryBuilder join(String join)
Dont need 'on', used for JPQL.

Parameters:
join -
cond -
Returns:

joinIf

public QueryBuilder joinIf(String join,
                           boolean cond)
Dont need 'on', used for JPQL.

Parameters:
join -
cond -
Returns:

startAnd

public QueryBuilder.ConditionList<QueryBuilder> startAnd()
marks the beginning of a list of predicates to be ANDed.

Returns:
condition object

startOr

public QueryBuilder.ConditionList<QueryBuilder> startOr()
marks the beginning of a list of predicates to be ORed.

Returns:
condition object

endBuilder

public QueryBuilder endBuilder()
Mark this query as completed. TODO: what is the role of completed vs. non-completed queries?

Returns:
this

orderBy

public QueryBuilder orderBy(SortOrder sortOrder,
                            String orderBy)
appends the given column to the ordering restrictions iff not already ordered by the given column.

Parameters:
sortOrder - ASC or DESC
orderBy - the column to order by
Returns:
this

orderByIf

public QueryBuilder orderByIf(SortOrder sortOrder,
                              String orderBy,
                              boolean cond)
Appends given column to the ordering restrictions iff cond evaluates to true.

Parameters:
sortOrder - ASC or DESC
orderBy - column
cond - predicate
Returns:
this

applySelect

public SelectQuery applySelect(javax.persistence.EntityManager entityManager)
creates select query with the given EntityManager and stores the prepared query which is then executed through the getResultList() methods.

Parameters:
entityManager - em to use
Returns:
this

applySelect

public SelectQuery applySelect(javax.persistence.EntityManager entityManager,
                               Class<?> resultClass)
creates select query with the given EntityManager and stores the prepared query which is then executed through the getResultList() methods.

Parameters:
entityManager - em to use
resultClass - class of result
Returns:
this

applyCount

public CountQuery applyCount(javax.persistence.EntityManager entityManager)
creates count query with the given EntityManager and stores the prepared query which is then executed through the getCount() method.

Parameters:
entityManager - em to use
Returns:
this

setParameters

public void setParameters(List<Object> parameters)
Set parameters of the query.

Parameters:
parameters - the parameters to set

getResultList

public <T> List<T> getResultList(Class<T> clazz,
                                 int firstRow,
                                 int maxNumOfRows)
gets at most maxNumOfRows results, first one being firstRow.

Specified by:
getResultList in interface SelectQuery
Type Parameters:
T - entity type
Parameters:
clazz - entity class
firstRow - first row to receive
maxNumOfRows - max number of rows to receive
Returns:
retrieved entities

getResultList

public List<?> getResultList(int firstRow,
                             int maxNumOfRows)
Description copied from interface: SelectQuery
returns the results in the given range.

Specified by:
getResultList in interface SelectQuery
Parameters:
firstRow - first row
maxNumOfRows - maxNumOfRows
Returns:
list of results.
See Also:
getResultList(Class, int, int).

getResultList

public <T> List<T> getResultList(Class<T> clazz)
returns all entities matching the query.

Specified by:
getResultList in interface SelectQuery
Type Parameters:
T - entity type
Parameters:
clazz - entity class
Returns:
results

getRawResultList

public List<?> getRawResultList()
Returns:
query.getResultList()

getSingleResult

public <T> T getSingleResult(Class<T> clazz)
Specified by:
getSingleResult in interface SelectQuery
Type Parameters:
T - entity type
Parameters:
clazz - entity class
Returns:
the unique entity matching the query

getCount

public int getCount()

Specified by:
getCount in interface CountQuery
Returns:
the result.

append

public void append(String query)
TODO: this seems wrong. what's this for?

Specified by:
append in interface Expression
Parameters:
query - the query to append

toString

public String toString()
Overrides:
toString in class Object
Returns:
the select query as string.

getBodyParameters

public List<Object> getBodyParameters()
Get all attached parameters.

Returns:
the parameters

clearOrderBy

public QueryBuilder clearOrderBy()
drops all ordering constraints.

Returns:
this


Copyright © 2005-2011 ELCA. All Rights Reserved.