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.util.Objects;
019import java.util.function.Predicate;
020
021/**
022 * 默认 {@link QueryColumn} 行为。
023 *
024 * @author michael
025 * @author 王帅
026 */
027public class QueryColumnBehavior {
028
029    private QueryColumnBehavior() {
030    }
031
032    /**
033     * 内置的可选的忽略规则
034     */
035    public static final Predicate<Object> IGNORE_NULL = Objects::isNull;
036    public static final Predicate<Object> IGNORE_EMPTY = o -> o == null || "".equals(o);
037    public static final Predicate<Object> IGNORE_BLANK = o -> o == null || "".equals(o.toString().trim());
038
039    /**
040     * 自定义全局的自动忽略参数的方法。
041     */
042    private static Predicate<Object> ignoreFunction = IGNORE_NULL;
043
044    /**
045     * 当 {@code IN(...)} 条件只有 1 个参数时,是否自动把的内容转换为相等。
046     */
047    private static boolean smartConvertInToEquals = false;
048
049    public static Predicate<Object> getIgnoreFunction() {
050        return ignoreFunction;
051    }
052
053    public static void setIgnoreFunction(Predicate<Object> ignoreFunction) {
054        QueryColumnBehavior.ignoreFunction = ignoreFunction;
055    }
056
057    public static boolean isSmartConvertInToEquals() {
058        return smartConvertInToEquals;
059    }
060
061    public static void setSmartConvertInToEquals(boolean smartConvertInToEquals) {
062        QueryColumnBehavior.smartConvertInToEquals = smartConvertInToEquals;
063    }
064
065    static boolean shouldIgnoreValue(Object value) {
066        return ignoreFunction.test(value);
067    }
068
069}