View Javadoc
1   /*
2    *    Copyright 2016-2026 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.Function;
20  
21  import org.jspecify.annotations.Nullable;
22  import org.mybatis.dynamic.sql.BasicColumn;
23  import org.mybatis.dynamic.sql.SqlBuilder;
24  import org.mybatis.dynamic.sql.SqlTable;
25  import org.mybatis.dynamic.sql.dsl.AbstractCountDSL;
26  
27  /**
28   * DSL for building count queries. Count queries are specializations of select queries. They have joins and where
29   * clauses, but not the other parts of a select (group by, order by, etc.) Count queries always return
30   * a long value. If these restrictions are not acceptable, then use the Select DSL for an unrestricted select statement.
31   *
32   * @param <R> the type of model built by this Builder. Typically, SelectModel.
33   *
34   * @author Jeff Butler
35   */
36  public class CountDSL<R> extends AbstractCountDSL<R, CountDSL<R>> {
37      private final Function<SelectModel, R> adapterFunction;
38  
39      private CountDSL(Builder<R> builder) {
40          super(Objects.requireNonNull(builder.column));
41          adapterFunction = Objects.requireNonNull(builder.adapterFunction);
42      }
43  
44      @Override
45      public R build() {
46          return buildSelectModel().map(adapterFunction);
47      }
48  
49      @Override
50      protected CountDSL<R> getThis() {
51          return this;
52      }
53  
54      public static CountDSL<SelectModel> countFrom(SqlTable table) {
55          return countFrom(Function.identity(), table);
56      }
57  
58      public static CountDSL<SelectModel> countFrom(SqlTable table, String tableAlias) {
59          return new Builder<SelectModel>()
60                  .withAdapterFunction(Function.identity())
61                  .withColumn(SqlBuilder.count())
62                  .build()
63                  .from(table, tableAlias);
64      }
65  
66      public static <R> CountDSL<R> countFrom(Function<SelectModel, R> adapterFunction, SqlTable table) {
67          return new Builder<R>()
68                  .withAdapterFunction(adapterFunction)
69                  .withColumn(SqlBuilder.count())
70                  .build()
71                  .from(table);
72      }
73  
74      public static CountDSL<SelectModel> count(BasicColumn column) {
75          return count(Function.identity(), column);
76      }
77  
78      public static <R> CountDSL<R> count(Function<SelectModel, R> adapterFunction, BasicColumn column) {
79          return new Builder<R>()
80                  .withAdapterFunction(adapterFunction)
81                  .withColumn(SqlBuilder.count(column))
82                  .build();
83      }
84  
85      public static CountDSL<SelectModel> countDistinct(BasicColumn column) {
86          return countDistinct(Function.identity(), column);
87      }
88  
89      public static <R> CountDSL<R> countDistinct(Function<SelectModel, R> adapterFunction, BasicColumn column) {
90          return new Builder<R>()
91                  .withAdapterFunction(adapterFunction)
92                  .withColumn(SqlBuilder.countDistinct(column))
93                  .build();
94      }
95  
96      public static class Builder<R> {
97          private @Nullable BasicColumn column;
98          private @Nullable Function<SelectModel, R> adapterFunction;
99  
100         public Builder<R> withColumn(BasicColumn column) {
101             this.column = column;
102             return this;
103         }
104 
105         public Builder<R> withAdapterFunction(Function<SelectModel, R> adapterFunction) {
106             this.adapterFunction = adapterFunction;
107             return this;
108         }
109 
110         public CountDSL<R> build() {
111             return new CountDSL<>(this);
112         }
113     }
114 }