#1619 - Support aggregation formula on fetch clause e.g. query.fetch("machineUsage", "sum(totalKms)")

This commit is contained in:
rob bygrave
2019-01-23 16:45:47 +13:00
15 changed files with 572 additions and 35 deletions
@@ -120,7 +120,7 @@ public interface STreeType {
/**
* Find and return property allowing for dynamic formula properties.
*/
STreeProperty findPropertyWithDynamic(String baseName);
STreeProperty findPropertyWithDynamic(String baseName, String path);
/**
* Return an extra join if the property path requires it.
@@ -273,7 +273,10 @@ public final class SqlTreeBuilder {
OrmQueryProperties queryProps = queryDetail.getChunk(prefix, false);
SqlTreeProperties props = getBaseSelect(desc, queryProps);
if (prefix == null && !rawSql) {
if (prefix != null) {
// check for aggregation on a fetch
props.checkAggregation();
} else if (!rawSql) {
if (props.requireSqlDistinct(manyWhereJoins)) {
sqlDistinct = true;
}
@@ -417,10 +420,10 @@ public final class SqlTreeBuilder {
// make sure we only included the base/embedded bean once
if (!selectProps.containsProperty(baseName)) {
STreeProperty p = desc.findPropertyWithDynamic(baseName);
STreeProperty p = desc.findPropertyWithDynamic(baseName, null);
if (p == null) {
// maybe dynamic formula with schema prefix
p = desc.findPropertyWithDynamic(propName);
p = desc.findPropertyWithDynamic(propName, null);
if (p != null) {
selectProps.add(p);
} else {
@@ -437,7 +440,7 @@ public final class SqlTreeBuilder {
} else {
// find the property including searching the
// sub class hierarchy if required
STreeProperty p = desc.findPropertyWithDynamic(propName);
STreeProperty p = desc.findPropertyWithDynamic(propName, queryProps.getPath());
if (p == null) {
logger.error("property [" + propName + "] not found on " + desc + " for query - excluding it.");
p = desc.findProperty("id");
@@ -378,7 +378,7 @@ class SqlTreeNodeBean implements SqlTreeNode {
if (!readId || temporalVersions) {
// a bean with no Id (never found in context)
if (lazyLoadParentId != null && desc.isElementType()) {
if (lazyLoadParentId != null) {
ctx.setLazyLoadedChildBean(localBean, lazyLoadParentId);
}
return localBean;
@@ -422,6 +422,9 @@ class SqlTreeNodeBean implements SqlTreeNode {
ctx.pushJoin(prefix);
ctx.pushTableAlias(prefix);
if (lazyLoadParent != null) {
lazyLoadParent.addSelectExported(ctx, prefix);
}
if (readId) {
appendSelectId(ctx, idBinder.getBeanProperty());
}
@@ -486,7 +489,15 @@ class SqlTreeNodeBean implements SqlTreeNode {
@Override
public boolean isAggregation() {
return aggregation;
if (aggregation) {
return true;
}
for (SqlTreeNode child : children) {
if (child.isAggregation()) {
return true;
}
}
return false;
}
/**
@@ -92,6 +92,13 @@ public class SqlTreeProperties {
return aggregation;
}
/**
* Check for aggregation (need for groug by clause).
*/
public void checkAggregation() {
aggregationJoin();
}
/**
* Return the property to join for aggregation.
*/