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

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

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

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jpattern.orm.classtool.IOrmClassTool;
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.OrmWhere;
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;
import com.jpattern.orm.util.GenericWrapper;

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

	private INameSolver nameSolver = new NullNameSolver();
	private final IOrmClassToolMap ormClassToolMap;
	private final Class clazz;
	private boolean distinct;
	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 OrmFindQuery(IOrmClassToolMap ormClassToolMap, SessionSqlPerformer session, Class clazz) {
		this.ormClassToolMap = ormClassToolMap;
		this.session = session;
		this.clazz = clazz;
		from = new OrmFrom>(ormClassToolMap, 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 int getMaxRows() throws OrmException {
		return this.maxRows;
	}

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

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

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

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

	@Override
	public List findList() {
		final List values = new ArrayList();
		where().appendValues(values);
		final IOrmClassTool ormClassTool = this.ormClassToolMap.getOrmClassTool(this.clazz);

		final ResultSetReader> resultSetReader = new ResultSetReader>() {

			@Override
			public List read(ResultSet resultSet) throws SQLException {
				final List resultList = new ArrayList();
				int rowCount = 0;
				while ( resultSet.next() ) {
					resultList.add( ormClassTool.getOrmPersistor().mapRow("", resultSet, rowCount++) );
				}
				return resultList;
			}
		};
		final SqlPerformer sqlExec = this.session.sqlPerformer();
		sqlExec.setMaxRows(this.getMaxRows());
		sqlExec.setQueryTimeout(this.getQueryTimeout());
		return sqlExec.query(this.renderSql(), resultSetReader, values.toArray());
	}

	@Override
	public void find(final OrmRowMapper srr) throws OrmException {
		final List values = new ArrayList();
		where().appendValues(values);
		final IOrmClassTool ormClassTool = this.ormClassToolMap.getOrmClassTool(this.clazz);

		final ResultSetReader resultSetReader = new ResultSetReader() {
			@Override
			public Object read(ResultSet resultSet) throws SQLException {
				int rowCount = 0;
				while ( resultSet.next() ) {
					srr.read( ormClassTool.getOrmPersistor().mapRow("", resultSet, rowCount) , rowCount );
					rowCount++;
				}
				return null;
			}
		};
		final SqlPerformer sqlExec = this.session.sqlPerformer();
		sqlExec.setMaxRows(this.getMaxRows());
		sqlExec.setQueryTimeout(this.getQueryTimeout());
		sqlExec.query(this.renderSql(), resultSetReader, values.toArray());
	}

	@Override
	public T findUnique() throws OrmNotUniqueResultException {
		final List values = new ArrayList();
		where().appendValues(values);
		final IOrmClassTool ormClassTool = this.ormClassToolMap.getOrmClassTool(this.clazz);
		final GenericWrapper resultsCount = new GenericWrapper(null);
		final GenericWrapper wrapper = new GenericWrapper(null);
		final ResultSetReader resultSetReader = new ResultSetReader() {
			@Override
			public Object read(ResultSet resultSet) throws SQLException {
				if ( resultSet.next() ) {
					resultsCount.setValue(1);
					wrapper.setValue( ormClassTool.getOrmPersistor().mapRow("", resultSet, 0) );
					if ( resultSet.next() ) {
						resultsCount.setValue(2);
					}
				} else {
					resultsCount.setValue(0);
				}
				return null;
			}
		};
		final SqlPerformer sqlExec = this.session.sqlPerformer();
		sqlExec.setMaxRows(2);
		sqlExec.setQueryTimeout(this.getQueryTimeout());
		sqlExec.query(this.renderSql(), resultSetReader, values.toArray());
		if ( resultsCount.getValue() != 1) {
			if ( resultsCount.getValue() == 0 ) {
				throw new OrmNotUniqueResultException("The query execution returned a number of rows different than one: no results found");
			}
			throw new OrmNotUniqueResultException("The query execution returned a number of rows different than one: more than one result found");
		}
		return wrapper.getValue();
	}

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

	@Override
	public String getGeneratedRowCountSql() {
		final StringBuilder stringBuilder = new StringBuilder();
		this.renderSelectRowCount(stringBuilder);
		this.renderFrom(stringBuilder);
		this.renderWhere(stringBuilder);
		return stringBuilder.toString();
	}

	@Override
	public FindQuery setDistinct() {
		this.distinct = true;
		return this;
	}

	private void renderSelectRowCount(StringBuilder stringBuilder) {
		stringBuilder.append("SELECT COUNT(*) ");
	}

	@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) {
		final String alias = this.nameSolver.alias(this.clazz);
		stringBuilder.append("SELECT ");
		if (this.distinct) {
			stringBuilder.append("DISTINCT ");
		}
		stringBuilder.append(this.ormClassToolMap.getOrmClassTool(this.clazz).getOrmCRUDQuery().getBaseSelectClause(alias + ".") );
		stringBuilder.append(" ");
	}

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

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

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

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

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

	@Override
	public boolean isDistinct() throws OrmException {
		return this.distinct;
	}

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

}