All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.db.builder.DefaultWhereCauseBuilder Maven / Gradle / Ivy

package org.onetwo.common.db.builder;

import java.util.Map;
import java.util.function.Supplier;

import javax.persistence.metamodel.SingularAttribute;

import org.onetwo.common.db.builder.QueryBuilderImpl.SubQueryBuilder;
import org.onetwo.common.db.filter.DataQueryParamaterEnhancer;
import org.onetwo.common.db.sqlext.ExtQuery.K;
import org.onetwo.common.db.sqlext.ExtQuery.KeyObject;
import org.onetwo.common.reflect.ReflectUtils;
import org.onetwo.common.utils.LangUtils;
import org.onetwo.dbm.core.spi.DbmSessionFactory;
import org.onetwo.dbm.exception.DbmException;
import org.onetwo.dbm.mapping.DbmMappedEntry;

import com.google.common.collect.Maps;

public class DefaultWhereCauseBuilder implements WhereCauseBuilder {
	public static final String[] EXCLUDE_PROPERTIES = new String[] { "page", "pageNo", "pageSize", "pagination", "autoCount" };
	
	final protected QueryBuilderImpl queryBuilder;
	protected Map params;
	private DefaultWhereCauseBuilder parent;
	private KeyObject keyObject;
	private WhereCauseBuilderField lastField;
	
	public DefaultWhereCauseBuilder(QueryBuilderImpl queryBuilder) {
		super();
		this.queryBuilder = queryBuilder;
		this.params = queryBuilder.getParams();
	}
	
	private DefaultWhereCauseBuilder(DefaultWhereCauseBuilder parent, KeyObject keyObject) {
		super();
		this.parent = parent;
		this.queryBuilder = parent.queryBuilder;
		this.params = Maps.newLinkedHashMap();
		this.keyObject = keyObject;
	}
	
	Map getParams() {
		return params;
	}
	
	public WhereCauseBuilder getParent() {
		return parent;
	}

	@Override
	public DefaultWhereCauseBuilder addField(WhereCauseBuilderField field){
		this.params.put(field.getOPFields(), field.getValues());
		return self();
	}

	@Override
	public DefaultWhereCauseBuilder addFields(Object entity){
		return addFields(entity, true);
	}

	@Override
	public DefaultWhereCauseBuilder operatorFields(String[] operatorFields, Object[] values) {
		this.params.put(operatorFields, values);
		return this;
	}
	
	public DefaultWhereCauseBuilder addFields(Object entity, boolean useLikeIfStringVlue){
		if (entity==null) {
			return self();
		}
		
		DbmSessionFactory sf = queryBuilder.getBaseEntityManager().getSessionFactory();
		DbmMappedEntry entry = sf.getMappedEntryManager().findEntry(entity);
		
		// 排除分页参数
		Map fieldMap = null;
		if (entry!=null) {
			fieldMap = ReflectUtils.toMap(entity, (p, v)->{
				return v!=null && entry.contains(p.getName());
			}, EXCLUDE_PROPERTIES);
		} else {
			fieldMap = ReflectUtils.toMap(entity, (p, v)->{
				return v!=null;
			}, EXCLUDE_PROPERTIES);
		}
		
		fieldMap.entrySet().forEach(e->{
			if (String.class.isInstance(e.getValue())) {
				if(useLikeIfStringVlue){
					field(e.getKey()).like(e.getValue().toString());
				}else{
					field(e.getKey()).equalTo(e.getValue());
				}
			} else {
				field(e.getKey()).equalTo(e.getValue());
			}
		});
		return self();
	}

	protected DefaultWhereCauseBuilder self(){
		return (DefaultWhereCauseBuilder)this;
	}
	
	@Override
	public DefaultWhereCauseBuilder debug(){
		this.params.put(K.DEBUG, true);
		return self();
	}
	
	@Override
	public DefaultWhereCauseBuilder or(QueryBuilder subQuery){
		this.checkSubQuery(subQuery);
		this.params.put(K.OR, subQuery.getParams());
		return self();
	}
	
	protected void checkSubQuery(QueryBuilder subQuery){
		if(!(subQuery instanceof SubQueryBuilder)){
			LangUtils.throwBaseException("please use "+SubQueryBuilder.class.getSimpleName()+".sub() method to create sub query .");
		}
	}
	
	@Override
	public DefaultWhereCauseBuilder and(QueryBuilder subQuery){
		this.checkSubQuery(subQuery);
		this.params.put(K.AND, subQuery.getParams());
		return self();
	}
	
	@Override
	public DefaultWhereCauseBuilder ignoreIfNull(){
		this.params.put(K.IF_NULL, K.IfNull.Ignore);
		return self();
	}
	
	@Override
	public DefaultWhereCauseBuilder disabledDataFilter() {
		this.params.put(K.DATA_FILTER, false);
		return self();
	}
	
	/***
	 * 
	 * @author weishao zeng
	 * @param predicate 返回true时,禁止使用DataQueryParamaterEnhancer
	 * @return
	 */
	@Override
	public DefaultWhereCauseBuilder disabledDataQueryParamaterEnhancer(Supplier predicate) {
		if (predicate!=null && predicate.get()) {
			disabledDataQueryParamaterEnhancer();
		}
		return self();
	}
	
	@Override
	public DefaultWhereCauseBuilder disabledDataQueryParamaterEnhancer() {
		this.params.put(DataQueryParamaterEnhancer.class, false);
		return self();
	}
	
	@Override
	public DefaultWhereCauseBuilder throwIfNull(){
		this.params.put(K.IF_NULL, K.IfNull.Throw);
		return self();
	}
	
	@Override
	public DefaultWhereCauseBuilder calmIfNull(){
		this.params.put(K.IF_NULL, K.IfNull.Calm);
		return self();
	}
	
	@Override
	public SingleFieldWhereCauseBuilderField field(String fieldName){
//		this.addLastField();
		SingleFieldWhereCauseBuilderField field = new SingleFieldWhereCauseBuilderField<>(this, fieldName);
		this.lastField = field;
		return field;
	}
	
	@Override
	public DefaultWhereCauseBuilderField field(String...fields){
		this.addLastField();
		DefaultWhereCauseBuilderField field = new DefaultWhereCauseBuilderField<>(this, fields);
		this.lastField = field;
		return field;
	}
	
	private void addLastField() {
		if (this.lastField!=null) {
			this.lastField.addField();
			this.lastField = null;
		}
	}
	
	@Override
	public WhereCauseBuilder or() {
//		if (parent!=null) {
//			endSub();
//		}
		return new DefaultWhereCauseBuilder<>(this, (KeyObject)K.OR);
	}
	
	@Override
	public WhereCauseBuilder and() {
		return new DefaultWhereCauseBuilder<>(this, (KeyObject)K.AND);
	}

	@Override
	public DefaultWhereCauseBuilderField field(SingularAttribute... fields) {
		return new DefaultWhereCauseBuilderField<>(this, fields);
	}

	public DefaultWhereCauseBuilder endSub(){
//		throw new DbmException("sub query not found!");
		if (parent==null) {
			throw new DbmException("sub query not found!");
		}
		if (!params.isEmpty()) {
			parent.getParams().put(keyObject, params);
		}
		return parent;
	}

	@Override
	public QueryBuilder end(){
		addLastField();
		if (parent!=null) {
			endSub();
		}
		return queryBuilder;
	}

	@Override
	public QueryAction toQuery(){
		end();
		return queryBuilder.toQuery();
	}

	@Override
	public ExecuteAction toExecute() {
		return queryBuilder.toExecute();
	}

	@Override
	public QueryBuilder getQueryBuilder() {
		return queryBuilder;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy