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.Objects;
19  import java.util.function.Consumer;
20  import java.util.function.Function;
21  
22  import org.jspecify.annotations.Nullable;
23  import org.mybatis.dynamic.sql.BasicColumn;
24  import org.mybatis.dynamic.sql.SqlBuilder;
25  import org.mybatis.dynamic.sql.SqlTable;
26  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
27  import org.mybatis.dynamic.sql.util.Buildable;
28  import org.mybatis.dynamic.sql.where.AbstractWhereFinisher;
29  import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;
30  
31  /**
32   * DSL for building count queries. Count queries are specializations of select queries. They have joins and where
33   * clauses, but not the other parts of a select (group by, order by, etc.) Count queries always return
34   * a long. If these restrictions are not acceptable, then use the Select DSL for an unrestricted select statement.
35   *
36   * @param <R> the type of model built by this Builder. Typically, SelectModel.
37   *
38   * @author Jeff Butler
39   */
40  public class CountDSL<R> extends AbstractQueryExpressionDSL<CountDSL<R>.CountWhereBuilder, CountDSL<R>>
41          implements Buildable<R> {
42  
43      private final Function<SelectModel, R> adapterFunction;
44      private @Nullable CountWhereBuilder whereBuilder;
45      private final BasicColumn countColumn;
46      private final StatementConfiguration statementConfiguration = new StatementConfiguration();
47  
48      private CountDSL(BasicColumn countColumn, SqlTable table, Function<SelectModel, R> adapterFunction) {
49          super(table);
50          this.countColumn = Objects.requireNonNull(countColumn);
51          this.adapterFunction = Objects.requireNonNull(adapterFunction);
52      }
53  
54      @Override
55      public CountWhereBuilder where() {
56          whereBuilder = Objects.requireNonNullElseGet(whereBuilder, CountWhereBuilder::new);
57          return whereBuilder;
58      }
59  
60      @Override
61      public R build() {
62          return adapterFunction.apply(buildModel());
63      }
64  
65      @Override
66      public CountDSL<R> configureStatement(Consumer<StatementConfiguration> consumer) {
67          consumer.accept(statementConfiguration);
68          return this;
69      }
70  
71      private SelectModel buildModel() {
72          QueryExpressionModel queryExpressionModel = new QueryExpressionModel.Builder()
73                  .withSelectColumn(countColumn)
74                  .withTable(table())
75                  .withTableAliases(tableAliases())
76                  .withJoinModel(buildJoinModel().orElse(null))
77                  .withWhereModel(whereBuilder == null ? null : whereBuilder.buildWhereModel())
78                  .build();
79  
80          return new SelectModel.Builder()
81                  .withQueryExpression(queryExpressionModel)
82                  .withStatementConfiguration(statementConfiguration)
83                  .build();
84      }
85  
86      public static CountDSL<SelectModel> countFrom(SqlTable table) {
87          return countFrom(Function.identity(), table);
88      }
89  
90      public static <R> CountDSL<R> countFrom(Function<SelectModel, R> adapterFunction, SqlTable table) {
91          return new CountDSL<>(SqlBuilder.count(), table, adapterFunction);
92      }
93  
94      public static FromGatherer<SelectModel> count(BasicColumn column) {
95          return count(Function.identity(), column);
96      }
97  
98      public static <R> FromGatherer<R> count(Function<SelectModel, R> adapterFunction, BasicColumn column) {
99          return new FromGatherer<>(adapterFunction, SqlBuilder.count(column));
100     }
101 
102     public static FromGatherer<SelectModel> countDistinct(BasicColumn column) {
103         return countDistinct(Function.identity(), column);
104     }
105 
106     public static <R> FromGatherer<R> countDistinct(Function<SelectModel, R> adapterFunction, BasicColumn column) {
107         return new FromGatherer<>(adapterFunction, SqlBuilder.countDistinct(column));
108     }
109 
110     @Override
111     protected CountDSL<R> getThis() {
112         return this;
113     }
114 
115     public static class FromGatherer<R> {
116         private final BasicColumn column;
117         private final Function<SelectModel, R> adapterFunction;
118 
119         public FromGatherer(Function<SelectModel, R> adapterFunction, BasicColumn column) {
120             this.adapterFunction = adapterFunction;
121             this.column = column;
122         }
123 
124         public CountDSL<R> from(SqlTable table) {
125             return new CountDSL<>(column, table, adapterFunction);
126         }
127     }
128 
129     public class CountWhereBuilder extends AbstractWhereFinisher<CountWhereBuilder>
130             implements Buildable<R> {
131         private CountWhereBuilder() {
132             super(CountDSL.this);
133         }
134 
135         @Override
136         public R build() {
137             return CountDSL.this.build();
138         }
139 
140         @Override
141         protected CountWhereBuilder getThis() {
142             return this;
143         }
144 
145         protected EmbeddedWhereModel buildWhereModel() {
146             return super.buildModel();
147         }
148     }
149 }