diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d3a6cc9..65976c625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/org/mybatis/dynamic/sql/select/render/SimpleCaseWhenConditionRenderer.java b/src/main/java/org/mybatis/dynamic/sql/select/render/SimpleCaseWhenConditionRenderer.java index 4bb8d8aff..e9108806e 100644 --- a/src/main/java/org/mybatis/dynamic/sql/select/render/SimpleCaseWhenConditionRenderer.java +++ b/src/main/java/org/mybatis/dynamic/sql/select/render/SimpleCaseWhenConditionRenderer.java @@ -46,9 +46,14 @@ public SimpleCaseWhenConditionRenderer(RenderingContext renderingContext, Bindab @Override public FragmentAndParameters visit(ConditionBasedWhenCondition 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 @@ -58,8 +63,11 @@ public FragmentAndParameters visit(BasicWhenCondition whenCondition) { .toFragmentAndParameters(Collectors.joining(", ")); //$NON-NLS-1$ } + private boolean shouldRender(VisitableCondition condition) { + return condition.shouldRender(renderingContext); + } + private FragmentAndParameters renderCondition(VisitableCondition condition) { - Validator.assertTrue(condition.shouldRender(renderingContext), "ERROR.39"); //$NON-NLS-1$ return condition.accept(conditionVisitor); } diff --git a/src/main/java/org/mybatis/dynamic/sql/util/FragmentCollector.java b/src/main/java/org/mybatis/dynamic/sql/util/FragmentCollector.java index 553b2d352..cc043572f 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/FragmentCollector.java +++ b/src/main/java/org/mybatis/dynamic/sql/util/FragmentCollector.java @@ -66,6 +66,10 @@ public boolean hasMultipleFragments() { return fragments.size() > 1; } + public boolean isEmpty() { + return fragments.isEmpty(); + } + public static Collector collect() { return Collector.of(FragmentCollector::new, FragmentCollector::add, diff --git a/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties b/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties index e7b125583..9927dbeec 100644 --- a/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties +++ b/src/main/resources/org/mybatis/dynamic/sql/util/messages.properties @@ -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 diff --git a/src/site/markdown/docs/caseExpressions.md b/src/site/markdown/docs/caseExpressions.md index fe076811b..9b83d4f3e 100644 --- a/src/site/markdown/docs/caseExpressions.md +++ b/src/site/markdown/docs/caseExpressions.md @@ -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 diff --git a/src/site/markdown/docs/kotlinCaseExpressions.md b/src/site/markdown/docs/kotlinCaseExpressions.md index f34a30f18..f35a298da 100644 --- a/src/site/markdown/docs/kotlinCaseExpressions.md +++ b/src/site/markdown/docs/kotlinCaseExpressions.md @@ -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