org.apache.openjpa.kernel
Class QueryImpl

java.lang.Object
  extended by org.apache.openjpa.kernel.QueryImpl
All Implemented Interfaces:
Serializable, Query, QueryContext, QueryFlushModes, QueryOperations
Direct Known Subclasses:
DistributedQueryImpl

public class QueryImpl
extends Object
implements Query

Implementation of the Query interface.

Author:
Abe White
See Also:
Serialized Form

Nested Class Summary
protected static class QueryImpl.Compilation
          Struct of compiled query properties.
static class QueryImpl.PackingResultObjectProvider
          Result object provider that packs results before returning them.
 class QueryImpl.RemoveOnCloseResultList
          Result list that removes itself from the query's open result list when it is closed.
 
Field Summary
 
Fields inherited from interface org.apache.openjpa.kernel.QueryOperations
OP_DELETE, OP_SELECT, OP_UPDATE
 
Fields inherited from interface org.apache.openjpa.kernel.QueryFlushModes
FLUSH_FALSE, FLUSH_TRUE, FLUSH_WITH_CONNECTION
 
Constructor Summary
QueryImpl(Broker broker, String language, StoreQuery storeQuery)
          Construct a query managed by the given broker.
 
Method Summary
 void addAggregateListener(AggregateListener listener)
          Register an aggregate listener for the query.
 void addFilterListener(FilterListener listener)
          Register a filter listener for the query.
 void assertNotReadOnly()
          Assert that the query is not read-only.
 void assertNotSerialized()
          Check that the query has not been serialized, which causes it to lose its association with its Broker.
 void assertOpen()
          Assert that the query's broker is still open.
protected  void assertParameters(StoreQuery q, StoreQuery.Executor ex, Map params)
           
protected  void assertParameters(StoreQuery q, StoreQuery.Executor ex, Object[] params)
          Checks that the passed parameters match the declarations.
 Class classForName(String name, String[] imports)
          Helper method to instantiate the class with the given name, taking into account the query's candidate package, automatic imports, and the given imports (if any).
 void closeAll()
          Close all open query results.
 void closeResources()
          Close query results that are consuming resources.
protected  QueryImpl.Compilation compilationFromCache()
          Find the cached compilation for the current query, creating one if it does not exist.
 void compile()
          Compile the query.
 void declareParameters(String params)
          Declared parameters, for query languages that use them.
protected  QueryImpl.RemoveOnCloseResultList decorateResultList(ResultList<?> res)
          Optionally decorate the native result.
 long deleteAll()
          Deletes all of the instances that are satisfied by the query.
 long deleteAll(Map params)
          Deletes all of the instances that are satisfied by the query.
 long deleteAll(Object[] params)
          Deletes all of the instances that are satisfied by the query.
 Number deleteInMemory(StoreQuery q, StoreQuery.Executor executor, Object[] params)
          Helper method to delete the objects found by executing a query on the given executor.
 Object execute()
          Execute.
 Object execute(Map params)
          Execute with parameter map.
 Object execute(Object[] params)
          Execute with parameter array.
 ClassMetaData[] getAccessPathMetaDatas()
          Return the classes that affect this query.
 AggregateListener getAggregateListener(String tag)
          Return the filter listener for the given tag, or null.
 Collection<AggregateListener> getAggregateListeners()
          The set of aggregate listeners.
 String getAlias()
          If this query is not a projection but places candidate results into a result class under an alias, return that alias.
 Broker getBroker()
          The broker that generated this query.
 Collection<?> getCandidateCollection()
          Return the candidate collection, or null if an extent was specified instead of a collection.
 Extent getCandidateExtent()
          Return the candidate extent, or null if a collection was specified instead of an extent.
 Class getCandidateType()
          Return the class of the objects that this query will return, or null if this information is not available / not relevant.
 Object getCompilation()
          Return the query's compilation state.
 String[] getDataStoreActions(Map params)
          Returns a description of the commands that will be sent to the datastore in order to execute this query.
 long getEndRange()
          Return the 0-based exclusive end index for the returned results, or Long.MAX_VALUE for no limit.
 FetchConfiguration getFetchConfiguration()
          Return the fetch configuration for this query.
 FilterListener getFilterListener(String tag)
          Return the filter listener for the given tag, or null.
 Collection<FilterListener> getFilterListeners()
          The set of filter listeners.
 boolean getIgnoreChanges()
          Whether to ignore changes in the current transaction.
 String getLanguage()
          The query language.
 int getOperation()
          Returns the operation that this query will be expected to perform.
 OrderedMap<Object,Class<?>> getOrderedParameterTypes()
          Return a map of parameter name to type for this query.
 String getParameterDeclaration()
          The parameter declaration.
 LinkedMap getParameterTypes()
          Return a map of parameter name to type for this query.
 String[] getProjectionAliases()
          If this query is a projection, return the projection aliases.
 Class<?>[] getProjectionTypes()
          If this query is a projection, return the projection types.
 Query getQuery()
          Return the query for this context.
 String getQueryString()
          The query string.
 String getResultMappingName()
          Name of a mapping from the result data to its object representation.
 Class getResultMappingScope()
          Scope of a mapping from the result data to its object representation.
 Class getResultType()
          Returns the result class that has been set through QueryContext.setResultType(java.lang.Class), or null if none.
 long getStartRange()
          Return the 0-based start index for the returned results.
 StoreContext getStoreContext()
          The persistence context for the query.
 StoreQuery getStoreQuery()
          Internal store query.
 Map getUpdates()
          If this query is a bulk update, return a map of the FieldMetaDatas to Constants.
 boolean hasGrouping()
          Return true if the query uses grouping.
 boolean hasSubclasses()
          Whether query results will include subclasses of the candidate class.
static boolean isAccessPathDirty(Broker broker, ClassMetaData[] accessMetas)
           
 boolean isAggregate()
          Return true if the query is an aggregate.
 boolean isDistinct()
          Affirms if this query results are distinct instance(s).
 boolean isParsedQuery()
          Affirms if this query has originated by parsing a string-based query.
 boolean isReadOnly()
          Whether the query has been marked read-only.
 boolean isUnique()
          The unique flag.
 void lock()
          Synchronize on the query's internal lock.
 void removeAggregateListener(AggregateListener listener)
          Remove an aggregate listener from the query.
 void removeFilterListener(FilterListener listener)
          Remove a filter listener from the query.
 void setCandidateCollection(Collection<?> candidateCollection)
          Set a collection of candidates.
 void setCandidateExtent(Extent candidateExtent)
          Set the candidate extent.
 void setCandidateType(Class candidateClass, boolean subs)
          Set the candidate type.
 void setIgnoreChanges(boolean flag)
          Whether to ignore changes in the current transaction.
 boolean setQuery(Object query)
          The query string or template.
 void setRange(long start, long end)
          Set the range of results to return.
 void setReadOnly(boolean flag)
          Whether the query has been marked read-only.
 void setResultMapping(Class<?> scope, String name)
          Name and scope of a mapping from the result data to its object representation.
 void setResultType(Class cls)
          Specify the type of object in which the result of evaluating this query.
 void setUnique(boolean unique)
          Specify that the query will return only 1 result, rather than a collection.
 void startLocking()
           
 void stopLocking()
           
protected  Object toResult(StoreQuery q, StoreQuery.Executor ex, ResultObjectProvider rop, StoreQuery.Range range)
          Return the query result for the given result object provider.
 String toString()
           
 void unlock()
          Unlock the query's internal lock.
 long updateAll()
          Performs an update of the instances that are satisfied by the query.
 long updateAll(Map params)
          Performs an update of the instances that are satisfied by the query.
 long updateAll(Object[] params)
          Performs an update of the instances that are satisfied by the query.
 Number updateInMemory(StoreQuery q, StoreQuery.Executor executor, Object[] params)
          Helper method to update the objects found by executing a query on the given executor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

QueryImpl

public QueryImpl(Broker broker,
                 String language,
                 StoreQuery storeQuery)
Construct a query managed by the given broker.

Method Detail

getStoreQuery

public StoreQuery getStoreQuery()
Internal store query.


getBroker

public Broker getBroker()
Description copied from interface: Query
The broker that generated this query.

Specified by:
getBroker in interface Query

getQuery

public Query getQuery()
Description copied from interface: QueryContext
Return the query for this context. Note that the query will be unavailable in remote contexts, and this method may throw an exception to that effect.

Specified by:
getQuery in interface QueryContext

getStoreContext

public StoreContext getStoreContext()
Description copied from interface: QueryContext
The persistence context for the query.

Specified by:
getStoreContext in interface QueryContext

getLanguage

public String getLanguage()
Description copied from interface: QueryContext
The query language.

Specified by:
getLanguage in interface QueryContext

getFetchConfiguration

public FetchConfiguration getFetchConfiguration()
Description copied from interface: QueryContext
Return the fetch configuration for this query.

Specified by:
getFetchConfiguration in interface QueryContext

getQueryString

public String getQueryString()
Description copied from interface: QueryContext
The query string.

Specified by:
getQueryString in interface QueryContext

getIgnoreChanges

public boolean getIgnoreChanges()
Description copied from interface: QueryContext
Whether to ignore changes in the current transaction.

Specified by:
getIgnoreChanges in interface QueryContext

setIgnoreChanges

public void setIgnoreChanges(boolean flag)
Description copied from interface: Query
Whether to ignore changes in the current transaction.

Specified by:
setIgnoreChanges in interface Query

isReadOnly

public boolean isReadOnly()
Description copied from interface: QueryContext
Whether the query has been marked read-only.

Specified by:
isReadOnly in interface QueryContext

setReadOnly

public void setReadOnly(boolean flag)
Description copied from interface: QueryContext
Whether the query has been marked read-only.

Specified by:
setReadOnly in interface QueryContext

addFilterListener

public void addFilterListener(FilterListener listener)
Description copied from interface: Query
Register a filter listener for the query.

Specified by:
addFilterListener in interface Query

removeFilterListener

public void removeFilterListener(FilterListener listener)
Description copied from interface: Query
Remove a filter listener from the query.

Specified by:
removeFilterListener in interface Query

getFilterListeners

public Collection<FilterListener> getFilterListeners()
Description copied from interface: QueryContext
The set of filter listeners.

Specified by:
getFilterListeners in interface QueryContext

getFilterListener

public FilterListener getFilterListener(String tag)
Description copied from interface: QueryContext
Return the filter listener for the given tag, or null.

Specified by:
getFilterListener in interface QueryContext

addAggregateListener

public void addAggregateListener(AggregateListener listener)
Description copied from interface: Query
Register an aggregate listener for the query.

Specified by:
addAggregateListener in interface Query

removeAggregateListener

public void removeAggregateListener(AggregateListener listener)
Description copied from interface: Query
Remove an aggregate listener from the query.

Specified by:
removeAggregateListener in interface Query

getAggregateListeners

public Collection<AggregateListener> getAggregateListeners()
Description copied from interface: QueryContext
The set of aggregate listeners.

Specified by:
getAggregateListeners in interface QueryContext

getAggregateListener

public AggregateListener getAggregateListener(String tag)
Description copied from interface: QueryContext
Return the filter listener for the given tag, or null.

Specified by:
getAggregateListener in interface QueryContext

getCandidateExtent

public Extent getCandidateExtent()
Description copied from interface: Query
Return the candidate extent, or null if a collection was specified instead of an extent.

Specified by:
getCandidateExtent in interface Query

setCandidateExtent

public void setCandidateExtent(Extent candidateExtent)
Description copied from interface: Query
Set the candidate extent.

Specified by:
setCandidateExtent in interface Query

getCandidateCollection

public Collection<?> getCandidateCollection()
Description copied from interface: QueryContext
Return the candidate collection, or null if an extent was specified instead of a collection.

Specified by:
getCandidateCollection in interface QueryContext

setCandidateCollection

public void setCandidateCollection(Collection<?> candidateCollection)
Description copied from interface: Query
Set a collection of candidates.

Specified by:
setCandidateCollection in interface Query

getCandidateType

public Class getCandidateType()
Description copied from interface: QueryContext
Return the class of the objects that this query will return, or null if this information is not available / not relevant.

Specified by:
getCandidateType in interface QueryContext

setCandidateType

public void setCandidateType(Class candidateClass,
                             boolean subs)
Description copied from interface: QueryContext
Set the candidate type.

Specified by:
setCandidateType in interface QueryContext

hasSubclasses

public boolean hasSubclasses()
Description copied from interface: QueryContext
Whether query results will include subclasses of the candidate class.

Specified by:
hasSubclasses in interface QueryContext

getResultMappingName

public String getResultMappingName()
Description copied from interface: QueryContext
Name of a mapping from the result data to its object representation.

Specified by:
getResultMappingName in interface QueryContext

getResultMappingScope

public Class getResultMappingScope()
Description copied from interface: QueryContext
Scope of a mapping from the result data to its object representation.

Specified by:
getResultMappingScope in interface QueryContext

setResultMapping

public void setResultMapping(Class<?> scope,
                             String name)
Description copied from interface: QueryContext
Name and scope of a mapping from the result data to its object representation.

Specified by:
setResultMapping in interface QueryContext

isUnique

public boolean isUnique()
Description copied from interface: QueryContext
The unique flag.

Specified by:
isUnique in interface QueryContext

isParsedQuery

public boolean isParsedQuery()
Affirms if this query has originated by parsing a string-based query.


setUnique

public void setUnique(boolean unique)
Description copied from interface: QueryContext
Specify that the query will return only 1 result, rather than a collection. The execute method will return null if the query result size is 0.

Specified by:
setUnique in interface QueryContext

getResultType

public Class getResultType()
Description copied from interface: QueryContext
Returns the result class that has been set through QueryContext.setResultType(java.lang.Class), or null if none.

Specified by:
getResultType in interface QueryContext

setResultType

public void setResultType(Class cls)
Description copied from interface: QueryContext
Specify the type of object in which the result of evaluating this query.

Specified by:
setResultType in interface QueryContext

getStartRange

public long getStartRange()
Description copied from interface: QueryContext
Return the 0-based start index for the returned results.

Specified by:
getStartRange in interface QueryContext

getEndRange

public long getEndRange()
Description copied from interface: QueryContext
Return the 0-based exclusive end index for the returned results, or Long.MAX_VALUE for no limit.

Specified by:
getEndRange in interface QueryContext

setRange

public void setRange(long start,
                     long end)
Description copied from interface: QueryContext
Set the range of results to return.

Specified by:
setRange in interface QueryContext
Parameters:
start - 0-based inclusive start index
end - 0-based exclusive end index, or Long.MAX_VALUE for no limit

getParameterDeclaration

public String getParameterDeclaration()
Description copied from interface: QueryContext
The parameter declaration.

Specified by:
getParameterDeclaration in interface QueryContext

declareParameters

public void declareParameters(String params)
Description copied from interface: QueryContext
Declared parameters, for query languages that use them.

Specified by:
declareParameters in interface QueryContext

compile

public void compile()
Description copied from interface: Query
Compile the query.

Specified by:
compile in interface Query

getCompilation

public Object getCompilation()
Description copied from interface: QueryContext
Return the query's compilation state.

Specified by:
getCompilation in interface QueryContext

compilationFromCache

protected QueryImpl.Compilation compilationFromCache()
Find the cached compilation for the current query, creating one if it does not exist.


execute

public Object execute()
Description copied from interface: Query
Execute.

Specified by:
execute in interface Query

execute

public Object execute(Object[] params)
Description copied from interface: Query
Execute with parameter array.

Specified by:
execute in interface Query

execute

public Object execute(Map params)
Description copied from interface: Query
Execute with parameter map.

Specified by:
execute in interface Query

deleteAll

public long deleteAll()
Description copied from interface: Query
Deletes all of the instances that are satisfied by the query.

Specified by:
deleteAll in interface Query
Returns:
the number of instances that were deleted

deleteAll

public long deleteAll(Object[] params)
Description copied from interface: Query
Deletes all of the instances that are satisfied by the query.

Specified by:
deleteAll in interface Query
Parameters:
params - the positional parameters for the query
Returns:
the number of instances that were deleted

deleteAll

public long deleteAll(Map params)
Description copied from interface: Query
Deletes all of the instances that are satisfied by the query.

Specified by:
deleteAll in interface Query
Parameters:
params - the named parameter map
Returns:
the number of instances that were deleted

updateAll

public long updateAll()
Description copied from interface: Query
Performs an update of the instances that are satisfied by the query.

Specified by:
updateAll in interface Query
Returns:
the number of instances that were update

updateAll

public long updateAll(Object[] params)
Description copied from interface: Query
Performs an update of the instances that are satisfied by the query.

Specified by:
updateAll in interface Query
Parameters:
params - the positional parameter array
Returns:
the number of instances that were update

updateAll

public long updateAll(Map params)
Description copied from interface: Query
Performs an update of the instances that are satisfied by the query.

Specified by:
updateAll in interface Query
Parameters:
params - the named parameter map
Returns:
the number of instances that were update

deleteInMemory

public Number deleteInMemory(StoreQuery q,
                             StoreQuery.Executor executor,
                             Object[] params)
Description copied from interface: QueryContext
Helper method to delete the objects found by executing a query on the given executor.

Specified by:
deleteInMemory in interface QueryContext

updateInMemory

public Number updateInMemory(StoreQuery q,
                             StoreQuery.Executor executor,
                             Object[] params)
Description copied from interface: QueryContext
Helper method to update the objects found by executing a query on the given executor.

Specified by:
updateInMemory in interface QueryContext

toResult

protected Object toResult(StoreQuery q,
                          StoreQuery.Executor ex,
                          ResultObjectProvider rop,
                          StoreQuery.Range range)
                   throws Exception
Return the query result for the given result object provider.

Throws:
Exception

decorateResultList

protected QueryImpl.RemoveOnCloseResultList decorateResultList(ResultList<?> res)
Optionally decorate the native result.


isAccessPathDirty

public static boolean isAccessPathDirty(Broker broker,
                                        ClassMetaData[] accessMetas)

closeAll

public void closeAll()
Description copied from interface: Query
Close all open query results.

Specified by:
closeAll in interface Query

closeResources

public void closeResources()
Description copied from interface: Query
Close query results that are consuming resources. Allow results that are not consuming resources to remain open so that they continue to function normally.

Specified by:
closeResources in interface Query

getDataStoreActions

public String[] getDataStoreActions(Map params)
Description copied from interface: Query
Returns a description of the commands that will be sent to the datastore in order to execute this query. This will typically be in the native query language of the database (e.g., SQL).

Specified by:
getDataStoreActions in interface Query
Parameters:
params - the named parameter map for the query invocation

setQuery

public boolean setQuery(Object query)
Description copied from interface: Query
The query string or template.

Specified by:
setQuery in interface Query

getAlias

public String getAlias()
Description copied from interface: QueryContext
If this query is not a projection but places candidate results into a result class under an alias, return that alias.

Specified by:
getAlias in interface QueryContext

getProjectionAliases

public String[] getProjectionAliases()
Description copied from interface: QueryContext
If this query is a projection, return the projection aliases.

Specified by:
getProjectionAliases in interface QueryContext

getProjectionTypes

public Class<?>[] getProjectionTypes()
Description copied from interface: QueryContext
If this query is a projection, return the projection types.

Specified by:
getProjectionTypes in interface QueryContext

getOperation

public int getOperation()
Description copied from interface: QueryContext
Returns the operation that this query will be expected to perform.

Specified by:
getOperation in interface QueryContext
See Also:
QueryOperations

isAggregate

public boolean isAggregate()
Description copied from interface: QueryContext
Return true if the query is an aggregate.

Specified by:
isAggregate in interface QueryContext

isDistinct

public boolean isDistinct()
Description copied from interface: QueryContext
Affirms if this query results are distinct instance(s).

Specified by:
isDistinct in interface QueryContext

hasGrouping

public boolean hasGrouping()
Description copied from interface: QueryContext
Return true if the query uses grouping.

Specified by:
hasGrouping in interface QueryContext

getAccessPathMetaDatas

public ClassMetaData[] getAccessPathMetaDatas()
Description copied from interface: QueryContext
Return the classes that affect this query.

Specified by:
getAccessPathMetaDatas in interface QueryContext

getOrderedParameterTypes

public OrderedMap<Object,Class<?>> getOrderedParameterTypes()
Description copied from interface: QueryContext
Return a map of parameter name to type for this query. The returned map will iterate in the order that the parameters were declared or, if they're implicit, used.

Specified by:
getOrderedParameterTypes in interface QueryContext

getParameterTypes

public LinkedMap getParameterTypes()
Description copied from interface: QueryContext
Return a map of parameter name to type for this query. The returned map will iterate in the order that the parameters were declared or, if they're implicit, used.

Specified by:
getParameterTypes in interface QueryContext

getUpdates

public Map getUpdates()
Description copied from interface: QueryContext
If this query is a bulk update, return a map of the FieldMetaDatas to Constants.

Specified by:
getUpdates in interface QueryContext

lock

public void lock()
Description copied from interface: QueryContext
Synchronize on the query's internal lock.

Specified by:
lock in interface QueryContext

unlock

public void unlock()
Description copied from interface: QueryContext
Unlock the query's internal lock.

Specified by:
unlock in interface QueryContext

startLocking

public void startLocking()

stopLocking

public void stopLocking()

classForName

public Class classForName(String name,
                          String[] imports)
Description copied from interface: QueryContext
Helper method to instantiate the class with the given name, taking into account the query's candidate package, automatic imports, and the given imports (if any). Returns null if the type cannot be found.

Specified by:
classForName in interface QueryContext

assertOpen

public void assertOpen()
Description copied from interface: Query
Assert that the query's broker is still open.

Specified by:
assertOpen in interface Query

assertNotReadOnly

public void assertNotReadOnly()
Description copied from interface: Query
Assert that the query is not read-only.

Specified by:
assertNotReadOnly in interface Query

assertNotSerialized

public void assertNotSerialized()
Description copied from interface: Query
Check that the query has not been serialized, which causes it to lose its association with its Broker.

Specified by:
assertNotSerialized in interface Query

assertParameters

protected void assertParameters(StoreQuery q,
                                StoreQuery.Executor ex,
                                Object[] params)
Checks that the passed parameters match the declarations.


assertParameters

protected void assertParameters(StoreQuery q,
                                StoreQuery.Executor ex,
                                Map params)

toString

public String toString()
Overrides:
toString in class Object


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