forked from mybatis/mybatis-dynamic-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiSelectDSL.java
153 lines (129 loc) · 5.17 KB
/
MultiSelectDSL.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
/*
* 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.Optional;
import java.util.function.Consumer;
import org.jspecify.annotations.Nullable;
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.util.Buildable;
import org.mybatis.dynamic.sql.util.ConfigurableStatement;
public class MultiSelectDSL implements Buildable<MultiSelectModel>, ConfigurableStatement<MultiSelectDSL> {
private final List<UnionQuery> unionQueries = new ArrayList<>();
private final SelectModel initialSelect;
private @Nullable OrderByModel orderByModel;
private @Nullable Long limit;
private @Nullable Long offset;
private @Nullable Long fetchFirstRows;
private final StatementConfiguration statementConfiguration = new StatementConfiguration();
public MultiSelectDSL(Buildable<SelectModel> builder) {
initialSelect = builder.build();
}
public MultiSelectDSL union(Buildable<SelectModel> builder) {
unionQueries.add(new UnionQuery("union", builder.build())); //$NON-NLS-1$
return this;
}
public MultiSelectDSL unionAll(Buildable<SelectModel> builder) {
unionQueries.add(new UnionQuery("union all", builder.build())); //$NON-NLS-1$
return this;
}
public MultiSelectDSL orderBy(SortSpecification... columns) {
return orderBy(Arrays.asList(columns));
}
public MultiSelectDSL orderBy(Collection<? extends SortSpecification> columns) {
orderByModel = OrderByModel.of(columns);
return this;
}
public MultiSelectDSL.LimitFinisher limit(long limit) {
return limitWhenPresent(limit);
}
public MultiSelectDSL.LimitFinisher limitWhenPresent(@Nullable Long limit) {
this.limit = limit;
return new LimitFinisher();
}
public MultiSelectDSL.OffsetFirstFinisher offset(long offset) {
return offsetWhenPresent(offset);
}
public MultiSelectDSL.OffsetFirstFinisher offsetWhenPresent(@Nullable Long offset) {
this.offset = offset;
return new OffsetFirstFinisher();
}
public MultiSelectDSL.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
return fetchFirstWhenPresent(fetchFirstRows);
}
public MultiSelectDSL.FetchFirstFinisher fetchFirstWhenPresent(@Nullable Long fetchFirstRows) {
this.fetchFirstRows = fetchFirstRows;
return new FetchFirstFinisher();
}
@Override
public MultiSelectModel build() {
return new MultiSelectModel.Builder()
.withInitialSelect(initialSelect)
.withUnionQueries(unionQueries)
.withOrderByModel(orderByModel)
.withPagingModel(buildPagingModel().orElse(null))
.withStatementConfiguration(statementConfiguration)
.build();
}
private Optional<PagingModel> buildPagingModel() {
return new PagingModel.Builder()
.withLimit(limit)
.withOffset(offset)
.withFetchFirstRows(fetchFirstRows)
.build();
}
@Override
public MultiSelectDSL configureStatement(Consumer<StatementConfiguration> consumer) {
consumer.accept(statementConfiguration);
return this;
}
public class OffsetFirstFinisher implements Buildable<MultiSelectModel> {
public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
return fetchFirstWhenPresent(fetchFirstRows);
}
public FetchFirstFinisher fetchFirstWhenPresent(@Nullable Long fetchFirstRows) {
MultiSelectDSL.this.fetchFirstRows = fetchFirstRows;
return new FetchFirstFinisher();
}
@Override
public MultiSelectModel build() {
return MultiSelectDSL.this.build();
}
}
public class LimitFinisher implements Buildable<MultiSelectModel> {
public MultiSelectDSL offset(long offset) {
return offsetWhenPresent(offset);
}
public MultiSelectDSL offsetWhenPresent(@Nullable Long offset) {
MultiSelectDSL.this.offset = offset;
return MultiSelectDSL.this;
}
@Override
public MultiSelectModel build() {
return MultiSelectDSL.this.build();
}
}
public class FetchFirstFinisher {
public MultiSelectDSL rowsOnly() {
return MultiSelectDSL.this;
}
}
}