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

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

The newest version!
package org.onetwo.common.db.builder;

import java.util.Collection;
import java.util.Date;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.ArrayUtils;
import org.onetwo.common.db.builder.SingleFieldWhereCauseBuilderField.SingleFieldWhereCauseBuilderFieldEnder;
import org.onetwo.common.db.sqlext.ExtQueryUtils;
import org.onetwo.common.db.sqlext.QueryDSLOps;
import org.onetwo.common.utils.CUtils;
import org.onetwo.common.utils.StringUtils;

@SuppressWarnings("unchecked")
public class SingleFieldWhereCauseBuilderField extends WhereCauseBuilderField> {
	
	private String field;
	private QueryDSLOps[] ops;
	private Object values;
	private SingleFieldWhereCauseBuilderFieldEnder ender;
	

	public SingleFieldWhereCauseBuilderField(WhereCauseBuilder squery, String field) {
		super(squery);
		this.field = field;
		this.ender = new SingleFieldWhereCauseBuilderFieldEnder<>(this);
	}

	public SingleFieldWhereCauseBuilderField when(Supplier predicate) {
		this.whenPredicate = predicate;
		return this;
	}
	
	/***
	 *  like '%value'
	 * @author weishao zeng
	 * @param values
	 * @return
	 */
	public SingleFieldWhereCauseBuilderFieldEnder prelike(String... values) {
		Object[] strings = Stream.of(values)
							.map(val -> StringUtils.appendStartWith(val, "%"))
							.collect(Collectors.toList())
							.toArray();
		setOpValues(QueryDSLOps.LIKE, strings);
		return ender();
//		this.op = FieldOP.like;
//		this.values = Stream.of(values)
//							.map(val -> StringUtils.appendStartWith(val, "%"))
//							.collect(Collectors.toList())
//							.toArray(new String[0]);
//		this.queryBuilder.addField(this);
//		return queryBuilder;
	}

	/***
	 *  like 'value%'
	 * @author weishao zeng
	 * @param values
	 * @return
	 */
	public SingleFieldWhereCauseBuilderFieldEnder postlike(String... values) {
		Object[] strings = Stream.of(values)
				.map(val -> StringUtils.appendEndWith(val, "%"))
				.collect(Collectors.toList())
				.toArray();
		setOpValues(QueryDSLOps.LIKE, strings);
		return ender();
	}

	public SingleFieldWhereCauseBuilderFieldEnder notLike(String... values) {
		setOpValues(QueryDSLOps.NOT_LIKE, (Object[])values);
		return ender();
	}
	
	public SingleFieldWhereCauseBuilderFieldEnder like(String... values) {
		setOpValues(QueryDSLOps.LIKE, (Object[])values);
		return ender();
	}
	
	/***
	 * 等于
	 * @param values
	 * @return
	 */
	public  SingleFieldWhereCauseBuilderFieldEnder equalTo(T... values) {
		setOpValues(QueryDSLOps.EQ, values);
		return ender();
	}

	public SingleFieldWhereCauseBuilderFieldEnder value(QueryDSLOps sqlOp, Supplier valueSupplier) {
		setOpValues(sqlOp, valueSupplier.get());
		return ender();
	}


	public WhereCauseBuilder end() {
		this.addField();
		return queryBuilder;
	}
	
	public  SingleFieldWhereCauseBuilderFieldEnder is(T... values) {
		return equalTo(values);
	}
	
	public SingleFieldWhereCauseBuilderFieldEnder isNull(boolean isNull) {
//		return this.doWhenPredicate(()->{
//			this.op = QueryDSLOps.IS_NULL;
//			this.values = new Object[]{isNull};
//		});
//		this.setOpValue(QueryDSLOps.IS_NULL, isNull);
		this.setOpValues(QueryDSLOps.IS_NULL, isNull);
		return ender();
	}
	

	/****
	 * 不等于
	 * @param values
	 * @return
	 */
	public  SingleFieldWhereCauseBuilderFieldEnder notEqualTo(T... values) {
		this.setOpValues(QueryDSLOps.NEQ, values);
		return ender();
	}

	/****
	 * 大于
	 * @param values
	 * @return
	 */
	public  SingleFieldWhereCauseBuilderFieldEnder greaterThan(T... values) {
		this.setOpValues(QueryDSLOps.GT, values);
		return ender();
	}
	
	public  SingleFieldWhereCauseBuilderFieldEnder in(T... values) {
//		this.setOpValues(QueryDSLOps.IN, values);
		this.setOpValues(QueryDSLOps.IN, new Object[] { values });
		return ender();
	}
	
	public  SingleFieldWhereCauseBuilderFieldEnder in(Collection values) {
//		this.setOpValues(QueryDSLOps.IN, values.toArray());
		this.setOpValues(QueryDSLOps.IN, new Object[] { values });
		return ender();
	}
	
	public  SingleFieldWhereCauseBuilderFieldEnder notIn(T... values) {
//		this.setOpValues(QueryDSLOps.NOT_IN, values);
		this.setOpValues(QueryDSLOps.NOT_IN, new Object[] { values });
		return ender();
	}
	
	/***
	 * 如果只有第一个参数,则条件为:>=start这天的零点,=start,  dateIn(Date start, Date end) {
		Object[] values = null;
		if (end==null) {
			values = new Date[]{start};
		} else {
			values = new Date[]{start, end};
		}
		setOpValues(QueryDSLOps.DATE_IN, new Object[] { values });
		return ender();
	}

	/****
	 * 解释为sql的between start and end
	 * 是否包含边界值需要根据数据库来确定
	 * mysql 和 oracle均包含边界值
	 * @author weishao zeng
	 * @param start
	 * @param end
	 * @return
	 */
	public SingleFieldWhereCauseBuilderFieldEnder between(final Object start, final Object end) {
		Object[] range = new Object[]{start, end};
		setOpValues(QueryDSLOps.BETWEEN, new Object[] { range });
		return ender();
	}
	
	/****
	 * 大于或者等于
	 * @param values
	 * @return
	 */
	public  SingleFieldWhereCauseBuilderFieldEnder greaterEqual(T... values) {
		this.setOpValues(QueryDSLOps.GE, values);
		return ender();
	}

	/****
	 * 少于
	 * @param values
	 * @return
	 */
	public  SingleFieldWhereCauseBuilderFieldEnder lessThan(T... values) {
		this.setOpValues(QueryDSLOps.LT, values);
		return ender();
	}

	/****
	 * 少于或等于
	 * @param values
	 * @return
	 */
	public  SingleFieldWhereCauseBuilderFieldEnder lessEqual(T... values) {
		this.setOpValues(QueryDSLOps.LE, values);
		return ender();
	}
	
	public SingleFieldWhereCauseBuilderFieldEnder isNull(){
		this.isNull(true);
		return ender();
	}
	
	protected SingleFieldWhereCauseBuilderFieldEnder ender() {
		return ender;
	}
	
	public SingleFieldWhereCauseBuilderFieldEnder isNotNull(){
		this.isNull(false);
		return ender();
	}
	
	@Override
	public String[] getOPFields(){
		String[] opFields = ExtQueryUtils.appendOperationToFields(field, this.ops);
		return opFields;
	}

	@Override
	public Object getValues() {
		return values;
	}

	protected void setOpValue(QueryDSLOps op, Object values) {
		this.setOp(op);
		this.values = new Object[] { values };
	}
	
	protected void setOpValues(QueryDSLOps op, Object... values) {
		QueryDSLOps[] tempOps = null;
		Object[] tempValues = null;
		if (values==null || values.length==0) {
			tempOps = new QueryDSLOps[] { op };
			tempValues = new Object[] { null };
		} else {
			tempOps = new QueryDSLOps[values.length];
			int index = 0;
			for (int i = 0; i < values.length; i++) {
				tempOps[index] = op;
				index++;
			}
			tempValues = values;
		}
		
		this.ops = ArrayUtils.addAll(this.ops, tempOps);
		this.values = CUtils.addAll((Object[])this.values, tempValues);
	}
	
	public void setOp(QueryDSLOps op) {
		this.ops = new QueryDSLOps[] { op };
	}

	static public class SingleFieldWhereCauseBuilderFieldEnder {
		SingleFieldWhereCauseBuilderField field;
		public SingleFieldWhereCauseBuilderFieldEnder(SingleFieldWhereCauseBuilderField field) {
			super();
			this.field = field;
		}
		
		public SingleFieldWhereCauseBuilderField field(String fieldName) {
			this.field.addField();
			return this.field.queryBuilder.field(fieldName);
		}

		public DefaultWhereCauseBuilderField field(String... fieldName) {
			this.field.addField();
			return this.field.queryBuilder.field(fieldName);
		}
		
		public SingleFieldWhereCauseBuilderField or() {
			return this.field;
		}

		public WhereCauseBuilder orQuery() {
			this.field.addField();
			return this.field.queryBuilder.or();
		}

		public WhereCauseBuilder andQuery() {
			this.field.addField();
			return this.field.queryBuilder.and();
		}

		public WhereCauseBuilder ignoreIfNull(){
			WhereCauseBuilder wb = endSub();
			wb.ignoreIfNull();
			return wb;
		}


		public QueryAction toQuery(){
			return end().toQuery();
		}
		
		public QueryAction toSelect() {
			return end().toSelect();
		}
		
		public ExecuteAction toExecute() {
			WhereCauseBuilder wb = endSub();
			return wb.toExecute();
		}
		
		public QueryBuilder end() {
			this.field.addField();
			return this.field.queryBuilder.getQueryBuilder();
		}
		
		public WhereCauseBuilder endSub() {
			this.field.addField();
			WhereCauseBuilder q = this.field.queryBuilder;
			if (q.getParent()!=null) {
				return q.endSub();
			}
			return q;
		}
	}

}