com.jn.sqlhelper.mango.CustomMangoJdbcTemplate Maven / Gradle / Ivy
package com.jn.sqlhelper.mango;
import com.jn.sqlhelper.dialect.pagination.RowSelection;
import org.jfaster.mango.binding.BoundSql;
import org.jfaster.mango.jdbc.JdbcTemplate;
import org.jfaster.mango.jdbc.ResultSetExtractor;
import org.jfaster.mango.jdbc.exception.DataAccessException;
import org.jfaster.mango.transaction.DataSourceUtils;
import org.jfaster.mango.type.TypeHandler;
import org.jfaster.mango.util.logging.InternalLogger;
import org.jfaster.mango.util.logging.InternalLoggerFactory;
import javax.sql.DataSource;
import java.sql.*;
import java.util.List;
public class CustomMangoJdbcTemplate extends JdbcTemplate {
private final static InternalLogger logger = InternalLoggerFactory.getInstance(JdbcTemplate.class);
protected T executeQuery(DataSource dataSource, BoundSql boundSql, ResultSetExtractor rse)
throws DataAccessException {
Connection conn = DataSourceUtils.getConnection(dataSource);
PreparedStatement ps = null;
ResultSet rs = null;
String sql = boundSql.getSql();
try {
ps = conn.prepareStatement(sql);
setValues(ps, boundSql);
if (logger.isDebugEnabled()) {
logger.debug("Executing \"{}\" {}", sql, boundSql.getArgs());
}
rs = ps.executeQuery();
return rse.extractData(rs);
} catch (SQLException e) {
closeResultSet(rs);
rs = null;
closeStatement(ps);
ps = null;
DataSourceUtils.releaseConnection(conn, dataSource);
conn = null;
throw new DataAccessException("Execute sql [" + sql + "] error.", e) {
};
} finally {
closeResultSet(rs);
closeStatement(ps);
DataSourceUtils.releaseConnection(conn, dataSource);
}
}
private void setValues(PreparedStatement ps, BoundSql boundSql) throws SQLException {
RowSelection rowSelection = MangoPagingContext.pagingRequest.get();
if (rowSelection != null) {
MangoPagingContext.pagingRequest.remove();
MangoQueryParameters queryParameters = new MangoQueryParameters();
queryParameters.setRowSelection(rowSelection);
queryParameters.setParameters(boundSql);
MangoPagingContext.instrumentor.bindParameters(ps, new MangoPrepareStatementSetter(), queryParameters, true);
} else {
setValues0(ps, boundSql);
}
}
private void setValues0(PreparedStatement ps, BoundSql boundSql) throws SQLException {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy