org.apache.openjpa.persistence.criteria
Class CriteriaQueryImpl<T>

java.lang.Object
  extended by org.apache.openjpa.persistence.criteria.CriteriaQueryImpl<T>
All Implemented Interfaces:
AbstractQuery<T>, CriteriaQuery<T>, AliasContext, OpenJPACriteriaQuery<T>

 class CriteriaQueryImpl<T>
extends Object
implements OpenJPACriteriaQuery<T>, AliasContext

Criteria query implementation. Collects clauses of criteria query (e.g. select projections, from/join, where conditions, order by). Eventually translates these clauses to a similar form of Expression tree that can be interpreted and executed against a data store by OpenJPA kernel.

Since:
2.0.0
Author:
Pinaki Poddar, Fay Wang

Constructor Summary
CriteriaQueryImpl(MetamodelImpl model, Class<T> resultClass)
           
CriteriaQueryImpl(MetamodelImpl model, SubqueryImpl<T> delegator)
          Used by a subquery to delegate to this receiver.
 
Method Summary
(package private)  void addRoot(RootImpl<?> root)
           
 void assertRoot()
           
 void assertSelection()
           
 OpenJPACriteriaQuery<T> compile()
          Compiles to verify that at least one root is defined, a selection term is present and, most importantly, collects all the parameters so that they can be bound to the executable query.
(package private)  Context ctx()
          Gets the current context.
 CriteriaQuery<T> distinct(boolean distinct)
          Sets whether this query as distinct.
<X> Root<X>
from(Class<X> cls)
           
<X> Root<X>
from(EntityType<X> entity)
           
 String getAlias(Selection<?> selection)
          Gets the alias of the given node.
 Map<Selection<?>,String> getAliases()
           
(package private)  CriteriaQueryImpl<?> getAncestor()
          Gets either this query itself if this is not a captive query for a subquery.
(package private)  Stack<Context> getContexts()
          Gets the stack of contexts used by this query.
(package private)  SubqueryImpl<?> getDelegator()
          Gets the subquery, if any, which is delegating to this receiver.
(package private)  CriteriaQueryImpl<?> getDelegatorParent()
           
 List<Expression<?>> getGroupList()
           
 PredicateImpl getGroupRestriction()
           
 MetamodelImpl getMetamodel()
          Gets the metamodel which defines the scope of all persistent entity references.
 List<Order> getOrderList()
          Gets the list of ordering elements.
 Set<ParameterExpression<?>> getParameters()
           
 OrderedMap<Object,Class<?>> getParameterTypes()
          Return map where key is the parameter expression itself and value is the expected type.
(package private)  QueryExpressions getQueryExpressions(ExpressionFactory factory)
          Populate a kernel expression tree by translating the components of this receiver with the help of the given ExpressionFactory.
 Value getRegisteredRootVariable(Root<?> root)
          Gets the registered variable for the given root.
 Value getRegisteredValue(Selection<?> selection)
          Gets the registered path value for the given node.
 Value getRegisteredVariable(Selection<?> selection)
          Gets the registered variable for the given node.
 PredicateImpl getRestriction()
           
 Class<T> getResultType()
           
 Root<?> getRoot()
           
(package private)  Root<?> getRoot(boolean mustExist)
           
 Set<Root<?>> getRoots()
           
 Selection<T> getSelection()
          Return the selection of the query
 List<Selection<?>> getSelectionList()
          Return the selection items of the query as a list
 CriteriaQuery<T> groupBy(Expression<?>... grouping)
           
 CriteriaQuery<T> groupBy(List<Expression<?>> grouping)
           
 CriteriaQuery<T> having(Expression<Boolean> restriction)
           
 CriteriaQuery<T> having(Predicate... restrictions)
           
(package private)  void invalidateCompilation()
           
protected  boolean isDefaultProjection()
           
 boolean isDistinct()
          Affirms if selection of this query is distinct.
(package private)  boolean isMultiselect()
           
 boolean isRegistered(Selection<?> selection)
          Affirms if the given node has been registered.
 CriteriaQuery<T> multiselect(List<Selection<?>> list)
           
 CriteriaQuery<T> multiselect(Selection<?>... selections)
          Specify the items that are to be returned in the query result.
 CriteriaQuery<T> orderBy(List<Order> orders)
           
 CriteriaQuery<T> orderBy(Order... orders)
           
(package private)  void registerParameter(ParameterExpressionImpl<?> p)
          Registers the given parameter.
(package private)  void registerRoot(Root<?> root, Value var)
          Registers a variable for the given root expression.
 void registerVariable(Selection<?> node, Value var, Value path)
          Register the given variable of given path value against the given node.
(package private)  void render(StringBuilder buffer, Set<Root<?>> roots, List<Join<?,?>> correlatedJoins)
           
 CriteriaQuery<T> select(Selection<? extends T> selection)
          Specify the item that is to be returned in the query result.
(package private)  void setContexts(Stack<Context> contexts)
           
<U> Subquery<U>
subquery(Class<U> type)
           
 String toCQL()
          Gets the string representation of the query.
 String toString()
          Returns a JPQL-like string, if this receiver is populated.
 CriteriaQuery<T> where(Expression<Boolean> restriction)
           
 CriteriaQuery<T> where(Predicate... restrictions)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CriteriaQueryImpl

public CriteriaQueryImpl(MetamodelImpl model,
                         Class<T> resultClass)

CriteriaQueryImpl

CriteriaQueryImpl(MetamodelImpl model,
                  SubqueryImpl<T> delegator)
Used by a subquery to delegate to this receiver.

Parameters:
model - the metamodel defines the scope of all persistent entity references.
delegator - the subquery which will delegate to this receiver.
Method Detail

getDelegator

SubqueryImpl<?> getDelegator()
Gets the subquery, if any, which is delegating to this receiver.


getMetamodel

public MetamodelImpl getMetamodel()
Gets the metamodel which defines the scope of all persistent entity references.


getContexts

Stack<Context> getContexts()
Gets the stack of contexts used by this query.


distinct

public CriteriaQuery<T> distinct(boolean distinct)
Sets whether this query as distinct.

Specified by:
distinct in interface AbstractQuery<T>
Specified by:
distinct in interface CriteriaQuery<T>

getOrderList

public List<Order> getOrderList()
Gets the list of ordering elements.

Specified by:
getOrderList in interface CriteriaQuery<T>
Returns:
Empty list if there is no ordering elements. The returned list if mutable but mutation has no impact on this query.

getSelection

public Selection<T> getSelection()
Return the selection of the query

Specified by:
getSelection in interface AbstractQuery<T>
Returns:
the item to be returned in the query result

multiselect

public CriteriaQuery<T> multiselect(Selection<?>... selections)
Specify the items that are to be returned in the query result. Replaces the previously specified selection(s), if any. The type of the result of the query execution depends on the specification of the criteria query object as well as the arguments to the multiselect method as follows: If the type of the criteria query is CriteriaQuery, a Tuple object corresponding to the arguments of the multiselect method will be instantiated and returned for each row that results from the query execution. If the type of the criteria query is CriteriaQuery for some user-defined class X, then the arguments to the multiselect method will be passed to the X constructor and an instance of type X will be returned for each row. The IllegalStateException will be thrown if a constructor for the given argument types does not exist. If the type of the criteria query is CriteriaQuery for some class X, an instance of type X[] will be returned for each row. The elements of the array will correspond to the arguments of the multiselect method. The IllegalStateException will be thrown if the arguments to the multiselect method are not of type X. If the type of the criteria query is CriteriaQuery, and only a single argument is passed to the multiselect method, an instance of type Object will be returned for each row. If the type of the criteria query is CriteriaQuery, and more than one argument is passed to the multiselect method, an instance of type Object[] will be instantiated and returned for each row. The elements of the array will correspond to the arguments to the multiselect method.

Specified by:
multiselect in interface CriteriaQuery<T>
Parameters:
selections - expressions specifying the items that are returned in the query result
Returns:
the modified query

registerParameter

void registerParameter(ParameterExpressionImpl<?> p)
Registers the given parameter.


getParameters

public Set<ParameterExpression<?>> getParameters()
Specified by:
getParameters in interface CriteriaQuery<T>

getSelectionList

public List<Selection<?>> getSelectionList()
Return the selection items of the query as a list

Returns:
the selection items of the query as a list

groupBy

public CriteriaQuery<T> groupBy(Expression<?>... grouping)
Specified by:
groupBy in interface AbstractQuery<T>
Specified by:
groupBy in interface CriteriaQuery<T>

groupBy

public CriteriaQuery<T> groupBy(List<Expression<?>> grouping)
Specified by:
groupBy in interface AbstractQuery<T>
Specified by:
groupBy in interface CriteriaQuery<T>

having

public CriteriaQuery<T> having(Expression<Boolean> restriction)
Specified by:
having in interface AbstractQuery<T>
Specified by:
having in interface CriteriaQuery<T>

having

public CriteriaQuery<T> having(Predicate... restrictions)
Specified by:
having in interface AbstractQuery<T>
Specified by:
having in interface CriteriaQuery<T>

orderBy

public CriteriaQuery<T> orderBy(Order... orders)
Specified by:
orderBy in interface CriteriaQuery<T>

orderBy

public CriteriaQuery<T> orderBy(List<Order> orders)
Specified by:
orderBy in interface CriteriaQuery<T>

select

public CriteriaQuery<T> select(Selection<? extends T> selection)
Specify the item that is to be returned in the query result. Replaces the previously specified selection(s), if any.

Specified by:
select in interface CriteriaQuery<T>
Parameters:
selection - selection specifying the item that is to be returned in the query result
Returns:
the modified query

where

public CriteriaQuery<T> where(Expression<Boolean> restriction)
Specified by:
where in interface AbstractQuery<T>
Specified by:
where in interface CriteriaQuery<T>

where

public CriteriaQuery<T> where(Predicate... restrictions)
Specified by:
where in interface AbstractQuery<T>
Specified by:
where in interface CriteriaQuery<T>

from

public <X> Root<X> from(EntityType<X> entity)
Specified by:
from in interface AbstractQuery<T>

from

public <X> Root<X> from(Class<X> cls)
Specified by:
from in interface AbstractQuery<T>

getGroupList

public List<Expression<?>> getGroupList()
Specified by:
getGroupList in interface AbstractQuery<T>

getGroupRestriction

public PredicateImpl getGroupRestriction()
Specified by:
getGroupRestriction in interface AbstractQuery<T>

getRestriction

public PredicateImpl getRestriction()
Specified by:
getRestriction in interface AbstractQuery<T>

getRoots

public Set<Root<?>> getRoots()
Specified by:
getRoots in interface AbstractQuery<T>

getRoot

public Root<?> getRoot()

getRoot

Root<?> getRoot(boolean mustExist)

addRoot

void addRoot(RootImpl<?> root)

isDistinct

public boolean isDistinct()
Affirms if selection of this query is distinct.

Specified by:
isDistinct in interface AbstractQuery<T>

subquery

public <U> Subquery<U> subquery(Class<U> type)
Specified by:
subquery in interface AbstractQuery<T>

getParameterTypes

public OrderedMap<Object,Class<?>> getParameterTypes()
Return map where key is the parameter expression itself and value is the expected type. Empty map if no parameter has been declared.


getQueryExpressions

QueryExpressions getQueryExpressions(ExpressionFactory factory)
Populate a kernel expression tree by translating the components of this receiver with the help of the given ExpressionFactory.


assertRoot

public void assertRoot()

assertSelection

public void assertSelection()

setContexts

void setContexts(Stack<Context> contexts)

getAncestor

CriteriaQueryImpl<?> getAncestor()
Gets either this query itself if this is not a captive query for a subquery. Otherwise gets the parent query of the delegating subquery.


getAliases

public Map<Selection<?>,String> getAliases()

ctx

Context ctx()
Gets the current context.


getAlias

public String getAlias(Selection<?> selection)
Gets the alias of the given node. Creates an automatic alias, if necessary.

Specified by:
getAlias in interface AliasContext

registerVariable

public void registerVariable(Selection<?> node,
                             Value var,
                             Value path)
Register the given variable of given path value against the given node. If the given node has no alias then an alias is set to the given node. If the variable or the path has non-null alias, then that alias must be equal to the alias of the given node. Otherwise, the node alias is set on the variable and path.

Specified by:
registerVariable in interface AliasContext
Parameters:
node - of query tree
var - must be a variable

isRegistered

public boolean isRegistered(Selection<?> selection)
Description copied from interface: AliasContext
Affirms if the given node has been registered.

Specified by:
isRegistered in interface AliasContext

getRegisteredVariable

public Value getRegisteredVariable(Selection<?> selection)
Description copied from interface: AliasContext
Gets the registered variable for the given node. Return null if the node is not registered.

Specified by:
getRegisteredVariable in interface AliasContext

getRegisteredValue

public Value getRegisteredValue(Selection<?> selection)
Description copied from interface: AliasContext
Gets the registered path value for the given node. Return null if the node is not registered.

Specified by:
getRegisteredValue in interface AliasContext

registerRoot

void registerRoot(Root<?> root,
                  Value var)
Registers a variable for the given root expression. A root expression is registered only for cross join.

Parameters:
root -
var -

getRegisteredRootVariable

public Value getRegisteredRootVariable(Root<?> root)
Gets the registered variable for the given root.

Specified by:
getRegisteredRootVariable in interface AliasContext

getDelegatorParent

CriteriaQueryImpl<?> getDelegatorParent()

getResultType

public Class<T> getResultType()
Specified by:
getResultType in interface AbstractQuery<T>

multiselect

public CriteriaQuery<T> multiselect(List<Selection<?>> list)
Specified by:
multiselect in interface CriteriaQuery<T>

isMultiselect

boolean isMultiselect()

isDefaultProjection

protected boolean isDefaultProjection()

invalidateCompilation

void invalidateCompilation()

compile

public OpenJPACriteriaQuery<T> compile()
Compiles to verify that at least one root is defined, a selection term is present and, most importantly, collects all the parameters so that they can be bound to the executable query.

Specified by:
compile in interface OpenJPACriteriaQuery<T>
Returns:
the same instance compiled.

toCQL

public String toCQL()
Gets the string representation of the query.

Specified by:
toCQL in interface OpenJPACriteriaQuery<T>
Returns:
a JPQL-like string.

render

void render(StringBuilder buffer,
            Set<Root<?>> roots,
            List<Join<?,?>> correlatedJoins)

toString

public String toString()
Returns a JPQL-like string, if this receiver is populated. Otherwise returns Object.toString().

Overrides:
toString in class Object


Copyright © 2006-2010 Apache Software Foundation. All Rights Reserved.