diff --git a/src/main/java/org/apache/ibatis/type/BlobTypeHandler.java b/src/main/java/org/apache/ibatis/type/BlobTypeHandler.java index 878ca167935..4d34a34a0f0 100644 --- a/src/main/java/org/apache/ibatis/type/BlobTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/BlobTypeHandler.java @@ -36,31 +36,20 @@ public void setNonNullParameter(PreparedStatement ps, int i, byte[] parameter, J @Override public byte[] getNullableResult(ResultSet rs, String columnName) throws SQLException { - Blob blob = rs.getBlob(columnName); - byte[] returnValue = null; - if (null != blob) { - returnValue = blob.getBytes(1, (int) blob.length()); - } - return returnValue; + return toPrimitiveBytes(rs.getBlob(columnName)); } @Override public byte[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException { - Blob blob = rs.getBlob(columnIndex); - byte[] returnValue = null; - if (null != blob) { - returnValue = blob.getBytes(1, (int) blob.length()); - } - return returnValue; + return toPrimitiveBytes(rs.getBlob(columnIndex)); } @Override public byte[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { - Blob blob = cs.getBlob(columnIndex); - byte[] returnValue = null; - if (null != blob) { - returnValue = blob.getBytes(1, (int) blob.length()); - } - return returnValue; + return toPrimitiveBytes(cs.getBlob(columnIndex)); + } + + private byte[] toPrimitiveBytes(Blob blob) throws SQLException { + return blob == null ? null : blob.getBytes(1, (int) blob.length()); } } diff --git a/src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java b/src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java index 3b99b39606f..4815ff52735 100644 --- a/src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java @@ -33,28 +33,20 @@ public void setNonNullParameter(PreparedStatement ps, int i, Character parameter @Override public Character getNullableResult(ResultSet rs, String columnName) throws SQLException { - String columnValue = rs.getString(columnName); - if (columnValue != null && !columnValue.isEmpty()) { - return columnValue.charAt(0); - } - return null; + return toCharacter(rs.getString(columnName)); } @Override public Character getNullableResult(ResultSet rs, int columnIndex) throws SQLException { - String columnValue = rs.getString(columnIndex); - if (columnValue != null && !columnValue.isEmpty()) { - return columnValue.charAt(0); - } - return null; + return toCharacter(rs.getString(columnIndex)); } @Override public Character getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { - String columnValue = cs.getString(columnIndex); - if (columnValue != null && !columnValue.isEmpty()) { - return columnValue.charAt(0); - } - return null; + return toCharacter(cs.getString(columnIndex)); + } + + private Character toCharacter(String value) { + return value == null || value.isEmpty() ? null : value.charAt(0); } } diff --git a/src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java b/src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java index a571fa321bf..b88fb312e43 100644 --- a/src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java @@ -33,29 +33,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, Jdb @Override public Date getNullableResult(ResultSet rs, String columnName) throws SQLException { - java.sql.Date sqlDate = rs.getDate(columnName); - if (sqlDate != null) { - return new Date(sqlDate.getTime()); - } - return null; + return toSqlDate(rs.getDate(columnName)); } @Override public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException { - java.sql.Date sqlDate = rs.getDate(columnIndex); - if (sqlDate != null) { - return new Date(sqlDate.getTime()); - } - return null; + return toSqlDate(rs.getDate(columnIndex)); } @Override public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { - java.sql.Date sqlDate = cs.getDate(columnIndex); - if (sqlDate != null) { - return new Date(sqlDate.getTime()); - } - return null; + return toSqlDate(cs.getDate(columnIndex)); + } + + private java.sql.Date toSqlDate(Date date) { + return date == null ? null : new java.sql.Date(date.getTime()); } } diff --git a/src/main/java/org/apache/ibatis/type/DateTypeHandler.java b/src/main/java/org/apache/ibatis/type/DateTypeHandler.java index 72354ae0f3f..91b9b4f1835 100644 --- a/src/main/java/org/apache/ibatis/type/DateTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/DateTypeHandler.java @@ -34,28 +34,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, Jdb @Override public Date getNullableResult(ResultSet rs, String columnName) throws SQLException { - Timestamp sqlTimestamp = rs.getTimestamp(columnName); - if (sqlTimestamp != null) { - return new Date(sqlTimestamp.getTime()); - } - return null; + return toDate(rs.getTimestamp(columnName)); } @Override public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException { - Timestamp sqlTimestamp = rs.getTimestamp(columnIndex); - if (sqlTimestamp != null) { - return new Date(sqlTimestamp.getTime()); - } - return null; + return toDate(rs.getTimestamp(columnIndex)); } @Override public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { - Timestamp sqlTimestamp = cs.getTimestamp(columnIndex); - if (sqlTimestamp != null) { - return new Date(sqlTimestamp.getTime()); - } - return null; + return toDate(cs.getTimestamp(columnIndex)); } + + private Date toDate(Timestamp timestamp) { + return timestamp == null ? null : new Date(timestamp.getTime()); + } + } diff --git a/src/main/java/org/apache/ibatis/type/TimeOnlyTypeHandler.java b/src/main/java/org/apache/ibatis/type/TimeOnlyTypeHandler.java index c1aad7f832d..f33e6da64fb 100644 --- a/src/main/java/org/apache/ibatis/type/TimeOnlyTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/TimeOnlyTypeHandler.java @@ -34,28 +34,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, Jdb @Override public Date getNullableResult(ResultSet rs, String columnName) throws SQLException { - Time sqlTime = rs.getTime(columnName); - if (sqlTime != null) { - return new Date(sqlTime.getTime()); - } - return null; + return toDate(rs.getTime(columnName)); } @Override public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException { - Time sqlTime = rs.getTime(columnIndex); - if (sqlTime != null) { - return new Date(sqlTime.getTime()); - } - return null; + return toDate(rs.getTime(columnIndex)); } @Override public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { - Time sqlTime = cs.getTime(columnIndex); - if (sqlTime != null) { - return new Date(sqlTime.getTime()); - } - return null; + return toDate(cs.getTime(columnIndex)); } + + private Date toDate(Time time) { + return time == null ? null : new Date(time.getTime()); + } + } diff --git a/src/main/java/org/apache/ibatis/type/YearMonthTypeHandler.java b/src/main/java/org/apache/ibatis/type/YearMonthTypeHandler.java index a99968c0153..17c27483908 100644 --- a/src/main/java/org/apache/ibatis/type/YearMonthTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/YearMonthTypeHandler.java @@ -40,19 +40,20 @@ public void setNonNullParameter(PreparedStatement ps, int i, YearMonth yearMonth @Override public YearMonth getNullableResult(ResultSet rs, String columnName) throws SQLException { - String value = rs.getString(columnName); - return value == null ? null : YearMonth.parse(value); + return toYearMonth(rs.getString(columnName)); } @Override public YearMonth getNullableResult(ResultSet rs, int columnIndex) throws SQLException { - String value = rs.getString(columnIndex); - return value == null ? null : YearMonth.parse(value); + return toYearMonth(rs.getString(columnIndex)); } @Override public YearMonth getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { - String value = cs.getString(columnIndex); + return toYearMonth(cs.getString(columnIndex)); + } + + private YearMonth toYearMonth(String value) { return value == null ? null : YearMonth.parse(value); }