Skip to content

Commit 46d0a06

Browse files
committed
Polishing.
Minor formatting. Adding author tag in Javadoc. Fixing warnings. Original pull request #1844
1 parent b11b85a commit 46d0a06

File tree

7 files changed

+84
-82
lines changed

7 files changed

+84
-82
lines changed
Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.springframework.data.relational.core.sql;
22

3+
import org.springframework.lang.Nullable;
4+
35
import java.util.ArrayList;
46
import java.util.List;
57

6-
import static java.util.stream.Collectors.joining;
8+
import static java.util.stream.Collectors.*;
79

810
/**
911
* Case with one or more conditions expression.
@@ -22,73 +24,64 @@
2224
* @since 3.4
2325
*/
2426
public class CaseExpression extends AbstractSegment implements Expression {
25-
private final List<When> whenList;
26-
private final Expression elseExpression;
27-
28-
private CaseExpression(List<When> whenList, Expression elseExpression) {
29-
30-
super(children(whenList, elseExpression));
31-
this.whenList = whenList;
32-
this.elseExpression = elseExpression;
33-
}
34-
35-
/**
36-
* Create CASE {@link Expression} with initial {@link When} condition.
37-
* @param condition initial {@link When} condition
38-
* @return the {@link CaseExpression}
39-
*/
40-
public static CaseExpression create(When condition) {
41-
return new CaseExpression(List.of(condition), null);
42-
}
43-
44-
/**
45-
* Add additional {@link When} condition
46-
* @param condition the {@link When} condition
47-
* @return the {@link CaseExpression}
48-
*/
49-
public CaseExpression when(When condition) {
50-
List<When> conditions = new ArrayList<>(this.whenList);
51-
conditions.add(condition);
52-
return new CaseExpression(conditions, elseExpression);
53-
}
54-
55-
/**
56-
* Add ELSE clause
57-
* @param elseExpression the {@link Expression} else value
58-
* @return the {@link CaseExpression}
59-
*/
60-
public CaseExpression elseExpression(Literal elseExpression) {
61-
return new CaseExpression(whenList, elseExpression);
62-
}
63-
64-
/**
65-
* @return the {@link When} conditions
66-
*/
67-
public List<When> getWhenList() {
68-
return whenList;
69-
}
70-
71-
/**
72-
* @return the ELSE {@link Literal} value
73-
*/
74-
public Expression getElseExpression() {
75-
return elseExpression;
76-
}
77-
78-
@Override
79-
public String toString() {
80-
return "CASE " + whenList.stream().map(When::toString).collect(joining(" ")) + (elseExpression != null ? " ELSE " + elseExpression : "") + " END";
81-
}
82-
83-
private static Segment[] children(List<When> whenList, Expression elseExpression) {
84-
85-
List<Segment> segments = new ArrayList<>();
86-
segments.addAll(whenList);
87-
88-
if (elseExpression != null) {
89-
segments.add(elseExpression);
90-
}
91-
92-
return segments.toArray(new Segment[segments.size()]);
93-
}
27+
28+
private final List<When> whenList;
29+
@Nullable
30+
private final Expression elseExpression;
31+
32+
private static Segment[] children(List<When> whenList, @Nullable Expression elseExpression) {
33+
34+
List<Segment> segments = new ArrayList<>(whenList);
35+
36+
if (elseExpression != null) {
37+
segments.add(elseExpression);
38+
}
39+
40+
return segments.toArray(new Segment[0]);
41+
}
42+
43+
private CaseExpression(List<When> whenList, @Nullable Expression elseExpression) {
44+
45+
super(children(whenList, elseExpression));
46+
47+
this.whenList = whenList;
48+
this.elseExpression = elseExpression;
49+
}
50+
51+
/**
52+
* Create CASE {@link Expression} with initial {@link When} condition.
53+
*
54+
* @param condition initial {@link When} condition
55+
* @return the {@link CaseExpression}
56+
*/
57+
public static CaseExpression create(When condition) {
58+
return new CaseExpression(List.of(condition), null);
59+
}
60+
61+
/**
62+
* Add additional {@link When} condition
63+
*
64+
* @param condition the {@link When} condition
65+
* @return the {@link CaseExpression}
66+
*/
67+
public CaseExpression when(When condition) {
68+
List<When> conditions = new ArrayList<>(this.whenList);
69+
conditions.add(condition);
70+
return new CaseExpression(conditions, elseExpression);
71+
}
72+
73+
/**
74+
* Add ELSE clause
75+
*
76+
* @param elseExpression the {@link Expression} else value
77+
* @return the {@link CaseExpression}
78+
*/
79+
public CaseExpression elseExpression(Expression elseExpression) {
80+
return new CaseExpression(whenList, elseExpression);
81+
}
82+
83+
@Override
84+
public String toString() {
85+
return "CASE " + whenList.stream().map(When::toString).collect(joining(" ")) + (elseExpression != null ? " ELSE " + elseExpression : "") + " END";
86+
}
9487
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/When.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @since 3.4
1111
*/
1212
public class When extends AbstractSegment {
13+
1314
private final Condition condition;
1415
private final Expression value;
1516

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/ExpressionVisitor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @author Mark Paluch
2626
* @author Jens Schauder
27-
* @since 1.1
27+
* @author Sven Rienstra
2828
* @see Column
2929
* @see SubselectExpression
3030
*/
@@ -48,7 +48,7 @@ class ExpressionVisitor extends TypedSubtreeVisitor<Expression> implements PartR
4848
/**
4949
* Creates an {@code ExpressionVisitor}.
5050
*
51-
* @param context must not be {@literal null}.
51+
* @param context must not be {@literal null}.
5252
* @param aliasHandling controls if columns should be rendered as their alias or using their table names.
5353
* @since 2.3
5454
*/
@@ -109,6 +109,7 @@ Delegation enterMatched(Expression segment) {
109109
partRenderer = visitor;
110110
return Delegation.delegateTo(visitor);
111111
} else if (segment instanceof CaseExpression) {
112+
112113
CaseExpressionVisitor visitor = new CaseExpressionVisitor(context);
113114
partRenderer = visitor;
114115
return Delegation.delegateTo(visitor);
@@ -132,7 +133,7 @@ Delegation enterNested(Visitable segment) {
132133

133134
if (segment instanceof InlineQuery) {
134135

135-
NoopVisitor<InlineQuery> partRenderer = new NoopVisitor(InlineQuery.class);
136+
NoopVisitor<InlineQuery> partRenderer = new NoopVisitor<>(InlineQuery.class);
136137
return Delegation.delegateTo(partRenderer);
137138
}
138139
return super.enterNested(segment);

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/OrderByClauseVisitor.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package org.springframework.data.relational.core.sql.render;
1717

1818

19-
import org.springframework.data.relational.core.sql.Column;
2019
import org.springframework.data.relational.core.sql.CaseExpression;
20+
import org.springframework.data.relational.core.sql.Column;
2121
import org.springframework.data.relational.core.sql.Expressions;
2222
import org.springframework.data.relational.core.sql.OrderByField;
2323
import org.springframework.data.relational.core.sql.SimpleFunction;
@@ -31,6 +31,7 @@
3131
* @author Jens Schauder
3232
* @author Chirag Tailor
3333
* @author Koen Punt
34+
* @author Sven Rienstra
3435
* @since 1.1
3536
*/
3637
class OrderByClauseVisitor extends TypedSubtreeVisitor<OrderByField> implements PartRenderer {
@@ -39,7 +40,8 @@ class OrderByClauseVisitor extends TypedSubtreeVisitor<OrderByField> implements
3940

4041
private final StringBuilder builder = new StringBuilder();
4142

42-
@Nullable private PartRenderer delegate;
43+
@Nullable
44+
private PartRenderer delegate;
4345

4446
private boolean first = true;
4547

@@ -69,7 +71,7 @@ Delegation leaveMatched(OrderByField segment) {
6971

7072
String nullPrecedence = context.getSelectRenderContext().evaluateOrderByNullHandling(segment.getNullHandling());
7173
if (!nullPrecedence.isEmpty()) {
72-
74+
7375
builder.append(" ") //
7476
.append(nullPrecedence);
7577
}
@@ -82,12 +84,12 @@ Delegation enterNested(Visitable segment) {
8284

8385
if (segment instanceof SimpleFunction) {
8486
delegate = new SimpleFunctionVisitor(context);
85-
return Delegation.delegateTo((SimpleFunctionVisitor)delegate);
87+
return Delegation.delegateTo((SimpleFunctionVisitor) delegate);
8688
}
8789

8890
if (segment instanceof Expressions.SimpleExpression || segment instanceof CaseExpression) {
8991
delegate = new ExpressionVisitor(context);
90-
return Delegation.delegateTo((ExpressionVisitor)delegate);
92+
return Delegation.delegateTo((ExpressionVisitor) delegate);
9193
}
9294

9395
return super.enterNested(segment);

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/WhenVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @since 3.4
1111
*/
1212
public class WhenVisitor extends TypedSingleConditionRenderSupport<When> implements PartRenderer {
13+
1314
private final StringBuilder part = new StringBuilder();
1415
private boolean conditionRendered;
1516

spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/OrderByClauseVisitorUnitTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @author Mark Paluch
3030
* @author Jens Schauder
3131
* @author Koen Punt
32+
* @author Sven Rienstra
3233
*/
3334
class OrderByClauseVisitorUnitTests {
3435

@@ -125,15 +126,16 @@ void shouldRenderOrderBySimpleExpression() {
125126

126127
@Test
127128
void shouldRenderOrderByCase() {
129+
128130
Table employee = SQL.table("employee").as("emp");
129131
Column column = employee.column("name");
130132

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

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

137-
assertThat(visitor.getRenderedPart().toString()).isEqualTo("CASE WHEN emp.name IS NULL THEN 1 ELSE 2 END ASC");
139+
assertThat(visitor.getRenderedPart().toString()).isEqualTo("CASE WHEN emp.name IS NULL THEN 1 ELSE emp.name END ASC");
138140
}
139141
}

spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*
3232
* @author Mark Paluch
3333
* @author Jens Schauder
34+
* @author Sven Rienstra
3435
*/
3536
class SelectRendererUnitTests {
3637

@@ -690,19 +691,20 @@ void asteriskOfAliasedTableUsesAlias() {
690691

691692
@Test
692693
void rendersCaseExpression() {
694+
693695
Table table = SQL.table("table");
694696
Column column = table.column("name");
695697

696698
CaseExpression caseExpression = CaseExpression.create(When.when(column.isNull(), SQL.literalOf(1))) //
697-
.when(When.when(column.isNotNull(), SQL.literalOf(2))) //
699+
.when(When.when(column.isNotNull(), column)) //
698700
.elseExpression(SQL.literalOf(3));
699701

700702
Select select = StatementBuilder.select(caseExpression) //
701703
.from(table) //
702704
.build();
703705

704706
String rendered = SqlRenderer.toString(select);
705-
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");
707+
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");
706708
}
707709

708710
/**

0 commit comments

Comments
 (0)