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 java.io.Serializable;
019import java.util.*;
020
021public class BaseQueryWrapper<T> implements Serializable {
022
023
024    protected List<QueryTable> queryTables;
025    protected String dataSource;
026
027    protected List<QueryColumn> selectColumns;
028    protected List<Join> joins;
029    protected List<QueryTable> joinTables;
030    protected QueryCondition whereQueryCondition;
031    protected List<QueryColumn> groupByColumns;
032    protected QueryCondition havingQueryCondition;
033    protected List<QueryOrderBy> orderBys;
034
035    protected List<UnionWrapper> unions;
036
037    protected Integer limitOffset;
038    protected Integer limitRows;
039
040    protected Map<String, Object> context;
041
042//    protected boolean ignoreBlankStrings = false;
043
044
045    protected T addSelectColumn(QueryColumn queryColumn) {
046        if (selectColumns == null) {
047            selectColumns = new LinkedList<>();
048        }
049
050        selectColumns.add(queryColumn);
051        return (T) this;
052    }
053
054
055    protected T AddJoin(Join join) {
056        if (joins == null) {
057            joins = new LinkedList<>();
058        }
059        joins.add(join);
060        return (T) this;
061    }
062
063
064    protected T setWhereQueryCondition(QueryCondition queryCondition) {
065        if (whereQueryCondition != null) {
066            queryCondition.connect(whereQueryCondition, SqlConnector.AND);
067        }
068
069        whereQueryCondition = queryCondition;
070        return (T) this;
071    }
072
073
074    protected T addWhereQueryCondition(QueryCondition queryCondition, SqlConnector connector) {
075        if (queryCondition != null) {
076            if (whereQueryCondition == null) {
077                whereQueryCondition = queryCondition;
078            } else {
079                whereQueryCondition.connect(queryCondition, connector);
080            }
081        }
082        return (T) this;
083    }
084
085
086    protected T addGroupByColumns(QueryColumn queryColumn) {
087        if (groupByColumns == null) {
088            groupByColumns = new LinkedList<>();
089        }
090
091        groupByColumns.add(queryColumn);
092        return (T) this;
093    }
094
095
096    protected T addHavingQueryCondition(QueryCondition queryCondition, SqlConnector connector) {
097        if (havingQueryCondition == null) {
098            havingQueryCondition = queryCondition;
099        } else {
100            havingQueryCondition.connect(queryCondition, connector);
101        }
102        return (T) this;
103    }
104
105
106    protected T addOrderBy(QueryOrderBy queryOrderBy) {
107        if (orderBys == null) {
108            orderBys = new LinkedList<>();
109        }
110        orderBys.add(queryOrderBy);
111        return (T) this;
112    }
113
114
115    protected void addJoinTable(QueryTable queryTable) {
116        if (joinTables == null) {
117            joinTables = new ArrayList<>();
118        }
119        joinTables.add(queryTable);
120    }
121
122
123    protected List<QueryTable> getQueryTables() {
124        return queryTables;
125    }
126
127    protected void setQueryTables(List<QueryTable> queryTables) {
128        this.queryTables = queryTables;
129    }
130
131    protected String getDataSource() {
132        return dataSource;
133    }
134
135    protected void setDataSource(String dataSource) {
136        this.dataSource = dataSource;
137    }
138
139    protected List<QueryColumn> getSelectColumns() {
140        return selectColumns;
141    }
142
143    protected void setSelectColumns(List<QueryColumn> selectColumns) {
144        this.selectColumns = selectColumns;
145    }
146
147    protected List<Join> getJoins() {
148        return joins;
149    }
150
151    protected void setJoins(List<Join> joins) {
152        this.joins = joins;
153    }
154
155    protected List<QueryTable> getJoinTables() {
156        return joinTables;
157    }
158
159    protected void setJoinTables(List<QueryTable> joinTables) {
160        this.joinTables = joinTables;
161    }
162
163    protected QueryCondition getWhereQueryCondition() {
164        return whereQueryCondition;
165    }
166
167    protected List<QueryColumn> getGroupByColumns() {
168        return groupByColumns;
169    }
170
171    protected void setGroupByColumns(List<QueryColumn> groupByColumns) {
172        this.groupByColumns = groupByColumns;
173    }
174
175    protected QueryCondition getHavingQueryCondition() {
176        return havingQueryCondition;
177    }
178
179    protected void setHavingQueryCondition(QueryCondition havingQueryCondition) {
180        this.havingQueryCondition = havingQueryCondition;
181    }
182
183    protected List<QueryOrderBy> getOrderBys() {
184        return orderBys;
185    }
186
187    protected void setOrderBys(List<QueryOrderBy> orderBys) {
188        this.orderBys = orderBys;
189    }
190
191    protected List<UnionWrapper> getUnions() {
192        return unions;
193    }
194
195    protected void setUnions(List<UnionWrapper> unions) {
196        this.unions = unions;
197    }
198
199    protected Integer getLimitOffset() {
200        return limitOffset;
201    }
202
203    protected void setLimitOffset(Integer limitOffset) {
204        this.limitOffset = limitOffset;
205    }
206
207    protected Integer getLimitRows() {
208        return limitRows;
209    }
210
211    protected void setLimitRows(Integer limitRows) {
212        this.limitRows = limitRows;
213    }
214
215    protected Map<String, Object> getContext() {
216        return context;
217    }
218
219    protected void setContext(Map<String, Object> context) {
220        this.context = context;
221    }
222
223    protected void putContext(String key, Object value){
224        if (context == null){
225            context = new HashMap<>();
226        }
227        context.put(key,value);
228    }
229
230    protected <R> R getContext(String key){
231        return context == null ? null : (R) context.get(key);
232    }
233}