com.jpattern.orm.jdbctemplate.JdbcTemplateSqlPerformerStrategy Maven / Gradle / Ivy
package com.jpattern.orm.jdbctemplate;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import com.jpattern.orm.exception.OrmException;
import com.jpattern.orm.session.GeneratedKeyReader;
import com.jpattern.orm.session.PreparedStatementSetter;
import com.jpattern.orm.session.ResultSetReader;
import com.jpattern.orm.session.SqlPerformerStrategy;
/**
*
* @author Francesco Cina
*
* 02/lug/2011
*
* ISqlExecutor implementation using JdbcTemplate as backend
*/
public class JdbcTemplateSqlPerformerStrategy implements SqlPerformerStrategy {
private final JdbcTemplate jdbcTemplate;
public JdbcTemplateSqlPerformerStrategy(final JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void execute(final String sql, final int timeout) throws OrmException {
final int oldTimeout = this.jdbcTemplate.getQueryTimeout();
try {
this.jdbcTemplate.setQueryTimeout(timeout);
this.jdbcTemplate.execute(sql);
} catch (final Exception e) {
throw JdbcTemplateExceptionTranslator.doTranslate(e);
} finally {
this.jdbcTemplate.setQueryTimeout(oldTimeout);
}
}
@Override
public T query(final String sql, final int timeout, final int maxRows, final PreparedStatementSetter pss, final ResultSetReader rse) throws OrmException {
final int oldMaxRows = this.jdbcTemplate.getMaxRows();
final int oldTimeout = this.jdbcTemplate.getQueryTimeout();
try {
this.jdbcTemplate.setMaxRows(maxRows);
this.jdbcTemplate.setQueryTimeout(timeout);
return this.jdbcTemplate.query(sql, new org.springframework.jdbc.core.PreparedStatementSetter() {
@Override
public void setValues(final PreparedStatement ps) throws SQLException {
pss.set(ps);
}
}, new ResultSetReaderWrapper(rse) );
} catch (final Exception e) {
throw JdbcTemplateExceptionTranslator.doTranslate(e);
} finally {
this.jdbcTemplate.setMaxRows(oldMaxRows);
this.jdbcTemplate.setQueryTimeout(oldTimeout);
}
}
@Override
public int update(final String sql, final int timeout, final PreparedStatementSetter pss) throws OrmException {
final int oldTimeout = this.jdbcTemplate.getQueryTimeout();
try {
this.jdbcTemplate.setQueryTimeout(timeout);
return this.jdbcTemplate.update(sql, new org.springframework.jdbc.core.PreparedStatementSetter() {
@Override
public void setValues(final PreparedStatement ps) throws SQLException {
pss.set(ps);
}
});
} catch (final Exception e) {
throw JdbcTemplateExceptionTranslator.doTranslate(e);
} finally {
this.jdbcTemplate.setQueryTimeout(oldTimeout);
}
}
@Override
public int update(final String sql, final int timeout, final GeneratedKeyReader generatedKeyReader, final PreparedStatementSetter pss) throws OrmException {
final int oldTimeout = this.jdbcTemplate.getQueryTimeout();
try {
final org.springframework.jdbc.core.PreparedStatementCreator psc = new org.springframework.jdbc.core.PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(final Connection con) throws SQLException {
PreparedStatement ps = null;
ps = con.prepareStatement( sql , generatedKeyReader.generatedColumnNames());
pss.set(ps);
return ps;
}
};
final KeyHolder keyHolder = new GeneratedKeyHolder();
this.jdbcTemplate.setQueryTimeout(timeout);
final int result = this.jdbcTemplate.update(psc, keyHolder);
generatedKeyReader.read(new GeneratorKeyResultSet(keyHolder));
return result;
} catch (final Exception e) {
throw JdbcTemplateExceptionTranslator.doTranslate(e);
} finally {
this.jdbcTemplate.setQueryTimeout(oldTimeout);
}
}
@Override
public int[] batchUpdate(final List sqls, final int timeout) throws OrmException {
final int oldTimeout = this.jdbcTemplate.getQueryTimeout();
try {
this.jdbcTemplate.setQueryTimeout(timeout);
return this.jdbcTemplate.batchUpdate(sqls.toArray(new String[0]));
} catch (final Exception e) {
throw JdbcTemplateExceptionTranslator.doTranslate(e);
} finally {
this.jdbcTemplate.setQueryTimeout(oldTimeout);
}
}
@Override
public int[] batchUpdate(final String sql, final List