Skip to content

Commit

Permalink
Fix | Add null check for getObject() with LocalTime and LocalDate (#1250
Browse files Browse the repository at this point in the history
)
  • Loading branch information
peterbae authored Feb 8, 2020
1 parent 2e62464 commit c2ff74d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2394,12 +2394,16 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
} else if (type == java.time.LocalDateTime.class || type == java.time.LocalDate.class
|| type == java.time.LocalTime.class) {
java.time.LocalDateTime ldt = getLocalDateTime(columnIndex);
if (type == java.time.LocalDateTime.class) {
returnValue = ldt;
} else if (type == java.time.LocalDate.class) {
returnValue = ldt.toLocalDate();
if (null == ldt) {
returnValue = null;
} else {
returnValue = ldt.toLocalTime();
if (type == java.time.LocalDateTime.class) {
returnValue = ldt;
} else if (type == java.time.LocalDate.class) {
returnValue = ldt.toLocalDate();
} else {
returnValue = ldt.toLocalTime();
}
}
} else if (type == java.time.OffsetDateTime.class) {
microsoft.sql.DateTimeOffset dateTimeOffset = getDateTimeOffset(columnIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.text.DateFormatSymbols;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.time.zone.ZoneOffsetTransition;
Expand Down Expand Up @@ -1821,6 +1823,28 @@ public void testGetLocalDateTimePriorGregorian() throws Exception {
}
}
}

@Test
public void testNullValuesWithGetObject() throws Exception {
String ldtTable = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("ldtTable"));
try (Connection conn = getConnection(); Statement st = conn.createStatement();) {
TestUtils.dropTableIfExists(ldtTable, st);
st.execute("CREATE TABLE " + ldtTable + " (c1 datetime2)");
st.execute("INSERT INTO " + ldtTable + " VALUES (NULL)");

try (ResultSet rs = st.executeQuery("SELECT c1 FROM " + ldtTable);) {
rs.next();
LocalDateTime ldtActual = rs.getObject(1, LocalDateTime.class);
assertEquals(ldtActual, null);
LocalTime ltActual = rs.getObject(1, LocalTime.class);
assertEquals(ltActual, null);
LocalDate ldActual = rs.getObject(1, LocalDate.class);
assertEquals(ldActual, null);
} finally {
TestUtils.dropTableIfExists(ldtTable, st);
}
}
}

/**
* Test example from https://github.com/microsoft/mssql-jdbc/issues/1143
Expand Down

0 comments on commit c2ff74d

Please sign in to comment.