#1331 - ENH: Support Aggregation formula with findSingleAttribute() ... max, min, avg, count and count(distinct ..)

This commit is contained in:
Rob Bygrave
2018-03-06 12:58:43 +13:00
parent ec2c3b13d4
commit 3ca4efc818
23 changed files with 783 additions and 51 deletions
@@ -1,7 +1,6 @@
package io.ebeaninternal.server.query;
import io.ebeaninternal.api.ManyWhereJoins;
import io.ebeaninternal.server.deploy.BeanProperty;
import java.util.ArrayList;
import java.util.Arrays;
@@ -21,7 +20,7 @@ public class SqlTreeProperties {
/**
* The bean properties in order.
*/
private final List<BeanProperty> propsList = new ArrayList<>();
private final List<SqlTreeProperty> propsList = new ArrayList<>();
/**
* Maintain a list of property names to detect embedded bean additions.
@@ -32,6 +31,8 @@ public class SqlTreeProperties {
private boolean aggregation;
private String aggregationPath;
SqlTreeProperties() {
}
@@ -39,17 +40,17 @@ public class SqlTreeProperties {
return propNames.contains(propName);
}
public void add(BeanProperty[] props) {
public void add(SqlTreeProperty[] props) {
propsList.addAll(Arrays.asList(props));
}
public void add(BeanProperty prop) {
public void add(SqlTreeProperty prop) {
propsList.add(prop);
propNames.add(prop.getName());
}
public BeanProperty[] getProps() {
return propsList.toArray(new BeanProperty[propsList.size()]);
public SqlTreeProperty[] getProps() {
return propsList.toArray(new SqlTreeProperty[propsList.size()]);
}
boolean isPartialObject() {
@@ -77,7 +78,6 @@ public class SqlTreeProperties {
boolean requireSqlDistinct(ManyWhereJoins manyWhereJoins) {
String joinProperty = aggregationJoin();
if (joinProperty != null) {
aggregation = true;
manyWhereJoins.addAggregationJoin(joinProperty);
return false;
} else {
@@ -97,12 +97,21 @@ public class SqlTreeProperties {
*/
private String aggregationJoin() {
if (!allProperties) {
for (BeanProperty beanProperty : propsList) {
for (SqlTreeProperty beanProperty : propsList) {
if (beanProperty.isAggregation()) {
return beanProperty.getElPrefix();
aggregation = true;
aggregationPath = beanProperty.getElPrefix();
return aggregationPath;
}
}
}
return null;
}
/**
* Return true if a top level aggregation which means the Id property must be excluded.
*/
public boolean isAggregationRoot() {
return aggregation && (aggregationPath == null);
}
}