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)")
Support cast of formula like ... select("max(updtime)::Instant")
This commit is contained in:
@@ -31,12 +31,50 @@ public class FormulaPropertyPathTest extends BaseTestCase {
|
||||
assertFormula("concat(name,'-end')", "concat", "name,'-end'");
|
||||
}
|
||||
|
||||
private void assertFormula(String input, String aggType, String baseProperty) {
|
||||
@Test
|
||||
public void castFormula() {
|
||||
|
||||
assertFormula("concat(name,'-end')::String", "concat", "name,'-end'", "String", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cast_javaInstant() {
|
||||
|
||||
assertFormula("max(updtime)::Instant", "max", "updtime", "Instant", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alias() {
|
||||
assertFormula("concat(name,'-end') name", "concat", "name,'-end'", null, "name");
|
||||
assertFormula("concat(name,'-end') as name", "concat", "name,'-end'", null, "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castAndAlias() {
|
||||
assertFormula("concat(name,'-end')::String name", "concat", "name,'-end'", "String", "name");
|
||||
assertFormula("concat(name,'-end')::String as name", "concat", "name,'-end'", "String", "name");
|
||||
}
|
||||
|
||||
private void assertFormula(String input, String funcName, String expression) {
|
||||
assertFormula(input, funcName, expression, null, null);
|
||||
}
|
||||
|
||||
private void assertFormula(String input, String funcName, String expression, String cast, String alias) {
|
||||
|
||||
FormulaPropertyPath propertyPath = new FormulaPropertyPath(customerDesc, input);
|
||||
|
||||
assertThat(propertyPath.basePropertyName()).isEqualTo(baseProperty);
|
||||
assertThat(propertyPath.aggType()).isEqualTo(aggType);
|
||||
assertThat(propertyPath.internalExpression()).isEqualTo(expression);
|
||||
assertThat(propertyPath.outerFunction()).isEqualTo(funcName);
|
||||
if (cast != null) {
|
||||
assertThat(propertyPath.cast()).isEqualTo(cast);
|
||||
} else {
|
||||
assertThat(propertyPath.cast()).isNull();
|
||||
}
|
||||
if (alias != null) {
|
||||
assertThat(propertyPath.alias()).isEqualTo(alias);
|
||||
} else {
|
||||
assertThat(propertyPath.alias()).isNull();
|
||||
}
|
||||
|
||||
SqlTreeProperty treeProperty = propertyPath.build();
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.tests.model.tevent.TEventMany;
|
||||
import org.tests.model.tevent.TEventOne;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -423,4 +424,24 @@ public class TestAggregationCount extends BaseTestCase {
|
||||
assertThat(sql.get(0)).contains("select concat(t0.updtime,', ',t0.first_name) from contact t0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void explicitCast() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Instant instant =
|
||||
|
||||
Ebean.find(Contact.class)
|
||||
.select("max(updtime)::Instant")
|
||||
.where().isNull("phone")
|
||||
.findSingleAttribute();
|
||||
|
||||
assertThat(instant).isNotNull();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql.get(0)).contains("select max(t0.updtime) from contact t0 where t0.phone is null");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user