Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null check for getObject() with LocalTime and LocalDate #1250

Merged
merged 2 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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