Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Minor formatting.
Adding author tag in Javadoc.
Fixing warnings.

Original pull request #1844
  • Loading branch information
schauder committed Aug 5, 2024
1 parent b11b85a commit 46d0a06
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.springframework.data.relational.core.sql;

import org.springframework.lang.Nullable;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.*;

/**
* Case with one or more conditions expression.
Expand All @@ -22,73 +24,64 @@
* @since 3.4
*/
public class CaseExpression extends AbstractSegment implements Expression {
private final List<When> whenList;
private final Expression elseExpression;

private CaseExpression(List<When> whenList, Expression elseExpression) {

super(children(whenList, elseExpression));
this.whenList = whenList;
this.elseExpression = elseExpression;
}

/**
* Create CASE {@link Expression} with initial {@link When} condition.
* @param condition initial {@link When} condition
* @return the {@link CaseExpression}
*/
public static CaseExpression create(When condition) {
return new CaseExpression(List.of(condition), null);
}

/**
* Add additional {@link When} condition
* @param condition the {@link When} condition
* @return the {@link CaseExpression}
*/
public CaseExpression when(When condition) {
List<When> conditions = new ArrayList<>(this.whenList);
conditions.add(condition);
return new CaseExpression(conditions, elseExpression);
}

/**
* Add ELSE clause
* @param elseExpression the {@link Expression} else value
* @return the {@link CaseExpression}
*/
public CaseExpression elseExpression(Literal elseExpression) {
return new CaseExpression(whenList, elseExpression);
}

/**
* @return the {@link When} conditions
*/
public List<When> getWhenList() {
return whenList;
}

/**
* @return the ELSE {@link Literal} value
*/
public Expression getElseExpression() {
return elseExpression;
}

@Override
public String toString() {
return "CASE " + whenList.stream().map(When::toString).collect(joining(" ")) + (elseExpression != null ? " ELSE " + elseExpression : "") + " END";
}

private static Segment[] children(List<When> whenList, Expression elseExpression) {

List<Segment> segments = new ArrayList<>();
segments.addAll(whenList);

if (elseExpression != null) {
segments.add(elseExpression);
}

return segments.toArray(new Segment[segments.size()]);
}

private final List<When> whenList;
@Nullable
private final Expression elseExpression;

private static Segment[] children(List<When> whenList, @Nullable Expression elseExpression) {

List<Segment> segments = new ArrayList<>(whenList);

if (elseExpression != null) {
segments.add(elseExpression);
}

return segments.toArray(new Segment[0]);
}

private CaseExpression(List<When> whenList, @Nullable Expression elseExpression) {

super(children(whenList, elseExpression));

this.whenList = whenList;
this.elseExpression = elseExpression;
}

/**
* Create CASE {@link Expression} with initial {@link When} condition.
*
* @param condition initial {@link When} condition
* @return the {@link CaseExpression}
*/
public static CaseExpression create(When condition) {
return new CaseExpression(List.of(condition), null);
}

/**
* Add additional {@link When} condition
*
* @param condition the {@link When} condition
* @return the {@link CaseExpression}
*/
public CaseExpression when(When condition) {
List<When> conditions = new ArrayList<>(this.whenList);
conditions.add(condition);
return new CaseExpression(conditions, elseExpression);
}

/**
* Add ELSE clause
*
* @param elseExpression the {@link Expression} else value
* @return the {@link CaseExpression}
*/
public CaseExpression elseExpression(Expression elseExpression) {
return new CaseExpression(whenList, elseExpression);
}

@Override
public String toString() {
return "CASE " + whenList.stream().map(When::toString).collect(joining(" ")) + (elseExpression != null ? " ELSE " + elseExpression : "") + " END";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @since 3.4
*/
public class When extends AbstractSegment {

private final Condition condition;
private final Expression value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author Mark Paluch
* @author Jens Schauder
* @since 1.1
* @author Sven Rienstra
* @see Column
* @see SubselectExpression
*/
Expand All @@ -48,7 +48,7 @@ class ExpressionVisitor extends TypedSubtreeVisitor<Expression> implements PartR
/**
* Creates an {@code ExpressionVisitor}.
*
* @param context must not be {@literal null}.
* @param context must not be {@literal null}.
* @param aliasHandling controls if columns should be rendered as their alias or using their table names.
* @since 2.3
*/
Expand Down Expand Up @@ -109,6 +109,7 @@ Delegation enterMatched(Expression segment) {
partRenderer = visitor;
return Delegation.delegateTo(visitor);
} else if (segment instanceof CaseExpression) {

CaseExpressionVisitor visitor = new CaseExpressionVisitor(context);
partRenderer = visitor;
return Delegation.delegateTo(visitor);
Expand All @@ -132,7 +133,7 @@ Delegation enterNested(Visitable segment) {

if (segment instanceof InlineQuery) {

NoopVisitor<InlineQuery> partRenderer = new NoopVisitor(InlineQuery.class);
NoopVisitor<InlineQuery> partRenderer = new NoopVisitor<>(InlineQuery.class);
return Delegation.delegateTo(partRenderer);
}
return super.enterNested(segment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package org.springframework.data.relational.core.sql.render;


import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.CaseExpression;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.Expressions;
import org.springframework.data.relational.core.sql.OrderByField;
import org.springframework.data.relational.core.sql.SimpleFunction;
Expand All @@ -31,6 +31,7 @@
* @author Jens Schauder
* @author Chirag Tailor
* @author Koen Punt
* @author Sven Rienstra
* @since 1.1
*/
class OrderByClauseVisitor extends TypedSubtreeVisitor<OrderByField> implements PartRenderer {
Expand All @@ -39,7 +40,8 @@ class OrderByClauseVisitor extends TypedSubtreeVisitor<OrderByField> implements

private final StringBuilder builder = new StringBuilder();

@Nullable private PartRenderer delegate;
@Nullable
private PartRenderer delegate;

private boolean first = true;

Expand Down Expand Up @@ -69,7 +71,7 @@ Delegation leaveMatched(OrderByField segment) {

String nullPrecedence = context.getSelectRenderContext().evaluateOrderByNullHandling(segment.getNullHandling());
if (!nullPrecedence.isEmpty()) {

builder.append(" ") //
.append(nullPrecedence);
}
Expand All @@ -82,12 +84,12 @@ Delegation enterNested(Visitable segment) {

if (segment instanceof SimpleFunction) {
delegate = new SimpleFunctionVisitor(context);
return Delegation.delegateTo((SimpleFunctionVisitor)delegate);
return Delegation.delegateTo((SimpleFunctionVisitor) delegate);
}

if (segment instanceof Expressions.SimpleExpression || segment instanceof CaseExpression) {
delegate = new ExpressionVisitor(context);
return Delegation.delegateTo((ExpressionVisitor)delegate);
return Delegation.delegateTo((ExpressionVisitor) delegate);
}

return super.enterNested(segment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @since 3.4
*/
public class WhenVisitor extends TypedSingleConditionRenderSupport<When> implements PartRenderer {

private final StringBuilder part = new StringBuilder();
private boolean conditionRendered;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @author Mark Paluch
* @author Jens Schauder
* @author Koen Punt
* @author Sven Rienstra
*/
class OrderByClauseVisitorUnitTests {

Expand Down Expand Up @@ -125,15 +126,16 @@ void shouldRenderOrderBySimpleExpression() {

@Test
void shouldRenderOrderByCase() {

Table employee = SQL.table("employee").as("emp");
Column column = employee.column("name");

CaseExpression caseExpression = CaseExpression.create(When.when(column.isNull(), SQL.literalOf(1))).elseExpression(SQL.literalOf(2));
CaseExpression caseExpression = CaseExpression.create(When.when(column.isNull(), SQL.literalOf(1))).elseExpression(SQL.literalOf(column));
Select select = Select.builder().select(column).from(employee).orderBy(OrderByField.from(caseExpression).asc()).build();

OrderByClauseVisitor visitor = new OrderByClauseVisitor(new SimpleRenderContext(NamingStrategies.asIs()));
select.visit(visitor);

assertThat(visitor.getRenderedPart().toString()).isEqualTo("CASE WHEN emp.name IS NULL THEN 1 ELSE 2 END ASC");
assertThat(visitor.getRenderedPart().toString()).isEqualTo("CASE WHEN emp.name IS NULL THEN 1 ELSE emp.name END ASC");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Mark Paluch
* @author Jens Schauder
* @author Sven Rienstra
*/
class SelectRendererUnitTests {

Expand Down Expand Up @@ -690,19 +691,20 @@ void asteriskOfAliasedTableUsesAlias() {

@Test
void rendersCaseExpression() {

Table table = SQL.table("table");
Column column = table.column("name");

CaseExpression caseExpression = CaseExpression.create(When.when(column.isNull(), SQL.literalOf(1))) //
.when(When.when(column.isNotNull(), SQL.literalOf(2))) //
.when(When.when(column.isNotNull(), column)) //
.elseExpression(SQL.literalOf(3));

Select select = StatementBuilder.select(caseExpression) //
.from(table) //
.build();

String rendered = SqlRenderer.toString(select);
assertThat(rendered).isEqualTo("SELECT CASE WHEN table.name IS NULL THEN 1 WHEN table.name IS NOT NULL THEN 2 ELSE 3 END FROM table");
assertThat(rendered).isEqualTo("SELECT CASE WHEN table.name IS NULL THEN 1 WHEN table.name IS NOT NULL THEN table.name ELSE 3 END FROM table");
}

/**
Expand Down

0 comments on commit 46d0a06

Please sign in to comment.