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.where.render;
17  
18  import java.util.Optional;
19  
20  import org.mybatis.dynamic.sql.common.AbstractBooleanExpressionModel;
21  import org.mybatis.dynamic.sql.common.AbstractBooleanExpressionRenderer;
22  import org.mybatis.dynamic.sql.exception.NonRenderingWhereClauseException;
23  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
24  
25  public class WhereRenderer extends AbstractBooleanExpressionRenderer {
26      private WhereRenderer(Builder builder) {
27          super("where", builder); //$NON-NLS-1$
28      }
29  
30      @Override
31      public Optional<FragmentAndParameters> render() {
32          Optional<FragmentAndParameters> whereClause = super.render();
33  
34          if (whereClause.isPresent() || renderingContext.isNonRenderingClauseAllowed()) {
35              return whereClause;
36          } else {
37              throw new NonRenderingWhereClauseException();
38          }
39      }
40  
41      public static Builder withWhereModel(AbstractBooleanExpressionModel whereModel) {
42          return new Builder(whereModel);
43      }
44  
45      public static class Builder extends AbstractBuilder<Builder> {
46          public Builder(AbstractBooleanExpressionModel whereModel) {
47              super(whereModel);
48          }
49  
50          @Override
51          protected Builder getThis() {
52              return this;
53          }
54  
55          public WhereRenderer build() {
56              return new WhereRenderer(this);
57          }
58      }
59  }