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.Arrays;
19  import java.util.List;
20  
21  import org.jspecify.annotations.Nullable;
22  import org.mybatis.dynamic.sql.AndOrCriteriaGroup;
23  import org.mybatis.dynamic.sql.BindableColumn;
24  import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
25  import org.mybatis.dynamic.sql.CriteriaGroup;
26  import org.mybatis.dynamic.sql.ExistsCriterion;
27  import org.mybatis.dynamic.sql.ExistsPredicate;
28  import org.mybatis.dynamic.sql.RenderableCondition;
29  import org.mybatis.dynamic.sql.SqlCriterion;
30  import org.mybatis.dynamic.sql.util.ConfigurableStatement;
31  
32  /**
33   * Base class for DSLs that support where clauses - which is every DSL except Insert.
34   * The purpose of the class is to provide a common set of where methods that can be used by
35   * any statement.
36   *
37   * @param <F> the implementation of the Where DSL customized for a particular SQL statement.
38   */
39  public interface AbstractWhereStarter<F extends AbstractWhereFinisher<?>, D extends AbstractWhereStarter<F, D>>
40          extends ConfigurableStatement<D> {
41  
42      default <T> F where(BindableColumn<T> column, RenderableCondition<T> condition, AndOrCriteriaGroup... subCriteria) {
43          return where(column, condition, Arrays.asList(subCriteria));
44      }
45  
46      default <T> F where(BindableColumn<T> column, RenderableCondition<T> condition,
47                         List<AndOrCriteriaGroup> subCriteria) {
48          SqlCriterion sqlCriterion = ColumnAndConditionCriterion.withColumn(column)
49                  .withCondition(condition)
50                  .withSubCriteria(subCriteria)
51                  .build();
52  
53          return initialize(sqlCriterion);
54      }
55  
56      default F where(ExistsPredicate existsPredicate, AndOrCriteriaGroup... subCriteria) {
57          return where(existsPredicate, Arrays.asList(subCriteria));
58      }
59  
60      default F where(ExistsPredicate existsPredicate, List<AndOrCriteriaGroup> subCriteria) {
61          ExistsCriterion sqlCriterion = new ExistsCriterion.Builder()
62                  .withExistsPredicate(existsPredicate)
63                  .withSubCriteria(subCriteria)
64                  .build();
65  
66          return initialize(sqlCriterion);
67      }
68  
69      default F where(SqlCriterion initialCriterion, AndOrCriteriaGroup... subCriteria) {
70          return where(initialCriterion, Arrays.asList(subCriteria));
71      }
72  
73      default F where(@Nullable SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria) {
74          SqlCriterion sqlCriterion = new CriteriaGroup.Builder()
75                  .withInitialCriterion(initialCriterion)
76                  .withSubCriteria(subCriteria)
77                  .build();
78  
79          return initialize(sqlCriterion);
80      }
81  
82      default F where(List<AndOrCriteriaGroup> subCriteria) {
83          SqlCriterion sqlCriterion = new CriteriaGroup.Builder()
84                  .withSubCriteria(subCriteria)
85                  .build();
86  
87          return initialize(sqlCriterion);
88      }
89  
90      F where();
91  
92      default F applyWhere(WhereApplier whereApplier) {
93          F finisher = where();
94          whereApplier.accept(finisher);
95          return finisher;
96      }
97  
98      private F initialize(SqlCriterion sqlCriterion) {
99          F finisher = where();
100         finisher.initialize(sqlCriterion);
101         return finisher;
102     }
103 }