forked from mybatis/mybatis-dynamic-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectDSL.java
255 lines (215 loc) · 8.68 KB
/
SelectDSL.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright 2016-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.select;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import org.jspecify.annotations.Nullable;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SortSpecification;
import org.mybatis.dynamic.sql.common.OrderByModel;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.ConfigurableStatement;
import org.mybatis.dynamic.sql.util.Validator;
/**
* Implements a SQL DSL for building select statements.
*
* @author Jeff Butler
*
* @param <R>
* the type of model produced by this builder, typically SelectModel
*/
public class SelectDSL<R> implements Buildable<R>, ConfigurableStatement<SelectDSL<R>> {
private final Function<SelectModel, R> adapterFunction;
private final List<QueryExpressionDSL<R>> queryExpressions = new ArrayList<>();
private @Nullable OrderByModel orderByModel;
private @Nullable Long limit;
private @Nullable Long offset;
private @Nullable Long fetchFirstRows;
final StatementConfiguration statementConfiguration = new StatementConfiguration();
private @Nullable String forClause;
private @Nullable String waitClause;
private SelectDSL(Function<SelectModel, R> adapterFunction) {
this.adapterFunction = Objects.requireNonNull(adapterFunction);
}
public static QueryExpressionDSL.FromGatherer<SelectModel> select(BasicColumn... selectList) {
return select(Arrays.asList(selectList));
}
public static QueryExpressionDSL.FromGatherer<SelectModel> select(Collection<? extends BasicColumn> selectList) {
return select(Function.identity(), selectList);
}
public static <R> QueryExpressionDSL.FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
BasicColumn... selectList) {
return select(adapterFunction, Arrays.asList(selectList));
}
public static <R> QueryExpressionDSL.FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
Collection<? extends BasicColumn> selectList) {
return new FromGatherer.Builder<R>()
.withSelectList(selectList)
.withSelectDSL(new SelectDSL<>(adapterFunction))
.build();
}
public static QueryExpressionDSL.FromGatherer<SelectModel> selectDistinct(BasicColumn... selectList) {
return selectDistinct(Function.identity(), selectList);
}
public static QueryExpressionDSL.FromGatherer<SelectModel> selectDistinct(
Collection<? extends BasicColumn> selectList) {
return selectDistinct(Function.identity(), selectList);
}
public static <R> QueryExpressionDSL.FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
BasicColumn... selectList) {
return selectDistinct(adapterFunction, Arrays.asList(selectList));
}
public static <R> QueryExpressionDSL.FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
Collection<? extends BasicColumn> selectList) {
return new FromGatherer.Builder<R>()
.withSelectList(selectList)
.withSelectDSL(new SelectDSL<>(adapterFunction))
.isDistinct()
.build();
}
void registerQueryExpression(QueryExpressionDSL<R> queryExpression) {
queryExpressions.add(queryExpression);
}
void orderBy(Collection<? extends SortSpecification> columns) {
orderByModel = OrderByModel.of(columns);
}
public SelectDSL<R>.LimitFinisher limit(long limit) {
return limitWhenPresent(limit);
}
public SelectDSL<R>.LimitFinisher limitWhenPresent(@Nullable Long limit) {
this.limit = limit;
return new LimitFinisher();
}
public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
return offsetWhenPresent(offset);
}
public SelectDSL<R>.OffsetFirstFinisher offsetWhenPresent(@Nullable Long offset) {
this.offset = offset;
return new OffsetFirstFinisher();
}
public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
return fetchFirstWhenPresent(fetchFirstRows);
}
public SelectDSL<R>.FetchFirstFinisher fetchFirstWhenPresent(@Nullable Long fetchFirstRows) {
this.fetchFirstRows = fetchFirstRows;
return new FetchFirstFinisher();
}
public SelectDSL<R> forUpdate() {
Validator.assertNull(forClause, "ERROR.48"); //$NON-NLS-1$
forClause = "for update"; //$NON-NLS-1$
return this;
}
public SelectDSL<R> forNoKeyUpdate() {
Validator.assertNull(forClause, "ERROR.48"); //$NON-NLS-1$
forClause = "for no key update"; //$NON-NLS-1$
return this;
}
public SelectDSL<R> forShare() {
Validator.assertNull(forClause, "ERROR.48"); //$NON-NLS-1$
forClause = "for share"; //$NON-NLS-1$
return this;
}
public SelectDSL<R> forKeyShare() {
Validator.assertNull(forClause, "ERROR.48"); //$NON-NLS-1$
forClause = "for key share"; //$NON-NLS-1$
return this;
}
public SelectDSL<R> skipLocked() {
Validator.assertNull(waitClause, "ERROR.49"); //$NON-NLS-1$
waitClause = "skip locked"; //$NON-NLS-1$
return this;
}
public SelectDSL<R> nowait() {
Validator.assertNull(waitClause, "ERROR.49"); //$NON-NLS-1$
waitClause = "nowait"; //$NON-NLS-1$
return this;
}
@Override
public SelectDSL<R> configureStatement(Consumer<StatementConfiguration> consumer) {
consumer.accept(statementConfiguration);
return this;
}
@Override
public R build() {
SelectModel selectModel = SelectModel.withQueryExpressions(buildModels())
.withOrderByModel(orderByModel)
.withPagingModel(buildPagingModel().orElse(null))
.withStatementConfiguration(statementConfiguration)
.withForClause(forClause)
.withWaitClause(waitClause)
.build();
return adapterFunction.apply(selectModel);
}
private List<QueryExpressionModel> buildModels() {
return queryExpressions.stream()
.map(QueryExpressionDSL::buildModel)
.toList();
}
private Optional<PagingModel> buildPagingModel() {
return new PagingModel.Builder()
.withLimit(limit)
.withOffset(offset)
.withFetchFirstRows(fetchFirstRows)
.build();
}
public class OffsetFirstFinisher implements SelectDSLForAndWaitOperations<R>, Buildable<R> {
public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
return fetchFirstWhenPresent(fetchFirstRows);
}
public FetchFirstFinisher fetchFirstWhenPresent(@Nullable Long fetchFirstRows) {
SelectDSL.this.fetchFirstRows = fetchFirstRows;
return new FetchFirstFinisher();
}
@Override
public SelectDSL<R> getSelectDSL() {
return SelectDSL.this;
}
@Override
public R build() {
return SelectDSL.this.build();
}
}
public class LimitFinisher implements SelectDSLForAndWaitOperations<R>, Buildable<R> {
public SelectDSL<R> offset(long offset) {
return offsetWhenPresent(offset);
}
public SelectDSL<R> offsetWhenPresent(@Nullable Long offset) {
SelectDSL.this.offset = offset;
return SelectDSL.this;
}
@Override
public SelectDSL<R> getSelectDSL() {
return SelectDSL.this;
}
@Override
public R build() {
return SelectDSL.this.build();
}
}
public class FetchFirstFinisher {
public SelectDSL<R> rowsOnly() {
return SelectDSL.this;
}
}
}