
cn.org.atool.fluent.mybatis.segment.WhereApply Maven / Gradle / Ivy
package cn.org.atool.fluent.mybatis.segment;
import cn.org.atool.fluent.mybatis.base.crud.IBaseQuery;
import cn.org.atool.fluent.mybatis.base.crud.IQuery;
import cn.org.atool.fluent.mybatis.base.model.ISqlOp;
import cn.org.atool.fluent.mybatis.exception.FluentMybatisException;
import cn.org.atool.fluent.mybatis.functions.QFunction;
import cn.org.atool.fluent.mybatis.ifs.Ifs;
import cn.org.atool.fluent.mybatis.ifs.IfsPredicate;
import cn.org.atool.fluent.mybatis.segment.where.BooleanWhere;
import cn.org.atool.fluent.mybatis.segment.where.NumericWhere;
import cn.org.atool.fluent.mybatis.segment.where.ObjectWhere;
import cn.org.atool.fluent.mybatis.segment.where.StringWhere;
import cn.org.atool.fluent.mybatis.utility.NestedQueryFactory;
import java.util.Collection;
import java.util.function.Predicate;
import java.util.stream.Stream;
import static cn.org.atool.fluent.mybatis.base.model.SqlOp.*;
import static cn.org.atool.fluent.mybatis.utility.MybatisUtil.*;
/**
* AndObject
*
* @param 条件设置器
* @param 对应的嵌套查询器
* @author darui.wu
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class WhereApply<
WHERE extends WhereBase,
NQ extends IBaseQuery, NQ>
>
extends BaseApply
implements
ObjectWhere,
NumericWhere,
StringWhere,
BooleanWhere {
public WhereApply(WHERE where) {
super(where);
}
@Override
public WHERE apply(ISqlOp op, O... args) {
if (op.getArgSize() > 0) {
assertNotEmpty(this.current().name, args);
if (args.length != op.getArgSize()) {
throw new FluentMybatisException(op.getArgSize() + " parameters are required, but " + args.length + " is passed in");
}
Stream.of(args).forEach(arg -> assertNotNull(this.current().name, arg));
}
if (op.getArgSize() == -1) {
assertNotEmpty(this.current().name, args);
}
if (op == IN && args.length == 1) {
Object value = args[0];
if (value instanceof Collection) {
Object[] arr = ((Collection) value).toArray();
assertNotEmpty(this.current().name, arr);
if (arr.length > 1) {
return this.segment.apply(this.column(), IN, arr);
} else {
value = arr[0];
}
}
return this.segment.apply(this.column(), EQ, value);
} else {
return this.segment.apply(this.column(), op, (Object[]) args);
}
}
@Override
public WHERE apply(ISqlOp op, Ifs ifs) {
/* 重载(实际入参为null)时兼容处理 **/
if (ifs == null) {
return this.apply(op, (Object) null);
}
if (op.getArgSize() > 1) {
throw new IllegalArgumentException("Ifs condition does not apply to the operation:" + op.name());
}
for (IfsPredicate predicate : ifs.predicates) {
Object value = predicate.value(op);
if (predicate.predicate.test(value)) {
return this.apply(op, value);
}
}
return segment;
}
@Override
public WHERE apply(Predicate
© 2015 - 2025 Weber Informatics LLC | Privacy Policy