mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1333 - ENH: Support "dynamic formula" with findSingleAttributeList() ... e.g. .select("concat(lastName,', ',firstName)")
Concat automatically maps to String ScalarType
This commit is contained in:
@@ -10,7 +10,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
class FormulaPropertyPath {
|
||||
|
||||
private static final String[] AGG_FUNCTIONS = {"count","max","min","avg"};
|
||||
private static final String[] AGG_FUNCTIONS = {"count", "max", "min", "avg"};
|
||||
|
||||
private static final Pattern pattern = Pattern.compile("([a-zA-Z]*)\\((.*)\\)");
|
||||
|
||||
@@ -41,7 +41,7 @@ class FormulaPropertyPath {
|
||||
}
|
||||
|
||||
private String trimDistinct(String propertyName) {
|
||||
if (propertyName.startsWith(DISTINCT_)){
|
||||
if (propertyName.startsWith(DISTINCT_)) {
|
||||
countDistinct = true;
|
||||
return propertyName.substring(DISTINCT_.length());
|
||||
} else {
|
||||
@@ -67,19 +67,22 @@ class FormulaPropertyPath {
|
||||
|
||||
ScalarType<?> scalarType;
|
||||
if (isCount()) {
|
||||
// count maps to Long / BIGINT
|
||||
scalarType = descriptor.getScalarType(Types.BIGINT);
|
||||
|
||||
} else if (isConcat()) {
|
||||
scalarType = descriptor.getScalarType(Types.VARCHAR);
|
||||
|
||||
} else {
|
||||
// determine scalarType based on first property found by parser
|
||||
if (firstProp != null) {
|
||||
scalarType = firstProp.getBeanProperty().getScalarType();
|
||||
} else {
|
||||
throw new IllegalStateException("unable to determine scalarType of formula [" + formula + "] for type " + descriptor+" - maybe use a cast like ::String ?");
|
||||
throw new IllegalStateException("unable to determine scalarType of formula [" + formula + "] for type " + descriptor + " - maybe use a cast like ::String ?");
|
||||
}
|
||||
}
|
||||
|
||||
String parsedAggregation = buildFormula(parsed);
|
||||
return new DynamicPropertyAggregationFormula(formula, scalarType, parsedAggregation, isAggregate(),null);
|
||||
return new DynamicPropertyAggregationFormula(formula, scalarType, parsedAggregation, isAggregate(), null);
|
||||
}
|
||||
|
||||
private boolean isAggregate() {
|
||||
@@ -93,9 +96,9 @@ class FormulaPropertyPath {
|
||||
|
||||
private String buildFormula(String parsed) {
|
||||
if (countDistinct) {
|
||||
return "count(distinct "+parsed+")";
|
||||
return "count(distinct " + parsed + ")";
|
||||
} else {
|
||||
return outerFunction +"("+parsed+")";
|
||||
return outerFunction + "(" + parsed + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,4 +106,8 @@ class FormulaPropertyPath {
|
||||
return outerFunction.equals("count");
|
||||
}
|
||||
|
||||
private boolean isConcat() {
|
||||
return outerFunction.equals("concat");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user