Skip to content

Commit c3c9717

Browse files
committed
Better support for string constants
1 parent faa811d commit c3c9717

File tree

9 files changed

+377
-100
lines changed

9 files changed

+377
-100
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,13 @@ static AndOrCriteriaGroup and(List<AndOrCriteriaGroup> subCriteria) {
428428
}
429429

430430
// case expressions
431-
static <T> SimpleCaseDSL<T> simpleCase(BindableColumn<T> column) {
431+
@SuppressWarnings("java:S100")
432+
static <T> SimpleCaseDSL<T> case_(BindableColumn<T> column) {
432433
return SimpleCaseDSL.simpleCase(column);
433434
}
434435

435-
static SearchedCaseDSL searchedCase() {
436+
@SuppressWarnings("java:S100")
437+
static SearchedCaseDSL case_() {
436438
return SearchedCaseDSL.searchedCase();
437439
}
438440

src/main/java/org/mybatis/dynamic/sql/select/SearchedCaseDSL.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql.select;
1717

18+
import static org.mybatis.dynamic.sql.util.StringUtilities.quoteStringForSQL;
19+
1820
import java.util.ArrayList;
1921
import java.util.Arrays;
2022
import java.util.List;
@@ -30,7 +32,7 @@
3032

3133
public class SearchedCaseDSL {
3234
private final List<SearchedCaseModel.SearchedWhenCondition> whenConditions = new ArrayList<>();
33-
private String elseValue;
35+
private Object elseValue;
3436

3537
public <T> WhenDSL when(BindableColumn<T> column, VisitableCondition<T> condition,
3638
AndOrCriteriaGroup... subCriteria) {
@@ -64,8 +66,15 @@ private WhenDSL initialize(SqlCriterion sqlCriterion) {
6466
return new WhenDSL(sqlCriterion);
6567
}
6668

67-
public SearchedCaseEnder else_(String elseValue) {
68-
this.elseValue = elseValue;
69+
@SuppressWarnings("java:S100")
70+
public SearchedCaseEnder else_(String value) {
71+
this.elseValue = quoteStringForSQL(value);
72+
return new SearchedCaseEnder();
73+
}
74+
75+
@SuppressWarnings("java:S100")
76+
public SearchedCaseEnder else_(Object value) {
77+
this.elseValue = value;
6978
return new SearchedCaseEnder();
7079
}
7180

@@ -82,6 +91,12 @@ private WhenDSL(SqlCriterion sqlCriterion) {
8291
}
8392

8493
public SearchedCaseDSL then(String value) {
94+
whenConditions.add(new SearchedCaseModel.SearchedWhenCondition(getInitialCriterion(), subCriteria,
95+
quoteStringForSQL(value)));
96+
return SearchedCaseDSL.this;
97+
}
98+
99+
public SearchedCaseDSL then(Object value) {
85100
whenConditions.add(new SearchedCaseModel.SearchedWhenCondition(getInitialCriterion(), subCriteria, value));
86101
return SearchedCaseDSL.this;
87102
}

src/main/java/org/mybatis/dynamic/sql/select/SearchedCaseModel.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
public class SearchedCaseModel implements BasicColumn {
3434
private final List<SearchedWhenCondition> whenConditions;
35-
private final String elseValue;
35+
private final Object elseValue;
3636
private final String alias;
3737

3838
private SearchedCaseModel(Builder builder) {
@@ -46,7 +46,7 @@ public Stream<SearchedWhenCondition> whenConditions() {
4646
return whenConditions.stream();
4747
}
4848

49-
public Optional<String> elseValue() {
49+
public Optional<Object> elseValue() {
5050
return Optional.ofNullable(elseValue);
5151
}
5252

@@ -70,30 +70,30 @@ public FragmentAndParameters render(RenderingContext renderingContext) {
7070

7171
public static class SearchedWhenCondition extends AbstractBooleanExpressionModel {
7272

73-
private final String thenValue;
73+
private final Object thenValue;
7474

75-
public String thenValue() {
75+
public Object thenValue() {
7676
return thenValue;
7777
}
7878

7979
public SearchedWhenCondition(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria,
80-
String thenValue) {
80+
Object thenValue) {
8181
super(initialCriterion, subCriteria);
8282
this.thenValue = Objects.requireNonNull(thenValue);
8383
}
8484
}
8585

8686
public static class Builder {
8787
private final List<SearchedWhenCondition> whenConditions = new ArrayList<>();
88-
private String elseValue;
88+
private Object elseValue;
8989
private String alias;
9090

9191
public Builder withWhenConditions(List<SearchedWhenCondition> whenConditions) {
9292
this.whenConditions.addAll(whenConditions);
9393
return this;
9494
}
9595

96-
public Builder withElseValue(String elseValue) {
96+
public Builder withElseValue(Object elseValue) {
9797
this.elseValue = elseValue;
9898
return this;
9999
}

src/main/java/org/mybatis/dynamic/sql/select/SimpleCaseDSL.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql.select;
1717

18+
import static org.mybatis.dynamic.sql.util.StringUtilities.quoteStringForSQL;
19+
1820
import java.util.ArrayList;
1921
import java.util.Arrays;
2022
import java.util.List;
@@ -27,7 +29,7 @@
2729
public class SimpleCaseDSL<T> {
2830
private final BindableColumn<T> column;
2931
private final List<SimpleCaseModel.SimpleWhenCondition<T>> whenConditions = new ArrayList<>();
30-
private String elseValue;
32+
private Object elseValue;
3133

3234
private SimpleCaseDSL(BindableColumn<T> column) {
3335
this.column = Objects.requireNonNull(column);
@@ -44,7 +46,14 @@ public WhenFinisher when(VisitableCondition<T> condition,
4446
return new WhenFinisher(condition, subsequentConditions);
4547
}
4648

49+
@SuppressWarnings("java:S100")
4750
public SimpleCaseEnder else_(String value) {
51+
elseValue = quoteStringForSQL(value);
52+
return new SimpleCaseEnder();
53+
}
54+
55+
@SuppressWarnings("java:S100")
56+
public SimpleCaseEnder else_(Object value) {
4857
elseValue = value;
4958
return new SimpleCaseEnder();
5059
}
@@ -66,6 +75,11 @@ private WhenFinisher(VisitableCondition<T> condition, List<VisitableCondition<T>
6675
}
6776

6877
public SimpleCaseDSL<T> then(String value) {
78+
whenConditions.add(new SimpleCaseModel.SimpleWhenCondition<>(conditions, quoteStringForSQL(value)));
79+
return SimpleCaseDSL.this;
80+
}
81+
82+
public SimpleCaseDSL<T> then(Object value) {
6983
whenConditions.add(new SimpleCaseModel.SimpleWhenCondition<>(conditions, value));
7084
return SimpleCaseDSL.this;
7185
}

src/main/java/org/mybatis/dynamic/sql/select/SimpleCaseModel.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public class SimpleCaseModel<T> implements BasicColumn {
3333
private final BindableColumn<T> column;
3434
private final List<SimpleWhenCondition<T>> whenConditions;
35-
private final String elseValue;
35+
private final Object elseValue;
3636
private final String alias;
3737

3838
private SimpleCaseModel(Builder<T> builder) {
@@ -77,7 +77,7 @@ public FragmentAndParameters render(RenderingContext renderingContext) {
7777

7878
public static class SimpleWhenCondition<T> {
7979
private final List<VisitableCondition<T>> conditions = new ArrayList<>();
80-
private final String thenValue;
80+
private final Object thenValue;
8181

8282
public Stream<VisitableCondition<T>> conditions() {
8383
return conditions.stream();
@@ -87,7 +87,7 @@ public Object thenValue() {
8787
return thenValue;
8888
}
8989

90-
public SimpleWhenCondition(List<VisitableCondition<T>> conditions, String thenValue) {
90+
public SimpleWhenCondition(List<VisitableCondition<T>> conditions, Object thenValue) {
9191
this.conditions.addAll(conditions);
9292
this.thenValue = Objects.requireNonNull(thenValue);
9393
}
@@ -96,7 +96,7 @@ public SimpleWhenCondition(List<VisitableCondition<T>> conditions, String thenVa
9696
public static class Builder<T> {
9797
private BindableColumn<T> column;
9898
private final List<SimpleWhenCondition<T>> whenConditions = new ArrayList<>();
99-
private String elseValue;
99+
private Object elseValue;
100100
private String alias;
101101

102102
public Builder<T> withColumn(BindableColumn<T> column) {
@@ -109,7 +109,7 @@ public Builder<T> withWhenConditions(List<SimpleWhenCondition<T>> whenConditions
109109
return this;
110110
}
111111

112-
public Builder<T> withElseValue(String elseValue) {
112+
public Builder<T> withElseValue(Object elseValue) {
113113
this.elseValue = elseValue;
114114
return this;
115115
}

src/main/java/org/mybatis/dynamic/sql/util/StringUtilities.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ static String toCamelCase(String inputString) {
5252

5353
return sb.toString();
5454
}
55+
56+
static String quoteStringForSQL(String value) {
57+
return "'" + value + "'"; //$NON-NLS-1$ //$NON-NLS-2$
58+
}
5559
}

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/CaseDSLs.kt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.mybatis.dynamic.sql.util.kotlin.GroupingCriteriaCollector
2222
import org.mybatis.dynamic.sql.util.kotlin.assertNull
2323

2424
class KSearchedCaseDSL {
25-
internal var elseValue: String? = null
25+
internal var elseValue: Any? = null
2626
private set(value) {
2727
assertNull(field, "ERROR.42") //$NON-NLS-1$
2828
field = value
@@ -35,24 +35,32 @@ class KSearchedCaseDSL {
3535
}
3636

3737
fun `else`(value: String) {
38+
this.elseValue = "'$value'"
39+
}
40+
41+
fun `else`(value: Any) {
3842
this.elseValue = value
3943
}
4044
}
4145

4246
class SearchedCaseCriteriaCollector : GroupingCriteriaCollector() {
43-
internal var thenValue: String? = null
47+
internal var thenValue: Any? = null
4448
private set(value) {
4549
assertNull(field, "ERROR.41") //$NON-NLS-1$
4650
field = value
4751
}
4852

4953
fun then(value: String) {
54+
this.thenValue = "'$value'"
55+
}
56+
57+
fun then(value: Any) {
5058
this.thenValue = value
5159
}
5260
}
5361

5462
class KSimpleCaseDSL<T : Any> {
55-
internal var elseValue: String? = null
63+
internal var elseValue: Any? = null
5664
private set(value) {
5765
assertNull(field, "ERROR.42") //$NON-NLS-1$
5866
field = value
@@ -63,6 +71,10 @@ class KSimpleCaseDSL<T : Any> {
6371
SimpleCaseThenGatherer(condition, conditions.asList())
6472

6573
fun `else`(value: String) {
74+
this.elseValue = "'$value'"
75+
}
76+
77+
fun `else`(value: Any) {
6678
this.elseValue = value
6779
}
6880

@@ -74,6 +86,15 @@ class KSimpleCaseDSL<T : Any> {
7486
addAll(conditions)
7587
}
7688

89+
whenConditions.add(SimpleWhenCondition(allConditions, "'$value'"))
90+
}
91+
92+
fun then(value: Any) {
93+
val allConditions = buildList {
94+
add(condition)
95+
addAll(conditions)
96+
}
97+
7798
whenConditions.add(SimpleWhenCondition(allConditions, value))
7899
}
79100
}

0 commit comments

Comments
 (0)