View Javadoc
1   /*
2    *    Copyright 2016-2025 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.render;
17  
18  import java.util.Objects;
19  import java.util.stream.Collectors;
20  
21  import org.mybatis.dynamic.sql.BindableColumn;
22  import org.mybatis.dynamic.sql.RenderableCondition;
23  import org.mybatis.dynamic.sql.render.RenderedParameterInfo;
24  import org.mybatis.dynamic.sql.render.RenderingContext;
25  import org.mybatis.dynamic.sql.select.caseexpression.BasicWhenCondition;
26  import org.mybatis.dynamic.sql.select.caseexpression.ConditionBasedWhenCondition;
27  import org.mybatis.dynamic.sql.select.caseexpression.SimpleCaseWhenConditionVisitor;
28  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
29  import org.mybatis.dynamic.sql.util.FragmentCollector;
30  import org.mybatis.dynamic.sql.util.Validator;
31  
32  public class SimpleCaseWhenConditionRenderer<T> implements SimpleCaseWhenConditionVisitor<T, FragmentAndParameters> {
33      private final RenderingContext renderingContext;
34      private final BindableColumn<T> column;
35  
36      public SimpleCaseWhenConditionRenderer(RenderingContext renderingContext, BindableColumn<T> column) {
37          this.renderingContext = Objects.requireNonNull(renderingContext);
38          this.column = Objects.requireNonNull(column);
39      }
40  
41      @Override
42      public FragmentAndParameters visit(ConditionBasedWhenCondition<T> whenCondition) {
43          FragmentCollector fragmentCollector = whenCondition.conditions()
44                  .filter(this::shouldRender)
45                  .map(this::renderCondition)
46                  .collect(FragmentCollector.collect());
47  
48          Validator.assertFalse(fragmentCollector.isEmpty(), "ERROR.39"); //$NON-NLS-1$
49  
50          return fragmentCollector.toFragmentAndParameters(Collectors.joining(", ")); //$NON-NLS-1$
51      }
52  
53      @Override
54      public FragmentAndParameters visit(BasicWhenCondition<T> whenCondition) {
55          return whenCondition.conditions().map(this::renderBasicValue)
56                  .collect(FragmentCollector.collect())
57                  .toFragmentAndParameters(Collectors.joining(", ")); //$NON-NLS-1$
58      }
59  
60      private boolean shouldRender(RenderableCondition<T> condition) {
61          return condition.shouldRender(renderingContext);
62      }
63  
64      private FragmentAndParameters renderCondition(RenderableCondition<T> condition) {
65          return condition.renderCondition(renderingContext, column);
66      }
67  
68      private FragmentAndParameters renderBasicValue(T value) {
69          RenderedParameterInfo rpi = renderingContext.calculateParameterInfo(column);
70          return FragmentAndParameters.withFragment(rpi.renderedPlaceHolder())
71                  .withParameter(rpi.parameterMapKey(), value)
72                  .build();
73      }
74  }