ModSQL
Class Function_Math

java.lang.Object
  |
  +--ModSQL.AbstractFunction
        |
        +--ModSQL.Function_Math
All Implemented Interfaces:
Function

final class Function_Math
extends AbstractFunction

This class implements standard SQL math functions: ABS, CEIL, FLOOR, GREATEST, LEAST, MOD, POWER, ROUND, SIGN, and SQRT.

Author:
chris.studholme@utoronto.ca

Field Summary
static int ABS
          Function is ABS().
static int CEIL
          Function is CEIL().
static int FLOOR
          Function is FLOOR().
static int GREATEST
          Function is GREATEST().
static int LEAST
          Function is LEAST().
static int MOD
          Function is MOD().
private  int op
          Which function are we? One of the above constants.
static int POWER
          Function is POWER().
private  int return_type
          SQL return type for this function.
static int ROUND
          Function is ROUND().
static int SIGN
          Function is SIGN().
static int SQRT
          Function is SQRT().
 
Fields inherited from class ModSQL.AbstractFunction
all_constant, constant_non_null, evaluate_order, evaluate_order_array, out_of_order, parameter_constant, parameter_value, parameters, parameters_array
 
Fields inherited from interface ModSQL.Function
MATCH_BEGINS, MATCH_EQU, MATCH_GT, MATCH_GTE, MATCH_LT, MATCH_LTE, MATCH_NE
 
Constructor Summary
Function_Math(int op)
          Constructor.
 
Method Summary
 Object evaluate(boolean aggregate)
          Evaluate parameters and compute function.
static Function forName(String name)
          Returns a new instance of this class if name is recognized.
 String functionName()
          Returns the name of this function for use by toString() method.
 int getMaxResultSize()
          Returns -1 as this function will never return a String constant.
 int getSQLType()
          Returns the SQL type of this function.
 void optimize()
          Prepare the function for use.
static Number power(Number n, Number e, int return_type)
          Compute n to the power of e and return a number of the desired type.
static Number round(Number n)
          Round number to nearest integer.
static Number round(Number n, Number d)
          Round number of specified number of decimal places.
 
Methods inherited from class ModSQL.AbstractFunction
addParameter, convertToSQLType, evaluate, evaluateConstantParameters, evaluateOrder, evaluateParameters, evaluateParameters, getCompatableType, getObjectSQLType, getParameter, getParameterCount, isAggregate, isConstant, postfixParameters, prefixParameters, registerWith, reset, toString
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

ABS

public static final int ABS
Function is ABS().

CEIL

public static final int CEIL
Function is CEIL().

FLOOR

public static final int FLOOR
Function is FLOOR().

GREATEST

public static final int GREATEST
Function is GREATEST().

LEAST

public static final int LEAST
Function is LEAST().

MOD

public static final int MOD
Function is MOD().

POWER

public static final int POWER
Function is POWER().

ROUND

public static final int ROUND
Function is ROUND().

SIGN

public static final int SIGN
Function is SIGN().

SQRT

public static final int SQRT
Function is SQRT().

op

private int op
Which function are we? One of the above constants.

return_type

private int return_type
SQL return type for this function.
Constructor Detail

Function_Math

public Function_Math(int op)
Constructor.
Parameters:
op - function type
Method Detail

forName

public static Function forName(String name)
Returns a new instance of this class if name is recognized. The instance is initialized as needed to represent the desired function.
Parameters:
name - name of function to find
Returns:
instance of this class or null if name does not match

optimize

public void optimize()
              throws SQLException
Prepare the function for use. This method figures out the return type of the function.
Overrides:
optimize in class AbstractFunction
Throws:
SQLException - if the parameters are invalid

getSQLType

public int getSQLType()
Returns the SQL type of this function.
Returns:
SQL type of data to be returned

getMaxResultSize

public int getMaxResultSize()
Returns -1 as this function will never return a String constant.
Returns:
maximum size of String returned or -1 if unknown

functionName

public String functionName()
Returns the name of this function for use by toString() method.
Overrides:
functionName in class AbstractFunction
Returns:
name of function

evaluate

public Object evaluate(boolean aggregate)
                throws SQLException,
                       EndOfTable

Evaluate parameters and compute function.

Parameters:
aggregate - passed to parameters
Returns:
result object
Throws:
SQLException - if a database-access error occurs
EndOfTable - if thrown by a parameter

round

public static Number round(Number n)
Round number to nearest integer.
Parameters:
n - number to round
Returns:
Double object with rounded value

round

public static Number round(Number n,
                           Number d)
Round number of specified number of decimal places.
Parameters:
n - number to round
d - number of decimal places to round to (integer)
Returns:
Double object with rounded value

power

public static Number power(Number n,
                           Number e,
                           int return_type)
                    throws SQLException
Compute n to the power of e and return a number of the desired type.
Parameters:
n - base
e - exponent
return_type - SQL type of object to return
Returns:
Number object with computed power
Throws:
SQLException - if type mismatch