From 61010ec39e6fddff1de04df125491e82611f6a8c Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Wed, 17 May 2017 10:15:27 -0400 Subject: [PATCH] Handle booleans same way in CompilingResultSetMapper as ResultSetMapper --- .../jdbc/CompilingRowToObjectMapper.java | 4 +-- .../jdbc/util/ResultSetUtil.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java index a79246c..54e678b 100644 --- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CompilingRowToObjectMapper.java @@ -257,7 +257,7 @@ public class CompilingRowToObjectMapper extends RowToObjectMapper { 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 extends RowToObjectMapper { 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: diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/util/ResultSetUtil.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/util/ResultSetUtil.java index 0a4b3e4..4a8b283 100644 --- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/util/ResultSetUtil.java +++ b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/util/ResultSetUtil.java @@ -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);