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

com.jpattern.orm.query.clause.OrmSetClause 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.List;

import com.jpattern.orm.query.INameSolver;
import com.jpattern.orm.query.IQueryRoot;
import com.jpattern.orm.query.NullNameSolver;
import com.jpattern.orm.query.where.EqExpressionElement;
import com.jpattern.orm.query.where.ExpressionElement;

/**
 * 
 * @author Francesco Cina
 *
 * 10/lug/2011
 */
public class OrmSetClause implements SetClause {

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

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

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

	@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("SET ");
			for (final ExpressionElement expressionElement : elementList) {
				if (!first) {
					StringBuilder.append(", ");
				}
				expressionElement.renderSql(StringBuilder);
				first = false;
			}
		}
	}

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

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

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