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