001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.query;
017
018import com.mybatisflex.core.constant.SqlConnector;
019import com.mybatisflex.core.exception.FlexExceptions;
020import com.mybatisflex.core.util.CollectionUtil;
021import com.mybatisflex.core.util.ObjectUtil;
022
023import java.util.*;
024
025@SuppressWarnings({"unchecked", "unused"})
026public class BaseQueryWrapper<T extends BaseQueryWrapper<T>> implements CloneSupport<T> {
027
028
029    protected With with;
030    protected List<QueryTable> queryTables;
031    protected String dataSource;
032    protected String hint;
033
034    protected List<QueryColumn> selectColumns;
035    protected List<Join> joins;
036    protected List<QueryTable> joinTables;
037    protected QueryCondition whereQueryCondition;
038    protected List<QueryColumn> groupByColumns;
039    protected QueryCondition havingQueryCondition;
040    protected List<QueryOrderBy> orderBys;
041
042    protected List<UnionWrapper> unions;
043
044    protected Long limitOffset;
045    protected Long limitRows;
046
047    protected List<String> endFragments;
048
049    protected Map<String, Object> context;
050
051//    protected boolean ignoreBlankStrings = false;
052
053    /**
054     * <p>Title: clear. </p>
055     * <p>Description: Default QueryWrapper values. </p>
056     * <p>Notice: When adding new attributes, it is necessary to supplement here. </p>
057     *
058     * @author dragon
059     */
060    public void clear() {
061        this.with = null;
062        this.queryTables = null;
063        this.dataSource = null;
064        this.hint = null;
065        this.selectColumns = null;
066        this.joins = null;
067        this.joinTables = null;
068        this.whereQueryCondition = null;
069        this.groupByColumns = null;
070        this.havingQueryCondition = null;
071        this.orderBys = null;
072        this.unions = null;
073        this.limitOffset = null;
074        this.limitRows = null;
075        this.endFragments = null;
076        this.context = null;
077    }
078
079    protected T addSelectColumn(QueryColumn queryColumn) {
080        if (selectColumns == null) {
081            selectColumns = new ArrayList<>();
082        }
083
084        selectColumns.add(queryColumn);
085        return (T) this;
086    }
087
088
089    protected T addJoin(Join join) {
090        if (joins == null) {
091            joins = new ArrayList<>();
092        }
093        joins.add(join);
094        return (T) this;
095    }
096
097    protected T setWhereQueryCondition(QueryCondition queryCondition) {
098        whereQueryCondition = queryCondition;
099        return (T) this;
100    }
101
102    protected T addWhereQueryCondition(QueryCondition queryCondition) {
103        if (queryCondition != null) {
104            if (whereQueryCondition != null) {
105                queryCondition.connect(whereQueryCondition, SqlConnector.AND);
106            }
107            whereQueryCondition = queryCondition;
108        }
109        return (T) this;
110    }
111
112
113    protected T addWhereQueryCondition(QueryCondition queryCondition, SqlConnector connector) {
114        if (queryCondition != null) {
115            if (whereQueryCondition == null) {
116                whereQueryCondition = queryCondition;
117            } else {
118                whereQueryCondition.connect(queryCondition, connector);
119            }
120        }
121        return (T) this;
122    }
123
124
125    protected T addGroupByColumns(QueryColumn queryColumn) {
126        if (groupByColumns == null) {
127            groupByColumns = new ArrayList<>();
128        }
129        groupByColumns.add(queryColumn);
130        return (T) this;
131    }
132
133
134    protected T addHavingQueryCondition(QueryCondition queryCondition, SqlConnector connector) {
135        if (havingQueryCondition == null) {
136            havingQueryCondition = queryCondition;
137        } else {
138            havingQueryCondition.connect(queryCondition, connector);
139        }
140        return (T) this;
141    }
142
143
144    protected T addOrderBy(QueryOrderBy queryOrderBy) {
145        if (orderBys == null) {
146            orderBys = new LinkedList<>();
147        }
148        orderBys.add(queryOrderBy);
149        return (T) this;
150    }
151
152
153    protected void addJoinTable(QueryTable queryTable) {
154        if (joinTables == null) {
155            joinTables = new ArrayList<>();
156        }
157        joinTables.add(queryTable);
158    }
159
160    protected void addEndFragment(String fragment) {
161        if (endFragments == null) {
162            endFragments = new ArrayList<>();
163        }
164        endFragments.add(fragment);
165    }
166
167
168    protected List<QueryTable> getQueryTables() {
169        return queryTables;
170    }
171
172    protected void setQueryTables(List<QueryTable> queryTables) {
173        this.queryTables = queryTables;
174    }
175
176    protected String getDataSource() {
177        return dataSource;
178    }
179
180    protected void setDataSource(String dataSource) {
181        this.dataSource = dataSource;
182    }
183
184    protected String getHint() {
185        return hint;
186    }
187
188    protected void setHint(String hint) {
189        this.hint = hint;
190    }
191
192    protected List<QueryColumn> getSelectColumns() {
193        return selectColumns;
194    }
195
196    protected void setSelectColumns(List<QueryColumn> selectColumns) {
197        this.selectColumns = selectColumns;
198    }
199
200    protected List<Join> getJoins() {
201        return joins;
202    }
203
204    protected void setJoins(List<Join> joins) {
205        this.joins = joins;
206    }
207
208    protected List<QueryTable> getJoinTables() {
209        return joinTables;
210    }
211
212    protected void setJoinTables(List<QueryTable> joinTables) {
213        this.joinTables = joinTables;
214    }
215
216    protected QueryCondition getWhereQueryCondition() {
217        return whereQueryCondition;
218    }
219
220    protected List<QueryColumn> getGroupByColumns() {
221        return groupByColumns;
222    }
223
224    protected void setGroupByColumns(List<QueryColumn> groupByColumns) {
225        this.groupByColumns = groupByColumns;
226    }
227
228    protected QueryCondition getHavingQueryCondition() {
229        return havingQueryCondition;
230    }
231
232    protected void setHavingQueryCondition(QueryCondition havingQueryCondition) {
233        this.havingQueryCondition = havingQueryCondition;
234    }
235
236    protected List<QueryOrderBy> getOrderBys() {
237        return orderBys;
238    }
239
240    protected void setOrderBys(List<QueryOrderBy> orderBys) {
241        this.orderBys = orderBys;
242    }
243
244    protected List<UnionWrapper> getUnions() {
245        return unions;
246    }
247
248    protected void setUnions(List<UnionWrapper> unions) {
249        this.unions = unions;
250    }
251
252    protected Long getLimitOffset() {
253        return limitOffset;
254    }
255
256    protected void setLimitOffset(Long limitOffset) {
257        this.limitOffset = limitOffset;
258    }
259
260    protected Long getLimitRows() {
261        return limitRows;
262    }
263
264    protected void setLimitRows(Long limitRows) {
265        this.limitRows = limitRows;
266    }
267
268    protected List<String> getEndFragments() {
269        return endFragments;
270    }
271
272    protected void setEndFragments(List<String> endFragments) {
273        this.endFragments = endFragments;
274    }
275
276    protected Map<String, Object> getContext() {
277        return context;
278    }
279
280    protected void setContext(Map<String, Object> context) {
281        this.context = context;
282    }
283
284    protected void putContext(String key, Object value) {
285        if (context == null) {
286            context = new HashMap<>();
287        }
288        context.put(key, value);
289    }
290
291    protected <R> R getContext(String key) {
292        return context == null ? null : (R) context.get(key);
293    }
294
295    @Override
296    public T clone() {
297        try {
298            T clone = (T) super.clone();
299            // deep clone ...
300            clone.with = ObjectUtil.clone(this.with);
301            clone.queryTables = CollectionUtil.cloneArrayList(this.queryTables);
302            clone.selectColumns = CollectionUtil.cloneArrayList(this.selectColumns);
303            clone.joins = CollectionUtil.cloneArrayList(this.joins);
304            clone.joinTables = CollectionUtil.cloneArrayList(this.joinTables);
305            clone.whereQueryCondition = ObjectUtil.clone(this.whereQueryCondition);
306            clone.groupByColumns = CollectionUtil.cloneArrayList(this.groupByColumns);
307            clone.havingQueryCondition = ObjectUtil.clone(this.havingQueryCondition);
308            clone.orderBys = CollectionUtil.cloneArrayList(this.orderBys);
309            clone.unions = CollectionUtil.cloneArrayList(this.unions);
310            // copy List if necessary ...
311            if (this.endFragments != null) {
312                clone.endFragments = CollectionUtil.newArrayList(this.endFragments);
313            }
314            // copy Map if necessary ...
315            if (this.context != null) {
316                clone.context = CollectionUtil.newHashMap(this.context);
317            }
318            return clone;
319        } catch (CloneNotSupportedException e) {
320            throw FlexExceptions.wrap(e);
321        }
322    }
323
324}