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;
17  
18  import java.util.Arrays;
19  import java.util.List;
20  
21  import org.mybatis.dynamic.sql.AndOrCriteriaGroup;
22  import org.mybatis.dynamic.sql.BindableColumn;
23  import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
24  import org.mybatis.dynamic.sql.CriteriaGroup;
25  import org.mybatis.dynamic.sql.RenderableCondition;
26  import org.mybatis.dynamic.sql.SqlCriterion;
27  
28  public interface AbstractHavingStarter<F extends AbstractHavingFinisher<?>> {
29  
30      default <T> F having(BindableColumn<T> column, RenderableCondition<T> condition,
31                          AndOrCriteriaGroup... subCriteria) {
32          return having(column, condition, Arrays.asList(subCriteria));
33      }
34  
35      default <T> F having(BindableColumn<T> column, RenderableCondition<T> condition,
36                          List<AndOrCriteriaGroup> subCriteria) {
37          SqlCriterion sqlCriterion = ColumnAndConditionCriterion.withColumn(column)
38                  .withCondition(condition)
39                  .withSubCriteria(subCriteria)
40                  .build();
41  
42          return initialize(sqlCriterion);
43      }
44  
45      default F having(SqlCriterion initialCriterion, AndOrCriteriaGroup... subCriteria) {
46          return having(initialCriterion, Arrays.asList(subCriteria));
47      }
48  
49      default F having(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria) {
50          SqlCriterion sqlCriterion = new CriteriaGroup.Builder()
51                  .withInitialCriterion(initialCriterion)
52                  .withSubCriteria(subCriteria)
53                  .build();
54  
55          return initialize(sqlCriterion);
56      }
57  
58      F having();
59  
60      default F applyHaving(HavingApplier havingApplier) {
61          F finisher = having();
62          havingApplier.accept(finisher);
63          return finisher;
64      }
65  
66      private F initialize(SqlCriterion sqlCriterion) {
67          F finisher = having();
68          finisher.initialize(sqlCriterion);
69          return finisher;
70      }
71  }