|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.torque.util.SummaryHelper
public class SummaryHelper
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.
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 |
---|
public SummaryHelper()
Method Detail |
---|
public java.util.List summarize(Criteria crit) throws TorqueException, com.workingdogs.village.DataSetException
crit
- The base criteria to build on.
TorqueException
com.workingdogs.village.DataSetException
public java.util.List summarize(Criteria crit, java.sql.Connection conn) throws TorqueException, com.workingdogs.village.DataSetException
crit
- The base criteria to build on.conn
- The DB Connection to use.
TorqueException
com.workingdogs.village.DataSetException
ListOrderedMapCI
public Criteria buildCriteria(Criteria c) throws TorqueException
c
- The base criteria to build the summary criteria from.
TorqueException
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
column
- public void addAggregate(java.lang.String alias, SQLFunction function)
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.public void clear()
public java.util.List getGroupByColumns()
public ListOrderedMapCI getAggregates()
public void dumpResults(java.io.Writer out, java.util.List results, boolean includeHeader) throws java.io.IOException
out
- results
- includeHeader
-
java.io.IOException
public boolean excludeExprColumns()
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.
excludeExprColumns
- if True, these columns won't be included.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |