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