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 018 019import com.mybatisflex.core.dialect.IDialect; 020import com.mybatisflex.core.table.TableDef; 021import com.mybatisflex.core.util.SqlUtil; 022import com.mybatisflex.core.util.StringUtil; 023 024import java.io.Serializable; 025import java.util.Collection; 026import java.util.List; 027 028/** 029 * 查询列,描述的是一张表的字段 030 */ 031public class QueryColumn implements Serializable { 032 033 protected QueryTable table; 034 protected String name; 035 protected String alias; 036 037 038 public QueryColumn() { 039 } 040 041 public QueryColumn(String name) { 042 SqlUtil.keepColumnSafely(name); 043 this.name = name; 044 } 045 046 public QueryColumn(String tableName, String name) { 047 SqlUtil.keepColumnSafely(name); 048 this.table = new QueryTable(tableName); 049 this.name = name; 050 } 051 052 public QueryColumn(TableDef tableDef, String name) { 053 SqlUtil.keepColumnSafely(name); 054 this.table = new QueryTable(tableDef.getTableName()); 055 this.name = name; 056 } 057 058 059 public QueryTable getTable() { 060 return table; 061 } 062 063 public void setTable(QueryTable table) { 064 this.table = table; 065 } 066 067 public String getName() { 068 return name; 069 } 070 071 public void setName(String name) { 072 this.name = name; 073 } 074 075 public String getAlias() { 076 return alias; 077 } 078 079 public void setAlias(String alias) { 080 this.alias = alias; 081 } 082 083 public QueryColumn as(String alias) { 084 SqlUtil.keepColumnSafely(alias); 085 QueryColumn newColumn = new QueryColumn(); 086 newColumn.table = this.table; 087 newColumn.name = this.name; 088 newColumn.alias = alias; 089 return newColumn; 090 } 091 092 093 // query methods /////// 094 095 /** 096 * equals 097 * 098 * @param value 099 */ 100 public QueryCondition eq(Object value) { 101 if (value == null) { 102 return QueryCondition.createEmpty(); 103 } 104 return QueryCondition.create(this, QueryCondition.LOGIC_EQUALS, value); 105 } 106 107 108 /** 109 * not equals != 110 * 111 * @param value 112 */ 113 public QueryCondition ne(Object value) { 114 if (value == null) { 115 return QueryCondition.createEmpty(); 116 } 117 return QueryCondition.create(this, QueryCondition.LOGIC_NOT_EQUALS, value); 118 } 119 120 121 public QueryCondition like(Object value) { 122 if (value == null) { 123 return QueryCondition.createEmpty(); 124 } 125 return QueryCondition.create(this, QueryCondition.LOGIC_LIKE, "%" + value + "%"); 126 } 127 128 129 public QueryCondition likeLeft(Object value) { 130 if (value == null) { 131 return QueryCondition.createEmpty(); 132 } 133 return QueryCondition.create(this, QueryCondition.LOGIC_LIKE, "%" + value); 134 } 135 136 137 public QueryCondition likeRight(Object value) { 138 if (value == null) { 139 return QueryCondition.createEmpty(); 140 } 141 return QueryCondition.create(this, QueryCondition.LOGIC_LIKE, value + "%"); 142 } 143 144 /** 145 * 大于 greater than 146 * 147 * @param value 148 */ 149 public QueryCondition gt(Object value) { 150 if (value == null) { 151 return QueryCondition.createEmpty(); 152 } 153 return QueryCondition.create(this, QueryCondition.LOGIC_GT, value); 154 } 155 156 /** 157 * 大于等于 greater or equal 158 * 159 * @param value 160 */ 161 public QueryCondition ge(Object value) { 162 if (value == null) { 163 return QueryCondition.createEmpty(); 164 } 165 return QueryCondition.create(this, QueryCondition.LOGIC_GE, value); 166 } 167 168 /** 169 * 小于 less than 170 * 171 * @param value 172 */ 173 public QueryCondition lt(Object value) { 174 if (value == null) { 175 return QueryCondition.createEmpty(); 176 } 177 return QueryCondition.create(this, QueryCondition.LOGIC_LT, value); 178 } 179 180 /** 181 * 小于等于 less or equal 182 * 183 * @param value 184 */ 185 public QueryCondition le(Object value) { 186 if (value == null) { 187 return QueryCondition.createEmpty(); 188 } 189 return QueryCondition.create(this, QueryCondition.LOGIC_LE, value); 190 } 191 192 193 /** 194 * IS NULL 195 * 196 * @return 197 */ 198 public QueryCondition isNull() { 199 return QueryCondition.create(this, QueryCondition.LOGIC_IS_NULL, null); 200 } 201 202 203 /** 204 * IS NOT NULL 205 * 206 * @return 207 */ 208 public QueryCondition isNotNull() { 209 return QueryCondition.create(this, QueryCondition.LOGIC_IS_NOT_NULL, null); 210 } 211 212 213 /** 214 * in arrays 215 * 216 * @param arrays 217 * @return 218 */ 219 public QueryCondition in(Object... arrays) { 220 //忽略 QueryWrapper.in("name", null) 的情况 221 if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) { 222 return QueryCondition.createEmpty(); 223 } 224 return QueryCondition.create(this, QueryCondition.LOGIC_IN, arrays); 225 } 226 227 /** 228 * in child select 229 * 230 * @param queryWrapper 231 * @return 232 */ 233 public QueryCondition in(QueryWrapper queryWrapper) { 234 return QueryCondition.create(this, QueryCondition.LOGIC_IN, queryWrapper); 235 } 236 237 238 /** 239 * in Collection 240 * 241 * @param collection 242 * @return 243 */ 244 public QueryCondition in(Collection<?> collection) { 245 if (collection != null && !collection.isEmpty()) { 246 return in(collection.toArray()); 247 } 248 return QueryCondition.createEmpty(); 249 } 250 251 /** 252 * not int arrays 253 * 254 * @param arrays 255 * @return 256 */ 257 public QueryCondition notIn(Object... arrays) { 258 //忽略 QueryWrapper.notIn("name", null) 的情况 259 if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) { 260 return QueryCondition.createEmpty(); 261 } 262 return QueryCondition.create(this, QueryCondition.LOGIC_NOT_IN, arrays); 263 } 264 265 266 /** 267 * not in Collection 268 * 269 * @param collection 270 * @return 271 */ 272 public QueryCondition notIn(Collection<?> collection) { 273 if (collection != null && !collection.isEmpty()) { 274 return notIn(collection.toArray()); 275 } 276 return QueryCondition.createEmpty(); 277 } 278 279 /** 280 * not in child select 281 * 282 * @param queryWrapper 283 */ 284 public QueryCondition notIn(QueryWrapper queryWrapper) { 285 return QueryCondition.create(this, QueryCondition.LOGIC_NOT_IN, queryWrapper); 286 } 287 288 289 /** 290 * between 291 * 292 * @param start 293 * @param end 294 */ 295 public QueryCondition between(Object start, Object end) { 296 return QueryCondition.create(this, QueryCondition.LOGIC_BETWEEN, new Object[]{start, end}); 297 } 298 299 /** 300 * not between 301 * 302 * @param start 303 * @param end 304 */ 305 public QueryCondition notBetween(Object start, Object end) { 306 return QueryCondition.create(this, QueryCondition.LOGIC_NOT_BETWEEN, new Object[]{start, end}); 307 } 308 309 310 ////order by //// 311 public QueryOrderBy asc() { 312 return new QueryOrderBy(this); 313 } 314 315 316 public QueryOrderBy desc() { 317 return new QueryOrderBy(this, "DESC"); 318 } 319 320 321 protected String wrap(IDialect dialect, String table, String column) { 322 if (StringUtil.isNotBlank(table)) { 323 return dialect.wrap(table) + "." + dialect.wrap(column); 324 } else { 325 return dialect.wrap(column); 326 } 327 } 328 329 String toConditionSql(List<QueryTable> queryTables, IDialect dialect) { 330 String tableName = WrapperUtil.getColumnTableName(queryTables, table); 331 return wrap(dialect, tableName, name); 332 } 333 334 335 String toSelectSql(List<QueryTable> queryTables, IDialect dialect) { 336 String tableName = WrapperUtil.getColumnTableName(queryTables, table); 337 return wrap(dialect, tableName, name) + WrapperUtil.buildAsAlias(dialect.wrap(alias)); 338 } 339 340 @Override 341 public String toString() { 342 return "QueryColumn{" + 343 "table=" + table + 344 ", name='" + name + '\'' + 345 ", alias='" + alias + '\'' + 346 '}'; 347 } 348 349 350}