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 com.mybatisflex.core.constant.SqlConsts;
019import com.mybatisflex.core.constant.SqlOperator;
020
021import java.util.Iterator;
022import java.util.Objects;
023import java.util.function.Function;
024import java.util.function.Predicate;
025
026/**
027 * 默认 {@link QueryColumn} 行为。
028 *
029 * @author michael
030 * @author 王帅
031 * @author CloudPlayer
032 */
033public class QueryColumnBehavior {
034
035    private QueryColumnBehavior() {
036    }
037
038    /**
039     * 内置的可选的忽略规则
040     */
041    public static final Predicate<Object> IGNORE_NULL = Objects::isNull;
042    public static final Predicate<Object> IGNORE_EMPTY = o -> o == null || "".equals(o);
043    public static final Predicate<Object> IGNORE_BLANK = o -> o == null || "".equals(o.toString().trim());
044
045    /**
046     * 在满足输入的数组或可迭代对象中的容量为 1 (即只有一个元素)时,自动将条件中的 in 转换为 =
047     */
048    public static final Function<? super QueryCondition, ? extends QueryCondition> CONVERT_IN_TO_EQUALS = it -> {
049        Object value = it.value;
050        if (it.logic.equalsIgnoreCase(SqlConsts.IN) || it.logic.equalsIgnoreCase(SqlConsts.NOT_IN)) {
051            Object firstValue;
052            if (value instanceof Iterable<?>) {
053                Iterator<?> iter = ((Iterable<?>) value).iterator();
054                if (!iter.hasNext()) {  // 没有元素,直接返回原条件
055                    return it;
056                }
057                firstValue = iter.next();  // 取第一个元素
058                if (iter.hasNext()) {  // 如果有后续元素,则直接返回原条件
059                    return it;
060                }
061            } else if (value instanceof Object[]) {
062                Object[] array = (Object[]) value;
063                if (array.length != 1) {  // 如果不是单元素的数组就直接返回
064                    return it;
065                }
066                firstValue = array[0];  // 取第一个元素
067            } else {
068                return it;
069            }
070
071            SqlOperator operator = it.logic.equalsIgnoreCase(SqlConsts.IN) ? SqlOperator.EQUALS : SqlOperator.NOT_EQUALS;
072            return QueryCondition.create(it.column, operator, firstValue);  // 将 in 转换为 =
073        } else {
074            return it;
075        }
076    };
077
078    /**
079     * 如果使用了 = 来比较 null ,则将其转为 is null 。
080     */
081    public static final Function<? super QueryCondition, ? extends QueryCondition> CONVERT_EQUALS_TO_IS_NULL = it ->
082        it.value == null && it.logic.equalsIgnoreCase(SqlConsts.EQUALS) ? it.column.isNull() : it;
083    /**
084     * 自定义全局的自动忽略参数的方法。
085     */
086    private static Predicate<Object> ignoreFunction = IGNORE_NULL;
087
088    /**
089     * 自定义全局的自动转换条件的方法。
090     */
091    private static Function<? super QueryCondition, ? extends QueryCondition> conditionCaster = Function.identity();
092
093    /**
094     * 当 {@code IN(...)} 条件只有 1 个参数时,是否自动把的内容转换为相等。
095     */
096    private static boolean smartConvertInToEquals = false;
097
098    public static Predicate<Object> getIgnoreFunction() {
099        return ignoreFunction;
100    }
101
102    public static void setIgnoreFunction(Predicate<Object> ignoreFunction) {
103        QueryColumnBehavior.ignoreFunction = ignoreFunction;
104    }
105
106    public static boolean isSmartConvertInToEquals() {
107        return smartConvertInToEquals;
108    }
109
110    public static void setSmartConvertInToEquals(boolean smartConvertInToEquals) {
111        QueryColumnBehavior.smartConvertInToEquals = smartConvertInToEquals;
112    }
113
114    static boolean shouldIgnoreValue(Object value) {
115        return ignoreFunction.test(value);
116    }
117
118    public static Function<? super QueryCondition, ? extends QueryCondition> getConditionCaster() {
119        return smartConvertInToEquals ? CONVERT_IN_TO_EQUALS.andThen(conditionCaster) : conditionCaster;
120    }
121
122    public static void setConditionCaster(Function<? super QueryCondition, ? extends QueryCondition> conditionCaster) {
123        QueryColumnBehavior.conditionCaster = conditionCaster;
124    }
125
126    public static QueryCondition castCondition(QueryCondition condition) {
127        return getConditionCaster().apply(condition);
128    }
129}