org.apache.torque.util
Class SummaryHelper

java.lang.Object
  extended by org.apache.torque.util.SummaryHelper

public class SummaryHelper
extends java.lang.Object

A utility to help produce aggregate summary information about a table. The default assumes that the underlying DB supports the SQL 99 Standard Aggregate functions, e.g. COUNT, SUM, AVG, MAX, & MIN. However, some non-standard functions (like MySQL's older LEAST instead of MIN can be handled programatically if needed (@see Aggregate class)

Here is a simple example to generate the results of a query like:

 SELECT EMPLOYEE, SUM(HOURS), MIN(HOURS), MAX(HOURS)
     FROM TIMESHEET WHERE TYPE = 1 GROUP BY EMPLOYEE ORDER BY EMPLOYEE ASC
 

Use the following code

    SummaryHelper sHelp = new SummaryHelper();
    Criteria c = new Criteria();
    c.add(TimeSheetPeer.TYPE, 1);
    c.addAscendingOrderBy(TimeSheetPeer.EMPLOYEE);
    sHelper.addGroupBy(TimeSheetPeer.EMPLOYEE);
    sHelper.addAggregate(FunctionFactory.Sum(TimeSheetPeer.HOURS),"Hours");
    sHelper.addAggregate(FunctionFactory.Min(TimeSheetPeer.HOURS),"Min_Hrs");
    sHelper.addAggregate(FunctionFactory.Max(TimeSheetPeer.HOURS),"Max_Hrs");
    List results = sHelper.summarize( c );
 

The results list will be an ListOrderedMapCI with a key of either the group by column name or the name specified for the aggregate function (e.g. EMPLOYEE or Hours). The value will be a Village Value Class. Below is a simple way to do this. See the dumpResults* method code for a more complex example.

    String emp = results.get("EMPLOYEE").asString();
    int hours = results.get("Hours").asInt();
 

Notes:

If there are no group by columns specified, the aggregate is over the whole table. The from table is defined either via the Criteria.addAlias(...) method or by the first table prefix in an aggregate function.

This will also work with joined tables if the criteria is creates as to create valid SQL.

Version:
$Id$
Author:
Greg Monroe
See Also:
FunctionFactory

Constructor Summary
SummaryHelper()
          Simple constructor
 
Method Summary
 void addAggregate(java.lang.String alias, SQLFunction function)
          Add in an Aggregate function to the summary information.
 void addGroupBy(java.lang.String column)
           Add a column that will be used to group the aggregate results by.
 Criteria buildCriteria(Criteria c)
          Builds the criteria to use in summarizing the information.
 void clear()
          Resets the class internal variables to their initial states so the class can be re-used like a new class.
 void dumpResults(java.io.Writer out, java.util.List results, boolean includeHeader)
          Convenience method to dump a summary results list to an output writer in a semi-CSV format.
 boolean excludeExprColumns()
          Should the results include unnamed columns, e.g.
 ListOrderedMapCI getAggregates()
          Get the order map list of aggregate functions to use in summarizing this table's informations.
 java.util.List getGroupByColumns()
           
 void setExcludeExprColumns(boolean excludeExprColumns)
          Define if unnamed output columns which get labeled as EXPR{index#}) should be included in the the output set.
 java.util.List summarize(Criteria crit)
          Return a list of ListOrderedMapCI objects with the results of the summary query.
 java.util.List summarize(Criteria crit, java.sql.Connection conn)
          Return a list of ListOrderedMapCI objects with the results of the summary query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SummaryHelper

public SummaryHelper()
Simple constructor

Method Detail

summarize

public java.util.List summarize(Criteria crit)
                         throws TorqueException,
                                com.workingdogs.village.DataSetException
Return a list of ListOrderedMapCI objects with the results of the summary query. The ListOrderedMapCI objects have a key of the column name or function alias and are in the order generated by the query.

Parameters:
crit - The base criteria to build on.
Returns:
Results as a ListOrderMapCI object.
Throws:
TorqueException
com.workingdogs.village.DataSetException

summarize

public java.util.List summarize(Criteria crit,
                                java.sql.Connection conn)
                         throws TorqueException,
                                com.workingdogs.village.DataSetException
Return a list of ListOrderedMapCI objects with the results of the summary query. The ListOrderedMapCI objects have a key of the column name or function alias and are in the order generated by the query.

Parameters:
crit - The base criteria to build on.
conn - The DB Connection to use.
Returns:
Results as a ListOrderMapCI object.
Throws:
TorqueException
com.workingdogs.village.DataSetException
See Also:
ListOrderedMapCI

buildCriteria

public Criteria buildCriteria(Criteria c)
                       throws TorqueException
Builds the criteria to use in summarizing the information. Note that the criteria passed in will be modified.

Parameters:
c - The base criteria to build the summary criteria from.
Returns:
A criteria to use in summarizing the information.
Throws:
TorqueException

addGroupBy

public void addGroupBy(java.lang.String column)

Add a column that will be used to group the aggregate results by. This is a first added / first listed on SQL method. E.g.,

    add(TablePeer.COL1);
    add(TablePeer.COL2);
 

Generates SQL like: SELECT .... GROUP BY Table.COL1, TABLE.COL2

Parameters:
column -

addAggregate

public void addAggregate(java.lang.String alias,
                         SQLFunction function)
Add in an Aggregate function to the summary information.

Parameters:
alias - A valid SQL99 column identifier ([_A-Z0-9] no spaces and no key words, e.g. function names.
function - One of the inner classes from the Aggregate class.

clear

public void clear()
Resets the class internal variables to their initial states so the class can be re-used like a new class.


getGroupByColumns

public java.util.List getGroupByColumns()

getAggregates

public ListOrderedMapCI getAggregates()
Get the order map list of aggregate functions to use in summarizing this table's informations. The key is used as the result column alias.

Returns:
the avgColumns. Will always return a ListOrderedMap object.

dumpResults

public void dumpResults(java.io.Writer out,
                        java.util.List results,
                        boolean includeHeader)
                 throws java.io.IOException
Convenience method to dump a summary results list to an output writer in a semi-CSV format. E.g., there is no handling of embedded quotes/special characters.

Parameters:
out -
results -
includeHeader -
Throws:
java.io.IOException

excludeExprColumns

public boolean excludeExprColumns()
Should the results include unnamed columns, e.g. EXPR{index#}.

Returns:
the excludeExprColumns

setExcludeExprColumns

public void setExcludeExprColumns(boolean excludeExprColumns)

Define if unnamed output columns which get labeled as EXPR{index#}) should be included in the the output set.

Note these are generally added by the criteria processing to handle special cases such as case insensitive ordering.

Parameters:
excludeExprColumns - if True, these columns won't be included.


Copyright © 2000-2012 Apache Software Foundation. All Rights Reserved.