All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.zodiac.mybatisplus.support.SqlKeyword Maven / Gradle / Ivy
package org.zodiac.mybatisplus.support;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.zodiac.commons.util.DateTimes;
import org.zodiac.commons.util.Func;
import org.zodiac.sdk.toolkit.constants.StringPool;
import org.zodiac.sdk.toolkit.util.DateTimeUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import java.util.Map;
/**
* 定义常用的SQL关键字。
*/
public class SqlKeyword {
private final static String SQL_REGEX =
"'|%|--|insert|delete|select|count|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql";
private static final String EQUAL = "_equal";
private static final String NOT_EQUAL = "_notequal";
private static final String LIKE = "_like";
private static final String LIKE_LEFT = "_likeleft";
private static final String LIKE_RIGHT = "_likeright";
private static final String NOT_LIKE = "_notlike";
private static final String GE = "_ge";
private static final String LE = "_le";
private static final String GT = "_gt";
private static final String LT = "_lt";
private static final String DATE_GE = "_datege";
private static final String DATE_GT = "_dategt";
private static final String DATE_EQUAL = "_dateequal";
private static final String DATE_LT = "_datelt";
private static final String DATE_LE = "_datele";
private static final String IS_NULL = "_null";
private static final String NOT_NULL = "_notnull";
private static final String IGNORE = "_ignore";
/**
* 条件构造器。
*
* @param query 查询字段
* @param qw 查询包装类。
*/
public static void buildCondition(Map query, QueryWrapper> qw) {
if (Func.isEmpty(query)) {
return;
}
query.forEach((k, v) -> {
if (Func.hasEmpty(k, v) || k.endsWith(IGNORE)) {
return;
}
if (k.endsWith(EQUAL)) {
qw.eq(getColumn(k, EQUAL), v);
} else if (k.endsWith(NOT_EQUAL)) {
qw.ne(getColumn(k, NOT_EQUAL), v);
} else if (k.endsWith(LIKE_LEFT)) {
qw.likeLeft(getColumn(k, LIKE_LEFT), v);
} else if (k.endsWith(LIKE_RIGHT)) {
qw.likeRight(getColumn(k, LIKE_RIGHT), v);
} else if (k.endsWith(NOT_LIKE)) {
qw.notLike(getColumn(k, NOT_LIKE), v);
} else if (k.endsWith(GE)) {
qw.ge(getColumn(k, GE), v);
} else if (k.endsWith(LE)) {
qw.le(getColumn(k, LE), v);
} else if (k.endsWith(GT)) {
qw.gt(getColumn(k, GT), v);
} else if (k.endsWith(LT)) {
qw.lt(getColumn(k, LT), v);
} else if (k.endsWith(DATE_GE)) {
qw.ge(getColumn(k, DATE_GE), DateTimeUtil.parseDate(String.valueOf(v), DateTimes.FORMAT_19));
} else if (k.endsWith(DATE_GT)) {
qw.gt(getColumn(k, DATE_GT), DateTimeUtil.parseDate(String.valueOf(v), DateTimes.FORMAT_19));
} else if (k.endsWith(DATE_EQUAL)) {
qw.eq(getColumn(k, DATE_EQUAL), DateTimeUtil.parseDate(String.valueOf(v), DateTimes.FORMAT_19));
} else if (k.endsWith(DATE_LE)) {
qw.le(getColumn(k, DATE_LE), DateTimeUtil.parseDate(String.valueOf(v), DateTimes.FORMAT_19));
} else if (k.endsWith(DATE_LT)) {
qw.lt(getColumn(k, DATE_LT), DateTimeUtil.parseDate(String.valueOf(v), DateTimes.FORMAT_19));
} else if (k.endsWith(IS_NULL)) {
qw.isNull(getColumn(k, IS_NULL));
} else if (k.endsWith(NOT_NULL)) {
qw.isNotNull(getColumn(k, NOT_NULL));
} else {
qw.like(getColumn(k, LIKE), v);
}
});
}
/**
* 获取数据库字段。
*
* @param column 字段名
* @param keyword 关键字
* @return 字段
*/
private static String getColumn(String column, String keyword) {
return StrUtil.camelToUnderscore(StrUtil.removeSuffix(column, keyword));
}
/**
* 把SQL关键字替换为空字符串。
*
* @param param 关键字
* @return 过滤结果
*/
public static String filter(String param) {
if (param == null) {
return null;
}
return param.replaceAll("(?i)" + SQL_REGEX, StringPool.EMPTY);
}
}