Skip to content

Commit

Permalink
Add initial support for ResultSet#getObject with type
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jul 29, 2020
1 parent 5d8d588 commit 0222860
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,15 @@ public void updateNClob(String columnLabel, Reader reader)
public <T> T getObject(int columnIndex, Class<T> type)
throws SQLException
{
throw new SQLFeatureNotSupportedException("getObject");
if (type == null) {
throw new SQLException("type is null");
}
Object object = getObject(columnIndex);
if (object == null || type.isInstance(object)) {
//noinspection unchecked
return (T) object;
}
throw new SQLException(format("Cannot convert an instance of %s to %s", object.getClass(), type));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void testDate()
{
checkRepresentation("DATE '2018-02-13'", Types.DATE, (rs, column) -> {
assertEquals(rs.getObject(column), Date.valueOf(LocalDate.of(2018, 2, 13)));
assertEquals(rs.getObject(column, Date.class), Date.valueOf(LocalDate.of(2018, 2, 13)));
assertEquals(rs.getDate(column), Date.valueOf(LocalDate.of(2018, 2, 13)));
assertThrows(IllegalArgumentException.class, () -> rs.getTime(column));
assertThrows(IllegalArgumentException.class, () -> rs.getTimestamp(column));
Expand All @@ -142,6 +143,7 @@ public void testTime()
{
checkRepresentation("TIME '09:39:05'", Types.TIME, (rs, column) -> {
assertEquals(rs.getObject(column), Time.valueOf(LocalTime.of(9, 39, 5)));
assertEquals(rs.getObject(column, Time.class), Time.valueOf(LocalTime.of(9, 39, 5)));
assertThrows(() -> rs.getDate(column));
assertEquals(rs.getTime(column), Time.valueOf(LocalTime.of(9, 39, 5)));
assertThrows(() -> rs.getTimestamp(column));
Expand Down Expand Up @@ -191,6 +193,7 @@ public void testTimestamp()
{
checkRepresentation("TIMESTAMP '2018-02-13 13:14:15.123'", Types.TIMESTAMP, (rs, column) -> {
assertEquals(rs.getObject(column), Timestamp.valueOf(LocalDateTime.of(2018, 2, 13, 13, 14, 15, 123_000_000)));
assertEquals(rs.getObject(column, Timestamp.class), Timestamp.valueOf(LocalDateTime.of(2018, 2, 13, 13, 14, 15, 123_000_000)));
assertThrows(() -> rs.getDate(column));
assertThrows(() -> rs.getTime(column));
assertEquals(rs.getTimestamp(column), Timestamp.valueOf(LocalDateTime.of(2018, 2, 13, 13, 14, 15, 123_000_000)));
Expand Down Expand Up @@ -287,7 +290,10 @@ public void testArray()
private void checkRepresentation(String expression, int expectedSqlType, Object expectedRepresentation)
throws Exception
{
checkRepresentation(expression, expectedSqlType, (rs, column) -> assertEquals(rs.getObject(column), expectedRepresentation));
checkRepresentation(expression, expectedSqlType, (rs, column) -> {
assertEquals(rs.getObject(column), expectedRepresentation);
assertEquals(rs.getObject(column, expectedRepresentation.getClass()), expectedRepresentation);
});
}

private void checkRepresentation(String expression, int expectedSqlType, ResultAssertion assertion)
Expand Down

0 comments on commit 0222860

Please sign in to comment.