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

com.jpattern.orm.query.OrmDeleteQuery Maven / Gradle / Ivy

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

import java.util.ArrayList;
import java.util.List;

import com.jpattern.orm.classtool.IOrmClassToolMap;
import com.jpattern.orm.query.clause.OrmWhere;
import com.jpattern.orm.query.clause.Where;
import com.jpattern.orm.session.SessionSqlPerformer;
import com.jpattern.orm.session.SqlPerformer;

/**
 * 
 * @author Francesco Cina
 *
 * 10/lug/2011
 */
public class OrmDeleteQuery implements DeleteQuery, INameSolverConsumer {

	/**
	 * 
	 */

	private final Where whereExpression = new OrmWhere(this);
	private final IOrmClassToolMap ormClassToolMap;
	private final SessionSqlPerformer session;
	//	private INameSolver nameSolver = new NullNameSolver();
	private int queryTimeout = 0;
	private final Class clazz;

	public OrmDeleteQuery(Class clazz, IOrmClassToolMap ormClassToolMap, SessionSqlPerformer session) {
		this.clazz = clazz;
		this.ormClassToolMap = ormClassToolMap;
		this.session = session;
	}

	@Override
	public Where where() {
		return whereExpression;
	}

	@Override
	public int perform() {
		final List values = new ArrayList();
		appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.update(renderSql(), values.toArray());
	}

	@Override
	public void appendValues(List values) {
		whereExpression.appendValues(values);
	}

	@Override
	public DeleteQuery setQueryTimeout(int queryTimeout) {
		this.queryTimeout = queryTimeout;
		return this;
	}

	@Override
	public int getQueryTimeout() {
		return queryTimeout;
	}

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

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

	@Override
	public void renderSql(StringBuilder StringBuilder) {
		StringBuilder.append("DELETE FROM ");
		StringBuilder.append(ormClassToolMap.getOrmClassTool(clazz).getClassMapper().getTableMap().getTableNameWithSchema() );
		StringBuilder.append(" ");
		where().renderSql(StringBuilder);
	}

}