ModSQL
Class Aggregate_Count

java.lang.Object
  |
  +--ModSQL.AbstractFunction
        |
        +--ModSQL.AbstractAggregate
              |
              +--ModSQL.Aggregate_Count
All Implemented Interfaces:
Aggregate, Function

final class Aggregate_Count
extends AbstractAggregate

Implementation of SQL aggregate function COUNT().

Author:
chris.studholme@utoronto.ca

Field Summary
private  long count
          Current count.
private  boolean count_all
          True if we are counting rows, false to count non-null values.
 
Fields inherited from class ModSQL.AbstractAggregate
distinct, distinct_set
 
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
(package private) Aggregate_Count()
           
 
Method Summary
 void addParameter(Function item)
          Adds a value (function) to the list of parameters maintained by this function.
 Object evaluate(boolean aggregate)
          Evaluate the parameter and update the count.
static Function forName(String name)
          Returns a new instance of this class if name is "count".
 String functionName()
          Returns "COUNT".
 int getMaxResultSize()
          Returns -1.
 int getSQLType()
          COUNT always returns BIGINT integers.
 void optimize()
          Prepare the function for use.
 void reset()
          Resets the current count to zero.
 
Methods inherited from class ModSQL.AbstractAggregate
addDistinctValue, getDistinctSetSize, isAggregate, isConstant, prefixParameters, registerWith, setDistinct
 
Methods inherited from class ModSQL.AbstractFunction
convertToSQLType, evaluate, evaluateConstantParameters, evaluateOrder, evaluateParameters, evaluateParameters, getCompatableType, getObjectSQLType, getParameter, getParameterCount, postfixParameters, toString
 
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, evaluateOrder, getParameter, getParameterCount
 

Field Detail

count_all

private boolean count_all
True if we are counting rows, false to count non-null values.

count

private long count
Current count.
Constructor Detail

Aggregate_Count

Aggregate_Count()
Method Detail

forName

public static Function forName(String name)
Returns a new instance of this class if name is "count".
Parameters:
name - name of function to find
Returns:
instance of this class or null if name does not match

addParameter

public void addParameter(Function item)
                  throws SQLException
Adds a value (function) to the list of parameters maintained by this function.
Overrides:
addParameter in class AbstractFunction
Parameters:
item - function to add
Throws:
SQLException - if too many parameters have been added

optimize

public void optimize()
              throws SQLException

Prepare the function for use. COUNT() requires exactly one parameter, unless count_all, in which case no parameters are acceptable. If we are doing COUNT(DISTINCT ...), we require a parameter.

Overrides:
optimize in class AbstractFunction
Throws:
SQLException - if the parameters are invalid

functionName

public String functionName()
Returns "COUNT".
Overrides:
functionName in class AbstractFunction
Returns:
name of function

getSQLType

public int getSQLType()
COUNT always returns BIGINT integers.
Returns:
SQL type of data to be returned

getMaxResultSize

public int getMaxResultSize()
Returns -1.
Returns:
maximum size of String returned or -1 if unknown

reset

public void reset()
           throws SQLException
Resets the current count to zero.
Overrides:
reset in class AbstractAggregate
Throws:
SQLException - if a database error occurs

evaluate

public Object evaluate(boolean aggregate)
                throws SQLException,
                       EndOfTable

Evaluate the parameter and update the count. If aggregate is true, the current count is returned and the parameter is not evaluated. This method returns null if aggregate is false.

Parameters:
aggregate - true to return current count
Returns:
Long object if aggregate is true, null otherwise
Throws:
SQLException - if a database-access error occurs
EndOfTable - if thrown by a parameter