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.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Objects;
24  import java.util.Optional;
25  import java.util.stream.Stream;
26  
27  import org.jspecify.annotations.Nullable;
28  import org.mybatis.dynamic.sql.BasicColumn;
29  import org.mybatis.dynamic.sql.SqlTable;
30  import org.mybatis.dynamic.sql.TableExpression;
31  import org.mybatis.dynamic.sql.select.join.JoinModel;
32  import org.mybatis.dynamic.sql.util.Validator;
33  import org.mybatis.dynamic.sql.where.WhereModel;
34  
35  public class QueryExpressionModel {
36      private final @Nullable String connector;
37      private final boolean isDistinct;
38      private final List<BasicColumn> selectList;
39      private final TableExpression table;
40      private final @Nullable JoinModel joinModel;
41      private final Map<SqlTable, String> tableAliases;
42      private final @Nullable WhereModel whereModel;
43      private final @Nullable GroupByModel groupByModel;
44      private final @Nullable HavingModel havingModel;
45  
46      private QueryExpressionModel(Builder builder) {
47          connector = builder.connector;
48          isDistinct = builder.isDistinct;
49          selectList = Objects.requireNonNull(builder.selectList);
50          table = Objects.requireNonNull(builder.table);
51          joinModel = builder.joinModel;
52          tableAliases = builder.tableAliases;
53          whereModel = builder.whereModel;
54          groupByModel = builder.groupByModel;
55          havingModel = builder.havingModel;
56          Validator.assertNotEmpty(selectList, "ERROR.13"); //$NON-NLS-1$
57      }
58  
59      public Optional<String> connector() {
60          return Optional.ofNullable(connector);
61      }
62  
63      public boolean isDistinct() {
64          return isDistinct;
65      }
66  
67      public Stream<BasicColumn> columns() {
68          return selectList.stream();
69      }
70  
71      public TableExpression table() {
72          return table;
73      }
74  
75      public Map<SqlTable, String> tableAliases() {
76          return tableAliases;
77      }
78  
79      public Optional<WhereModel> whereModel() {
80          return Optional.ofNullable(whereModel);
81      }
82  
83      public Optional<JoinModel> joinModel() {
84          return Optional.ofNullable(joinModel);
85      }
86  
87      public Optional<GroupByModel> groupByModel() {
88          return Optional.ofNullable(groupByModel);
89      }
90  
91      public Optional<HavingModel> havingModel() {
92          return Optional.ofNullable(havingModel);
93      }
94  
95      public static Builder withSelectList(List<? extends BasicColumn> columnList) {
96          return new Builder().withSelectList(columnList);
97      }
98  
99      public static class Builder {
100         private @Nullable String connector;
101         private boolean isDistinct;
102         private final List<BasicColumn> selectList = new ArrayList<>();
103         private @Nullable TableExpression table;
104         private final Map<SqlTable, String> tableAliases = new HashMap<>();
105         private @Nullable WhereModel whereModel;
106         private @Nullable JoinModel joinModel;
107         private @Nullable GroupByModel groupByModel;
108         private @Nullable HavingModel havingModel;
109 
110         public Builder withConnector(@Nullable String connector) {
111             this.connector = connector;
112             return this;
113         }
114 
115         public Builder withTable(TableExpression table) {
116             this.table = table;
117             return this;
118         }
119 
120         public Builder isDistinct(boolean isDistinct) {
121             this.isDistinct = isDistinct;
122             return this;
123         }
124 
125         public Builder withSelectColumn(BasicColumn selectColumn) {
126             this.selectList.add(selectColumn);
127             return this;
128         }
129 
130         public Builder withSelectList(Collection<? extends BasicColumn> selectList) {
131             this.selectList.addAll(selectList);
132             return this;
133         }
134 
135         public Builder withTableAliases(Map<SqlTable, String> tableAliases) {
136             this.tableAliases.putAll(tableAliases);
137             return this;
138         }
139 
140         public Builder withWhereModel(@Nullable WhereModel whereModel) {
141             this.whereModel = whereModel;
142             return this;
143         }
144 
145         public Builder withJoinModel(@Nullable JoinModel joinModel) {
146             this.joinModel = joinModel;
147             return this;
148         }
149 
150         public Builder withGroupByModel(@Nullable GroupByModel groupByModel) {
151             this.groupByModel = groupByModel;
152             return this;
153         }
154 
155         public Builder withHavingModel(@Nullable HavingModel havingModel) {
156             this.havingModel = havingModel;
157             return this;
158         }
159 
160         public QueryExpressionModel build() {
161             return new QueryExpressionModel(this);
162         }
163     }
164 }