org.apache.derby.impl.sql.compile
Class ExpressionClassBuilder

java.lang.Object
  extended byorg.apache.derby.impl.sql.compile.ExpressionClassBuilder
All Implemented Interfaces:
ExpressionClassBuilderInterface
Direct Known Subclasses:
ActivationClassBuilder

public abstract class ExpressionClassBuilder
extends java.lang.Object
implements ExpressionClassBuilderInterface

ExpressionClassBuilder provides an interface to satisfy generation's common tasks in building classes that involve expressions. This is the common superclass of ActivationClassBuilder and FilterClassBuilder. See the documentation on ActivationClassBuilder.

Author:
Rick Extracted out of ActivationClassBuilder

Field Summary
protected  ClassBuilder cb
           
protected  LocalField cdtField
           
protected  MethodBuilder constructor
           
protected static java.lang.String currentDatetimeFieldName
           
private  java.lang.String currentRowScanResultSetName
           
 MethodBuilder executeMethod
           
protected  GeneratedClass gc
           
private  java.lang.Object getDVF
          Get/reuse the Expression for getting the DataValueFactory
private  java.lang.Object getEF
          This is a utility method to get a common expression -- "BaseActivation.getExecutionFactory()".
private  java.lang.Object getRSF
          This is a utility method to get a common expression -- "BaseActivation.getResultSetFactory()".
 CompilerContext myCompCtx
           
protected  int nextExprNum
           
protected  int nextFieldNum
           
protected  int nextNonFastExpr
           
protected  MethodBuilder resultSetClosedMethod
           
 
Constructor Summary
ExpressionClassBuilder(java.lang.String superClass, java.lang.String className, CompilerContext cc)
          By the time this is done, it has constructed the following class: public class #className extends #superClass { public #className() { super(); } }
 
Method Summary
 FormatableArrayHolder addColumnToOrdering(FormatableArrayHolder orderingHolder, int columnNum)
          Add a column to the existing Ordering list.
 int addItem(java.lang.Object o)
           
private  void addNewArrayOfRows(int numResultSets)
          Generate the assignment for row = new ExecRow[numResultSets]
private  void beginConstructor()
           
abstract  MethodBuilder beginExecuteMethod()
          Build boiler plate for the Execute method
 void finishConstructor()
          Finish the constructor by newing the array of Rows and putting a return at the end of it.
abstract  void finishExecuteMethod(boolean genMarkAsTopNode)
          Finish up the Execute method.
 void generateDataValue(MethodBuilder mb, TypeCompiler tc, LocalField field)
          Generate a data value.
 void generateNull(MethodBuilder mb, TypeCompiler tc)
          Generate a Null data value.
 void generateNullWithExpress(MethodBuilder mb, TypeCompiler tc)
          Generate a Null data value.
abstract  java.lang.String getBaseClassName()
          Return the base class of the activation's hierarchy (the subclass of Object).
 ClassBuilder getClassBuilder()
           
 FormatableArrayHolder getColumnOrdering(OrderedColumnList oclist)
           
 FormatableArrayHolder getColumnOrdering(ResultColumnList rclist)
          These utility methods buffers compilation from the IndexColumnOrder class.
 MethodBuilder getConstructor()
           
 void getCurrentDateExpression(MethodBuilder mb)
          This utility method returns an expression for CURRENT_DATE.
protected  LocalField getCurrentSetup()
           
 void getCurrentTimeExpression(MethodBuilder mb)
          This utility method returns an expression for CURRENT_TIME.
 void getCurrentTimestampExpression(MethodBuilder mb)
          This utility method generates an expression for CURRENT_TIMESTAMP.
 MethodBuilder getExecuteMethod()
          The execute method returns a result set that will evaluate the statement this activation class is the compiled form of.
 GeneratedClass getGeneratedClass(ByteArray savedBytes)
          Take the generated class, and turn it into an actual class.
abstract  java.lang.String getPackageName()
          Get the name of the package that the generated class will live in.
abstract  void getParameterReference(java.lang.String name, int position, DataTypeDescriptor dataType, MethodBuilder mb)
          Generates an expression to refer to a named parameter.
abstract  int getRowCount()
          Get the number of ExecRows that must be allocated
 java.lang.String getRowLocationScanResultSetName()
           
protected  TypeCompiler getTypeCompiler(TypeId typeId)
          Get the TypeCompiler associated with the given TypeId
 MethodBuilder newExprFun()
          "ExprFun"s are the "expression functions" that are specific to a given JSQL statement.
 LocalField newFieldDeclaration(int modifiers, java.lang.String type)
          Add an arbitrarily named field to the generated class.
 LocalField newFieldDeclaration(int modifiers, java.lang.String type, java.lang.String name)
          Add a field declaration to the generated class
private  java.lang.String newFieldName()
          generated the next field name available.
 MethodBuilder newGeneratedFun(java.lang.String returnType, int modifiers)
          Activations might have need of internal functions that are not used by the result sets, but by other activation functions.
 MethodBuilder newGeneratedFun(java.lang.String returnType, int modifiers, java.lang.String[] params)
           
private  MethodBuilder newGeneratedFun(java.lang.String exprName, java.lang.String returnType, int modifiers, java.lang.String[] params)
           
 java.lang.String newRowLocationScanResultSetName()
          generates a variable name for the rowscanresultset.
 MethodBuilder newUserExprFun()
          Start a user expression.
 void pushColumnReference(MethodBuilder mb, int rsNumber, int colId)
          Generate a reference to a colunm in a result set.
 void pushDataValueFactory(MethodBuilder mb)
           
 void pushGetExecutionFactoryExpression(MethodBuilder mb)
           
 void pushGetResultSetFactoryExpression(MethodBuilder mb)
           
 void pushMethodReference(MethodBuilder mb, MethodBuilder exprMethod)
          Push an expression that is a GeneratedMethod reference to the passed in method. aka. a "function pointer".
 void pushPVSReference(MethodBuilder mb)
          Generate a reference to the parameter value set that all activations use.
 void pushResultSetClosedMethodFieldAccess(MethodBuilder mb)
          This utility method returns the resultSetClosed method reference that the activation wants called when a result set closes, to let it clean up.
 void pushThisAsActivation(MethodBuilder mb)
          Get a "this" expression declared as an Activation.
abstract  void setNumSubqueries()
          Sets the number of subqueries under this expression
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

currentDatetimeFieldName

protected static final java.lang.String currentDatetimeFieldName
See Also:
Constant Field Values

cb

protected ClassBuilder cb

gc

protected GeneratedClass gc

nextExprNum

protected int nextExprNum

nextNonFastExpr

protected int nextNonFastExpr

nextFieldNum

protected int nextFieldNum

constructor

protected MethodBuilder constructor

myCompCtx

public CompilerContext myCompCtx

executeMethod

public MethodBuilder executeMethod

cdtField

protected LocalField cdtField

resultSetClosedMethod

protected MethodBuilder resultSetClosedMethod

currentRowScanResultSetName

private java.lang.String currentRowScanResultSetName

getDVF

private java.lang.Object getDVF
Get/reuse the Expression for getting the DataValueFactory


getRSF

private java.lang.Object getRSF
This is a utility method to get a common expression -- "BaseActivation.getResultSetFactory()".

BaseActivation gets the factory from the context and caches it for faster retrieval.


getEF

private java.lang.Object getEF
This is a utility method to get a common expression -- "BaseActivation.getExecutionFactory()". REVISIT: could the same expression objects be reused within the tree and have the correct java generated each time?

BaseActivation gets the factory from the context and caches it for faster retrieval.

Constructor Detail

ExpressionClassBuilder

public ExpressionClassBuilder(java.lang.String superClass,
                              java.lang.String className,
                              CompilerContext cc)
                       throws StandardException
By the time this is done, it has constructed the following class:
    public class #className extends #superClass {
		public #className() { super(); }
    }
 

Throws:
StandardException - thrown on failure
Method Detail

getPackageName

public abstract java.lang.String getPackageName()
Get the name of the package that the generated class will live in.

Returns:
name of package that the generated class will live in.

getRowCount

public abstract int getRowCount()
                         throws StandardException
Get the number of ExecRows that must be allocated

Returns:
number of ExecRows that must be allocated
Throws:
StandardException - thrown on failure

setNumSubqueries

public abstract void setNumSubqueries()
                               throws StandardException
Sets the number of subqueries under this expression

Throws:
StandardException - thrown on failure

getParameterReference

public abstract void getParameterReference(java.lang.String name,
                                           int position,
                                           DataTypeDescriptor dataType,
                                           MethodBuilder mb)
                                    throws StandardException
Generates an expression to refer to a named parameter.

Parameters:
name - Parameter name
position - Parameter number
dataType - Parameter datatype
mb - The method to put the generated code into
Returns:
an expression encoding a reference to the named parameter
Throws:
StandardException - thrown on failure

beginExecuteMethod

public abstract MethodBuilder beginExecuteMethod()
                                          throws StandardException
Build boiler plate for the Execute method

Returns:
a method builder containing boiler plate for the Execute method
Throws:
StandardException - thrown on failure

finishExecuteMethod

public abstract void finishExecuteMethod(boolean genMarkAsTopNode)
                                  throws StandardException
Finish up the Execute method.

Throws:
StandardException - thrown on failure

getBaseClassName

public abstract java.lang.String getBaseClassName()
Return the base class of the activation's hierarchy (the subclass of Object). This class is expected to hold methods used by all compilation code, such as datatype compilation code, e.g. getDataValueFactory.


getConstructor

public MethodBuilder getConstructor()

getClassBuilder

public ClassBuilder getClassBuilder()

getExecuteMethod

public MethodBuilder getExecuteMethod()
The execute method returns a result set that will evaluate the statement this activation class is the compiled form of. REVISIT: do we need to give the caller the ability to touch it directly, or could we wrap the alterations to it in this class?


beginConstructor

private final void beginConstructor()

finishConstructor

public void finishConstructor()
                       throws StandardException
Finish the constructor by newing the array of Rows and putting a return at the end of it.

Returns:
Nothing
Throws:
StandardException - thrown on failure

addNewArrayOfRows

private void addNewArrayOfRows(int numResultSets)
Generate the assignment for row = new ExecRow[numResultSets]

Parameters:
numResultSets - The size of the array.
Returns:
Nothing.

newFieldDeclaration

public LocalField newFieldDeclaration(int modifiers,
                                      java.lang.String type,
                                      java.lang.String name)
Add a field declaration to the generated class

Parameters:
modifiers - The | of the modifier values such as public, static, etc.
type - The type of the field in java language.
name - The name of the field.
Returns:
None.

newFieldDeclaration

public LocalField newFieldDeclaration(int modifiers,
                                      java.lang.String type)
Add an arbitrarily named field to the generated class. This is used to generate fields where the caller doesn't care what the field is named. It is especially useful for generating arbitrary numbers of fields, where the caller doesn't know in advance how many fields will be used. For example, it is used for generating fields to hold intermediate values from expressions.

Parameters:
modifiers - The | of the modifier values such as public, static, etc.
type - The type of the field in java language.
Returns:
The name of the new field

newGeneratedFun

public MethodBuilder newGeneratedFun(java.lang.String returnType,
                                     int modifiers)
Activations might have need of internal functions that are not used by the result sets, but by other activation functions. Thus, we make it possible for functions to be generated directly as well as through the newExprFun interface. newExprFun should be used when a static field pointing to the expression function is needed.

The generated function will generally have a generated name that can be viewed through the MethodBuilder interface. This name is generated to ensure uniqueness from other function names in the activation class. If you pass in a function name, think carefully about whether it will collide with other names.

Parameters:
returnType - the return type of the function
modifiers - the modifiers on the function
See Also:
newExprFun()

newGeneratedFun

public MethodBuilder newGeneratedFun(java.lang.String returnType,
                                     int modifiers,
                                     java.lang.String[] params)

newGeneratedFun

private MethodBuilder newGeneratedFun(java.lang.String exprName,
                                      java.lang.String returnType,
                                      int modifiers,
                                      java.lang.String[] params)

newExprFun

public MethodBuilder newExprFun()
"ExprFun"s are the "expression functions" that are specific to a given JSQL statement. For example, an ExprFun is generated to evaluate the where clause of a select statement and return a boolean result.

All methods return by this are expected to be called via the GeneratedMethod interface. Thus the methods are public and return java.lang.Object.

Once the exprfun has been created, the caller will need to add statements to it, minimally a return statement.

ExprFuns return Object types, since they are invoked through reflection and thus their return type would get wrapped in an object anyway. For example: return java.lang.Boolean, not boolean.


pushMethodReference

public void pushMethodReference(MethodBuilder mb,
                                MethodBuilder exprMethod)
Push an expression that is a GeneratedMethod reference to the passed in method. aka. a "function pointer".


newUserExprFun

public MethodBuilder newUserExprFun()
Start a user expression. The difference between a normal expression (returned by newExprFun) and a user expression is that a user expression catches all exceptions (because we don't want random exceptions thrown from user methods to propagate to the rest of the system.

Returns:
A new MethodBuilder

getCurrentDateExpression

public void getCurrentDateExpression(MethodBuilder mb)
This utility method returns an expression for CURRENT_DATE. Get the expression this way, because the activation needs to generate support information for CURRENT_DATE, that would otherwise be painful to create manually.


getCurrentTimeExpression

public void getCurrentTimeExpression(MethodBuilder mb)
This utility method returns an expression for CURRENT_TIME. Get the expression this way, because the activation needs to generate support information for CURRENT_TIME, that would otherwise be painful to create manually.


getCurrentTimestampExpression

public void getCurrentTimestampExpression(MethodBuilder mb)
This utility method generates an expression for CURRENT_TIMESTAMP. Get the expression this way, because the activation needs to generate support information for CURRENT_TIMESTAMP, that would otherwise be painful to create manually.


getColumnOrdering

public FormatableArrayHolder getColumnOrdering(ResultColumnList rclist)
These utility methods buffers compilation from the IndexColumnOrder class. They create an ordering based on their parameter, stuff that into the prepared statement, and then return the entry # for use in the generated code. We could write another utility method to generate code to turn an entry # back into an object, but so far no-one needs it. WARNING: this is a crafty method that ASSUMES that you want every column in the list ordered, and that every column in the list is the entire actual result colunm. It is only useful for DISTINCT in select.


addColumnToOrdering

public FormatableArrayHolder addColumnToOrdering(FormatableArrayHolder orderingHolder,
                                                 int columnNum)
Add a column to the existing Ordering list. Takes a column id and only adds it if it isn't in the list.

Returns:
the ColumnOrdering array

getColumnOrdering

public FormatableArrayHolder getColumnOrdering(OrderedColumnList oclist)

addItem

public int addItem(java.lang.Object o)

pushDataValueFactory

public void pushDataValueFactory(MethodBuilder mb)

pushGetResultSetFactoryExpression

public void pushGetResultSetFactoryExpression(MethodBuilder mb)

pushGetExecutionFactoryExpression

public void pushGetExecutionFactoryExpression(MethodBuilder mb)

pushResultSetClosedMethodFieldAccess

public void pushResultSetClosedMethodFieldAccess(MethodBuilder mb)
This utility method returns the resultSetClosed method reference that the activation wants called when a result set closes, to let it clean up. This will be null if none was needed. REMIND: because ObjectManager returns exceptions on its invoke() method and close() is not supposed to return exceptions, we may want to move this to be something done on open() instead of on close(). Otherwise, we have to do try/catch/THROWASSERT in the close code, which looks unfriendly.


pushColumnReference

public void pushColumnReference(MethodBuilder mb,
                                int rsNumber,
                                int colId)
Generate a reference to a colunm in a result set.

Parameters:
rsNumber - the result set number
colId - the column number
Returns:
expression

pushPVSReference

public void pushPVSReference(MethodBuilder mb)
Generate a reference to the parameter value set that all activations use.

Returns:
expression

getCurrentSetup

protected LocalField getCurrentSetup()

newFieldName

private java.lang.String newFieldName()
generated the next field name available. these are of the form 'e#', where # is incremented each time. This shares the name space with the expression methods as Java allows names and fields to have the same name. This reduces the number of constant pool entries created for a generated class file.


getTypeCompiler

protected TypeCompiler getTypeCompiler(TypeId typeId)
Get the TypeCompiler associated with the given TypeId

Parameters:
typeId - The TypeId to get a TypeCompiler for
Returns:
The corresponding TypeCompiler

getGeneratedClass

public GeneratedClass getGeneratedClass(ByteArray savedBytes)
                                 throws StandardException
Take the generated class, and turn it into an actual class.

This method assumes, does not check, that the class and its parts are all complete.

Parameters:
savedBytes - place to save generated bytes. if null, it is ignored
Throws:
StandardException - thrown when exception occurs

pushThisAsActivation

public void pushThisAsActivation(MethodBuilder mb)
Get a "this" expression declared as an Activation. This is the commonly used type of the this expression.


generateNull

public void generateNull(MethodBuilder mb,
                         TypeCompiler tc)
Generate a Null data value. Nothing is required on the stack, a SQL null data value is pushed.


generateNullWithExpress

public void generateNullWithExpress(MethodBuilder mb,
                                    TypeCompiler tc)
Generate a Null data value. The express value is required on the stack and will be popped, a SQL null data value is pushed.


generateDataValue

public void generateDataValue(MethodBuilder mb,
                              TypeCompiler tc,
                              LocalField field)
Generate a data value. The value is to be set in the SQL data value is required on the stack and will be popped, a SQL data value is pushed.


newRowLocationScanResultSetName

public java.lang.String newRowLocationScanResultSetName()
generates a variable name for the rowscanresultset. This can not be a fixed name because in cases like cascade delete same activation class will be dealing more than one RowScanResultSets for dependent tables.


getRowLocationScanResultSetName

public java.lang.String getRowLocationScanResultSetName()

Built on Tue 2006-10-10 19:23:47+0200, from revision exported

Apache Derby V10.1 Engine Documentation - Copyright © 1997,2005 The Apache Software Foundation or its licensors, as applicable.