feat: add QueryColumnBehavior

This commit is contained in:
开源海哥 2023-08-08 10:31:47 +08:00
parent 59a2f4f183
commit a7b392c8a9
2 changed files with 138 additions and 44 deletions

View File

@ -140,7 +140,7 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* @param value
*/
public QueryCondition eq(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.EQUALS, value);
@ -148,7 +148,7 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
public <T> QueryCondition eq(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.EQUALS, value).when(fn);
@ -161,14 +161,14 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* @param value
*/
public QueryCondition ne(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_EQUALS, value);
}
public <T> QueryCondition ne(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_EQUALS, value).when(fn);
@ -181,14 +181,14 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* @param value
*/
public QueryCondition like(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LIKE, "%" + value + "%");
}
public <T> QueryCondition like(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LIKE, "%" + value + "%").when(fn);
@ -196,14 +196,14 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
public QueryCondition likeLeft(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LIKE, value + "%");
}
public <T> QueryCondition likeLeft(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LIKE, value + "%").when(fn);
@ -211,14 +211,14 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
public QueryCondition likeRight(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LIKE, "%" + value);
}
public <T> QueryCondition likeRight(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LIKE, "%" + value).when(fn);
@ -226,35 +226,34 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
public QueryCondition likeRaw(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LIKE, value);
}
public <T> QueryCondition likeRaw(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LIKE, value).when(fn);
}
/**
* not like %%
*
* @param value
*/
public QueryCondition notLike(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value + "%");
}
public <T> QueryCondition notLike(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value + "%").when(fn);
@ -262,14 +261,14 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
public QueryCondition notLikeLeft(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, value + "%");
}
public <T> QueryCondition notLikeLeft(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, value + "%").when(fn);
@ -277,35 +276,34 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
public QueryCondition notLikeRight(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value);
}
public <T> QueryCondition notLikeRight(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value).when(fn);
}
/**
* 大于 greater than
*
* @param value
*/
public QueryCondition gt(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.GT, value);
}
public <T> QueryCondition gt(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.GT, value).when(fn);
@ -317,14 +315,14 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* @param value
*/
public QueryCondition ge(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.GE, value);
}
public <T> QueryCondition ge(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.GE, value).when(fn);
@ -336,14 +334,14 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* @param value
*/
public QueryCondition lt(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LT, value);
}
public <T> QueryCondition lt(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LT, value).when(fn);
@ -355,14 +353,14 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* @param value
*/
public QueryCondition le(Object value) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LE, value);
}
public <T> QueryCondition le(Object value, Predicate<T> fn) {
if (value == null) {
if (value == null || QueryColumnBehavior.shouldIgnoreValue(value)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.LE, value).when(fn);
@ -372,7 +370,7 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
/**
* IS NULL
*
* @return
* @return QueryCondition
*/
public QueryCondition isNull() {
return QueryCondition.create(this, SqlConsts.IS_NULL, null);
@ -386,7 +384,7 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
/**
* IS NOT NULL
*
* @return
* @return QueryCondition
*/
public QueryCondition isNotNull() {
return QueryCondition.create(this, SqlConsts.IS_NOT_NULL, null);
@ -401,21 +399,32 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* in arrays
*
* @param arrays
* @return
* @return QueryCondition
*/
public QueryCondition in(Object... arrays) {
//忽略 QueryWrapper.in("name", null) 的情况
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) {
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null) || QueryColumnBehavior.shouldIgnoreValue(arrays)) {
return QueryCondition.createEmpty();
}
if (arrays.length == 1 && QueryColumnBehavior.isSmartConvertInToEquals()) {
return QueryCondition.create(this, SqlConsts.EQUALS, arrays[0]);
}
return QueryCondition.create(this, SqlConsts.IN, arrays);
}
public <T> QueryCondition in(Object[] arrays, Predicate<T> fn) {
//忽略 QueryWrapper.in("name", null) 的情况
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) {
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null) || QueryColumnBehavior.shouldIgnoreValue(arrays)) {
return QueryCondition.createEmpty();
}
if (arrays.length == 1 && QueryColumnBehavior.isSmartConvertInToEquals()) {
return QueryCondition.create(this, SqlConsts.EQUALS, arrays[0]);
}
return QueryCondition.create(this, SqlConsts.IN, arrays).when(fn);
}
@ -423,13 +432,20 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* in child select
*
* @param queryWrapper
* @return
* @return QueryCondition
*/
public QueryCondition in(QueryWrapper queryWrapper) {
if (queryWrapper == null || QueryColumnBehavior.shouldIgnoreValue(queryWrapper)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.IN, queryWrapper);
}
public <T> QueryCondition in(QueryWrapper queryWrapper, Predicate<T> fn) {
if (queryWrapper == null || QueryColumnBehavior.shouldIgnoreValue(queryWrapper)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.IN, queryWrapper).when(fn);
}
@ -438,17 +454,17 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* in Collection
*
* @param collection
* @return
* @return QueryCondition
*/
public QueryCondition in(Collection<?> collection) {
if (collection != null && !collection.isEmpty()) {
if (collection != null && !collection.isEmpty() && !QueryColumnBehavior.shouldIgnoreValue(collection)) {
return in(collection.toArray());
}
return QueryCondition.createEmpty();
}
public <T> QueryCondition in(Collection<?> collection, Predicate<T> fn) {
if (collection != null && !collection.isEmpty()) {
if (collection != null && !collection.isEmpty() && !QueryColumnBehavior.shouldIgnoreValue(collection)) {
return in(collection.toArray(), fn);
}
return QueryCondition.createEmpty();
@ -458,21 +474,31 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* not int arrays
*
* @param arrays
* @return
* @return QueryCondition
*/
public QueryCondition notIn(Object... arrays) {
//忽略 QueryWrapper.notIn("name", null) 的情况
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) {
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null) || QueryColumnBehavior.shouldIgnoreValue(arrays)) {
return QueryCondition.createEmpty();
}
if (arrays.length == 1 && QueryColumnBehavior.isSmartConvertInToEquals()) {
return QueryCondition.create(this, SqlConsts.NOT_EQUALS, arrays[0]);
}
return QueryCondition.create(this, SqlConsts.NOT_IN, arrays);
}
public <T> QueryCondition notIn(Object[] arrays, Predicate<T> fn) {
//忽略 QueryWrapper.notIn("name", null) 的情况
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) {
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null) || QueryColumnBehavior.shouldIgnoreValue(arrays)) {
return QueryCondition.createEmpty();
}
if (arrays.length == 1 && QueryColumnBehavior.isSmartConvertInToEquals()) {
return QueryCondition.create(this, SqlConsts.NOT_EQUALS, arrays[0]);
}
return QueryCondition.create(this, SqlConsts.NOT_IN, arrays).when(fn);
}
@ -481,17 +507,17 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* not in Collection
*
* @param collection
* @return
* @return QueryCondition
*/
public QueryCondition notIn(Collection<?> collection) {
if (collection != null && !collection.isEmpty()) {
if (collection != null && !collection.isEmpty() && !QueryColumnBehavior.shouldIgnoreValue(collection)) {
return notIn(collection.toArray());
}
return QueryCondition.createEmpty();
}
public <T> QueryCondition notIn(Collection<?> collection, Predicate<T> fn) {
if (collection != null && !collection.isEmpty()) {
if (collection != null && !collection.isEmpty() && !QueryColumnBehavior.shouldIgnoreValue(collection)) {
return notIn(collection.toArray(), fn);
}
return QueryCondition.createEmpty();
@ -503,10 +529,17 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
* @param queryWrapper
*/
public QueryCondition notIn(QueryWrapper queryWrapper) {
if (queryWrapper == null || QueryColumnBehavior.shouldIgnoreValue(queryWrapper)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_IN, queryWrapper);
}
public <T> QueryCondition notIn(QueryWrapper queryWrapper, Predicate<T> fn) {
if (queryWrapper == null || QueryColumnBehavior.shouldIgnoreValue(queryWrapper)) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_IN, queryWrapper).when(fn);
}
@ -615,7 +648,7 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
return null;
}
if (selfTable != null && StringUtil.isNotBlank(selfTable.alias)){
if (selfTable != null && StringUtil.isNotBlank(selfTable.alias)) {
return selfTable;
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.query;
import java.util.function.Function;
/**
* @author michael
*/
public class QueryColumnBehavior {
/**
* 自定义全局的自动忽略参数的方法
*/
private static Function<Object,Boolean> ignoreFunction;
/**
* 是否自动把 in(...) 只有 1 个参数的内容转换为相等 =
*/
private static boolean smartConvertInToEquals = false;
public static Function<Object, Boolean> getIgnoreFunction() {
return ignoreFunction;
}
public static void setIgnoreFunction(Function<Object, Boolean> ignoreFunction) {
QueryColumnBehavior.ignoreFunction = ignoreFunction;
}
public static boolean isSmartConvertInToEquals() {
return smartConvertInToEquals;
}
public static void setSmartConvertInToEquals(boolean smartConvertInToEquals) {
QueryColumnBehavior.smartConvertInToEquals = smartConvertInToEquals;
}
static boolean shouldIgnoreValue(Object value){
if (ignoreFunction == null){
return false;
}
return ignoreFunction.apply(value);
}
}