Skip to content

Commit 0bd2d2d

Browse files
authored
Merge pull request #784 from jeffgbutler/improve-case-statement
Support Optionals in Case Expressions
2 parents b611ad8 + 43e2377 commit 0bd2d2d

File tree

6 files changed

+23
-9
lines changed

6 files changed

+23
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This log will detail notable changes to MyBatis Dynamic SQL. Full details are av
66

77
This is a minor release with several enhancements.
88

9+
**Important:** This is the last release that will be compatible with Java 8.
10+
911
GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/milestone/13?closed=1](https://github.com/mybatis/mybatis-dynamic-sql/milestone/13?closed=1)
1012

1113
### Case Expressions and Cast Function

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ public SimpleCaseWhenConditionRenderer(RenderingContext renderingContext, Bindab
4646

4747
@Override
4848
public FragmentAndParameters visit(ConditionBasedWhenCondition<T> whenCondition) {
49-
return whenCondition.conditions().map(this::renderCondition)
50-
.collect(FragmentCollector.collect())
51-
.toFragmentAndParameters(Collectors.joining(", ")); //$NON-NLS-1$
49+
FragmentCollector fragmentCollector = whenCondition.conditions()
50+
.filter(this::shouldRender)
51+
.map(this::renderCondition)
52+
.collect(FragmentCollector.collect());
53+
54+
Validator.assertFalse(fragmentCollector.isEmpty(), "ERROR.39"); //$NON-NLS-1$
55+
56+
return fragmentCollector.toFragmentAndParameters(Collectors.joining(", ")); //$NON-NLS-1$
5257
}
5358

5459
@Override
@@ -58,8 +63,11 @@ public FragmentAndParameters visit(BasicWhenCondition<T> whenCondition) {
5863
.toFragmentAndParameters(Collectors.joining(", ")); //$NON-NLS-1$
5964
}
6065

66+
private boolean shouldRender(VisitableCondition<T> condition) {
67+
return condition.shouldRender(renderingContext);
68+
}
69+
6170
private FragmentAndParameters renderCondition(VisitableCondition<T> condition) {
62-
Validator.assertTrue(condition.shouldRender(renderingContext), "ERROR.39"); //$NON-NLS-1$
6371
return condition.accept(conditionVisitor);
6472
}
6573

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public boolean hasMultipleFragments() {
6666
return fragments.size() > 1;
6767
}
6868

69+
public boolean isEmpty() {
70+
return fragments.isEmpty();
71+
}
72+
6973
public static Collector<FragmentAndParameters, FragmentCollector, FragmentCollector> collect() {
7074
return Collector.of(FragmentCollector::new,
7175
FragmentCollector::add,

src/main/resources/org/mybatis/dynamic/sql/util/messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ERROR.35=Multi-select statements must have at least one "union" or "union all" e
5555
ERROR.36=You must either implement the "render" or "renderWithTableAlias" method in a column or function
5656
ERROR.37=The "{0}" function does not support conditions that fail to render
5757
ERROR.38=Bound values cannot be aliased
58-
ERROR.39=When clauses in case expressions must render (optional conditions are not supported)
58+
ERROR.39=When clauses in case expressions must render
5959
ERROR.40=Case expressions must have at least one "when" clause
6060
ERROR.41=You cannot call "then" in a Kotlin case expression more than once
6161
ERROR.42=You cannot call `else` in a Kotlin case expression more than once

src/site/markdown/docs/caseExpressions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ select(case_()
180180
```
181181

182182
The full syntax of "where" and "having" clauses is supported in the "when" clause - but that may or may not be supported
183-
by your database. Testing is crucial. In addition, the library does not support conditions that don't render in a case
184-
statement - so avoid the use of conditions like "isEqualToWhenPresent", etc.
183+
by your database. Testing is crucial. The library supports optional conditions in "when" clauses, but at least one
184+
condition must render, else the library will throw an `InvalidSqlException`.
185185

186186
The rendered SQL will be as follows (without the line breaks):
187187
```sql

src/site/markdown/docs/kotlinCaseExpressions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ select(case {
200200
```
201201

202202
The full syntax of "where" and "having" clauses is supported in the "when" clause - but that may or may not be supported
203-
by your database. Testing is crucial. In addition, the library does not support conditions that don't render in a case
204-
statement - so avoid the use of conditions like "isEqualToWhenPresent", etc.
203+
by your database. Testing is crucial. The library supports optional conditions in "when" clauses, but at least one
204+
condition must render, else the library will throw an `InvalidSqlException`.
205205

206206
The rendered SQL will be as follows (without the line breaks):
207207
```sql

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy