org.datanucleus.store.rdbms.query
Class QueryCompiler

java.lang.Object
  extended by org.datanucleus.store.rdbms.query.QueryCompiler
All Implemented Interfaces:
org.datanucleus.store.mapped.expression.UnboundVariable.VariableBinder
Direct Known Subclasses:
JDOQLQueryCompiler, JPQLQueryCompiler

public abstract class QueryCompiler
extends java.lang.Object
implements org.datanucleus.store.mapped.expression.UnboundVariable.VariableBinder

Base definition of a query compiler for a query language. Should be extended to compile details of individual query languages. This was designed for the old-style compilation process, and is being phased out. New implementations of querying in DataNucleus should make use of compilers under "org.datanucleus.query.compiler".


Field Summary
protected  java.lang.String candidateAlias
          Alias for the candidate class.
protected  java.lang.Class candidateClass
          Candidate class for the query.
protected  org.datanucleus.metadata.AbstractClassMetaData candidateCmd
          ClassMetaData for the candidate.
protected  org.datanucleus.store.mapped.expression.Queryable candidates
          Candidates for the query.
static int COMPILE_EXECUTION
           
static int COMPILE_EXPLICIT_PARAMETERS
           
static int COMPILE_EXPLICIT_VARIABLES
           
static int COMPILE_SYNTAX
           
protected  boolean executionCompile
          Flag for whether the current compile is for execution (using param values).
protected  java.util.Map<java.lang.String,org.datanucleus.store.mapped.expression.ScalarExpression> expressionsByVariableName
           
protected  org.datanucleus.util.Imports imports
          Imports to use for the resolution of classes.
protected  java.lang.String language
          Language of this query (e.g JDOQL, JPQL).
protected static org.datanucleus.util.Localiser LOCALISER
          Localiser for messages.
protected  Parser p
          Parser for the query.
protected  java.util.List<java.lang.String> parameterNames
          The parameter names.
protected  java.util.Map parameters
          Map of parameter values, keyed by their name.
protected  java.util.Map<java.lang.String,java.lang.Class> parameterTypesByName
          Look-up for the parameter types, keyed by the name.
protected  org.datanucleus.store.mapped.expression.QueryExpression parentExpr
          Parent query expression (if this is a subquery).
protected  org.datanucleus.store.mapped.expression.QueryExpression qs
          QueryExpression for this query.
protected  org.datanucleus.store.query.Query query
          The query being compiled.
protected  long rangeFromIncl
          Range from position (inclusive).
protected  long rangeToExcl
          Range to position (exclusive).
protected  java.lang.Class resultClass
          Result class to be used.
protected  QueryResultsMetaData resultMetaData
          ResultMetaData for the query (set in the compile process, so is null before that).
protected  java.lang.String subqueryCandidateExpr
          Candidate expression when treating as a subquery.
protected  org.datanucleus.store.rdbms.query.QueryCompiler.AliasJoinInformation subqueryCandidateExprRootAliasInfo
          Alias join info for the subquery candidate expression root (if not using parent query candidate).
protected static java.util.Map userDefinedScalarExpressions
          Register of user-defined ScalarExpression, provided via plugins.
protected  java.util.List<java.lang.String> variableNames
          List of variable names.
protected  java.util.Map<java.lang.String,java.lang.Class> variableTypesByName
          Look-up for the variables types, keyed by the name.
 
Constructor Summary
QueryCompiler(org.datanucleus.store.query.Query query, org.datanucleus.util.Imports imports, java.util.Map parameters)
          Constructor for a compiler of java queries.
 
Method Summary
 void bindVariable(java.lang.String name, org.datanucleus.store.mapped.expression.ScalarExpression expr)
          Bind a variable to the query.
protected  org.datanucleus.store.mapped.expression.ScalarExpression callUserDefinedScalarExpression(java.lang.String method)
          Instanciate a ScalarExpression and invoke a method
protected  void checkExpressionsAgainstGrouping(org.datanucleus.store.mapped.expression.ScalarExpression[] exprs, org.datanucleus.store.mapped.expression.ScalarExpression[] groupExprs, java.lang.String localiserErrorString)
          Convenience method to check the expressions against those specified in the grouping.
protected  void checkVariableBinding()
          Convenience method to check that all variables have been bound to the query.
 void close()
          Method to close the Compiler.
 java.lang.Object compile(int type)
          Method to compile the query.
protected  org.datanucleus.store.mapped.expression.ScalarExpression compileAdditiveExpression()
           
protected abstract  void compileCandidates()
          Convenience method to process the candidates for this query.
protected  org.datanucleus.store.mapped.expression.ScalarExpression compileCastExpression()
           
protected  void compileExplicitParameters()
          Method to compile all parameters declared for this query.
protected  org.datanucleus.store.mapped.expression.ScalarExpression compileExplicitVariable(java.lang.String id)
          Method to compile an explicit variable.
protected  void compileExplicitVariables()
          Method to compile all variables declared for this query.
protected abstract  org.datanucleus.store.mapped.expression.ScalarExpression compileExpression()
          Principal method for compiling an expression.
protected abstract  org.datanucleus.store.mapped.expression.ScalarExpression compileExpressionFromString(java.lang.String str)
          Convenience method to parse an expression string into its query expression.
protected  org.datanucleus.store.mapped.expression.ScalarExpression[] compileExpressionsFromString(java.lang.String str)
          Convenience method to parse an expression string into its component query expressions.
protected  void compileFilter(org.datanucleus.store.mapped.expression.QueryExpression qs, java.lang.String filter)
          Convenience method to compile the filter.
protected  void compileGrouping(org.datanucleus.store.mapped.expression.QueryExpression qs, java.lang.String groupingClause)
          Convenience method to compile the grouping.
protected  void compileHaving(org.datanucleus.store.mapped.expression.QueryExpression qs, java.lang.String havingClause)
          Convenience method to compile the having clause
protected  org.datanucleus.store.mapped.expression.ScalarExpression compileLiteral()
          A literal is one value of any type.
protected  org.datanucleus.store.mapped.expression.ScalarExpression compileMultiplicativeExpression()
           
protected  org.datanucleus.store.mapped.expression.ScalarExpression compileNewObject()
          Method to generate an expression for a new object.
protected abstract  org.datanucleus.store.mapped.expression.ScalarExpression compilePrimary()
          Compiles a primary.
protected  void compileRange(org.datanucleus.store.mapped.expression.QueryExpression qs)
          Convenience method to compile the range.
protected  void compileResult(org.datanucleus.store.mapped.expression.QueryExpression qs, java.lang.String result)
          Compile the result expressions and class.
protected  void compileSubqueryCandidateExpression(boolean caseSensitive)
           Method to process any "" when this is a subquery.
protected  org.datanucleus.store.mapped.expression.ScalarExpression compileUnaryExpression()
           
protected  org.datanucleus.store.mapped.expression.ScalarExpression compileUnaryExpressionNotPlusMinus()
           
protected  org.datanucleus.store.mapped.expression.QueryExpression executionCompile()
          Method to execution-compile the query.
 java.lang.String getCandidateAlias()
          Accessor for the candidate alias.
 java.lang.Class getCandidateClass()
          Accessor for the candidate class.
 org.datanucleus.store.mapped.expression.Queryable getCandidates()
          Accessor for the candidates for the query.
 java.lang.String[] getParameterNames()
          Accessor for the (explicit) parameter names.
 java.util.Map getParameterTypesByName()
          Accessor for the parameter types keyed by the parameter name.
 long getRangeFromIncl()
          Accessor for the range "from" value.
 long getRangeToExcl()
          Accessor for the range "to" value.
 java.lang.Class getResultClass()
          Accessor for the result class.
 QueryResultsMetaData getResultMetaData()
          Accessor for the result MetaData.
static java.util.Map getUserDefinedScalarExpressions()
          Accessor for the user-defined scalar expressions.
protected abstract  void performCompile(org.datanucleus.store.mapped.expression.QueryExpression qs)
          Perform the actual compilation of the query.
protected  void preCompile()
          Method to provide pre-compilation checks to catch errors.
static void registerScalarExpression(java.lang.Class literal, java.lang.Class scalarExpressionClass, java.lang.String name)
          Register ScalarExpressions for the given cls.
protected  void registerScalarExpressions(org.datanucleus.plugin.PluginManager pluginMgr, org.datanucleus.ClassLoaderResolver clr)
          Register ScalarExpression classes delcared as plug-ins extensions
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMPILE_EXPLICIT_PARAMETERS

public static final int COMPILE_EXPLICIT_PARAMETERS
See Also:
Constant Field Values

COMPILE_EXPLICIT_VARIABLES

public static final int COMPILE_EXPLICIT_VARIABLES
See Also:
Constant Field Values

COMPILE_SYNTAX

public static final int COMPILE_SYNTAX
See Also:
Constant Field Values

COMPILE_EXECUTION

public static final int COMPILE_EXECUTION
See Also:
Constant Field Values

LOCALISER

protected static final org.datanucleus.util.Localiser LOCALISER
Localiser for messages.


language

protected java.lang.String language
Language of this query (e.g JDOQL, JPQL).


query

protected org.datanucleus.store.query.Query query
The query being compiled.


imports

protected org.datanucleus.util.Imports imports
Imports to use for the resolution of classes.


parameters

protected java.util.Map parameters
Map of parameter values, keyed by their name.


executionCompile

protected boolean executionCompile
Flag for whether the current compile is for execution (using param values).


candidateClass

protected java.lang.Class candidateClass
Candidate class for the query. May be updated during the compilation.


candidateAlias

protected java.lang.String candidateAlias
Alias for the candidate class.


parameterNames

protected java.util.List<java.lang.String> parameterNames
The parameter names.


parameterTypesByName

protected java.util.Map<java.lang.String,java.lang.Class> parameterTypesByName
Look-up for the parameter types, keyed by the name.


variableNames

protected java.util.List<java.lang.String> variableNames
List of variable names.


variableTypesByName

protected java.util.Map<java.lang.String,java.lang.Class> variableTypesByName
Look-up for the variables types, keyed by the name.


resultMetaData

protected QueryResultsMetaData resultMetaData
ResultMetaData for the query (set in the compile process, so is null before that).


qs

protected org.datanucleus.store.mapped.expression.QueryExpression qs
QueryExpression for this query. Generated by the compile.


parentExpr

protected org.datanucleus.store.mapped.expression.QueryExpression parentExpr
Parent query expression (if this is a subquery).


expressionsByVariableName

protected java.util.Map<java.lang.String,org.datanucleus.store.mapped.expression.ScalarExpression> expressionsByVariableName

p

protected Parser p
Parser for the query.


candidateCmd

protected org.datanucleus.metadata.AbstractClassMetaData candidateCmd
ClassMetaData for the candidate.


candidates

protected org.datanucleus.store.mapped.expression.Queryable candidates
Candidates for the query.


resultClass

protected java.lang.Class resultClass
Result class to be used. May be updated during the compilation.


rangeFromIncl

protected long rangeFromIncl
Range from position (inclusive).


rangeToExcl

protected long rangeToExcl
Range to position (exclusive).


subqueryCandidateExpr

protected java.lang.String subqueryCandidateExpr
Candidate expression when treating as a subquery.


subqueryCandidateExprRootAliasInfo

protected org.datanucleus.store.rdbms.query.QueryCompiler.AliasJoinInformation subqueryCandidateExprRootAliasInfo
Alias join info for the subquery candidate expression root (if not using parent query candidate).


userDefinedScalarExpressions

protected static transient java.util.Map userDefinedScalarExpressions
Register of user-defined ScalarExpression, provided via plugins.

Constructor Detail

QueryCompiler

public QueryCompiler(org.datanucleus.store.query.Query query,
                     org.datanucleus.util.Imports imports,
                     java.util.Map parameters)
Constructor for a compiler of java queries.

Parameters:
query - The query to compile
imports - The imports to use
parameters - Any parameters
Method Detail

close

public void close()
Method to close the Compiler.


compile

public java.lang.Object compile(int type)
Method to compile the query.

Parameters:
type - Type of compilation. This compiler only supports explicit parameters, explicit variables
Returns:
the compilation artifact (if any)

preCompile

protected void preCompile()
Method to provide pre-compilation checks to catch errors. This is performed when calling the JDO Query.compile() method.


executionCompile

protected org.datanucleus.store.mapped.expression.QueryExpression executionCompile()
Method to execution-compile the query. Generates the query and returns it.

Returns:
the execution compiled query

performCompile

protected abstract void performCompile(org.datanucleus.store.mapped.expression.QueryExpression qs)
Perform the actual compilation of the query.

Parameters:
qs - The QueryExpression to use during compilation (if required)

compileCandidates

protected abstract void compileCandidates()
Convenience method to process the candidates for this query. Processes the "candidateClassName" and "candidateClass" and sets up "candidates".


getResultMetaData

public QueryResultsMetaData getResultMetaData()
Accessor for the result MetaData. Will be null until the query is compiled.

Returns:
ResultMetaData for the query

getCandidates

public org.datanucleus.store.mapped.expression.Queryable getCandidates()
Accessor for the candidates for the query.

Returns:
Candidates for the query

getResultClass

public java.lang.Class getResultClass()
Accessor for the result class. May have been updated during the compile process.

Returns:
Result class

getRangeFromIncl

public long getRangeFromIncl()
Accessor for the range "from" value. May have been set during compilation where the "from" was an expression.

Returns:
Range "from" value

getRangeToExcl

public long getRangeToExcl()
Accessor for the range "to" value. May have been set during compilation where the "to" was an expression.

Returns:
Range "to" value

getCandidateClass

public java.lang.Class getCandidateClass()
Accessor for the candidate class. May have been updated during the compile process.

Returns:
Candidate class

getCandidateAlias

public java.lang.String getCandidateAlias()
Accessor for the candidate alias.

Returns:
Candidate alias

getParameterNames

public java.lang.String[] getParameterNames()
Accessor for the (explicit) parameter names.

Returns:
Parameter names

getParameterTypesByName

public java.util.Map getParameterTypesByName()
Accessor for the parameter types keyed by the parameter name. Generated during compile of explicit parameters.

Returns:
Map of parameter type keyed by name

compileExplicitParameters

protected void compileExplicitParameters()
Method to compile all parameters declared for this query. Takes the input "parameters" and populates "parameterNames", "parameterTypesByName" for convenience.


compileExplicitVariables

protected void compileExplicitVariables()
Method to compile all variables declared for this query. Takes the input "variables" and populates "variableNames", "variableTypesByName" for convenience.


compileSubqueryCandidateExpression

protected void compileSubqueryCandidateExpression(boolean caseSensitive)

Method to process any "" when this is a subquery. Converts the candidate expression into a series of INNER JOINs from the subquery candidate table back to the candidate table of the outer query. For example,

this.department.employees
would result in a table expression for the Department table being added to this subquery FROM, with an INNER JOIN to the subquery candidate table, and an AND condition from the Department table back to the outer query candidate table.

The subqueryCandidateExpr should either start "this." (hence from the parent query candidate) or, if not, the "subqueryCandidateExprRootAliasInfo" will be set and will define the candidate root of the subquery candidate expression. This second use is used in JPQL where we have a subquery using a field of another alias from the parent query e.g

 SELECT c FROM Customer c JOIN c.orders o WHERE EXISTS (SELECT o FROM o.lineItems l where l.quantity > 3)
 
so "o" is the root alias for the subquery, and the "subqueryCandidateExprRootAliasInfo" will define the table expression in the parent query, and the class it represents.


compileFilter

protected void compileFilter(org.datanucleus.store.mapped.expression.QueryExpression qs,
                             java.lang.String filter)
Convenience method to compile the filter. Processes the filter and updates the QueryExpression accordingly.

Parameters:
qs - The Query Expression to apply the filter to (if specified)
filter - The filter specification

compileResult

protected void compileResult(org.datanucleus.store.mapped.expression.QueryExpression qs,
                             java.lang.String result)
Compile the result expressions and class.

Parameters:
qs - Query Expression to apply the result to (if required)
result - Result clause to compile

compileGrouping

protected void compileGrouping(org.datanucleus.store.mapped.expression.QueryExpression qs,
                               java.lang.String groupingClause)
Convenience method to compile the grouping. Compiles the grouping definition and applies it to the passed Query Expression as appropriate.

Parameters:
qs - The QueryExpression to update (if specified)
groupingClause - The grouping clause string

compileHaving

protected void compileHaving(org.datanucleus.store.mapped.expression.QueryExpression qs,
                             java.lang.String havingClause)
Convenience method to compile the having clause

Parameters:
qs - The QueryExpression to update (if specified)
havingClause - The having clause string

compileRange

protected void compileRange(org.datanucleus.store.mapped.expression.QueryExpression qs)
Convenience method to compile the range. Compiles any range string and extracts the fromInclNo, toExclNo as appropriate.

Parameters:
qs - QueryExpression to apply the range to (if specified)

checkExpressionsAgainstGrouping

protected void checkExpressionsAgainstGrouping(org.datanucleus.store.mapped.expression.ScalarExpression[] exprs,
                                               org.datanucleus.store.mapped.expression.ScalarExpression[] groupExprs,
                                               java.lang.String localiserErrorString)
Convenience method to check the expressions against those specified in the grouping. Throws a JPOXUserException if one of the expressions is not present in the grouping expressions.

Parameters:
exprs - The expressions to check
groupExprs - The grouping expressions
localiserErrorString - Name of a localiser error message to throw as the JPOXUserException message.

bindVariable

public void bindVariable(java.lang.String name,
                         org.datanucleus.store.mapped.expression.ScalarExpression expr)
Bind a variable to the query.

Specified by:
bindVariable in interface org.datanucleus.store.mapped.expression.UnboundVariable.VariableBinder
Parameters:
name - Name of the variable
expr - The expression

checkVariableBinding

protected void checkVariableBinding()
Convenience method to check that all variables have been bound to the query.

Throws:
org.datanucleus.exceptions.NucleusUserException - Thrown if a variable is found that is not bound.

compileExpressionsFromString

protected org.datanucleus.store.mapped.expression.ScalarExpression[] compileExpressionsFromString(java.lang.String str)
Convenience method to parse an expression string into its component query expressions. This splits expressions at comma boundaries, whilst respecting that the string could include expressions like "new MyClass(a, b, c)" and so keeping braced arguments together. If it wasn't for this requirement we would have been able to just use a StringTokenizer.

Parameters:
str - The string
Returns:
The query expressions for the passed string

compileExpressionFromString

protected abstract org.datanucleus.store.mapped.expression.ScalarExpression compileExpressionFromString(java.lang.String str)
Convenience method to parse an expression string into its query expression.

Parameters:
str - The string
Returns:
The query expression for the passed string

compileExpression

protected abstract org.datanucleus.store.mapped.expression.ScalarExpression compileExpression()
Principal method for compiling an expression. An expression could be the filter, the range, the result, etc.

Returns:
The compiled expression

compileAdditiveExpression

protected org.datanucleus.store.mapped.expression.ScalarExpression compileAdditiveExpression()

compileMultiplicativeExpression

protected org.datanucleus.store.mapped.expression.ScalarExpression compileMultiplicativeExpression()

compileUnaryExpression

protected org.datanucleus.store.mapped.expression.ScalarExpression compileUnaryExpression()

compileUnaryExpressionNotPlusMinus

protected org.datanucleus.store.mapped.expression.ScalarExpression compileUnaryExpressionNotPlusMinus()

compileCastExpression

protected org.datanucleus.store.mapped.expression.ScalarExpression compileCastExpression()

compilePrimary

protected abstract org.datanucleus.store.mapped.expression.ScalarExpression compilePrimary()
Compiles a primary. First look for a literal (e.g. "text"), then an identifier(e.g. variable). In the next step, call a function, if executing a function, on the literal or the identifier found.

Returns:
Scalar Expression for the primary compiled expression

compileLiteral

protected org.datanucleus.store.mapped.expression.ScalarExpression compileLiteral()
A literal is one value of any type. Supported literals are of types String, Floating Point, Integer, Character, Boolean and null e.g. 'J', "String", 1, 1.8, true, false, null.

Returns:
The compiled literal

compileNewObject

protected org.datanucleus.store.mapped.expression.ScalarExpression compileNewObject()
Method to generate an expression for a new object. Parser has just parsed "new" and what follows is of the form
 new MyObject(param1, param2)
 

Returns:
Expression for the new object

compileExplicitVariable

protected org.datanucleus.store.mapped.expression.ScalarExpression compileExplicitVariable(java.lang.String id)
Method to compile an explicit variable. Identifier passed in is a known explicit variable name.

Parameters:
id - Identifier of the variable
Returns:
Variable expression

callUserDefinedScalarExpression

protected org.datanucleus.store.mapped.expression.ScalarExpression callUserDefinedScalarExpression(java.lang.String method)
Instanciate a ScalarExpression and invoke a method

Parameters:
method - the method name prefixed by the class name (fully qualified or not)
Returns:
the ScalarExpression instance

registerScalarExpressions

protected void registerScalarExpressions(org.datanucleus.plugin.PluginManager pluginMgr,
                                         org.datanucleus.ClassLoaderResolver clr)
Register ScalarExpression classes delcared as plug-ins extensions

Parameters:
pluginMgr - The PluginManager
clr - The ClassLoaderResolver to load the literal and ScalarExpression classes

registerScalarExpression

public static void registerScalarExpression(java.lang.Class literal,
                                            java.lang.Class scalarExpressionClass,
                                            java.lang.String name)
Register ScalarExpressions for the given cls. It allows to perform operations in the query on cls.method([arglist]).

Parameters:
literal - the class providing the operations; e.g. java.lang.Math.class
scalarExpressionClass - the class with the corresponding ScalarExpression. eg. org.datanucleus.store.expression.MathExpression.class
name - alternative name of the given literal class

getUserDefinedScalarExpressions

public static java.util.Map getUserDefinedScalarExpressions()
Accessor for the user-defined scalar expressions.

Returns:
Map of user-defined scalar expressions


Copyright © 2009. All Rights Reserved.