Class JPQLQueryContext
java.lang.Object
org.eclipse.persistence.jpa.jpql.tools.JPQLQueryContext
- Direct Known Subclasses:
DefaultJPQLQueryContext,EclipseLinkJPQLQueryContext
This context is used to store information related to the JPQL query.
IQuery externalQuery = ...;
JPQLQueryContext context = new JPQLQueryContext(DefaultJPQLGrammar.instance());
context.setQuery(query);
If the JPQL query is already parsed, then the context can use it and it needs to be set before
setting the IQuery:
JPQLExpression jpqlExpression = ...;
JPQLQueryContext context = new JPQLQueryContext(DefaultJPQLGrammar.instance());
context.setJPQLExpression(jpqlExpression);
context.setQuery(query);
Provisional API: This interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.
- Since:
- 2.3
- Version:
- 2.5
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classThis visitor is responsible to find theInputParameterswith a certain parameter name.protected static classThis visitor is responsible to retrieve theExpressionthat is the beginning of a query. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected JPQLQueryContextThe currentJPQLQueryContextis the context used for the current query or subquery.protected JPQLQueryContextWhen this context is a sub-context used for a subquery, then this is the context for the parent query. -
Constructor Summary
ConstructorsModifierConstructorDescriptionJPQLQueryContext(JPQLGrammar jpqlGrammar) Creates a newJPQLQueryContext.protectedJPQLQueryContext(JPQLQueryContext parent, Expression currentQuery) Creates a new sub-JPQLQueryContext. -
Method Summary
Modifier and TypeMethodDescriptionprotected DeclarationResolverprotected DeclarationResolverprotected JPQLQueryContext.InputParameterVisitorprotected abstract JPQLQueryContextbuildJPQLQueryContext(JPQLQueryContext currentContext, Expression currentQuery) protected abstract LiteralVisitorprotected abstract ParameterTypeVisitorprotected JPQLQueryContext.QueryExpressionVisitorprotected abstract ResolverBuildervoidconvertUnqualifiedDeclaration(Declaration declaration) Converts the givenDeclarationfrom being set as a range variable declaration to a path expression declaration.voiddispose()Disposes the internal data.voidDisposes this context, which is the current context being used by a subquery.findInputParameters(String parameterName) Retrieves all theInputParameterswith the given parameter name.Returns the currentExpressionbeing manipulated, which is either the top-level query or a subquery.Returns theDeclarationResolverof this context and not from the current query's declaration.Returns the currentJPQLQueryContext, i.e.Returns the currentExpressionbeing manipulated, which is either the top-level query or a subquery.getDeclaration(String variableName) Retrieves theDeclarationfor which the given variable name is used to navigate to the "root" object.Returns theDeclarationResolverof the current query's declaration.getDeclarationResolver(Expression expression) Returns theDeclarationResolverof the current query's declaration.protected DeclarationResolverReturns theDeclarationResolverof the current query's declaration.Returns the ordered list ofDeclarations.getEnumType(String enumTypeName) Returns theITyperepresenting the possible given enum type.Returns the registry containing theJPQLQueryBNFsand theExpressionFactoriesthat are used to properly parse a JPQL query.Returns the JPQL grammar that will be used to define how to parse a JPQL query.protected JPQLQueryContext.InputParameterVisitorReturns the version of the Java Persistence to support, which dictates which version of the JPQL grammar to support.Returns the parsed tree representation of the JPQL query.Returns the string representation of the JPQL query.protected LiteralVisitorgetMapping(Expression expression) Returns theIMappingfor the field represented by the givenExpression.getParameterType(InputParameter inputParameter) Retrieves, if it can be determined, the type of the givenInputParameter.protected ParameterTypeVisitorReturns the parent context if the current context is not the root context.Retrieves the provider of managed types.Returns the version of the persistence provider.getQuery()Returns the external form of the JPQL query.getQueryExpression(Expression expression) Retrieves theExpressionrepresenting the query statement (either the top-level queryJPQLExpressionor the subquerySimpleSelectStatement) owning the givenExpression.protected JPQLQueryContext.QueryExpressionVisitorgetResolver(String variableName) Retrieves theResolvermapped with the given identification variable.getResolver(Expression expression) Creates or retrieved the cachedResolverfor the givenExpression.protected ResolverBuilderReturns the variables that got defined in the select expression.Retrieves the external type for the given Java type.Retrieves the external class with the given fully qualified class name.getType(Expression expression) Returns theITypeof the givenExpression.getTypeDeclaration(Expression expression) Returns theITypeDeclarationof the field handled by thisResolver.Returns a helper that gives access to the most commontypes.Returns the type repository for the application.booleanhasJoins()Determines whether the JPQL expression has JOIN expressions.protected voidinitialize(JPQLGrammar jpqlGrammar) Initializes thisJPQLQueryContext.protected voidInitializes the parsed tree representation of the JPQL query if it has not been set before setting theIQuery.booleanisCollectionIdentificationVariable(String variableName) Determines whether the given identification variable is defining a join or a collection member declaration expressions.booleanisRangeIdentificationVariable(String variableName) Determines whether the given variable name is an identification variable name used to define an abstract schema name.booleanisResultVariable(String variable) Determines if the given variable is a result variable.booleanDetermines whether thisJPQLQueryContextcurrently holds the information of a subquery or for the top-level query.booleanDetermines if the parser is in tolerant mode or is in fast mode.literal(Expression expression, LiteralType type) Retrieves the "literal" from the givenExpression.voidnewSubqueryContext(Expression currentQuery) Changes the state of this context to use the given subquery.voidsetJPQLExpression(JPQLExpression jpqlExpression) Sets the parsed tree representation of the JPQL query.voidSets the external form of the JPQL query, which will be parsed and information will be extracted for later access.voidsetTolerant(boolean tolerant) Sets whether the parser is in tolerant mode or is in fast mode.protected voidstore(JPQLQueryContext parent, Expression currentQuery) Stores the information contained in the given parent into this one.toString()
-
Field Details
-
currentContext
The currentJPQLQueryContextis the context used for the current query or subquery. If the current context is not the global context, then its parent is nonnull. -
parent
When this context is a sub-context used for a subquery, then this is the context for the parent query.
-
-
Constructor Details
-
JPQLQueryContext
Creates a newJPQLQueryContext.- Parameters:
jpqlGrammar- TheJPQLGrammardefines how to parse a JPQL query
-
JPQLQueryContext
Creates a new sub-JPQLQueryContext.- Parameters:
parent- The parent contextcurrentQuery- The parsed tree representation of the subquery
-
-
Method Details
-
buildDeclarationResolver
-
buildDeclarationResolver
-
buildInputParameter
-
buildJPQLQueryContext
protected abstract JPQLQueryContext buildJPQLQueryContext(JPQLQueryContext currentContext, Expression currentQuery) -
buildLiteralVisitor
-
buildParameterTypeVisitor
-
buildQueryExpressionVisitor
-
buildResolverBuilder
-
convertUnqualifiedDeclaration
Converts the givenDeclarationfrom being set as a range variable declaration to a path expression declaration.In this query "
UPDATE Employee SET firstName = 'MODIFIED' WHERE (SELECT COUNT(m) FROM managedEmployees m) > 0" managedEmployees is an unqualified collection-valued path expression (employee.managedEmployees).- Parameters:
declaration- TheDeclarationthat was parsed to range over an abstract schema name but is actually ranging over a path expression
-
dispose
public void dispose()Disposes the internal data. -
disposeSubqueryContext
public void disposeSubqueryContext()Disposes this context, which is the current context being used by a subquery. Once it is disposed, any information retrieved will be for the subquery's parent query. -
findInputParameters
Retrieves all theInputParameterswith the given parameter name.- Parameters:
parameterName- The parameter used to find theInputParameterswith the same value- Returns:
- Either the
InputParametersthat has the given parameter or an empty collection
-
getActualCurrentQuery
Returns the currentExpressionbeing manipulated, which is either the top-level query or a subquery.- Returns:
- Either the top-level query or a subquery
-
getActualDeclarationResolver
Returns theDeclarationResolverof this context and not from the current query's declaration.- Returns:
- The
DeclarationResolverfor this context
-
getCurrentContext
Returns the currentJPQLQueryContext, i.e. the context of the query being manipulated, which can either be the top-level query or a subquery.- Returns:
- The active context
-
getCurrentQuery
Returns the currentExpressionbeing manipulated, which is either the top-level query or a subquery.- Returns:
- Either the top-level query or a subquery
-
getDeclaration
Retrieves theDeclarationfor which the given variable name is used to navigate to the "root" object. This does not go up the hierarchy when looking for theDeclaration.- Parameters:
variableName- The name of the identification variable that is used to navigate a "root" object- Returns:
- The
Declarationcontaining the information about the identification variable declaration - Since:
- 2.5
-
getDeclarationResolver
Returns theDeclarationResolverof the current query's declaration. For a SELECT query, it contains the information defined in the FROM clause. For DELETE and UPDATE queries, it contains a single range declaration variable. If the current query is a subquery, then it contains the information defined in theFROMclause.- Returns:
- The
DeclarationResolverfor the current query being visited
-
getDeclarationResolver
Returns theDeclarationResolverof the current query's declaration. For a SELECT query, it contains the information defined in the FROM clause. For DELETE and UPDATE queries, it contains a single range variable declaration. If the current query is a subquery, then it contains the information defined in the subqueryFROMclause.- Parameters:
expression- TheExpressionthat will be used to retrieve its query expression, i.e. eitherJPQLExpressionorSimpleSelectStatement- Returns:
- The
DeclarationResolverfor the current query being visited
-
getDeclarationResolverImp
Returns theDeclarationResolverof the current query's declaration.- Returns:
- The
DeclarationResolverfor the current query being visited
-
getDeclarations
Returns the ordered list ofDeclarations.- Returns:
- The
Declarationsof the current query that was parsed
-
getEnumType
Returns theITyperepresenting the possible given enum type. If the type name- Parameters:
enumTypeName- The fully qualified enum type with the constant- Returns:
- The external form for the given Enum type
-
getExpressionRegistry
Returns the registry containing theJPQLQueryBNFsand theExpressionFactoriesthat are used to properly parse a JPQL query.- Returns:
- The registry containing the information related to the JPQL grammar
-
getGrammar
Returns the JPQL grammar that will be used to define how to parse a JPQL query.- Returns:
- The grammar that was used to parse this
Expression
-
getInputParameterVisitor
-
getJPAVersion
Returns the version of the Java Persistence to support, which dictates which version of the JPQL grammar to support.- Returns:
- The version of the supported Java Persistence functional specification
-
getJPQLExpression
Returns the parsed tree representation of the JPQL query.- Returns:
- The parsed tree representation of the JPQL query
-
getJPQLQuery
Returns the string representation of the JPQL query.- Returns:
- The string representation of the JPQL query
-
getLiteralVisitor
-
getMapping
Returns theIMappingfor the field represented by the givenExpression.- Parameters:
expression- TheExpressionrepresenting a state field path expression or a collection-valued path expression- Returns:
- Either the
IMappingornullif none exists
-
getParameterType
Retrieves, if it can be determined, the type of the givenInputParameter. The type will be guessed based on its location within expression.Note: Both named and positional input parameter can be used.
- Parameters:
inputParameter- TheInputParameterto retrieve its type- Returns:
- Either the closest type of the input parameter or
nullif the type could not be determined
-
getParameterTypeVisitor
-
getParent
Returns the parent context if the current context is not the root context.- Returns:
- The parent context or
nullif the current context is the root
-
getProvider
Retrieves the provider of managed types.- Returns:
- The object that has access to the application's managed types.
-
getProviderVersion
Returns the version of the persistence provider.- Returns:
- The version of the persistence provider, if one is extending the default JPQL grammar defined in the Java Persistence specification, otherwise returns an empty string
- Since:
- 2.5
-
getQuery
Returns the external form of the JPQL query.- Returns:
- The external form of the JPQL query
-
getQueryExpression
Retrieves theExpressionrepresenting the query statement (either the top-level queryJPQLExpressionor the subquerySimpleSelectStatement) owning the givenExpression.- Parameters:
expression- A child of the "root"Expressionto retrieve- Returns:
- The query statement that is the "root" parent of the given
Expression
-
getQueryExpressionVisitor
-
getResolver
Creates or retrieved the cachedResolverfor the givenExpression. TheResolvercan return theITypeandITypeDeclarationof theExpressionand either theIManagedTypeor theIMapping.- Parameters:
expression- TheExpressionfor which itsResolverwill be retrieved- Returns:
Resolverfor the givenExpression
-
getResolver
Retrieves theResolvermapped with the given identification variable. If the identification is not defined in the declaration traversed by this resolver, than the search will traverse the parent hierarchy. -
getResolverBuilder
-
getResultVariables
Returns the variables that got defined in the select expression. This only applies to JPQL queries built for JPA 2.0.- Returns:
- The variables identifying the select expressions, if any was defined or an empty set if none were defined
-
getType
Retrieves the external type for the given Java type.- Parameters:
type- The Java type to wrap with an external form- Returns:
- The external form of the given type
-
getType
Returns theITypeof the givenExpression.- Parameters:
expression- TheExpressionfor which its type will be calculated- Returns:
- Either the
ITypethat was resolved by thisResolveror theITypeforIType.UNRESOLVABLE_TYPEif it could not be resolved
-
getType
Retrieves the external class with the given fully qualified class name.- Parameters:
typeName- The fully qualified class name of the class to retrieve- Returns:
- The external form of the class to retrieve
-
getTypeDeclaration
Returns theITypeDeclarationof the field handled by thisResolver.- Parameters:
expression- TheExpressionfor which its type declaration will be calculated- Returns:
- Either the
ITypeDeclarationthat was resolved by thisResolveror theITypeDeclarationforIType.UNRESOLVABLE_TYPEif it could not be resolved
-
getTypeHelper
Returns a helper that gives access to the most commontypes.- Returns:
- A helper containing a collection of methods related to
IType
-
getTypeRepository
Returns the type repository for the application.- Returns:
- The repository of
ITypes
-
hasJoins
public boolean hasJoins()Determines whether the JPQL expression has JOIN expressions.- Returns:
trueif the query or subquery being traversed contains JOIN expressions;falseotherwise
-
initialize
Initializes thisJPQLQueryContext.- Parameters:
jpqlGrammar- The grammar that defines how to parse a JPQL query
-
initializeRoot
protected void initializeRoot()Initializes the parsed tree representation of the JPQL query if it has not been set before setting theIQuery. -
isCollectionIdentificationVariable
Determines whether the given identification variable is defining a join or a collection member declaration expressions.- Parameters:
variableName- The identification variable to check for what it maps- Returns:
trueif the given identification variable maps a collection-valued field defined in aJOINorINexpression;falseif it's not defined or it's mapping an abstract schema name
-
isRangeIdentificationVariable
Determines whether the given variable name is an identification variable name used to define an abstract schema name.- Parameters:
variableName- The name of the variable to verify if it's defined in a range variable declaration in the current query or any parent query- Returns:
trueif the variable name is mapping an abstract schema name;falseif it's defined in a collection member declaration
-
isResultVariable
Determines if the given variable is a result variable.- Parameters:
variable- The variable to check if it's a result variable- Returns:
trueif the given variable is defined as a result variable;falseotherwise
-
isSubquery
public boolean isSubquery()Determines whether thisJPQLQueryContextcurrently holds the information of a subquery or for the top-level query.- Returns:
trueif the current context is for a subquery;falsefor the top-level query- Since:
- 2.5
-
isTolerant
public boolean isTolerant()Determines if the parser is in tolerant mode or is in fast mode. When the tolerant is turned on, it means the parser will attempt to parse incomplete or invalid queries.- Returns:
trueif the parsing system should parse invalid or incomplete queries;falsewhen the query is well-formed and valid- Since:
- 2.5
-
literal
Retrieves the "literal" from the givenExpression. The literal to retrieve depends on the giventype. The literal is basically a string value like an identification variable name, an input parameter, a path expression, an abstract schema name, etc.- Parameters:
expression- TheExpressionto visittype- TheLiteralTypehelps to determine what to retrieve from the visitedExpression- Returns:
- A value from the given
Expressionor an empty string if the givenExpressionand theLiteralTypedo not match
-
newSubqueryContext
Changes the state of this context to use the given subquery.- Parameters:
currentQuery- The parsed tree representation of the subquery that will become the current query- See Also:
-
setJPQLExpression
Sets the parsed tree representation of the JPQL query. If the expression was parsed outside of the scope of this context, then this method has to be invoked beforesetQuery(IQuery)because the JPQL query is automatically parsed by that method.- Parameters:
jpqlExpression- The parsed representation of the JPQL query to manipulate- See Also:
-
setQuery
Sets the external form of the JPQL query, which will be parsed and information will be extracted for later access.- Parameters:
query- The external form of the JPQL query- See Also:
-
setTolerant
public void setTolerant(boolean tolerant) Sets whether the parser is in tolerant mode or is in fast mode. When the tolerant is turned on, it means the parser will attempt to parse incomplete or invalid queries.Note: This needs to be set before
setQuery(IQuery)orsetJPQLExpression(JPQLExpression)is invoked.- Parameters:
tolerant- Determines if the parsing system should be tolerant, meaning if it should try to parse invalid or incomplete queries- Since:
- 2.5
-
store
Stores the information contained in the given parent into this one.- Parameters:
parent- The parent context, which is the context of the parent querycurrentQuery- The subquery becoming the current query
-
toString
-