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