com.jpattern.orm.session.PlainSqlPerformer Maven / Gradle / Ivy
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
© 2015 - 2024 Weber Informatics LLC | Privacy Policy