diff --git a/CHANGELOG.md b/CHANGELOG.md index b7f1aa710..73f0aea3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 0.9.2 +### Improvements +- [jdbc-v2] `ResultSetImpl.getObject()` handles `java.time.Instant` + ### Improvements - [jdbc-v2] Classes `com.clickhouse.jdbc.ClientInfoProperties` and `com.clickhouse.jdbc.DriverProperties` moved to public API. (https://github.com/ClickHouse/clickhouse-java/pull/2521) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java index 172f1c3be..2562b5529 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java @@ -20,11 +20,8 @@ import java.sql.SQLException; import java.sql.SQLType; import java.sql.Types; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.OffsetDateTime; -import java.time.ZonedDateTime; +import java.time.*; +import java.time.chrono.ChronoZonedDateTime; import java.time.temporal.TemporalAccessor; import java.util.ArrayList; import java.util.Collections; @@ -260,6 +257,8 @@ public static Object convert(Object value, Class type, ClickHouseColumn colum return OffsetDateTime.from((TemporalAccessor) value); } else if (type == ZonedDateTime.class && value instanceof TemporalAccessor) { return ZonedDateTime.from((TemporalAccessor) value); + } else if (type == Instant.class && value instanceof TemporalAccessor) { + return Instant.from((TemporalAccessor) value); } else if (type == Date.class && value instanceof TemporalAccessor) { return Date.valueOf(LocalDate.from((TemporalAccessor) value)); } else if (type == java.sql.Timestamp.class && value instanceof TemporalAccessor) { diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcUtilsTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcUtilsTest.java index ef6b63a84..ecf08a502 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcUtilsTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcUtilsTest.java @@ -2,10 +2,12 @@ import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader; import com.clickhouse.data.ClickHouseColumn; -import org.testng.annotations.Test; +import org.testng.annotations.*; import java.math.BigDecimal; import java.sql.SQLException; +import java.time.Instant; +import java.time.ZoneId; import java.util.Arrays; import java.util.List; @@ -78,4 +80,19 @@ public void testConvertToInetAddress() throws Exception { ClickHouseColumn column = ClickHouseColumn.of("ip", "IPv4"); assertEquals(JdbcUtils.convert(java.net.InetAddress.getByName("192.168.0.1"), java.net.Inet6Address.class, column).toString(), "/0:0:0:0:0:ffff:c0a8:1"); } + + @DataProvider(name = "timeZones") + public Object[][] timeZones() { + return new Object[][] { + { ZoneId.of("UTC") }, + { ZoneId.of("Europe/Paris") }, + }; + } + + @Test(groups = {"unit"}) + public void testConvertToJavaTimeInstant() throws Exception { + Instant timestamp = Instant.parse("2016-06-07T21:30:00Z"); + assertEquals(JdbcUtils.convert(timestamp.atZone(ZoneId.of("UTC")), Instant.class), timestamp); + assertEquals(JdbcUtils.convert(timestamp.atZone(ZoneId.of("UTC+7")), Instant.class), timestamp); + } }