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

com.jpattern.orm.query.clause.OrmWhere Maven / Gradle / Ivy

There is a newer version: 3.5.1
Show newest version
package com.jpattern.orm.query.clause;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.jpattern.orm.query.BaseFindQuery;
import com.jpattern.orm.query.INameSolver;
import com.jpattern.orm.query.IQueryRoot;
import com.jpattern.orm.query.NullNameSolver;
import com.jpattern.orm.query.where.AndExpressionElement;
import com.jpattern.orm.query.where.EqExpressionElement;
import com.jpattern.orm.query.where.EqPropertiesExpressionElement;
import com.jpattern.orm.query.where.ExpressionElement;
import com.jpattern.orm.query.where.GeExpressionElement;
import com.jpattern.orm.query.where.GePropertiesExpressionElement;
import com.jpattern.orm.query.where.GtExpressionElement;
import com.jpattern.orm.query.where.GtPropertiesExpressionElement;
import com.jpattern.orm.query.where.IEqExpressionElement;
import com.jpattern.orm.query.where.IEqPropertiesExpressionElement;
import com.jpattern.orm.query.where.ILikeExpressionElement;
import com.jpattern.orm.query.where.InExpressionElement;
import com.jpattern.orm.query.where.InSubQueryExpressionElement;
import com.jpattern.orm.query.where.IsNotNullExpressionElement;
import com.jpattern.orm.query.where.IsNullExpressionElement;
import com.jpattern.orm.query.where.LeExpressionElement;
import com.jpattern.orm.query.where.LePropertiesExpressionElement;
import com.jpattern.orm.query.where.LikeExpressionElement;
import com.jpattern.orm.query.where.LtExpressionElement;
import com.jpattern.orm.query.where.LtPropertiesExpressionElement;
import com.jpattern.orm.query.where.NInExpressionElement;
import com.jpattern.orm.query.where.NLikeExpressionElement;
import com.jpattern.orm.query.where.NeExpressionElement;
import com.jpattern.orm.query.where.NePropertiesExpressionElement;
import com.jpattern.orm.query.where.NotExpressionElement;
import com.jpattern.orm.query.where.OrExpressionElement;

/**
 * 
 * @author Francesco Cina
 *
 * 19/giu/2011
 */
public class OrmWhere implements Where {

	List elementList = new ArrayList();
	private INameSolver nameSolver = new NullNameSolver();
	private final T query;

	public OrmWhere(T query) {
		this.query = query;
	}

	@Override
	public Where allEq(Map propertyMap) {
		for (final Entry entry : propertyMap.entrySet()) {
			eq(entry.getKey(), entry.getValue());
		}
		return this;
	}

	@Override
	public Where eq(String property, Object value) {
		final ExpressionElement expressionElement = new EqExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where eqProperties(String firstProperty, String secondProperty) {
		final ExpressionElement expressionElement = new EqPropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where le(String property, Object value) {
		final ExpressionElement expressionElement = new LeExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where leProperties(String firstProperty, String secondProperty) {
		final ExpressionElement expressionElement = new LePropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where ge(String property, Object value) {
		final ExpressionElement expressionElement = new GeExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where geProperties(String firstProperty, String secondProperty) {
		final ExpressionElement expressionElement = new GePropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where lt(String property, Object value) {
		final ExpressionElement expressionElement = new LtExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where ltProperties(String firstProperty, String secondProperty) {
		final ExpressionElement expressionElement = new LtPropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where gt(String property, Object value) {
		final ExpressionElement expressionElement = new GtExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where gtProperties(String firstProperty, String secondProperty) {
		final ExpressionElement expressionElement = new GtPropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where ieq(String property, String value) {
		final ExpressionElement expressionElement = new IEqExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where ieqProperties(String firstProperty, String secondProperty) {
		final ExpressionElement expressionElement = new IEqPropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where ilike(String property, String value) {
		final ExpressionElement expressionElement = new ILikeExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where in(String property, Collection values) {
		final ExpressionElement expressionElement = new InExpressionElement(property, values);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where in(String property, Object[] values) {
		return in(property, Arrays.asList( values ));
	}

	@Override
	public Where in(String property, BaseFindQuery subQuery) {
		final ExpressionElement expressionElement = new InSubQueryExpressionElement(property, subQuery, true);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where nin(String property, Collection values) {
		final ExpressionElement expressionElement = new NInExpressionElement(property, values);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where nin(String property, Object[] values) {
		return nin(property, Arrays.asList( values ));
	}

	@Override
	public Where nin(String property, BaseFindQuery subQuery) {
		final ExpressionElement expressionElement = new InSubQueryExpressionElement(property, subQuery, false);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where isNotNull(String property) {
		final ExpressionElement expressionElement = new IsNotNullExpressionElement(property);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where isNull(String property) {
		final ExpressionElement expressionElement = new IsNullExpressionElement(property);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where like(String property, String value) {
		final ExpressionElement expressionElement = new LikeExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where nlike(String property, String value) {
		final ExpressionElement expressionElement = new NLikeExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where ne(String property, Object value) {
		final ExpressionElement expressionElement = new NeExpressionElement(property, value);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where neProperties(String firstProperty, String secondProperty) {
		final ExpressionElement expressionElement = new NePropertiesExpressionElement(firstProperty, secondProperty);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where not(ExpressionElement expression) {
		final ExpressionElement expressionElement = new NotExpressionElement(expression);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where or(ExpressionElement expressionOne, ExpressionElement expressionTwo) {
		final ExpressionElement expressionElement = new OrExpressionElement(expressionOne, expressionTwo);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public Where and(ExpressionElement expressionOne, ExpressionElement expressionTwo) {
		final ExpressionElement expressionElement = new AndExpressionElement(expressionOne, expressionTwo);
		expressionElement.setNameSolver(nameSolver);
		elementList.add(expressionElement);
		return this;
	}

	@Override
	public void setNameSolver(INameSolver nameSolver) {
		this.nameSolver = nameSolver;
	}

	@Override
	public String renderSql() {
		final StringBuilder StringBuilder = new StringBuilder();
		renderSql(StringBuilder);
		return StringBuilder.toString();
	}

	@Override
	public void renderSql(StringBuilder StringBuilder) {
		boolean first = true;
		if (!elementList.isEmpty()) {
			StringBuilder.append("WHERE ");
			for (final ExpressionElement expressionElement : elementList) {
				if (!first) {
					StringBuilder.append("AND ");
				}
				expressionElement.renderSql(StringBuilder);
				first = false;
			}
		}
	}

	@Override
	public void appendValues(List values) {
		if (!elementList.isEmpty()) {
			for (final ExpressionElement expressionElement : elementList) {
				expressionElement.appendValues(values);
			}
		}
	}

	@Override
	public T query() {
		return query;
	}

}