1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34
35
36
37
38
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 }