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

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

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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.jpattern.orm.classtool.IOrmClassToolMap;
import com.jpattern.orm.exception.OrmException;
import com.jpattern.orm.exception.OrmNotUniqueResultException;
import com.jpattern.orm.query.clause.From;
import com.jpattern.orm.query.clause.OrderBy;
import com.jpattern.orm.query.clause.OrmFrom;
import com.jpattern.orm.query.clause.OrmOrderBy;
import com.jpattern.orm.query.clause.OrmSelectClause;
import com.jpattern.orm.query.clause.OrmWhere;
import com.jpattern.orm.query.clause.SelectClause;
import com.jpattern.orm.query.clause.Where;
import com.jpattern.orm.session.ResultSetReader;
import com.jpattern.orm.session.SessionSqlPerformer;
import com.jpattern.orm.session.SqlPerformer;

/**
 * 
 * @author Francesco Cina
 *
 * 20/giu/2011
 */
public class OrmCustomFindQuery extends AQuery implements CustomFindQuery, INameSolverConsumer {

	private INameSolver nameSolver = new NullNameSolver();
	private final SelectClause select;
	private final IOrmClassToolMap ormClassToolMap;
	private final Class clazz;
	//	private final Class[] joinClasses;
	private final SessionSqlPerformer session;
	private int queryTimeout = 0;
	private int maxRows = 0;
	private LockMode lockMode = LockMode.NO_LOCK;
	private final Where whereExpression = new OrmWhere(this);
	private final OrmOrderBy orderBy = new OrmOrderBy(this);
	private final From from;

	public OrmCustomFindQuery(String selectClause, IOrmClassToolMap ormClassToolMap, SessionSqlPerformer session, Class clazz) {
		this.ormClassToolMap = ormClassToolMap;
		this.session = session;
		this.clazz = clazz;
		from = new OrmFrom(ormClassToolMap,this);
		select = new OrmSelectClause(selectClause, this);
	}

	@Override
	public void setNameSolver(INameSolver nameSolver) {
		this.nameSolver = nameSolver;
		where().setNameSolver(nameSolver);
		orderBy().setNameSolver(nameSolver);
		from().setNameSolver(nameSolver);
		select.setNameSolver(nameSolver);
	}

	@Override
	public List findList() {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.queryForList(renderSql(), values.toArray());
	}


	@Override
	public Object[] findUnique() throws OrmNotUniqueResultException {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.queryForArray(renderSql(), values.toArray());
	}

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

	@Override
	public final void renderSql(StringBuilder stringBuilder) {
		renderSelect(stringBuilder);
		renderFrom(stringBuilder);
		renderWhere(stringBuilder);
		renderOrderBy(stringBuilder);
		renderLockMode(stringBuilder);
	}

	private void renderSelect(StringBuilder StringBuilder) {
		select.renderSql(StringBuilder);
	}

	private void renderFrom(StringBuilder StringBuilder) {
		final String alias = nameSolver.alias(clazz);
		StringBuilder.append("FROM ");
		StringBuilder.append(ormClassToolMap.getOrmClassTool(clazz).getClassMapper().getTableMap().getTableNameWithSchema() );
		StringBuilder.append( " " );
		StringBuilder.append(alias);
		StringBuilder.append(" ");
		from().renderSql(StringBuilder);
	}

	private void renderWhere(StringBuilder StringBuilder) {
		where().renderSql(StringBuilder);
	}

	private void renderOrderBy(StringBuilder StringBuilder) {
		orderBy().renderSql(StringBuilder);
	}

	private void renderLockMode(StringBuilder stringBuilder) {
		stringBuilder.append(lockMode.getLockMode());
	}

	@Override
	public  T find(ResultSetReader rse) throws OrmException {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.query(renderSql(), rse, values.toArray());
	}

	@Override
	public int findInt() throws OrmException {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.queryForInt(renderSql(), values.toArray());
	}

	@Override
	public long findLong() throws OrmException {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.queryForLong(renderSql(), values.toArray());
	}

	@Override
	public double findDouble() throws OrmException {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.queryForDouble(renderSql(), values.toArray());
	}

	@Override
	public float findFloat() throws OrmException {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.queryForFloat(renderSql(), values.toArray());
	}

	@Override
	public String findString() throws OrmException {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.queryForString(renderSql(), values.toArray());
	}

	@Override
	public boolean findBoolean() throws OrmException {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.queryForBoolean(renderSql(), values.toArray());
	}

	@Override
	public BigDecimal findBigDecimal() throws OrmException {
		final List values = new ArrayList();
		where().appendValues(values);
		final SqlPerformer sqlExec = session.sqlPerformer();
		sqlExec.setMaxRows(getMaxRows());
		sqlExec.setQueryTimeout(getQueryTimeout());
		return sqlExec.queryForBigDecimal(renderSql(), values.toArray());
	}

	@Override
	public final int getMaxRows() throws OrmException {
		return maxRows;
	}

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

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

	@Override
	public final CustomFindQuery setMaxRows(int maxRows) throws OrmException {
		this.maxRows = maxRows;
		return this;
	}

	@Override
	public CustomFindQuery setLockMode(LockMode lockMode) {
		this.lockMode = lockMode;
		return this;
	}

	@Override
	public final From from() throws OrmException {
		return from;
	}

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

	@Override
	public final OrderBy orderBy() throws OrmException {
		return orderBy;
	}

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