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 support for LocalDate, LocalTime and LocalDateTime to be passed as 'type' in ResultSet.getObject() #749

Merged
merged 6 commits into from
Aug 18, 2018
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
18 changes: 18 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2373,6 +2373,24 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
returnValue = getTime(columnIndex);
} else if (type == java.sql.Timestamp.class) {
returnValue = getTimestamp(columnIndex);
} else if (type == java.time.LocalDateTime.class
|| type == java.time.LocalDate.class
|| type == java.time.LocalTime.class) {
java.sql.Timestamp ts = getTimestamp(columnIndex,
Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC")));
if (ts == null) {
returnValue = null;
} else {
java.time.LocalDateTime ldt = java.time.LocalDateTime
.ofInstant(ts.toInstant(), java.time.ZoneId.of("UTC"));
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 == microsoft.sql.DateTimeOffset.class) {
returnValue = getDateTimeOffset(columnIndex);
} else if (type == UUID.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.TimeZone;
import java.util.UUID;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -240,6 +244,48 @@ public void testJdbc41ResultSetMethods() throws SQLException {
}
}

/**
* Tests getObject(n, java.time.LocalDateTime.class).
*
* @throws SQLException
*/
@Test
public void testGetObjectAsLocalDateTime() throws SQLException {
try (Connection con = DriverManager.getConnection(connectionString); Statement stmt = con.createStatement()) {
TimeZone prevTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("America/Edmonton"));

// a local date/time that does not actually exist because of Daylight Saving Time
final String testValueDate = "2018-03-11";
final String testValueTime = "02:00:00.1234567";
final String testValueDateTime = testValueDate + "T" + testValueTime;

stmt.executeUpdate(
"CREATE TABLE " + tableName + " (id INT PRIMARY KEY, dt2 DATETIME2)");
stmt.executeUpdate(
"INSERT INTO " + tableName + " (id, dt2) VALUES (1, '" + testValueDateTime + "')");

try (ResultSet rs = stmt.executeQuery("SELECT dt2 FROM " + tableName + " WHERE id=1")) {
rs.next();

LocalDateTime expectedLocalDateTime = LocalDateTime.parse(testValueDateTime);
LocalDateTime actualLocalDateTime = rs.getObject(1, LocalDateTime.class);
assertEquals(expectedLocalDateTime, actualLocalDateTime);

LocalDate expectedLocalDate = LocalDate.parse(testValueDate);
LocalDate actualLocalDate = rs.getObject(1, LocalDate.class);
assertEquals(expectedLocalDate, actualLocalDate);

LocalTime expectedLocalTime = LocalTime.parse(testValueTime);
LocalTime actualLocalTime = rs.getObject(1, LocalTime.class);
assertEquals(expectedLocalTime, actualLocalTime);
} finally {
Utils.dropTableIfExists(tableName, stmt);
TimeZone.setDefault(prevTimeZone);
}
}
}

/**
* Tests ResultSet#isWrapperFor and ResultSet#unwrap.
*
Expand Down