win.doyto.query.core.QuerySuffix Maven / Gradle / Ivy
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