Skip to content

Commit f712cb3

Browse files
committed
Add support for simple case with basic values
1 parent c3c9717 commit f712cb3

17 files changed

+488
-79
lines changed

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
import org.mybatis.dynamic.sql.insert.InsertDSL;
2929
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
3030
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
31+
import org.mybatis.dynamic.sql.select.caseexpression.SearchedCaseDSL;
32+
import org.mybatis.dynamic.sql.select.caseexpression.SimpleCaseDSL;
3133
import org.mybatis.dynamic.sql.select.ColumnSortSpecification;
3234
import org.mybatis.dynamic.sql.select.CountDSL;
3335
import org.mybatis.dynamic.sql.select.HavingDSL;
3436
import org.mybatis.dynamic.sql.select.MultiSelectDSL;
3537
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
36-
import org.mybatis.dynamic.sql.select.SearchedCaseDSL;
3738
import org.mybatis.dynamic.sql.select.SelectDSL;
3839
import org.mybatis.dynamic.sql.select.SelectModel;
39-
import org.mybatis.dynamic.sql.select.SimpleCaseDSL;
4040
import org.mybatis.dynamic.sql.select.SimpleSortSpecification;
4141
import org.mybatis.dynamic.sql.select.aggregate.Avg;
4242
import org.mybatis.dynamic.sql.select.aggregate.Count;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select.caseexpression;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.stream.Stream;
21+
22+
public class BasicWhenCondition<T> extends SimpleCaseWhenCondition<T> {
23+
private final List<T> conditions = new ArrayList<>();
24+
25+
public BasicWhenCondition(List<T> conditions, Object thenValue) {
26+
super(thenValue);
27+
this.conditions.addAll(conditions);
28+
}
29+
30+
public Stream<T> conditions() {
31+
return conditions.stream();
32+
}
33+
34+
@Override
35+
public <R> R accept(SimpleCaseWhenConditionVisitor<T, R> visitor) {
36+
return visitor.visit(this);
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select.caseexpression;
17+
18+
import org.mybatis.dynamic.sql.VisitableCondition;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import java.util.stream.Stream;
23+
24+
public class ConditionBasedWhenCondition<T> extends SimpleCaseWhenCondition<T> {
25+
private final List<VisitableCondition<T>> conditions = new ArrayList<>();
26+
27+
public ConditionBasedWhenCondition(List<VisitableCondition<T>> conditions, Object thenValue) {
28+
super(thenValue);
29+
this.conditions.addAll(conditions);
30+
}
31+
32+
public Stream<VisitableCondition<T>> conditions() {
33+
return conditions.stream();
34+
}
35+
36+
@Override
37+
public <R> R accept(SimpleCaseWhenConditionVisitor<T, R> visitor) {
38+
return visitor.visit(this);
39+
}
40+
}

src/main/java/org/mybatis/dynamic/sql/select/SearchedCaseDSL.java renamed to src/main/java/org/mybatis/dynamic/sql/select/caseexpression/SearchedCaseDSL.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.mybatis.dynamic.sql.select;
16+
package org.mybatis.dynamic.sql.select.caseexpression;
1717

1818
import static org.mybatis.dynamic.sql.util.StringUtilities.quoteStringForSQL;
1919

src/main/java/org/mybatis/dynamic/sql/select/SearchedCaseModel.java renamed to src/main/java/org/mybatis/dynamic/sql/select/caseexpression/SearchedCaseModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.mybatis.dynamic.sql.select;
16+
package org.mybatis.dynamic.sql.select.caseexpression;
1717

1818
import java.util.ArrayList;
1919
import java.util.List;

src/main/java/org/mybatis/dynamic/sql/select/SimpleCaseDSL.java renamed to src/main/java/org/mybatis/dynamic/sql/select/caseexpression/SimpleCaseDSL.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.mybatis.dynamic.sql.select;
16+
package org.mybatis.dynamic.sql.select.caseexpression;
1717

1818
import static org.mybatis.dynamic.sql.util.StringUtilities.quoteStringForSQL;
1919

@@ -28,22 +28,31 @@
2828

2929
public class SimpleCaseDSL<T> {
3030
private final BindableColumn<T> column;
31-
private final List<SimpleCaseModel.SimpleWhenCondition<T>> whenConditions = new ArrayList<>();
31+
private final List<SimpleCaseWhenCondition<T>> whenConditions = new ArrayList<>();
3232
private Object elseValue;
3333

3434
private SimpleCaseDSL(BindableColumn<T> column) {
3535
this.column = Objects.requireNonNull(column);
3636
}
3737

3838
@SafeVarargs
39-
public final WhenFinisher when(VisitableCondition<T> condition,
40-
VisitableCondition<T>... subsequentConditions) {
39+
public final ConditionBasedWhenFinisher when(VisitableCondition<T> condition,
40+
VisitableCondition<T>... subsequentConditions) {
4141
return when(condition, Arrays.asList(subsequentConditions));
4242
}
4343

44-
public WhenFinisher when(VisitableCondition<T> condition,
45-
List<VisitableCondition<T>> subsequentConditions) {
46-
return new WhenFinisher(condition, subsequentConditions);
44+
public ConditionBasedWhenFinisher when(VisitableCondition<T> condition,
45+
List<VisitableCondition<T>> subsequentConditions) {
46+
return new ConditionBasedWhenFinisher(condition, subsequentConditions);
47+
}
48+
49+
@SafeVarargs
50+
public final BasicWhenFinisher when(T condition, T... subsequentConditions) {
51+
return when(condition, Arrays.asList(subsequentConditions));
52+
}
53+
54+
public BasicWhenFinisher when(T condition, List<T> subsequentConditions) {
55+
return new BasicWhenFinisher(condition, subsequentConditions);
4756
}
4857

4958
@SuppressWarnings("java:S100")
@@ -66,21 +75,40 @@ public BasicColumn end() {
6675
.build();
6776
}
6877

69-
public class WhenFinisher {
78+
public class ConditionBasedWhenFinisher {
7079
private final List<VisitableCondition<T>> conditions = new ArrayList<>();
7180

72-
private WhenFinisher(VisitableCondition<T> condition, List<VisitableCondition<T>> subsequentConditions) {
81+
private ConditionBasedWhenFinisher(VisitableCondition<T> condition, List<VisitableCondition<T>> subsequentConditions) {
7382
conditions.add(condition);
7483
conditions.addAll(subsequentConditions);
7584
}
7685

7786
public SimpleCaseDSL<T> then(String value) {
78-
whenConditions.add(new SimpleCaseModel.SimpleWhenCondition<>(conditions, quoteStringForSQL(value)));
87+
whenConditions.add(new ConditionBasedWhenCondition<>(conditions, quoteStringForSQL(value)));
88+
return SimpleCaseDSL.this;
89+
}
90+
91+
public SimpleCaseDSL<T> then(Object value) {
92+
whenConditions.add(new ConditionBasedWhenCondition<>(conditions, value));
93+
return SimpleCaseDSL.this;
94+
}
95+
}
96+
97+
public class BasicWhenFinisher {
98+
private final List<T> values = new ArrayList<>();
99+
100+
private BasicWhenFinisher(T value, List<T> subsequentValues) {
101+
values.add(value);
102+
values.addAll(subsequentValues);
103+
}
104+
105+
public SimpleCaseDSL<T> then(String value) {
106+
whenConditions.add(new BasicWhenCondition<>(values, quoteStringForSQL(value)));
79107
return SimpleCaseDSL.this;
80108
}
81109

82110
public SimpleCaseDSL<T> then(Object value) {
83-
whenConditions.add(new SimpleCaseModel.SimpleWhenCondition<>(conditions, value));
111+
whenConditions.add(new BasicWhenCondition<>(values, value));
84112
return SimpleCaseDSL.this;
85113
}
86114
}

src/main/java/org/mybatis/dynamic/sql/select/SimpleCaseModel.java renamed to src/main/java/org/mybatis/dynamic/sql/select/caseexpression/SimpleCaseModel.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.mybatis.dynamic.sql.select;
16+
package org.mybatis.dynamic.sql.select.caseexpression;
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
@@ -23,15 +23,14 @@
2323

2424
import org.mybatis.dynamic.sql.BasicColumn;
2525
import org.mybatis.dynamic.sql.BindableColumn;
26-
import org.mybatis.dynamic.sql.VisitableCondition;
2726
import org.mybatis.dynamic.sql.render.RenderingContext;
2827
import org.mybatis.dynamic.sql.select.render.SimpleCaseRenderer;
2928
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
3029
import org.mybatis.dynamic.sql.util.Validator;
3130

3231
public class SimpleCaseModel<T> implements BasicColumn {
3332
private final BindableColumn<T> column;
34-
private final List<SimpleWhenCondition<T>> whenConditions;
33+
private final List<SimpleCaseWhenCondition<T>> whenConditions;
3534
private final Object elseValue;
3635
private final String alias;
3736

@@ -47,7 +46,7 @@ public BindableColumn<T> column() {
4746
return column;
4847
}
4948

50-
public Stream<SimpleWhenCondition<T>> whenConditions() {
49+
public Stream<SimpleCaseWhenCondition<T>> whenConditions() {
5150
return whenConditions.stream();
5251
}
5352

@@ -75,27 +74,9 @@ public FragmentAndParameters render(RenderingContext renderingContext) {
7574
return new SimpleCaseRenderer<>(this, renderingContext).render();
7675
}
7776

78-
public static class SimpleWhenCondition<T> {
79-
private final List<VisitableCondition<T>> conditions = new ArrayList<>();
80-
private final Object thenValue;
81-
82-
public Stream<VisitableCondition<T>> conditions() {
83-
return conditions.stream();
84-
}
85-
86-
public Object thenValue() {
87-
return thenValue;
88-
}
89-
90-
public SimpleWhenCondition(List<VisitableCondition<T>> conditions, Object thenValue) {
91-
this.conditions.addAll(conditions);
92-
this.thenValue = Objects.requireNonNull(thenValue);
93-
}
94-
}
95-
9677
public static class Builder<T> {
9778
private BindableColumn<T> column;
98-
private final List<SimpleWhenCondition<T>> whenConditions = new ArrayList<>();
79+
private final List<SimpleCaseWhenCondition<T>> whenConditions = new ArrayList<>();
9980
private Object elseValue;
10081
private String alias;
10182

@@ -104,7 +85,7 @@ public Builder<T> withColumn(BindableColumn<T> column) {
10485
return this;
10586
}
10687

107-
public Builder<T> withWhenConditions(List<SimpleWhenCondition<T>> whenConditions) {
88+
public Builder<T> withWhenConditions(List<SimpleCaseWhenCondition<T>> whenConditions) {
10889
this.whenConditions.addAll(whenConditions);
10990
return this;
11091
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select.caseexpression;
17+
18+
import java.util.Objects;
19+
20+
public abstract class SimpleCaseWhenCondition<T> {
21+
private final Object thenValue;
22+
23+
protected SimpleCaseWhenCondition(Object thenValue) {
24+
this.thenValue = Objects.requireNonNull(thenValue);
25+
}
26+
27+
public Object thenValue() {
28+
return thenValue;
29+
}
30+
31+
public abstract <R> R accept(SimpleCaseWhenConditionVisitor<T, R> visitor);
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select.caseexpression;
17+
18+
public interface SimpleCaseWhenConditionVisitor<T, R> {
19+
R visit(ConditionBasedWhenCondition<T> whenCondition);
20+
21+
R visit(BasicWhenCondition<T> whenCondition);
22+
}

src/main/java/org/mybatis/dynamic/sql/select/render/SearchedCaseRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2424
import org.mybatis.dynamic.sql.render.RenderingContext;
25-
import org.mybatis.dynamic.sql.select.SearchedCaseModel;
25+
import org.mybatis.dynamic.sql.select.caseexpression.SearchedCaseModel;
2626
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
2727
import org.mybatis.dynamic.sql.util.FragmentCollector;
2828
import org.mybatis.dynamic.sql.util.Messages;

0 commit comments

Comments
 (0)