All Downloads are FREE. Search and download functionalities are using the official Maven repository.

win.doyto.query.core.QuerySuffix Maven / Gradle / Ivy

There is a newer version: 0.2.2.1-RELEASE
Show newest version
package win.doyto.query.core;

import lombok.Getter;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static win.doyto.query.core.CommonUtil.wrapWithParenthesis;
import static win.doyto.query.core.QueryBuilder.SEPARATOR;

/**
 * QuerySuffix
 *
 * @author f0rb
 */
@SuppressWarnings("squid:S00115")
@Getter
enum QuerySuffix {
    NotLike("NOT LIKE"),
    Like,
    NotIn("NOT IN"),
    In,
    NotNull("IS NOT NULL"),
    Null("IS NULL"),
    Gt(">"),
    Ge(">="),
    Lt("<"),
    Le("<="),
    NONE("=");

    QuerySuffix() {
        this.op = name().toUpperCase();
    }

    QuerySuffix(String op) {
        this.op = op;
    }

    private final String op;

    static final Pattern SUFFIX_PTN;

    private static final Map> sqlFuncMap = new EnumMap<>(QuerySuffix.class);

    static {
        List suffixList = Arrays.stream(values()).filter(querySuffix -> querySuffix != NONE).map(Enum::name).collect(Collectors.toList());
        String suffixPtn = StringUtils.join(suffixList, "|");
        SUFFIX_PTN = Pattern.compile("(" + suffixPtn + ")$");

        for (QuerySuffix querySuffix : values()) {
            sqlFuncMap.put(querySuffix, columnMeta -> columnMeta.defaultSql(querySuffix));
        }

        sqlFuncMap.put(Null, columnMeta -> columnMeta.defaultSql(Null, ""));
        sqlFuncMap.put(NotNull, columnMeta -> columnMeta.defaultSql(NotNull, ""));

        sqlFuncMap.put(In, columnMeta -> buildSqlForCollection(columnMeta, In));
        sqlFuncMap.put(NotIn, columnMeta -> buildSqlForCollection(columnMeta, NotIn));
    }

    static String buildAndSql(String fieldName, @NonNull Object value, List argList) {
        QuerySuffix querySuffix = resolve(fieldName);
        if (querySuffix == Like) {
            value = CommonUtil.escapeLike(String.valueOf(value));
        }
        return sqlFuncMap.get(querySuffix).apply(new ColumnMeta(fieldName, value, argList));
    }

    static QuerySuffix resolve(String fieldName) {
        QuerySuffix querySuffix = NONE;
        Matcher matcher = SUFFIX_PTN.matcher(fieldName);
        if (matcher.find()) {
            querySuffix = valueOf(matcher.group());
        }
        return querySuffix;
    }

    private static String buildSqlForCollection(ColumnMeta columnMeta, QuerySuffix querySuffix) {
        int size = ((Collection) columnMeta.value).size();
        String ex = StringUtils.join(IntStream.range(0, size).mapToObj(i -> QueryBuilder.REPLACE_HOLDER).collect(Collectors.toList()), SEPARATOR);
        return columnMeta.defaultSql(querySuffix, wrapWithParenthesis(StringUtils.trimToNull(ex)));
    }

    String resolveColumnName(String fieldName) {
        String suffix = this.name();
        return fieldName.endsWith(suffix) ? fieldName.substring(0, fieldName.length() - suffix.length()) : fieldName;
    }
}