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

com.jpattern.orm.session.PlainSqlPerformer Maven / Gradle / Ivy

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

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

import com.jpattern.logger.ILogger;
import com.jpattern.orm.exception.OrmException;
import com.jpattern.orm.exception.OrmNotUniqueResultException;
import com.jpattern.orm.logger.OrmLogger;

/**
 * 
 * @author Francesco Cina
 *
 * 02/lug/2011
 */
public class PlainSqlPerformer implements SqlPerformer {

	private final ILogger logger = OrmLogger.getOrmLogger(this.getClass());
	private final SqlPerformerStrategy sqlPerformerTemplate;
	private int queryTimeout = 0;
	private int maxRows = 0;

	public PlainSqlPerformer(SqlPerformerStrategy sqlPerformerTemplate) {
		this.sqlPerformerTemplate = sqlPerformerTemplate;
	}

	@Override
	public final void setMaxRows(int maxRows) {
		this.logger.debug("setMaxRows", "max rows sets to " + maxRows);
		this.maxRows = maxRows;
	}

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

	@Override
	public final void setQueryTimeout(int queryTimeout) {
		this.logger.debug("setQueryTimeout", "queryTimeout sets to " + queryTimeout);
		this.queryTimeout = queryTimeout;
	}

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

	@Override
	public final Integer queryForInt(String sql, Object... values) throws OrmException, OrmNotUniqueResultException {
		ResultSetReader resultSetReader = new ResultSetReader() {

			@Override
			public Integer read(ResultSet resultSet) throws SQLException {
				if ( resultSet.next() ) {
					int result = resultSet.getInt(1);
					if (resultSet.next()) {
						throw new OrmNotUniqueResultException("The query execution returned a number of rows higher than 1");
					}
					return result;
				}
				throw new OrmNotUniqueResultException("The query execution has returned zero rows. One row was expected");
			}
		};
		return this.query(sql, resultSetReader, values);
	}

	@Override
	public final Long queryForLong(String sql, Object... values) throws OrmException, OrmNotUniqueResultException {
		ResultSetReader resultSetReader = new ResultSetReader() {

			@Override
			public Long read(ResultSet resultSet) throws SQLException {
				if ( resultSet.next() ) {
					long result = resultSet.getLong(1) ;
					if (resultSet.next()) {
						throw new OrmNotUniqueResultException("The query execution returned a number of rows higher than 1");
					}
					return result;
				}
				throw new OrmNotUniqueResultException("The query execution has returned zero rows. One row was expected");
			}
		};
		return this.query(sql, resultSetReader, values);
	}

	@Override
	public final Double queryForDouble(String sql, Object... values) throws OrmException, OrmNotUniqueResultException {
		ResultSetReader resultSetReader = new ResultSetReader() {

			@Override
			public Double read(ResultSet resultSet) throws SQLException {
				if ( resultSet.next() ) {
					double result = resultSet.getDouble(1) ;
					if (resultSet.next()) {
						throw new OrmNotUniqueResultException("The query execution returned a number of rows higher than 1");
					}
					return result;
				}
				throw new OrmNotUniqueResultException("The query execution has returned zero rows. One row was expected");
			}
		};
		return this.query(sql, resultSetReader, values);
	}

	@Override
	public final Float queryForFloat(String sql, Object... values) throws OrmException, OrmNotUniqueResultException {
		ResultSetReader resultSetReader = new ResultSetReader() {

			@Override
			public Float read(ResultSet resultSet) throws SQLException {
				if ( resultSet.next() ) {
					float result = resultSet.getFloat(1) ;
					if (resultSet.next()) {
						throw new OrmNotUniqueResultException("The query execution returned a number of rows higher than 1");
					}
					return result;
				}
				throw new OrmNotUniqueResultException("The query execution has returned zero rows. One row was expected");
			}
		};
		return this.query(sql, resultSetReader, values);
	}

	@Override
	public final String queryForString(String sql, Object... values) throws OrmException, OrmNotUniqueResultException {
		ResultSetReader resultSetReader = new ResultSetReader() {

			@Override
			public String read(ResultSet resultSet) throws SQLException {
				if ( resultSet.next() ) {
					String result = resultSet.getString(1) ;
					if (resultSet.next()) {
						throw new OrmNotUniqueResultException("The query execution returned a number of rows higher than 1");
					}
					return result;
				}
				throw new OrmNotUniqueResultException("The query execution has returned zero rows. One row was expected");
			}
		};
		return this.query(sql, resultSetReader, values);
	}

	@Override
	public final Boolean queryForBoolean(String sql, Object... values) throws OrmException, OrmNotUniqueResultException {
		ResultSetReader resultSetReader = new ResultSetReader() {

			@Override
			public Boolean read(ResultSet resultSet) throws SQLException {
				if ( resultSet.next() ) {
					boolean result = resultSet.getBoolean(1) ;
					if (resultSet.next()) {
						throw new OrmNotUniqueResultException("The query execution returned a number of rows higher than 1");
					}
					return result;
				}
				throw new OrmNotUniqueResultException("The query execution has returned zero rows. One row was expected");
			}
		};
		return this.query(sql, resultSetReader, values);
	}

	@Override
	public final BigDecimal queryForBigDecimal(String sql, Object... values) throws OrmException, OrmNotUniqueResultException {
		ResultSetReader resultSetReader = new ResultSetReader() {

			@Override
			public BigDecimal read(ResultSet resultSet) throws SQLException {
				if ( resultSet.next() ) {
					BigDecimal result = resultSet.getBigDecimal(1) ;
					if (resultSet.next()) {
						throw new OrmNotUniqueResultException("The query execution returned a number of rows higher than 1");
					}
					return result;
				}
				throw new OrmNotUniqueResultException("The query execution has returned zero rows. One row was expected");
			}
		};
		return this.query(sql, resultSetReader, values);
	}

	@Override
	public final Object[] queryForArray(String sql, Object... values) throws OrmException, OrmNotUniqueResultException {
		ResultSetReader resultSetReader = new ResultSetReader() {

			@Override
			public Object[] read(ResultSet resultSet) throws SQLException {
				int columnNumber = resultSet.getMetaData().getColumnCount();
				if ( resultSet.next() ) {
					Object[] result = new Object[columnNumber];
					for (int i=0 ; i queryForList(String sql, Object... values) throws OrmException {
		ResultSetReader> resultSetReader = new ResultSetReader>() {

			@Override
			public List read(ResultSet resultSet) throws SQLException {
				int columnNumber = resultSet.getMetaData().getColumnCount();
				List resultList = new ArrayList();
				while ( resultSet.next() ) {
					Object[] objectArray = new Object[columnNumber];
					for (int i=0 ; i T query(String sql, ResultSetReader rse, Object... args) throws OrmException {
		SqlSelectQuery query = new SqlSelectQuery(sql, args, this.getMaxRows(), this.getQueryTimeout());
		return this.sqlPerformerTemplate.query(query, rse);
	}

	@Override
	public int update(String sql, Object... args) throws OrmException {
		return this.sqlPerformerTemplate.update(sql, this.getQueryTimeout(), args);
	}

	@Override
	public int update(String sql, GeneratedKeyReader generatedKeyReader, Object... args) throws OrmException {
		return this.sqlPerformerTemplate.update(sql, this.getQueryTimeout(), generatedKeyReader, args);
	}

	@Override
	public int[] batchUpdate(List sqls) throws OrmException {
		return this.sqlPerformerTemplate.batchUpdate(sqls, this.getQueryTimeout());
	}

	@Override
	public int[] batchUpdate(String sql, List args) throws OrmException {
		return this.sqlPerformerTemplate.batchUpdate(sql, args, this.getQueryTimeout());
	}

	@Override
	public int[] batchUpdate(String sql, PreparedStatementCreator psc) throws OrmException {
		return this.sqlPerformerTemplate.batchUpdate(sql, psc, this.getQueryTimeout());
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy