Skip to content

Commit

Permalink
fix(jdbc): ResultSet#getObject could throw a NPE
Browse files Browse the repository at this point in the history
Closes: #915
  • Loading branch information
gotson committed Jun 23, 2023
1 parent 3d28792 commit ea165fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main/java/org/sqlite/jdbc4/JDBC4ResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
if (type == Timestamp.class) return type.cast(getTimestamp(columnIndex));
if (type == LocalDate.class) {
try {
return type.cast(getDate(columnIndex).toLocalDate());
Date date = getDate(columnIndex);
if (date != null) return type.cast(date.toLocalDate());
else return null;
} catch (SQLException sqlException) {
// If the FastDateParser failed, try parse it with LocalDate.
// It's a workaround for a value like '2022-12-1' (i.e no time presents).
Expand All @@ -333,15 +335,19 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
}
if (type == LocalTime.class) {
try {
return type.cast(getTime(columnIndex).toLocalTime());
Time time = getTime(columnIndex);
if (time != null) return type.cast(time.toLocalTime());
else return null;
} catch (SQLException sqlException) {
// If the FastDateParser failed, try parse it with LocalTime.
// It's a workaround for a value like '11:22:22' (i.e no date presents).
return type.cast(LocalTime.parse(getString(columnIndex)));
}
}
if (type == LocalDateTime.class) {
return type.cast(getTimestamp(columnIndex).toLocalDateTime());
Timestamp timestamp = getTimestamp(columnIndex);
if (timestamp != null) return type.cast(timestamp.toLocalDateTime());
else return null;
}

int columnType = safeGetColumnType(markCol(columnIndex));
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/sqlite/ResultSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ void testJdk8AddedDateTimeObjects() throws SQLException {
stat.executeUpdate("insert into datetime_test values ('2021-11-09 11:20:58')");
stat.executeUpdate("insert into datetime_test values ('2021-11-09')");
stat.executeUpdate("insert into datetime_test values ('11:20:58')");
stat.executeUpdate("insert into datetime_test values (NULL)");

ResultSet rs = stat.executeQuery("select * from datetime_test");

Expand All @@ -349,6 +350,11 @@ void testJdk8AddedDateTimeObjects() throws SQLException {

rs.next();
assertThat(rs.getObject(1, LocalTime.class)).isEqualTo(LocalTime.of(11, 20, 58));

rs.next();
assertThat(rs.getObject(1, LocalDate.class)).isNull();
assertThat(rs.getObject(1, LocalTime.class)).isNull();
assertThat(rs.getObject(1, LocalDateTime.class)).isNull();
}

@Test
Expand Down

0 comments on commit ea165fa

Please sign in to comment.