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;
17  
18  import java.util.function.Consumer;
19  
20  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
21  import org.mybatis.dynamic.sql.util.Buildable;
22  
23  /**
24   *  DSL for standalone where clauses.
25   *
26   *  <p>This can also be used to create reusable where clauses for different statements.
27   */
28  public class WhereDSL implements AbstractWhereStarter<WhereDSL.StandaloneWhereFinisher, WhereDSL> {
29      private final StatementConfiguration statementConfiguration = new StatementConfiguration();
30      private final StandaloneWhereFinisher whereBuilder = new StandaloneWhereFinisher();
31  
32      @Override
33      public StandaloneWhereFinisher where() {
34          return whereBuilder;
35      }
36  
37      @Override
38      public WhereDSL configureStatement(Consumer<StatementConfiguration> consumer) {
39          consumer.accept(statementConfiguration);
40          return this;
41      }
42  
43      public class StandaloneWhereFinisher extends AbstractWhereFinisher<StandaloneWhereFinisher>
44              implements Buildable<WhereModel> {
45          private StandaloneWhereFinisher() {
46              super(WhereDSL.this);
47          }
48  
49          @Override
50          protected StandaloneWhereFinisher getThis() {
51              return this;
52          }
53  
54          @Override
55          public WhereModel build() {
56              return new WhereModel.Builder()
57                      .withInitialCriterion(getInitialCriterion())
58                      .withSubCriteria(subCriteria)
59                      .withStatementConfiguration(statementConfiguration)
60                      .build();
61          }
62  
63          public WhereApplier toWhereApplier() {
64              return d -> d.initialize(getInitialCriterion(), subCriteria);
65          }
66      }
67  }