org.ssssssss.magicapi.modules.SingleRowResultSetExtractor Maven / Gradle / Ivy
package org.ssssssss.magicapi.modules;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.JdbcUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SingleRowResultSetExtractor implements ResultSetExtractor{
private final boolean singleColumn;
private final RowMapper mapper;
private final Class requiredType;
public SingleRowResultSetExtractor(RowMapper mapper) {
this(mapper, null, false);
}
public SingleRowResultSetExtractor(Class requiredType) {
this(null, requiredType, true);
}
private SingleRowResultSetExtractor(RowMapper mapper, Class requiredType, boolean singleColumn) {
this.mapper = mapper;
this.requiredType = requiredType;
this.singleColumn = singleColumn;
}
@Override
@SuppressWarnings("unchecked")
public T extractData(ResultSet rs) throws SQLException, DataAccessException {
if(rs.next()){
if(singleColumn){
return (T) JdbcUtils.getResultSetValue(rs, 1, requiredType);
}
return mapper.mapRow(rs, 0);
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy