ModSQL
Class AbstractFunction

java.lang.Object
  |
  +--ModSQL.AbstractFunction
All Implemented Interfaces:
Function
Direct Known Subclasses:
AbstractAggregate, Function_Math, Function_NVL, Function_String, LiteralRow, Operator

public abstract class AbstractFunction
extends Object
implements Function

Abstract implementation of Function to simplify the implementation of real functions.

Author:
chris.studholme@utoronto.ca

Field Summary
protected  boolean all_constant
          Are all parameters constant?
protected  boolean constant_non_null
          Are all of the constant parameters non-null?
protected  int[] evaluate_order
          Order in which to evaluate parameters.
protected  ArrayList evaluate_order_array
          Parameter ordering before optimize.
protected  boolean out_of_order
          Are the parameters out-of-order? Used by toString().
protected  boolean[] parameter_constant
          Array to keep track of which parameters are constant.
protected  Object[] parameter_value
          Array for storing parameter values (constant or otherwise).
protected  Function[] parameters
          Array of parameters after optimize.
protected  ArrayList parameters_array
          Array of parameters before optimize.
 
Fields inherited from interface ModSQL.Function
MATCH_BEGINS, MATCH_EQU, MATCH_GT, MATCH_GTE, MATCH_LT, MATCH_LTE, MATCH_NE
 
Constructor Summary
AbstractFunction()
          Default constructor.
 
Method Summary
 void addParameter(Function item)
          Adds a parameter to the list of parameters maintained by this function.
static Object convertToSQLType(Object o, int type)
          Convert an Object to a compatible Object corresponding to the provided SQL type.
 Object evaluate(int match_op, Object match_value)
          Default implementation that simply calls evaluate(false).
 boolean evaluateConstantParameters()
          Evaluate parameters that advertise themselves as constant.
 void evaluateOrder(int index, int order)
          Specify the order in which the parameters should be evaluated.
protected  boolean evaluateParameters(boolean aggregate)
          Same as above version, but with stop_at_null set to false.
protected  boolean evaluateParameters(boolean aggregate, boolean stop_at_null)
          Evaluates all parameters and stores the results in the parameter_value array.
abstract  String functionName()
          Returns the name of this function for use by the toString() method.
static int getCompatableType(int t1, int t2)
          Determine a java.sql.Types type that is compatable with both of the passed parameters.
static int getObjectSQLType(Object o)
          Determine the SQL type of a java Object.
 Function getParameter(int index)
          Get a particular parameter.
 int getParameterCount()
          Get the number of parameters currently supplied to this function.
 boolean isAggregate()
          The implementation of this method is intended to be used by non-aggregate functions.
 boolean isConstant()
          Determine if this function returns a constant value.
 void optimize()
          This method sets up static arrays for the parameters and their values, calls optimize() for each parameter, and initializes the evalute_order array to appropriate values (using information provided using evaluateOrder(), if any).
 String postfixParameters()
          Optional String to insert after parameter list in toString().
 String prefixParameters()
          Optional String to insert before parameter list in toString().
 void registerWith(Object o)
          This implementation simply calls registerWith() for all parameters.
 void reset()
          This implementation simply calls reset() for all parameters and is intended to be used by non-aggregate functions.
 String toString()
          Outputs "functionName(parameter,...)".
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 
Methods inherited from interface ModSQL.Function
evaluate, getMaxResultSize, getSQLType
 

Field Detail

parameters_array

protected ArrayList parameters_array
Array of parameters before optimize.

evaluate_order_array

protected ArrayList evaluate_order_array
Parameter ordering before optimize.

parameters

protected Function[] parameters
Array of parameters after optimize.

parameter_value

protected Object[] parameter_value
Array for storing parameter values (constant or otherwise).

parameter_constant

protected boolean[] parameter_constant
Array to keep track of which parameters are constant.

all_constant

protected boolean all_constant
Are all parameters constant?

constant_non_null

protected boolean constant_non_null
Are all of the constant parameters non-null?

evaluate_order

protected int[] evaluate_order
Order in which to evaluate parameters. Each entry is an index into the parameters array.

out_of_order

protected boolean out_of_order
Are the parameters out-of-order? Used by toString().
Constructor Detail

AbstractFunction

public AbstractFunction()
Default constructor. Initializes pre-optimize arrays.
Method Detail

getParameterCount

public int getParameterCount()
Get the number of parameters currently supplied to this function.
Specified by:
getParameterCount in interface Function
Returns:
number of parameters

getParameter

public Function getParameter(int index)
Get a particular parameter.
Specified by:
getParameter in interface Function
Parameters:
index - index of parameter to get
Returns:
parameter (function)

addParameter

public void addParameter(Function item)
                  throws SQLException
Adds a parameter to the list of parameters maintained by this function.
Specified by:
addParameter in interface Function
Parameters:
item - function to add
Throws:
SQLException - if optimize has already been called

evaluateOrder

public void evaluateOrder(int index,
                          int order)
                   throws SQLException
Specify the order in which the parameters should be evaluated.
Specified by:
evaluateOrder in interface Function
Parameters:
index - index of parameter
order - number indicating order in which parameter is evaluated
Throws:
SQLException - if optimize has already been called

registerWith

public void registerWith(Object o)
                  throws SQLException
This implementation simply calls registerWith() for all parameters.
Specified by:
registerWith in interface Function
Parameters:
o - object to register with
Throws:
SQLException - if an error occurs

optimize

public void optimize()
              throws SQLException

This method sets up static arrays for the parameters and their values, calls optimize() for each parameter, and initializes the evalute_order array to appropriate values (using information provided using evaluateOrder(), if any).

The all_constant variable is initialized to true if and only if the number of parameters is 0. The constant_non_null variable is initialized to true. The parameter_constant array is not initialized. Use evaluateConstantParameters() after optimize() to initialize these variables to more useful values.

Specified by:
optimize in interface Function
Throws:
SQLException - if an error occurs

evaluateConstantParameters

public boolean evaluateConstantParameters()
                                   throws SQLException

Evaluate parameters that advertise themselves as constant. This method initializes the parameter_constant, all_constant, and constant_non_null variables to their correct values.

This method may only be used after optimize() has been called. Typically, a class that overrides optimize() will first call super.optimize() and then use this method later within their new optimize() implementation.

Returns:
true if all constant parameters were non-null
Throws:
SQLException - if an error occurs while evaluating a parameter

functionName

public abstract String functionName()
Returns the name of this function for use by the toString() method.
Returns:
name of function

prefixParameters

public String prefixParameters()
Optional String to insert before parameter list in toString(). This method is used by AbstractAggregate to insert DISTINCT or "*" as needed. The default implementation returns the empty string.

postfixParameters

public String postfixParameters()
Optional String to insert after parameter list in toString(). This method is used by Operator_In to add the table expression as the second parameter. If a comma is needed to seperate the parameter list from this postfix, this method is expected to include the initial comma. The default implementation returns the empty string.

toString

public String toString()
Outputs "functionName(parameter,...)".
Overrides:
toString in class Object
Returns:
user-readable function declaration

isConstant

public boolean isConstant()
                   throws SQLException
Determine if this function returns a constant value. If all of the functions parameters are constant, then the function is considered constant as well.
Specified by:
isConstant in interface Function
Returns:
true if the value returned is constant
Throws:
SQLException - if an error occurs

isAggregate

public boolean isAggregate()
                    throws SQLException

The implementation of this method is intended to be used by non-aggregate functions. Aggregate functions should extend AbstractAggregate instead of AbstractFunction. This implementation will return true if and only if all of the function's parameters are either aggregate or constant.

Specified by:
isAggregate in interface Function
Returns:
true if the function aggregates several rows of data
Throws:
SQLException - if an error occurs

evaluateParameters

protected boolean evaluateParameters(boolean aggregate,
                                     boolean stop_at_null)
                              throws SQLException,
                                     EndOfTable
Evaluates all parameters and stores the results in the parameter_value array. If evaluateConstantParameters() was called earlier, constant parameters will not be re-evaluated.
Parameters:
aggregate - evaluate aggregates
stop_at_null - stop if a null if found
Returns:
true if all parameters are non-null, false otherwise
Throws:
SQLException - if an error occurs
EndOfTable - if thrown by a parameter

evaluateParameters

protected boolean evaluateParameters(boolean aggregate)
                              throws SQLException,
                                     EndOfTable
Same as above version, but with stop_at_null set to false.
Parameters:
aggregate - evaluate aggregates
Returns:
true if all parameters are non-null, false otherwise
Throws:
SQLException - if an error occurs
EndOfTable - if thrown by a parameter

evaluate

public Object evaluate(int match_op,
                       Object match_value)
                throws SQLException,
                       EndOfTable
Default implementation that simply calls evaluate(false).
Specified by:
evaluate in interface Function
Parameters:
match_op - how the value should be matched
match_value - desired value
Returns:
result object
Throws:
SQLException - if an error occurs
EndOfTable - if thrown by a parameter

reset

public void reset()
           throws SQLException
This implementation simply calls reset() for all parameters and is intended to be used by non-aggregate functions. If any additional Function objects need to be reset, this method must be overridden.
Specified by:
reset in interface Function
Throws:
SQLException - if an error occurs

convertToSQLType

public static Object convertToSQLType(Object o,
                                      int type)
                               throws SQLException
Convert an Object to a compatible Object corresponding to the provided SQL type.
Parameters:
o - Object to convert
type - type from java.sql.Types to convert to
Returns:
Object of desired type (or null if o is null)
Throws:
SQLException - if no compatible type is available

getObjectSQLType

public static int getObjectSQLType(Object o)
Determine the SQL type of a java Object. If the reference is non-null, but the object type cannot be determined, java.sql.Types.JAVA_OBJECT is returned.
Parameters:
o - object to check
Returns:
value from java.sql.Types

getCompatableType

public static int getCompatableType(int t1,
                                    int t2)
                             throws SQLException
Determine a java.sql.Types type that is compatable with both of the passed parameters. If either parameter is NULL, the type of the other parameter will be returned.
Parameters:
t1 - type of first value
t2 - type of second value
Returns:
compatible type
Throws:
SQLException - if the types are incompatible