Skip to content

Commit

Permalink
New API | Add support for LocalDate, LocalTime and LocalDateTime to b…
Browse files Browse the repository at this point in the history
…e passed as 'type' in ResultSet.getObject() (#749)

* fix for issue 744

* remove "var" declaration

* simple change to re-run CI checks after GitHub outage

* avoid conversion to/from string

* add support for LocalDate and LocalTime

* LocalDate and LocalTime tweaks for Java 8
  • Loading branch information
gordthompson authored and cheenamalhotra committed Aug 18, 2018
1 parent 2c86309 commit f415394
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java
Expand Up @@ -2375,6 +2375,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
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

0 comments on commit f415394

Please sign in to comment.