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

com.jpattern.orm.query.find.FindQueryOrm Maven / Gradle / Ivy

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

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

import com.jpattern.orm.exception.OrmException;
import com.jpattern.orm.exception.OrmNotUniqueResultException;
import com.jpattern.orm.mapper.IOrmClassTool;
import com.jpattern.orm.mapper.IOrmClassToolMap;
import com.jpattern.orm.persistor.type.TypeFactory;
import com.jpattern.orm.persistor.type.ext.WrapperTypeList;
import com.jpattern.orm.query.NameSolver;
import com.jpattern.orm.query.NameSolverConsumer;
import com.jpattern.orm.query.LockMode;
import com.jpattern.orm.query.NullNameSolver;
import com.jpattern.orm.query.OrmRowMapper;
import com.jpattern.orm.query.SmartRenderableSqlQuery;
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 FindQueryOrm extends SmartRenderableSqlQuery implements FindQuery, NameSolverConsumer {

	private NameSolver 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 FindWhereImpl where = new FindWhereImpl(this);
	private final FindOrderByImpl orderBy = new FindOrderByImpl(this);
	private final FindFromImpl from;
	private final Integer nameSolverClassId;
	private final TypeFactory typeFactory;
	private int versionStatus = 0;

	public FindQueryOrm(final IOrmClassToolMap ormClassToolMap, final SessionSqlPerformer session, final Class clazz, final Integer nameSolverClassId, final TypeFactory typeFactory) {
		this.ormClassToolMap = ormClassToolMap;
		this.session = session;
		this.clazz = clazz;
		this.nameSolverClassId = nameSolverClassId;
		this.typeFactory = typeFactory;
		this.from = new FindFromImpl(this, ormClassToolMap, clazz, nameSolverClassId);
	}

	@Override
	public final FindWhere where() throws OrmException {
		return this.where;
	}

	@Override
	public final FindOrderBy orderBy() throws OrmException {
		return this.orderBy;
	}

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

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

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

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

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

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

		final ResultSetReader> resultSetReader = new ResultSetReader>() {

			@Override
			public List read(final ResultSet resultSet) throws SQLException {
				final List resultList = new java.util.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);
	}

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

		final ResultSetReader resultSetReader = new ResultSetReader() {
			@Override
			public Object read(final 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);
	}

	@Override
	public BEAN findUnique() throws OrmNotUniqueResultException {
		final List values = new WrapperTypeList(this.typeFactory);
		this.where.appendElementValues(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(final 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);
		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 WrapperTypeList(this.typeFactory);
		this.where.appendElementValues(values);
		final SqlPerformer sqlExec = this.session.sqlPerformer();
		sqlExec.setMaxRows(this.getMaxRows());
		sqlExec.setQueryTimeout(this.getQueryTimeout());
		return sqlExec.queryForLong(this.getGeneratedRowCountSql(), values);
	}

	@Override
	public FindQuery setDistinct(final boolean distinct) {
		this.distinct = distinct;
		this.versionStatus++;
		return this;
	}

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

	@Override
	public final int getStatusVersion() {
		return this.versionStatus + this.from.getElementStatusVersion() + this.where.getElementStatusVersion() + this.orderBy.getElementStatusVersion();

	}

	@Override
	public String getGeneratedRowCountSql() {
		final StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("SELECT COUNT(*) ");
		this.from.renderSqlElement(stringBuilder);
		this.where.renderSqlElement(stringBuilder);
		return stringBuilder.toString();
	}

	@Override
	public final void doRender(final StringBuilder stringBuilder) {
		renderSelect(stringBuilder);
		this.from.renderSqlElement(stringBuilder);
		this.where.renderSqlElement(stringBuilder);
		this.orderBy.renderSqlElement(stringBuilder);
		stringBuilder.append(this.lockMode.getMode());
	}

	private void renderSelect(final StringBuilder stringBuilder) {
		final String alias = this.nameSolver.alias(this.nameSolverClassId);
		stringBuilder.append("SELECT ");
		if (this.distinct) {
			stringBuilder.append("DISTINCT ");
		}
		stringBuilder.append(this.ormClassToolMap.getOrmClassTool(this.clazz).getOrmCRUDQuery().getBaseSelectClause(alias + ".") );
		stringBuilder.append(" ");
	}

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

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

	@Override
	public LockMode getLockMode() {
		return this.lockMode;
	}

	@Override
	public FindQuery join(final Class joinClass) {
		return this.from.join(joinClass);
	}

	@Override
	public FindQuery join(final Class joinClass, final String joinClassAlias) {
		return this.from.join(joinClass, joinClassAlias);
	}

	@Override
	public FindQuery naturalJoin(final Class joinClass) {
		return this.from.naturalJoin(joinClass);
	}

	@Override
	public FindQuery naturalJoin(final Class joinClass, final String joinClassAlias) {
		return this.from.naturalJoin(joinClass, joinClassAlias);
	}

	@Override
	public FindQuery innerJoin(final Class joinClass) {
		return this.from.innerJoin(joinClass);
	}

	@Override
	public FindQuery innerJoin(final Class joinClass, final String joinClassAlias) {
		return this.from.innerJoin(joinClass, joinClassAlias);
	}

	@Override
	public FindQuery innerJoin(final Class joinClass, final String onLeftProperty,
			final String onRigthProperty) {
		return this.from.innerJoin(joinClass, onLeftProperty, onRigthProperty);
	}

	@Override
	public FindQuery innerJoin(final Class joinClass, final String joinClassAlias,
			final String onLeftProperty, final String onRigthProperty) {
		return this.from.innerJoin(joinClass, joinClassAlias, onLeftProperty,
				onRigthProperty);
	}

	@Override
	public FindQuery leftOuterJoin(final Class joinClass) {
		return this.from.leftOuterJoin(joinClass);
	}

	@Override
	public FindQuery leftOuterJoin(final Class joinClass,
			final String joinClassAlias) {
		return this.from.leftOuterJoin(joinClass, joinClassAlias);
	}

	@Override
	public FindQuery leftOuterJoin(final Class joinClass,
			final String onLeftProperty, final String onRigthProperty) {
		return this.from.leftOuterJoin(joinClass, onLeftProperty, onRigthProperty);
	}

	@Override
	public FindQuery leftOuterJoin(final Class joinClass,
			final String joinClassAlias, final String onLeftProperty, final String onRigthProperty) {
		return this.from.leftOuterJoin(joinClass, joinClassAlias, onLeftProperty,
				onRigthProperty);
	}

	@Override
	public FindQuery rightOuterJoin(final Class joinClass) {
		return this.from.rightOuterJoin(joinClass);
	}

	@Override
	public FindQuery rightOuterJoin(final Class joinClass,
			final String joinClassAlias) {
		return this.from.rightOuterJoin(joinClass, joinClassAlias);
	}

	@Override
	public FindQuery rightOuterJoin(final Class joinClass,
			final String onLeftProperty, final String onRigthProperty) {
		return this.from.rightOuterJoin(joinClass, onLeftProperty, onRigthProperty);
	}

	@Override
	public FindQuery rightOuterJoin(final Class joinClass,
			final String joinClassAlias, final String onLeftProperty, final String onRigthProperty) {
		return this.from.rightOuterJoin(joinClass, joinClassAlias, onLeftProperty,
				onRigthProperty);
	}

	@Override
	public FindQuery fullOuterJoin(final Class joinClass) {
		return this.from.fullOuterJoin(joinClass);
	}

	@Override
	public FindQuery fullOuterJoin(final Class joinClass,
			final String joinClassAlias) {
		return this.from.fullOuterJoin(joinClass, joinClassAlias);
	}

	@Override
	public FindQuery fullOuterJoin(final Class joinClass,
			final String onLeftProperty, final String onRigthProperty) {
		return this.from.fullOuterJoin(joinClass, onLeftProperty, onRigthProperty);
	}

	@Override
	public FindQuery fullOuterJoin(final Class joinClass,
			final String joinClassAlias, final String onLeftProperty, final String onRigthProperty) {
		return this.from.fullOuterJoin(joinClass, joinClassAlias, onLeftProperty,
				onRigthProperty);
	}

}