mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
commit
5ab33e646e
@ -20,6 +20,8 @@ import com.mybatisflex.core.util.ObjectUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* 括号
|
||||
@ -32,6 +34,59 @@ public class Brackets extends QueryCondition {
|
||||
this.childCondition = childCondition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryColumn getColumn() {
|
||||
return childCondition.getColumn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColumn(QueryColumn column) {
|
||||
childCondition.setColumn(column);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Object value) {
|
||||
childCondition.setValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLogic() {
|
||||
return childCondition.getLogic();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLogic(String logic) {
|
||||
childCondition.setLogic(logic);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryCondition when(boolean effective) {
|
||||
return childCondition.when(effective);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryCondition when(BooleanSupplier fn) {
|
||||
return childCondition.when(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 继承自标记删除的方法
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public <T> QueryCondition when(Predicate<T> fn) {
|
||||
return childCondition.when(fn);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected QueryCondition getPrevEffectiveCondition() {
|
||||
return childCondition.getPrevEffectiveCondition();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected QueryCondition getNextEffectiveCondition() {
|
||||
return childCondition.getNextEffectiveCondition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryCondition and(QueryCondition nextCondition) {
|
||||
|
||||
@ -133,11 +133,22 @@ public class CPI {
|
||||
queryWrapper.addJoin(join);
|
||||
}
|
||||
|
||||
public static QueryCondition getPrevCondition(QueryCondition queryCondition) {
|
||||
return queryCondition.getPrevEffectiveCondition();
|
||||
}
|
||||
|
||||
public static QueryCondition getNextCondition(QueryCondition queryCondition) {
|
||||
return queryCondition.getNextEffectiveCondition();
|
||||
}
|
||||
|
||||
public static QueryCondition getWhereQueryCondition(QueryWrapper queryWrapper) {
|
||||
return queryWrapper.getWhereQueryCondition();
|
||||
}
|
||||
|
||||
public static void setWhereQueryCondition(QueryWrapper queryWrapper, QueryCondition queryCondition) {
|
||||
queryWrapper.setWhereQueryCondition(queryCondition);
|
||||
}
|
||||
|
||||
public static void addWhereQueryCondition(QueryWrapper queryWrapper, QueryCondition queryCondition, SqlConnector connector) {
|
||||
queryWrapper.addWhereQueryCondition(queryCondition, connector);
|
||||
}
|
||||
|
||||
@ -240,6 +240,13 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
|
||||
return prev.checkEffective() ? prev : prev.getPrevEffectiveCondition();
|
||||
}
|
||||
|
||||
protected QueryCondition getNextEffectiveCondition() {
|
||||
if (next == null) {
|
||||
return null;
|
||||
}
|
||||
return next.checkEffective() ? next : next.getNextEffectiveCondition();
|
||||
}
|
||||
|
||||
|
||||
protected void appendQuestionMark(StringBuilder sqlBuilder) {
|
||||
//noinspection StatementWithEmptyBody
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
|
||||
package com.mybatisflex.coretest;
|
||||
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.query.*;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
import org.junit.Test;
|
||||
@ -79,4 +79,56 @@ public class DynamicConditionTest {
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test05() {
|
||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
.from(ACCOUNT)
|
||||
.where(ACCOUNT.ID.in(1, 2, 3));
|
||||
|
||||
boolean anyMatch = CPI.getQueryTables(queryWrapper)
|
||||
.stream()
|
||||
.map(QueryTable::getName)
|
||||
.anyMatch(tableName -> tableName.equals(ACCOUNT.getTableName()));
|
||||
|
||||
if (anyMatch) {
|
||||
CPI.addWhereQueryCondition(queryWrapper, ACCOUNT.AGE.ge(18), SqlConnector.AND);
|
||||
}
|
||||
|
||||
System.out.println(queryWrapper.toSQL());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test06() {
|
||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
.from(ACCOUNT)
|
||||
.where(ACCOUNT.ID.in(1, 2, 3))
|
||||
.and(ACCOUNT.AGE.ge(18))
|
||||
.or(ACCOUNT.USER_NAME.eq("zhang san"));
|
||||
|
||||
for (QueryCondition condition = CPI.getWhereQueryCondition(queryWrapper); condition != null; condition = CPI.getNextCondition(condition)) {
|
||||
if (condition.getColumn().getName().equals(ACCOUNT.AGE.getName())) {
|
||||
condition.when(false);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(queryWrapper.toSQL());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test07() {
|
||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
.from(ACCOUNT)
|
||||
.where(ACCOUNT.ID.in(1, 2, 3)
|
||||
.and(ACCOUNT.AGE.ge(18))
|
||||
.or(ACCOUNT.USER_NAME.eq("zhang san")));
|
||||
|
||||
for (QueryCondition condition = CPI.getWhereQueryCondition(queryWrapper); condition != null; condition = CPI.getNextCondition(condition)) {
|
||||
if (condition.getColumn().getName().equals(ACCOUNT.AGE.getName())) {
|
||||
condition.when(false);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(queryWrapper.toSQL());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user