Skip to content

Support Optionals in Case Expressions #784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This log will detail notable changes to MyBatis Dynamic SQL. Full details are av

This is a minor release with several enhancements.

**Important:** This is the last release that will be compatible with Java 8.

GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/milestone/13?closed=1](https://github.com/mybatis/mybatis-dynamic-sql/milestone/13?closed=1)

### Case Expressions and Cast Function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ public SimpleCaseWhenConditionRenderer(RenderingContext renderingContext, Bindab

@Override
public FragmentAndParameters visit(ConditionBasedWhenCondition<T> whenCondition) {
return whenCondition.conditions().map(this::renderCondition)
.collect(FragmentCollector.collect())
.toFragmentAndParameters(Collectors.joining(", ")); //$NON-NLS-1$
FragmentCollector fragmentCollector = whenCondition.conditions()
.filter(this::shouldRender)
.map(this::renderCondition)
.collect(FragmentCollector.collect());

Validator.assertFalse(fragmentCollector.isEmpty(), "ERROR.39"); //$NON-NLS-1$

return fragmentCollector.toFragmentAndParameters(Collectors.joining(", ")); //$NON-NLS-1$
}

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

private boolean shouldRender(VisitableCondition<T> condition) {
return condition.shouldRender(renderingContext);
}

private FragmentAndParameters renderCondition(VisitableCondition<T> condition) {
Validator.assertTrue(condition.shouldRender(renderingContext), "ERROR.39"); //$NON-NLS-1$
return condition.accept(conditionVisitor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public boolean hasMultipleFragments() {
return fragments.size() > 1;
}

public boolean isEmpty() {
return fragments.isEmpty();
}

public static Collector<FragmentAndParameters, FragmentCollector, FragmentCollector> collect() {
return Collector.of(FragmentCollector::new,
FragmentCollector::add,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ERROR.35=Multi-select statements must have at least one "union" or "union all" e
ERROR.36=You must either implement the "render" or "renderWithTableAlias" method in a column or function
ERROR.37=The "{0}" function does not support conditions that fail to render
ERROR.38=Bound values cannot be aliased
ERROR.39=When clauses in case expressions must render (optional conditions are not supported)
ERROR.39=When clauses in case expressions must render
ERROR.40=Case expressions must have at least one "when" clause
ERROR.41=You cannot call "then" in a Kotlin case expression more than once
ERROR.42=You cannot call `else` in a Kotlin case expression more than once
Expand Down
4 changes: 2 additions & 2 deletions src/site/markdown/docs/caseExpressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ select(case_()
```

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

The rendered SQL will be as follows (without the line breaks):
```sql
Expand Down
4 changes: 2 additions & 2 deletions src/site/markdown/docs/kotlinCaseExpressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ select(case {
```

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

The rendered SQL will be as follows (without the line breaks):
```sql
Expand Down
Loading