Handle booleans same way in CompilingResultSetMapper as ResultSetMapper

This commit is contained in:
Travis Burtrum 2017-05-17 10:15:27 -04:00
parent 2e6fce9786
commit 61010ec39e
2 changed files with 27 additions and 2 deletions

View File

@ -257,7 +257,7 @@ public class CompilingRowToObjectMapper<T> extends RowToObjectMapper<T> {
java.append("rs.getInt(").append(index).append(")");
return;
case TypeMappingsFactory.TYPE_BOOLEAN:
java.append("rs.getInt(").append(index).append(") ? Boolean.TRUE : Boolean.FALSE");
java.append("getBooleanYN(rs, ").append(index).append(")");
return;
case TypeMappingsFactory.TYPE_INT_OBJ:
java.append("getObjectInt(rs, ").append(index).append(")");
@ -278,7 +278,7 @@ public class CompilingRowToObjectMapper<T> extends RowToObjectMapper<T> {
java.append("getObjectShort(rs, ").append(index).append(")");
return;
case TypeMappingsFactory.TYPE_BOOLEAN_OBJ:
java.append("getObjectBoolean(rs, ").append(index).append(")");
java.append("getObjectBooleanYN(rs, ").append(index).append(")");
return;
case TypeMappingsFactory.TYPE_STRING:
case TypeMappingsFactory.TYPE_XMLBEAN_ENUM:

View File

@ -1,11 +1,16 @@
package com.moparisthebest.jdbc.util;
import com.moparisthebest.jdbc.MapperException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import static com.moparisthebest.jdbc.UpdateableDTO.NO;
import static com.moparisthebest.jdbc.UpdateableDTO.YES;
/**
* Created by mopar on 5/16/17.
*/
@ -46,6 +51,26 @@ public class ResultSetUtil {
return rs.wasNull() ? null : (ret ? Boolean.TRUE : Boolean.FALSE);
}
public static Boolean getObjectBooleanYN(final ResultSet rs, final int index) throws SQLException {
try {
return getObjectBoolean(rs, index);
} catch (SQLException e) {
// if we are here, it wasn't a boolean or null, so try to grab a string instead
final String bool = rs.getString(index);//.toUpperCase(); // do we want it case-insensitive?
final boolean ret = YES.equals(bool);
if (!ret && !NO.equals(bool))
throw new MapperException(String.format("Implicit conversion of database string to boolean failed on column '%d'. Returned string needs to be 'Y' or 'N' and was instead '%s'.", index, bool));
return ret;
}
}
public static boolean getBooleanYN(final ResultSet rs, final int index) throws SQLException {
final Boolean ret = getObjectBooleanYN(rs, index);
if(ret == null)
throw new MapperException(String.format("Implicit conversion of database string to boolean failed on column '%d'. Returned string needs to be 'Y' or 'N' and was instead 'null'. If you want to accept null values, make it an object Boolean instead of primitive boolean.", index));
return ret;
}
public static Timestamp getTimestamp(final ResultSet _resultSet, final Calendar _cal, final int index) throws SQLException {
if (null == _cal)
return _resultSet.getTimestamp(index);