public class SummaryHelper extends 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 programmatically if needed AggregateFunction
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 OrderedMap 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.
Constructor and Description |
---|
SummaryHelper() |
Modifier and Type | Method and Description |
---|---|
void |
addAggregate(String alias,
SQLFunction function)
Add in an Aggregate function to the summary information.
|
void |
addGroupBy(Column 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(Writer out,
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<SQLFunction> |
getAggregates()
Get the order map list of aggregate functions to use in
summarizing this table's informations.
|
List<Column> |
getGroupByColumns() |
void |
setExcludeExprColumns(boolean excludeExprColumns)
Define if unnamed output columns which get labeled as EXPR{index#})
should be included in the the output set.
|
List<ListOrderedMapCI<Object>> |
summarize(Criteria crit)
Return a list of ListOrderedMapCI objects with the results of the summary
query.
|
List<ListOrderedMapCI<Object>> |
summarize(Criteria crit,
Connection conn)
Return a list of OrderedMap objects with the results of the summary
query.
|
List<ListOrderedMapCI<Object>> |
summarize(Criteria crit,
List<Class<?>> resultTypes)
Return a list of ListOrderedMapCI objects with the results of the summary
query.
|
List<ListOrderedMapCI<Object>> |
summarize(Criteria crit,
List<Class<?>> resultTypes,
Connection conn)
Return a list of ListOrderedMapCI objects with the results of the summary
query.
|
public List<ListOrderedMapCI<Object>> summarize(Criteria crit) throws TorqueException
crit
- The base criteria to build on.TorqueException
- if a database error occurs.public List<ListOrderedMapCI<Object>> summarize(Criteria crit, List<Class<?>> resultTypes) throws TorqueException
crit
- The base criteria to build on.resultTypes
- the classes to which the return values of the query
should be cast, or null to let the database driver decide.
See org.apache.torque.om.mapper.ObjectListMapper�for the supported
classes.TorqueException
- if a database error occurs.public List<ListOrderedMapCI<Object>> summarize(Criteria crit, Connection conn) throws TorqueException
crit
- The base criteria to build on.conn
- The DB Connection to use.TorqueException
- if a database error occurs.public List<ListOrderedMapCI<Object>> summarize(Criteria crit, List<Class<?>> resultTypes, Connection conn) throws TorqueException
crit
- The base criteria to build on.resultTypes
- the classes to which the return values of the query
should be cast, or null to let the database driver decide.
See org.apache.torque.om.mapper.ObjectListMapper�for the supported
classes.conn
- The DB Connection to use.TorqueException
- if a database error occurs.public Criteria buildCriteria(Criteria c) throws TorqueException
c
- The base criteria to build the summary criteria from.TorqueException
- if a database error occurs.public void addGroupBy(Column 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
- to group bypublic void addAggregate(String alias, SQLFunction function)
alias
- A valid SQL99 column identifier ([_A-Z0-9] no spaces and
no key words, e.g. function names.function
- An aggregate SQL function to evaluate.public void clear()
public ListOrderedMapCI<SQLFunction> getAggregates()
public void dumpResults(Writer out, List<?> results, boolean includeHeader) throws IOException
out
- the output writerresults
- results to storeincludeHeader
- boolean flag to include headerIOException
- if file not foundpublic 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.Copyright © 2000–2020 The Apache Software Foundation. All rights reserved.