org.jpox.store.rdbms.query
Class JavaQueryCompiler

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

public abstract class JavaQueryCompiler
extends org.jpox.store.query.QueryCompiler
implements org.jpox.store.mapped.expression.UnboundVariable.VariableBinder

Base definition of a query compiler for a Java query language. This is to be extended to provide query compilers for JDOQL, JPQL and any other java-based query languages.

Version:
$Revision$

Field Summary
protected  org.jpox.metadata.AbstractClassMetaData candidateCmd
          ClassMetaData for the candidate.
protected  org.jpox.store.mapped.query.Queryable candidates
          Candidates for the query.
protected  boolean distinct
          Whether we should use distinct for results.
protected  java.util.Map expressionsByVariableName
           
protected  org.jpox.store.mapped.expression.QueryExpression parentExpr
          Parent query expression (if this is a subquery).
protected  org.jpox.store.mapped.expression.QueryExpression qs
          QueryExpression for this query.
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  org.jpox.store.query.JPOXResultSetMetaData 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.jpox.store.rdbms.query.JavaQueryCompiler.AliasJoinInformation subqueryCandidateExprRootAliasInfo
          Alias join info for the subquery candidate expression root (if not using parent query candidate).
 
Fields inherited from class org.jpox.store.query.QueryCompiler
candidateAlias, candidateClass, COMPILE_EXECUTION, COMPILE_EXPLICIT_PARAMETERS, COMPILE_EXPLICIT_VARIABLES, COMPILE_SYNTAX, executionCompile, imports, language, LOCALISER, p, parameterNames, parameters, parameterTypesByName, query, variableNames, variableTypesByName
 
Constructor Summary
JavaQueryCompiler(org.jpox.store.query.AbstractJavaQuery query, org.jpox.util.Imports imports, java.util.Map parameters)
          Constructor for a compiler of java queries.
 
Method Summary
 void bindVariable(java.lang.String name, org.jpox.store.mapped.expression.ScalarExpression expr)
          Bind a variable to the query.
protected  org.jpox.store.mapped.expression.ScalarExpression callUserDefinedScalarExpression(java.lang.String method)
          Instanciate a ScalarExpression and invoke a method
protected  void checkExpressionsAgainstGrouping(org.jpox.store.mapped.expression.ScalarExpression[] exprs, org.jpox.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.jpox.store.mapped.expression.ScalarExpression compileAdditiveExpression()
           
protected abstract  void compileCandidates()
          Convenience method to process the candidates for this query.
protected  org.jpox.store.mapped.expression.ScalarExpression compileCastExpression()
           
protected  org.jpox.store.mapped.expression.ScalarExpression compileExplicitVariable(java.lang.String id)
          Method to compile an explicit variable.
protected abstract  org.jpox.store.mapped.expression.ScalarExpression compileExpression()
          Principal method for compiling an expression.
protected abstract  org.jpox.store.mapped.expression.ScalarExpression compileExpressionFromString(java.lang.String str)
          Convenience method to parse an expression string into its query expression.
protected  org.jpox.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.jpox.store.mapped.expression.QueryExpression qs, java.lang.String filter)
          Convenience method to compile the filter.
protected  void compileGrouping(org.jpox.store.mapped.expression.QueryExpression qs, java.lang.String groupingClause)
          Convenience method to compile the grouping.
protected  void compileHaving(org.jpox.store.mapped.expression.QueryExpression qs, java.lang.String havingClause)
          Convenience method to compile the having clause
protected  org.jpox.store.mapped.expression.ScalarExpression compileLiteral()
          A literal is one value of any type.
protected  org.jpox.store.mapped.expression.ScalarExpression compileMultiplicativeExpression()
           
protected  org.jpox.store.mapped.expression.ScalarExpression compileNewObject()
          Method to generate an expression for a new object.
protected abstract  org.jpox.store.mapped.expression.ScalarExpression compilePrimary()
          Compiles a primary.
protected  void compileRange(org.jpox.store.mapped.expression.QueryExpression qs)
          Convenience method to compile the range.
protected  void compileResult(org.jpox.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.jpox.store.mapped.expression.ScalarExpression compileUnaryExpression()
           
protected  org.jpox.store.mapped.expression.ScalarExpression compileUnaryExpressionNotPlusMinus()
           
protected  org.jpox.store.mapped.expression.QueryExpression executionCompile()
          Method to execution-compile the query.
 org.jpox.store.mapped.query.Queryable getCandidates()
          Accessor for the candidates for the query.
 boolean getDistinct()
          Accessor for whether the candidate result should be distinct.
 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.
 org.jpox.store.query.JPOXResultSetMetaData getResultMetaData()
          Accessor for the result MetaData.
protected abstract  void performCompile(org.jpox.store.mapped.expression.QueryExpression qs)
          Perform the actual compilation of the query.
protected  void preCompile()
          Method to provide pre-compilation checks to catch errors.
 
Methods inherited from class org.jpox.store.query.QueryCompiler
compileExplicitParameters, compileExplicitVariables, getCandidateAlias, getCandidateClass, getParameterNames, getParameterTypesByName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

resultMetaData

protected org.jpox.store.query.JPOXResultSetMetaData resultMetaData
ResultMetaData for the query (set in the compile process, so is null before that).


qs

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


parentExpr

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


expressionsByVariableName

protected java.util.Map expressionsByVariableName

candidateCmd

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


candidates

protected org.jpox.store.mapped.query.Queryable candidates
Candidates for the query.


distinct

protected boolean distinct
Whether we should use distinct for results.


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.jpox.store.rdbms.query.JavaQueryCompiler.AliasJoinInformation subqueryCandidateExprRootAliasInfo
Alias join info for the subquery candidate expression root (if not using parent query candidate).

Constructor Detail

JavaQueryCompiler

public JavaQueryCompiler(org.jpox.store.query.AbstractJavaQuery query,
                         org.jpox.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.

Overrides:
close in class org.jpox.store.query.QueryCompiler

compile

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

Overrides:
compile in class org.jpox.store.query.QueryCompiler
Parameters:
type - Type of compilation
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.jpox.store.mapped.expression.QueryExpression executionCompile()
Method to execution-compile the query. Generates the query and returns it.

Returns:
the execution compiled query

getResultMetaData

public org.jpox.store.query.JPOXResultSetMetaData getResultMetaData()
Accessor for the result MetaData. Will be null until the query is compiled.

Returns:
ResultMetaData for the query

getCandidates

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

Returns:
Candidates for the query

getDistinct

public boolean getDistinct()
Accessor for whether the candidate result should be distinct.

Returns:
Whether we should use distinct

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

performCompile

protected abstract void performCompile(org.jpox.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".


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.jpox.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.jpox.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.jpox.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.jpox.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.jpox.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.jpox.store.mapped.expression.ScalarExpression[] exprs,
                                               org.jpox.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.jpox.store.mapped.expression.ScalarExpression expr)
Bind a variable to the query.

Specified by:
bindVariable in interface org.jpox.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.jpox.exceptions.JPOXUserException - Thrown if a variable is found that is not bound.

compileExpressionsFromString

protected org.jpox.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.jpox.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.jpox.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.jpox.store.mapped.expression.ScalarExpression compileAdditiveExpression()

compileMultiplicativeExpression

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

compileUnaryExpression

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

compileUnaryExpressionNotPlusMinus

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

compileCastExpression

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

compilePrimary

protected abstract org.jpox.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.jpox.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.jpox.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.jpox.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.jpox.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


Copyright © -2008 . All Rights Reserved.