org.sormula.operation.aggregate
Class SelectAggregateOperation<R,T>

java.lang.Object
  extended by org.sormula.operation.SqlOperation<R>
      extended by org.sormula.operation.ScalarSelectOperation<R>
          extended by org.sormula.operation.aggregate.SelectAggregateOperation<R,T>
Type Parameters:
R - class type which contains members for columns of a row in a table
T - class type of aggregate result
Direct Known Subclasses:
SelectAvgOperation, SelectCountOperation, SelectMaxOperation, SelectMinOperation, SelectSumOperation

public class SelectAggregateOperation<R,T>
extends ScalarSelectOperation<R>

Operation to select a value using an aggregate sql function like MIN, MAX, SUM, AVG, etc. By default, operates on all rows in table. Set where condition with SqlOperation.setWhere(String) to limit to a subset of rows. To get the result, use readAggregate().

If expression is a column, then T should be same type as column type in row class R. Column translator associated with column in row R will be used.

If expression is not a column, then T should match the expression type. Override readAggregate() to provide a customized read for expression. By default where expression is not a column name, then ResultSet.getObject(int) is used.

Since:
1.1
Author:
Jeff Miller

Constructor Summary
SelectAggregateOperation(Table<R> table, java.lang.String function, java.lang.String expression)
          Constructs for standard sql select statement as:
SELECT f(e), ...
 
Method Summary
protected  void initBaseSql()
          Sets base sql with SqlOperation.setBaseSql(String).
 T readAggregate()
          Reads the aggregate value from the current result set.
 
Methods inherited from class org.sormula.operation.ScalarSelectOperation
close, execute, getMaximumRowsRead, getOrderByName, getOrderByTranslator, getResultSet, getRowsReadCount, getSql, isLazySelectsCascades, isNotifyLazySelects, postRead, postReadCascade, prepareCascades, preRead, preReadCascade, readNext, select, select, setMaximumRowsRead, setOrderBy, setOrderByTranslator, setParameters, setRowParameters
 
Methods inherited from class org.sormula.operation.SqlOperation
cancel, cascade, closeCascades, closeStatement, createTargetField, getBaseSql, getConnection, getCustomSql, getNextParameter, getOperationTime, getParameters, getPreparedSql, getPreparedStatement, getQueryTimeout, getTable, getTargetTable, getTimingId, getWhereConditionName, getWhereTranslator, initOperationTime, isAutoGeneratedKeys, isIncludeIdentityColumns, isReadOnly, isTimings, logTimings, prepare, prepareCascades, prepareCheck, prepareColumns, prepareParameters, prepareWhere, setAutoGeneratedKeys, setBaseSql, setCustomSql, setIncludeIdentityColumns, setNextParameter, setQueryTimeout, setReadOnly, setTimingId, setTimings, setWhere, setWhereTranslator, writeColumns, writeParameter, writeParameters, writeWhere
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SelectAggregateOperation

public SelectAggregateOperation(Table<R> table,
                                java.lang.String function,
                                java.lang.String expression)
                         throws OperationException
Constructs for standard sql select statement as:
SELECT f(e), ... FROM table
where f is an aggregate SQL function and e is a SQL expression (typically a column name).

Example:

 Database database = ...
 Table<Order> table = database.getTable(Order.class);
 // find largest order
 SelectAggregateOperation<Order> maxOrderOperation = 
     new SelectAggregateOperation<Order, Double>(table, "MAX", "amount");
 maxOrderOperation.execute();    
 double maxOrder = maxOrderOperation.readAggregate();
 

Parameters:
table - select from this table
function - aggregate function name like AVG, SUM, MIN, etc.
expression - expression to use as parameter to function; typically it is the name of a column to that aggregate function operates upon (example: SUM(amount) SUM is function and amount is expression)
Throws:
OperationException - if error
Method Detail

initBaseSql

protected void initBaseSql()
Sets base sql with SqlOperation.setBaseSql(String).

Overrides:
initBaseSql in class ScalarSelectOperation<R>

readAggregate

public T readAggregate()
                throws OperationException
Reads the aggregate value from the current result set.

Returns:
aggregate value
Throws:
OperationException - if error