feat: QueryWrapper add "not like" support

This commit is contained in:
开源海哥 2023-07-24 11:57:15 +08:00
parent c29e58ba10
commit 269cb1354d
5 changed files with 74 additions and 2 deletions

View File

@ -114,6 +114,7 @@ public final class SqlConsts {
public static final String LT = " < ";
public static final String LE = " <= ";
public static final String LIKE = " LIKE ";
public static final String NOT_LIKE = " NOT LIKE ";
public static final String EQUALS = " = ";
public static final String NOT_EQUALS = " != ";
public static final String IS_NULL = " IS NULL ";

View File

@ -35,6 +35,9 @@ public enum SqlOperator {
// like
LIKE(SqlConsts.LIKE),
// not like
NOT_LIKE(SqlConsts.NOT_LIKE),
// =
EQUALS(SqlConsts.EQUALS),

View File

@ -224,6 +224,7 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
return QueryCondition.create(this, SqlConsts.LIKE, "%" + value).when(fn);
}
public QueryCondition likeRaw(Object value) {
if (value == null) {
return QueryCondition.createEmpty();
@ -238,6 +239,59 @@ public class QueryColumn implements CloneSupport<QueryColumn> {
return QueryCondition.create(this, SqlConsts.LIKE, value).when(fn);
}
/**
* not like %%
*
* @param value
*/
public QueryCondition notLike(Object value) {
if (value == null) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value + "%");
}
public <T> QueryCondition notLike(Object value, Predicate<T> fn) {
if (value == null) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value + "%").when(fn);
}
public QueryCondition notLikeLeft(Object value) {
if (value == null) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, value + "%");
}
public <T> QueryCondition notLikeLeft(Object value, Predicate<T> fn) {
if (value == null) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, value + "%").when(fn);
}
public QueryCondition notLikeRight(Object value) {
if (value == null) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value);
}
public <T> QueryCondition notLikeRight(Object value, Predicate<T> fn) {
if (value == null) {
return QueryCondition.createEmpty();
}
return QueryCondition.create(this, SqlConsts.NOT_LIKE, "%" + value).when(fn);
}
/**
* 大于 greater than
*

View File

@ -109,7 +109,8 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
public <T> QueryCondition when(Predicate<T> fn) {
Object val = this.value;
if (SqlConsts.LIKE.equals(logic) && val instanceof String) {
if ((SqlConsts.LIKE.equals(logic) || SqlConsts.NOT_LIKE.equals(logic))
&& val instanceof String) {
String valStr = (String) val;
if (valStr.startsWith(SqlConsts.PERCENT_SIGN)) {
valStr = valStr.substring(1);

View File

@ -203,6 +203,19 @@ public class AccountSqlTester {
}
@Test
public void testWhereSql3() {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.from(ACCOUNT)
.where(ACCOUNT.ID.ge(100))
.and(ACCOUNT.USER_NAME.notLike("michael"));
System.out.println(queryWrapper.toSQL());
}
@Test
public void testWhereCond2Sql() {
boolean flag = false;