@SuppressWarnings("unchecked")
private ResultSetHandler<T> newResultSetHandler0(final ResultSetMetaData meta) throws SQLException {
final Getter[] getters;
final Setter[] setters;
final Converter converter;
final boolean useExecuteScalar;
//TODO: it's possible to cache converter/setters/getters
// cache key is ResultSetMetadata + Bean type
converter = quirks.converterOf(metadata.getType());
final int columnCount = meta.getColumnCount();
getters = new Getter[columnCount + 1]; // getters[0] is always null
for (int i = 1; i <= columnCount; i++) {
String colName = quirks.getColumnName(meta, i);
// behavior change: do not throw if POJO contains less properties
getters[i] = getGetter(quirks, colName, metadata);
// If more than 1 column is fetched (we cannot fall back to executeScalar),
// and the getter doesn't exist, throw exception.
if (getters[i] == null && columnCount > 1) {
throw new Sql2oException("Could not map " + colName + " to any property.");
}
}
setters = new Setter[columnCount + 1]; // setters[0] is always null
for (int i = 1; i <= columnCount; i++) {
String colName = quirks.getColumnName(meta, i);
// behavior change: do not throw if POJO contains less properties
setters[i] = getSetter(quirks, colName, metadata);
// If more than 1 column is fetched (we cannot fall back to executeScalar),
// and the setter doesn't exist, throw exception.
if (setters[i] == null && columnCount > 1) {
throw new Sql2oException("Could not map " + colName + " to any property.");
}
}
/**
* Fallback to executeScalar if converter exists,
* we're selecting 1 column, and no property setter exists for the column.
*/
useExecuteScalar = converter != null && columnCount == 1 && setters[1] == null;
return new ResultSetHandler<T>() {
@SuppressWarnings("unchecked")
public T handle(ResultSet resultSet) throws SQLException {
if (useExecuteScalar) {
try {
return (T) converter.convert(quirks.getRSVal(resultSet, 1));
} catch (ConverterException e) {
throw new Sql2oException("Error occurred while converting value from database to type " + metadata.getType(), e);
}
}