Skip to content

Commit

Permalink
Improving AliasExpression (#1071)
Browse files Browse the repository at this point in the history
* Improving AliasExpression

* Add AliasExpression to Expressions

* Can specify alias on orderBy

* Fix NativeSqlSelectTest.java for sqlserver

* Fix NativeSqlSelectTest.java for BigDecimal

* Revert redundant Salary fix
  • Loading branch information
momosetkn authored Feb 26, 2024
1 parent 79e2760 commit 8ed40ea
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

import java.util.Objects;
import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel;
import org.seasar.doma.jdbc.criteria.statement.NativeSqlSelectStarting;
import org.seasar.doma.jdbc.entity.EntityPropertyType;

/**
* Used to add an alias to a column in the select clause. For in adherence to standard SQL, It is
* recommended to specify only in the {@link NativeSqlSelectStarting#select} and {@link
* NativeSqlSelectStarting#orderBy} method.
*
* @param <PROPERTY> the property type
*/
public class AliasExpression<PROPERTY> implements PropertyMetamodel<PROPERTY> {

private final PropertyMetamodel<PROPERTY> originalPropertyMetamodel;
Expand All @@ -21,7 +29,7 @@ public AliasExpression(PropertyMetamodel<PROPERTY> originalPropertyMetamodel, St

@Override
public Class<?> asClass() {
return originalPropertyMetamodel.getClass();
return originalPropertyMetamodel.asClass();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,11 @@ public static <PROPERTY> SelectExpression<PROPERTY> select(
block.apply(new SelectExpression.Declaration());
return new SelectExpression<>(subSelectContext);
}

public static <PROPERTY> AliasExpression<PROPERTY> alias(
PropertyMetamodel<PROPERTY> propertyMetamodel, String alias) {
Objects.requireNonNull(propertyMetamodel);
Objects.requireNonNull(alias);
return new AliasExpression<>(propertyMetamodel, alias);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ protected Optional<String> getAlias(PropertyMetamodel<?> propertyMetamodel) {
});
}

public void selectColumn(PropertyMetamodel<?> propertyMetamodel) {
propertyMetamodel.accept(
new PropertyMetamodelVisitor() {
@Override
public void visit(AliasExpression<?> aliasExpression) {
aliasExpression.getOriginalPropertyMetamodel().accept(this);
buf.appendSql(" AS " + aliasExpression.getAlias());
}
});
}

public void param(Operand.Param param) {
InParameter<?> parameter = param.createInParameter(config);
param(parameter);
Expand Down Expand Up @@ -627,8 +638,7 @@ public void visit(PropertyMetamodel<?> propertyMetamodel) {

@Override
public void visit(AliasExpression<?> aliasExpression) {
aliasExpression.getOriginalPropertyMetamodel().accept(this);
buf.appendSql(" AS " + aliasExpression.getAlias());
buf.appendSql(aliasExpression.getAlias());
}

protected Optional<String> getAlias(PropertyMetamodel<?> propertyMetamodel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void select() {
buf.appendSql("*");
} else {
for (PropertyMetamodel<?> propertyMetamodel : propertyMetamodels) {
column(propertyMetamodel);
selectColumn(propertyMetamodel);
buf.appendSql(", ");
}
buf.cutBackSql(2);
Expand Down Expand Up @@ -234,6 +234,10 @@ private void column(PropertyMetamodel<?> propertyMetamodel) {
support.column(propertyMetamodel);
}

private void selectColumn(PropertyMetamodel<?> propertyMetamodel) {
support.selectColumn(propertyMetamodel);
}

private void visitCriterion(int index, Criterion criterion) {
support.visitCriterion(index, criterion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.seasar.doma.jdbc.SqlLogType;
import org.seasar.doma.jdbc.criteria.Entityql;
import org.seasar.doma.jdbc.criteria.NativeSql;
import org.seasar.doma.jdbc.criteria.expression.AliasExpression;
import org.seasar.doma.jdbc.criteria.expression.Expressions;
import org.seasar.doma.jdbc.criteria.option.AssociationOption;
import org.seasar.doma.jdbc.criteria.statement.EmptyWhereClauseException;
Expand Down Expand Up @@ -159,8 +158,8 @@ void from_subquery_alias() {
.innerJoin(d, c -> c.eq(e.departmentId, d.departmentId))
.groupBy(d.departmentName)
.select(
new AliasExpression<>(Expressions.sum(e.salary), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName()));
Expressions.alias(Expressions.sum(e.salary), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName()));

EntityqlSelectStarting<NameAndAmount> query =
entityql.from(t, subquery).orderBy(c -> c.asc(t.name));
Expand All @@ -184,7 +183,7 @@ void from_subquery_doesnot_match_alias() {
.from(e)
.innerJoin(d, c -> c.eq(e.departmentId, d.departmentId))
.groupBy(d.departmentName)
.select(new AliasExpression<>(d.departmentName, t.name.getName()));
.select(Expressions.alias(d.departmentName, t.name.getName()));
EntityqlSelectStarting<NameAndAmount> query =
entityql.from(t, subquery).orderBy(c -> c.asc(t.name));
DomaException ex = assertThrows(DomaException.class, () -> query.fetch());
Expand All @@ -202,15 +201,15 @@ void from_subquery_union() {
.from(d)
.where(c -> c.eq(d.departmentName, "ACCOUNTING"))
.select(
new AliasExpression<>(Expressions.literal(1200), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName()))
Expressions.alias(Expressions.literal(1200), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName()))
.union(
nativeSql
.from(d)
.where(c -> c.eq(d.departmentName, "OPERATIONS"))
.select(
new AliasExpression<>(Expressions.literal(900), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName())));
Expressions.alias(Expressions.literal(900), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName())));

EntityqlSelectStarting<NameAndAmount> query =
entityql.from(t, subquery).orderBy(c -> c.asc(t.name));
Expand All @@ -232,15 +231,15 @@ void from_subquery_union_all() {
.from(d)
.where(c -> c.eq(d.departmentName, "ACCOUNTING"))
.select(
new AliasExpression<>(Expressions.literal(1200), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName()))
Expressions.alias(Expressions.literal(1200), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName()))
.unionAll(
nativeSql
.from(d)
.where(c -> c.eq(d.departmentName, "OPERATIONS"))
.select(
new AliasExpression<>(Expressions.literal(900), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName())));
Expressions.alias(Expressions.literal(900), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName())));

EntityqlSelectStarting<NameAndAmount> query =
entityql.from(t, subquery).orderBy(c -> c.asc(t.name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ void from_subquery_alias() {
.innerJoin(d, c -> c.eq(e.departmentId, d.departmentId))
.groupBy(d.departmentName)
.select(
new AliasExpression<>(Expressions.sum(e.salary), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName()));
Expressions.alias(Expressions.sum(e.salary), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName()));

NativeSqlSelectStarting<NameAndAmount> query =
nativeSql.from(t, subquery).orderBy(c -> c.asc(t.name));
Expand All @@ -176,7 +176,7 @@ void from_subquery_doesnot_match_alias() {
.from(e)
.innerJoin(d, c -> c.eq(e.departmentId, d.departmentId))
.groupBy(d.departmentName)
.select(new AliasExpression<>(d.departmentName, t.name.getName()));
.select(Expressions.alias(d.departmentName, t.name.getName()));

NativeSqlSelectStarting<NameAndAmount> query =
nativeSql.from(t, subquery).orderBy(c -> c.asc(t.name));
Expand All @@ -195,15 +195,15 @@ void from_subquery_union() {
.from(d)
.where(c -> c.eq(d.departmentName, "ACCOUNTING"))
.select(
new AliasExpression<>(Expressions.literal(1200), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName()))
Expressions.alias(Expressions.literal(1200), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName()))
.union(
nativeSql
.from(d)
.where(c -> c.eq(d.departmentName, "OPERATIONS"))
.select(
new AliasExpression<>(Expressions.literal(900), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName())));
Expressions.alias(Expressions.literal(900), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName())));

NativeSqlSelectStarting<NameAndAmount> query =
nativeSql.from(t, subquery).orderBy(c -> c.asc(t.name));
Expand All @@ -225,15 +225,15 @@ void from_subquery_union_all() {
.from(d)
.where(c -> c.eq(d.departmentName, "ACCOUNTING"))
.select(
new AliasExpression<>(Expressions.literal(1200), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName()))
Expressions.alias(Expressions.literal(1200), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName()))
.unionAll(
nativeSql
.from(d)
.where(c -> c.eq(d.departmentName, "OPERATIONS"))
.select(
new AliasExpression<>(Expressions.literal(900), t.amount.getName()),
new AliasExpression<>(d.departmentName, t.name.getName())));
Expressions.alias(Expressions.literal(900), t.amount.getName()),
Expressions.alias(d.departmentName, t.name.getName())));

NativeSqlSelectStarting<NameAndAmount> query =
nativeSql.from(t, subquery).orderBy(c -> c.asc(t.name));
Expand Down Expand Up @@ -689,6 +689,50 @@ void distinct() {
assertEquals(4, list.size());
}

@Test
void alias_orderBy_asc() {
Department_ d = new Department_();
Employee_ e = new Employee_();

AliasExpression<Salary> salarySum = Expressions.alias(sum(e.salary), "SALARY_SUM");

SetOperand<Tuple2<Integer, Salary>> query =
nativeSql
.from(e)
.innerJoin(d, c -> c.eq(e.departmentId, d.departmentId))
.groupBy(d.departmentId)
.orderBy(c -> c.asc(salarySum))
.select(d.departmentId, salarySum);

List<Tuple2<Integer, Salary>> list = query.fetch();
// asc order
assertEquals(0, list.get(0).component2().getValue().compareTo(new BigDecimal(8750)));
assertEquals(0, list.get(1).component2().getValue().compareTo(new BigDecimal(9400)));
assertEquals(0, list.get(2).component2().getValue().compareTo(new BigDecimal(10875)));
}

@Test
void alias_orderBy_desc() {
Department_ d = new Department_();
Employee_ e = new Employee_();

AliasExpression<Salary> salarySum = Expressions.alias(sum(e.salary), "SALARY_SUM");

SetOperand<Tuple2<Integer, Salary>> query =
nativeSql
.from(e)
.innerJoin(d, c -> c.eq(e.departmentId, d.departmentId))
.groupBy(d.departmentId)
.orderBy(c -> c.desc(salarySum))
.select(d.departmentId, salarySum);

List<Tuple2<Integer, Salary>> list = query.fetch();
// desc order
assertEquals(0, list.get(0).component2().getValue().compareTo(new BigDecimal(10875)));
assertEquals(0, list.get(1).component2().getValue().compareTo(new BigDecimal(9400)));
assertEquals(0, list.get(2).component2().getValue().compareTo(new BigDecimal(8750)));
}

@SuppressWarnings("unused")
@Test
void peek() {
Expand Down

0 comments on commit 8ed40ea

Please sign in to comment.