
cn.org.atool.fluent.mybatis.segment.WhereBase Maven / Gradle / Ivy
package cn.org.atool.fluent.mybatis.segment;
import cn.org.atool.fluent.mybatis.If;
import cn.org.atool.fluent.mybatis.base.IEntity;
import cn.org.atool.fluent.mybatis.base.IRefs;
import cn.org.atool.fluent.mybatis.base.crud.*;
import cn.org.atool.fluent.mybatis.base.model.Column;
import cn.org.atool.fluent.mybatis.base.model.FieldMapping;
import cn.org.atool.fluent.mybatis.base.model.ISqlOp;
import cn.org.atool.fluent.mybatis.functions.GetterFunc;
import cn.org.atool.fluent.mybatis.functions.QFunction;
import cn.org.atool.fluent.mybatis.segment.model.KeyWordSegment;
import cn.org.atool.fluent.mybatis.segment.model.Parameters;
import cn.org.atool.fluent.mybatis.utility.MappingKits;
import cn.org.atool.fluent.mybatis.utility.NestedQueryFactory;
import lombok.experimental.Accessors;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Predicate;
import static cn.org.atool.fluent.mybatis.If.notNull;
import static cn.org.atool.fluent.mybatis.base.model.Column.EMPTY_COLUMN;
import static cn.org.atool.fluent.mybatis.base.model.SqlOp.*;
import static cn.org.atool.fluent.mybatis.segment.model.KeyWordSegment.AND;
import static cn.org.atool.fluent.mybatis.segment.model.KeyWordSegment.OR;
import static cn.org.atool.fluent.mybatis.utility.MybatisUtil.assertNotNull;
/**
* BaseQueryAnd: AND或者OR操作基类
*
* @param 更新器
* @param 查询或者更新
* @param WRAPPER 对应的嵌套查询器
* @author darui.wu
*/
@SuppressWarnings({"unchecked", "unused", "rawtypes"})
@Accessors(chain = true)
public abstract class WhereBase<
WHERE extends WhereBase,
WRAPPER extends IWrapper, WRAPPER, NestedQ>,
NestedQ extends IBaseQuery, NestedQ>
>
extends BaseSegment, WRAPPER> {
private final WhereApply apply = new WhereApply<>((WHERE) this);
/**
* 当前连接符: AND 连接, OR 连接
*/
private final KeyWordSegment currOp;
public WHERE and;
public WHERE or;
protected WhereBase(WRAPPER wrapper) {
super(wrapper);
this.currOp = AND;
this.and = (WHERE) this;
this.or = this.buildOr(this.and);
}
protected WhereBase(WRAPPER wrapper, WHERE and) {
super(wrapper);
this.currOp = OR;
this.and = and;
this.or = (WHERE) this;
}
/**
* 根据and where构造or where实例
*
* @param and and where
* @return or where
*/
protected abstract WHERE buildOr(WHERE and);
/**
* 设置默认条件
*
* Query: {@link IDefaultSetter#setQueryDefault(IQuery)}
* Update: {@link IDefaultSetter#setUpdateDefault(IUpdate)}
*
* @return WHERE
*/
public WHERE defaults() {
return this.and;
}
/**
* 根据entity非空字段设置where条件
*
* replaced by {@link #eqByEntity(IEntity, String...)}
*
* @param entity 实例
* @return 查询器StudentQuery
*/
@Deprecated
public WHERE eqNotNull(IEntity entity) {
return this.eqByEntity(entity);
}
/**
* 根据entity设置where条件
*
*
* o 指定字段列表, 可以是 null 值
* o 无指定字段时, 所有非空entity字段
*
*
* @param entity 实例
* @param columns 要设置条件的字段
* @return 查询器StudentQuery
*/
public WHERE eqByEntity(IEntity entity, String... columns) {
super.byEntity(entity, (column, value) -> {
if (value == null) {
this.apply(column, IS_NULL);
} else {
this.apply(column, EQ, value);
}
}, true, Arrays.asList(columns));
return this.and;
}
/**
* 根据entity指定字段(允许null)设置where条件
*
* @param entity 实例
* @param column 要设置条件的字段
* @param columns 要设置条件的字段
* @return 查询器StudentQuery
*/
public WHERE eqByEntity(IEntity entity, FieldMapping column, FieldMapping... columns) {
assertNotNull("entity", entity);
String[] arr = MappingKits.toColumns(column, columns);
return this.eqByEntity(entity, arr);
}
/**
* 根据entity指定字段(允许null)设置where条件
*
* @param entity 实例
* @param column 要设置条件的字段
* @param columns 要设置条件的字段
* @return 查询器StudentQuery
*/
public WHERE eqByEntity(E entity, GetterFunc column, GetterFunc... columns) {
assertNotNull("entity", entity);
Class klass = IRefs.instance().findFluentEntityClass(entity.getClass());
String[] arr = MappingKits.toColumns(klass, column, columns);
return this.eqByEntity(entity, arr);
}
/**
* 根据entity(排除指定字段)设置where条件
*
*
* o 无指定字段时, 条件等于所有字段(包括null值)
*
*
* @param entity 实例
* @param excludes 排除设置条件的字段
* @return 查询器StudentQuery
*/
public WHERE eqByExclude(IEntity entity, String... excludes) {
super.byExclude(entity, (column, value) -> {
if (value == null) {
this.apply(column, IS_NULL);
} else {
this.apply(column, EQ, value);
}
}, true, Arrays.asList(excludes));
return this.and;
}
public WHERE eqByExclude(E entity, GetterFunc exclude, GetterFunc... excludes) {
assertNotNull("entity", entity);
Class klass = IRefs.instance().findFluentEntityClass(entity.getClass());
String[] arr = MappingKits.toColumns(klass, exclude, excludes);
return this.eqByExclude(entity, arr);
}
public WHERE eqByExclude(IEntity entity, FieldMapping exclude, FieldMapping... excludes) {
assertNotNull("entity", entity);
String[] arr = MappingKits.toColumns(exclude, excludes);
return this.eqByExclude(entity, arr);
}
/**
* map 所有非空属性等于 =
* key: column字段名称
* value: 设置值, 忽略null值
*
* @param params map 类型的参数, key 是字段名, value 是字段值
* @param 值类型
* @return self
*/
public WHERE eqNotNull(Map params) {
return eqMap(params, true);
}
/**
* map 所有非空属性等于 =
* key: column字段名称
* value: 设置值
*
* @param params map 类型的参数, key 是字段名, value 是字段值
* @param ignoreNull value为null时,是否忽略。如果ignoreNull = false, 且value=null, 会执行 column is null判断
* @return self
*/
public WHERE eqMap(Map params, boolean ignoreNull) {
params.forEach((k, v) -> {
Column column = Column.column(k, this.wrapper);
if (notNull(v)) {
this.wrapper.getWrapperData().apply(AND, column, EQ, v);
} else if (!ignoreNull) {
this.wrapper.getWrapperData().apply(AND, column, IS_NULL);
}
});
return this.and;
}
/**
* EXISTS ( sql语句 )
*
* 例: EXISTS("select id from table where age = 1")
*
* @param select exists sql语句
* @param values 参数, 对应 select 语句中的 "?" 占位符
* @return self
*/
public WHERE exists(String select, Object... values) {
wrapper.getWrapperData().apply(currOp, EMPTY_COLUMN, EXISTS, select, values);
return this.and;
}
/**
* use {@link #exists(Predicate, String, Object...)}
*/
@Deprecated
public WHERE exists(boolean condition, String select, Object... values) {
return this.exists(a -> condition, select, values);
}
/**
* EXISTS ( sql语句 )
*
* 例: EXISTS("select id from table where age = 1")
*
* @param predicate true时条件成立
* @param select exists sql语句
* @param values 参数, 对应 select 语句中的 "?" 占位符
* @return self
*/
public WHERE exists(Predicate
© 2015 - 2025 Weber Informatics LLC | Privacy Policy