public abstract class JPQLQueryContext
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
protected class |
JPQLQueryContext.InputParameterVisitor
This visitor is responsible to find the
InputParameters with a certain
parameter name. |
protected static class |
JPQLQueryContext.QueryExpressionVisitor
This visitor is responsible to retrieve the
Expression that is the beginning of a
query. |
Modifier and Type | Field and Description |
---|---|
protected JPQLQueryContext |
currentContext
The current
JPQLQueryContext is the context used for the current query or subquery. |
protected JPQLQueryContext |
parent
When this context is a sub-context used for a subquery, then this is the context for the
parent query.
|
Modifier | Constructor and Description |
---|---|
|
JPQLQueryContext(JPQLGrammar jpqlGrammar)
Creates a new
JPQLQueryContext . |
protected |
JPQLQueryContext(JPQLQueryContext parent,
Expression currentQuery)
Creates a new sub-
JPQLQueryContext . |
Modifier and Type | Method and Description |
---|---|
protected DeclarationResolver |
buildDeclarationResolver() |
protected DeclarationResolver |
buildDeclarationResolver(DeclarationResolver parent) |
protected JPQLQueryContext.InputParameterVisitor |
buildInputParameter() |
protected abstract JPQLQueryContext |
buildJPQLQueryContext(JPQLQueryContext currentContext,
Expression currentQuery) |
protected abstract LiteralVisitor |
buildLiteralVisitor() |
protected abstract ParameterTypeVisitor |
buildParameterTypeVisitor() |
protected JPQLQueryContext.QueryExpressionVisitor |
buildQueryExpressionVisitor() |
protected abstract ResolverBuilder |
buildResolverBuilder() |
void |
convertUnqualifiedDeclaration(Declaration declaration)
Converts the given
Declaration from being set as a range variable declaration to
a path expression declaration. |
void |
dispose()
Disposes the internal data.
|
void |
disposeSubqueryContext()
Disposes this context, which is the current context being used by a subquery.
|
java.util.Collection<InputParameter> |
findInputParameters(java.lang.String parameterName)
Retrieves all the
InputParameters with the given parameter name. |
Expression |
getActualCurrentQuery()
Returns the current
Expression being manipulated, which is either the top-level query
or a subquery. |
DeclarationResolver |
getActualDeclarationResolver()
Returns the
DeclarationResolver of this context and not from the current query's
declaration. |
JPQLQueryContext |
getCurrentContext()
Returns the current
JPQLQueryContext , i.e. the context of the query being manipulated,
which can either be the top-level query or a subquery. |
Expression |
getCurrentQuery()
Returns the current
Expression being manipulated, which is either the top-level query
or a subquery. |
Declaration |
getDeclaration(java.lang.String variableName)
Retrieves the
Declaration for which the given variable name is used to navigate to the
"root" object. |
DeclarationResolver |
getDeclarationResolver()
Returns the
DeclarationResolver of the current query's declaration. |
DeclarationResolver |
getDeclarationResolver(Expression expression)
Returns the
DeclarationResolver of the current query's declaration. |
protected DeclarationResolver |
getDeclarationResolverImp()
Returns the
DeclarationResolver of the current query's declaration. |
java.util.List<Declaration> |
getDeclarations()
Returns the ordered list of
Declarations . |
IType |
getEnumType(java.lang.String enumTypeName)
Returns the
IType representing the possible given enum type. |
ExpressionRegistry |
getExpressionRegistry()
Returns the registry containing the
JPQLQueryBNFs and the ExpressionFactories that are used to properly parse a JPQL query. |
JPQLGrammar |
getGrammar()
Returns the JPQL grammar that will be used to define how to parse a JPQL query.
|
protected JPQLQueryContext.InputParameterVisitor |
getInputParameterVisitor() |
JPAVersion |
getJPAVersion()
Returns the version of the Java Persistence to support, which dictates which version of the
JPQL grammar to support.
|
JPQLExpression |
getJPQLExpression()
Returns the parsed tree representation of the JPQL query.
|
java.lang.String |
getJPQLQuery()
Returns the string representation of the JPQL query.
|
protected LiteralVisitor |
getLiteralVisitor() |
IMapping |
getMapping(Expression expression)
Returns the
IMapping for the field represented by the given Expression . |
IType |
getParameterType(InputParameter inputParameter)
Retrieves, if it can be determined, the type of the given
InputParameter . |
protected ParameterTypeVisitor |
getParameterTypeVisitor() |
JPQLQueryContext |
getParent()
Returns the parent context if the current context is not the root context.
|
IManagedTypeProvider |
getProvider()
Retrieves the provider of managed types.
|
java.lang.String |
getProviderVersion()
Returns the version of the persistence provider.
|
IQuery |
getQuery()
Returns the external form of the JPQL query.
|
Expression |
getQueryExpression(Expression expression)
Retrieves the
Expression representing the query statement (either the top-level query
JPQLExpression or the subquery SimpleSelectStatement ) owning the given Expression . |
protected JPQLQueryContext.QueryExpressionVisitor |
getQueryExpressionVisitor() |
Resolver |
getResolver(Expression expression)
Creates or retrieved the cached
Resolver for the given Expression . |
Resolver |
getResolver(java.lang.String variableName)
Retrieves the
Resolver mapped with the given identification variable. |
protected ResolverBuilder |
getResolverBuilder() |
java.util.Set<java.lang.String> |
getResultVariables()
Returns the variables that got defined in the select expression.
|
IType |
getType(java.lang.Class<?> type)
Retrieves the external type for the given Java type.
|
IType |
getType(Expression expression)
Returns the
IType of the given Expression . |
IType |
getType(java.lang.String typeName)
Retrieves the external class with the given fully qualified class name.
|
ITypeDeclaration |
getTypeDeclaration(Expression expression)
Returns the
ITypeDeclaration of the field handled by this Resolver . |
TypeHelper |
getTypeHelper()
Returns a helper that gives access to the most common
types . |
ITypeRepository |
getTypeRepository()
Returns the type repository for the application.
|
boolean |
hasJoins()
Determines whether the JPQL expression has JOIN expressions.
|
protected void |
initialize(JPQLGrammar jpqlGrammar)
Initializes this
JPQLQueryContext . |
protected void |
initializeRoot()
Initializes the parsed tree representation of the JPQL query if it has not been set before
setting the
IQuery . |
boolean |
isCollectionIdentificationVariable(java.lang.String variableName)
Determines whether the given identification variable is defining a join or a collection member
declaration expressions.
|
boolean |
isRangeIdentificationVariable(java.lang.String variableName)
Determines whether the given variable name is an identification variable name used to define
an abstract schema name.
|
boolean |
isResultVariable(java.lang.String variable)
Determines if the given variable is a result variable.
|
boolean |
isSubquery()
Determines whether this
JPQLQueryContext currently holds the information of a subquery
or for the top-level query. |
boolean |
isTolerant()
Determines if the parser is in tolerant mode or is in fast mode.
|
java.lang.String |
literal(Expression expression,
LiteralType type)
Retrieves the "literal" from the given
Expression . |
void |
newSubqueryContext(Expression currentQuery)
Changes the state of this context to use the given subquery.
|
void |
setJPQLExpression(JPQLExpression jpqlExpression)
Sets the parsed tree representation of the JPQL query.
|
void |
setQuery(IQuery query)
Sets the external form of the JPQL query, which will be parsed and information will be
extracted for later access.
|
void |
setTolerant(boolean tolerant)
Sets whether the parser is in tolerant mode or is in fast mode.
|
protected void |
store(JPQLQueryContext parent,
Expression currentQuery)
Stores the information contained in the given parent into this one.
|
java.lang.String |
toString() |
protected JPQLQueryContext currentContext
JPQLQueryContext
is the context used for the current query or subquery.
If the current context is not the global context, then its parent is non null
.protected JPQLQueryContext parent
public JPQLQueryContext(JPQLGrammar jpqlGrammar)
JPQLQueryContext
.jpqlGrammar
- The JPQLGrammar
defines how to parse a JPQL queryprotected JPQLQueryContext(JPQLQueryContext parent, Expression currentQuery)
JPQLQueryContext
.parent
- The parent contextcurrentQuery
- The parsed tree representation of the subqueryprotected DeclarationResolver buildDeclarationResolver()
protected DeclarationResolver buildDeclarationResolver(DeclarationResolver parent)
protected JPQLQueryContext.InputParameterVisitor buildInputParameter()
protected abstract JPQLQueryContext buildJPQLQueryContext(JPQLQueryContext currentContext, Expression currentQuery)
protected abstract LiteralVisitor buildLiteralVisitor()
protected abstract ParameterTypeVisitor buildParameterTypeVisitor()
protected JPQLQueryContext.QueryExpressionVisitor buildQueryExpressionVisitor()
protected abstract ResolverBuilder buildResolverBuilder()
public void convertUnqualifiedDeclaration(Declaration declaration)
Declaration
from 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
).
declaration
- The Declaration
that was parsed to range over an abstract schema
name but is actually ranging over a path expressionpublic void dispose()
public void disposeSubqueryContext()
public java.util.Collection<InputParameter> findInputParameters(java.lang.String parameterName)
InputParameters
with the given parameter name.parameterName
- The parameter used to find the InputParameters
with the same valueInputParameters
that has the given parameter or an
empty collectionpublic Expression getActualCurrentQuery()
Expression
being manipulated, which is either the top-level query
or a subquery.public DeclarationResolver getActualDeclarationResolver()
DeclarationResolver
of this context and not from the current query's
declaration.DeclarationResolver
for this contextpublic JPQLQueryContext getCurrentContext()
JPQLQueryContext
, i.e. the context of the query being manipulated,
which can either be the top-level query or a subquery.public Expression getCurrentQuery()
Expression
being manipulated, which is either the top-level query
or a subquery.public Declaration getDeclaration(java.lang.String variableName)
Declaration
for which the given variable name is used to navigate to the
"root" object. This does not go up the hierarchy when looking for the Declaration
.variableName
- The name of the identification variable that is used to navigate a "root" objectDeclaration
containing the information about the identification variable declaration#findDeclaration(String)
public DeclarationResolver getDeclarationResolver()
DeclarationResolver
of 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 the
FROM
clause.DeclarationResolver
for the current query being visitedpublic DeclarationResolver getDeclarationResolver(Expression expression)
DeclarationResolver
of 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 subquery
FROM
clause.expression
- The Expression
that will be used to retrieve its query expression,
i.e. either JPQLExpression
or SimpleSelectStatement
DeclarationResolver
for the current query being visitedprotected DeclarationResolver getDeclarationResolverImp()
DeclarationResolver
of the current query's declaration.DeclarationResolver
for the current query being visitedpublic java.util.List<Declaration> getDeclarations()
Declarations
.Declarations
of the current query that was parsedpublic IType getEnumType(java.lang.String enumTypeName)
IType
representing the possible given enum type. If the type nameenumTypeName
- The fully qualified enum type with the constantpublic ExpressionRegistry getExpressionRegistry()
JPQLQueryBNFs
and the ExpressionFactories
that are used to properly parse a JPQL query.public JPQLGrammar getGrammar()
Expression
protected JPQLQueryContext.InputParameterVisitor getInputParameterVisitor()
public JPAVersion getJPAVersion()
public JPQLExpression getJPQLExpression()
public java.lang.String getJPQLQuery()
protected LiteralVisitor getLiteralVisitor()
public IMapping getMapping(Expression expression)
IMapping
for the field represented by the given Expression
.expression
- The Expression
representing a state field path expression or a
collection-valued path expressionIMapping
or null
if none existspublic IType getParameterType(InputParameter inputParameter)
InputParameter
. The type
will be guessed based on its location within expression.
Note: Both named and positional input parameter can be used.
inputParameter
- The InputParameter
to retrieve its typenull
if the type could
not be determinedprotected ParameterTypeVisitor getParameterTypeVisitor()
public JPQLQueryContext getParent()
null
if the current context is the rootpublic IManagedTypeProvider getProvider()
public java.lang.String getProviderVersion()
public IQuery getQuery()
public Expression getQueryExpression(Expression expression)
Expression
representing the query statement (either the top-level query
JPQLExpression
or the subquery SimpleSelectStatement
) owning the given Expression
.expression
- A child of the "root" Expression
to retrieveExpression
protected JPQLQueryContext.QueryExpressionVisitor getQueryExpressionVisitor()
public Resolver getResolver(Expression expression)
Resolver
for the given Expression
. The
Resolver
can return the IType
and ITypeDeclaration
of the Expression
and either the IManagedType
or the IMapping
.expression
- The Expression
for which its Resolver
will be retrievedResolver
for the given Expression
public Resolver getResolver(java.lang.String variableName)
Resolver
mapped 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.protected ResolverBuilder getResolverBuilder()
public java.util.Set<java.lang.String> getResultVariables()
public IType getType(java.lang.Class<?> type)
type
- The Java type to wrap with an external formpublic IType getType(Expression expression)
IType
of the given Expression
.expression
- The Expression
for which its type will be calculatedIType
that was resolved by this Resolver
or the
IType
for IType.UNRESOLVABLE_TYPE
if it could not be resolvedpublic IType getType(java.lang.String typeName)
typeName
- The fully qualified class name of the class to retrievepublic ITypeDeclaration getTypeDeclaration(Expression expression)
ITypeDeclaration
of the field handled by this Resolver
.expression
- The Expression
for which its type declaration will be calculatedITypeDeclaration
that was resolved by this Resolver
or the
ITypeDeclaration
for IType.UNRESOLVABLE_TYPE
if it could not be resolvedpublic TypeHelper getTypeHelper()
types
.IType
public ITypeRepository getTypeRepository()
ITypes
public boolean hasJoins()
true
if the query or subquery being traversed contains JOIN
expressions; false
otherwiseprotected void initialize(JPQLGrammar jpqlGrammar)
JPQLQueryContext
.jpqlGrammar
- The grammar that defines how to parse a JPQL queryprotected void initializeRoot()
IQuery
.public boolean isCollectionIdentificationVariable(java.lang.String variableName)
variableName
- The identification variable to check for what it mapstrue
if the given identification variable maps a collection-valued field
defined in a JOIN
or IN
expression; false
if it's not
defined or it's mapping an abstract schema namepublic boolean isRangeIdentificationVariable(java.lang.String variableName)
variableName
- The name of the variable to verify if it's defined in a range variable
declaration in the current query or any parent querytrue
if the variable name is mapping an abstract schema name; false
if it's defined in a collection member declarationpublic boolean isResultVariable(java.lang.String variable)
variable
- The variable to check if it's a result variabletrue
if the given variable is defined as a result variable; false
otherwisepublic boolean isSubquery()
JPQLQueryContext
currently holds the information of a subquery
or for the top-level query.true
if the current context is for a subquery; false
for the
top-level querypublic boolean isTolerant()
true
if the parsing system should parse invalid or incomplete queries;
false
when the query is well-formed and validpublic java.lang.String literal(Expression expression, LiteralType type)
Expression
. The literal to retrieve depends on
the given type
. The literal is basically a string value like an
identification variable name, an input parameter, a path expression, an abstract schema name,
etc.expression
- The Expression
to visittype
- The LiteralType
helps to determine what to retrieve from the visited
Expression
Expression
or an empty string if the given Expression
and the LiteralType
do not matchpublic void newSubqueryContext(Expression currentQuery)
currentQuery
- The parsed tree representation of the subquery that will become the
current querydisposeSubqueryContext()
public void setJPQLExpression(JPQLExpression jpqlExpression)
setQuery(IQuery)
because the JPQL query is automatically parsed by that method.jpqlExpression
- The parsed representation of the JPQL query to manipulatesetQuery(IQuery)
public void setQuery(IQuery query)
query
- The external form of the JPQL querysetJPQLExpression(JPQLExpression)
public void setTolerant(boolean tolerant)
Note: This needs to be set before setQuery(IQuery)
or setJPQLExpression(JPQLExpression)
is invoked.
tolerant
- Determines if the parsing system should be tolerant, meaning if it should try
to parse invalid or incomplete queriesprotected void store(JPQLQueryContext parent, Expression currentQuery)
parent
- The parent context, which is the context of the parent querycurrentQuery
- The subquery becoming the current querypublic java.lang.String toString()
toString
in class java.lang.Object