diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java index 5b8bfcd58..9757503c7 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java @@ -28,10 +28,12 @@ import java.sql.Time; import java.sql.Timestamp; import java.sql.Types; +import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; @@ -39,6 +41,7 @@ import java.util.Collection; import java.util.GregorianCalendar; import java.util.Map; +import java.util.TimeZone; public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcV2Wrapper { private static final Logger LOG = LoggerFactory.getLogger(PreparedStatementImpl.class); @@ -159,19 +162,16 @@ public void setBytes(int parameterIndex, byte[] x) throws SQLException { @Override public void setDate(int parameterIndex, Date x) throws SQLException { - checkClosed(); setDate(parameterIndex, x, null); } @Override public void setTime(int parameterIndex, Time x) throws SQLException { - checkClosed(); setTime(parameterIndex, x, null); } @Override public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { - checkClosed(); setTimestamp(parameterIndex, x, null); } @@ -269,42 +269,42 @@ public ResultSetMetaData getMetaData() throws SQLException { public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { checkClosed(); if (cal == null) { - cal = new GregorianCalendar(); - cal.setTime(x); + cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));//This says whatever date is in UTC } - ZoneId tz = cal.getTimeZone().toZoneId(); + LocalDate d = x.toLocalDate(); Calendar c = (Calendar) cal.clone(); - c.setTime(x); - parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(tz).toLocalDate()); + c.clear(); + c.set(d.getYear(), d.getMonthValue() - 1, d.getDayOfMonth(), 0, 0, 0); + parameters[parameterIndex - 1] = encodeObject(c.toInstant()); } @Override public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { checkClosed(); if (cal == null) { - cal = new GregorianCalendar(); - cal.setTime(x); + cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); } - ZoneId tz = cal.getTimeZone().toZoneId(); + LocalTime t = x.toLocalTime(); Calendar c = (Calendar) cal.clone(); - c.setTime(x); - parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(tz).toLocalTime()); + c.clear(); + c.set(1970, Calendar.JANUARY, 1, t.getHour(), t.getMinute(), t.getSecond()); + parameters[parameterIndex - 1] = encodeObject(c.toInstant()); } @Override public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { checkClosed(); if (cal == null) { - cal = new GregorianCalendar(); - cal.setTime(x); + cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); } - ZoneId tz = cal.getTimeZone().toZoneId(); + LocalDateTime ldt = x.toLocalDateTime(); Calendar c = (Calendar) cal.clone(); - c.setTime(x); - parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(tz).withNano(x.getNanos()).toLocalDateTime()); + c.clear(); + c.set(ldt.getYear(), ldt.getMonthValue() - 1, ldt.getDayOfMonth(), ldt.getHour(), ldt.getMinute(), ldt.getSecond()); + parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(ZoneId.of("UTC")).withNano(x.getNanos())); } @Override @@ -479,6 +479,10 @@ private static String encodeObject(Object x) throws SQLException { return "'" + DATETIME_FORMATTER.format(((Timestamp) x).toLocalDateTime()) + "'"; } else if (x instanceof LocalDateTime) { return "'" + DATETIME_FORMATTER.format((LocalDateTime) x) + "'"; + } else if (x instanceof ZonedDateTime) { + return encodeObject(((ZonedDateTime) x).toInstant()); + } else if (x instanceof Instant) { + return "fromUnixTimestamp64Nano(" + (((Instant) x).getEpochSecond() * 1_000_000_000L + ((Instant) x).getNano())+ ")"; } else if (x instanceof Array) { StringBuilder listString = new StringBuilder(); listString.append("["); @@ -571,6 +575,7 @@ private static String encodeObject(Object x) throws SQLException { } } + private static String escapeString(String x) { return x.replace("\\", "\\\\").replace("'", "\\'");//Escape single quotes } diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java index 2b59f931a..9bf8de717 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java @@ -11,6 +11,9 @@ import java.sql.*; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Map; @@ -179,12 +182,12 @@ public byte[] getBytes(int columnIndex) throws SQLException { @Override public Date getDate(int columnIndex) throws SQLException { - return getDate(columnIndexToName(columnIndex)); + return getDate(columnIndex, null); } @Override public Time getTime(int columnIndex) throws SQLException { - return getTime(columnIndexToName(columnIndex)); + return getTime(columnIndex, null); } @Override @@ -369,37 +372,12 @@ public byte[] getBytes(String columnLabel) throws SQLException { @Override public Date getDate(String columnLabel) throws SQLException { - checkClosed(); - try { - //TODO: Add this to ClickHouseBinaryFormatReader - LocalDate localDate = reader.getLocalDate(columnLabel); - if (localDate == null) { - wasNull = true; - return null; - } - - wasNull = false; - return Date.valueOf(localDate); - } catch (Exception e) { - throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getDate(%s)", parentStatement.getLastSql(), columnLabel), e); - } + return getDate(columnLabel, null); } @Override public Time getTime(String columnLabel) throws SQLException { - checkClosed(); - try { - LocalDateTime localDateTime = reader.getLocalDateTime(columnLabel); - if(localDateTime == null) { - wasNull = true; - return null; - } - - wasNull = false; - return Time.valueOf(localDateTime.toLocalTime()); - } catch (Exception e) { - throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getTime(%s)", parentStatement.getLastSql(), columnLabel), e); - } + return getTime(columnLabel, null); } @Override @@ -1068,11 +1046,13 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException { @Override public Date getDate(String columnLabel, Calendar cal) throws SQLException { checkClosed(); - Date date = getDate(columnLabel); - if (date == null) { + LocalDate d = reader.getLocalDate(columnLabel); + if (d == null) { + wasNull = true; return null; } - LocalDate d = date.toLocalDate(); + wasNull = false; + Calendar c = (Calendar)( cal != null ? cal : defaultCalendar).clone(); c.clear(); c.set(d.getYear(), d.getMonthValue() - 1, d.getDayOfMonth(), 0, 0, 0); @@ -1087,7 +1067,21 @@ public Time getTime(int columnIndex, Calendar cal) throws SQLException { @Override public Time getTime(String columnLabel, Calendar cal) throws SQLException { checkClosed(); - return getTime(columnLabel); + try { + ZonedDateTime zdt = reader.getZonedDateTime(columnLabel); + if (zdt == null) { + wasNull = true; + return null; + } + wasNull = false; + + Calendar c = (Calendar)( cal != null ? cal : defaultCalendar).clone(); + c.clear(); + c.set(1970, Calendar.JANUARY, 1, zdt.getHour(), zdt.getMinute(), zdt.getSecond()); + return new Time(c.getTimeInMillis()); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getTime(%s, %s)", parentStatement.getLastSql(), columnLabel, cal), e); + } } @Override @@ -1104,12 +1098,13 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept wasNull = true; return null; } + wasNull = false; + Calendar c = (Calendar) (cal != null ? cal : defaultCalendar).clone(); c.set(localDateTime.getYear(), localDateTime.getMonthValue() - 1, localDateTime.getDayOfMonth(), localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond()); Timestamp timestamp = new Timestamp(c.getTimeInMillis()); timestamp.setNanos(localDateTime.getNano()); - wasNull = false; return timestamp; } catch (Exception e) { throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getTimestamp(%s)", parentStatement.getLastSql(), columnLabel), e); diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java index a30c8531b..cb85158b1 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java @@ -23,10 +23,12 @@ import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Random; +import java.util.TimeZone; import java.util.UUID; import static org.testng.Assert.assertEquals; @@ -279,31 +281,31 @@ public void testDateTypes() throws SQLException { try (Statement stmt = conn.createStatement()) { try (ResultSet rs = stmt.executeQuery("SELECT * FROM test_dates ORDER BY order")) { assertTrue(rs.next()); - assertEquals(rs.getDate("date"), Date.valueOf("1970-01-01")); - assertEquals(rs.getDate("date32"), Date.valueOf("1970-01-01")); - assertEquals(rs.getTimestamp("dateTime"), new java.sql.Timestamp(Date.valueOf("1970-01-01").getTime())); - assertEquals(rs.getTimestamp("dateTime32"), new java.sql.Timestamp(Date.valueOf("1970-01-01").getTime())); - assertEquals(rs.getTimestamp("dateTime643"), new java.sql.Timestamp(Date.valueOf("1970-01-01").getTime())); - assertEquals(rs.getTimestamp("dateTime646"), new java.sql.Timestamp(Date.valueOf("1970-01-01").getTime())); - assertEquals(rs.getTimestamp("dateTime649"), new java.sql.Timestamp(Date.valueOf("1970-01-01").getTime())); + assertEquals(rs.getDate("date", new GregorianCalendar()), Date.valueOf("1970-01-01")); + assertEquals(rs.getDate("date32", new GregorianCalendar()), Date.valueOf("1970-01-01")); + assertEquals(rs.getTimestamp("dateTime").toInstant().toString(), "1970-01-01T00:00:00Z"); + assertEquals(rs.getTimestamp("dateTime32").toInstant().toString(), "1970-01-01T00:00:00Z"); + assertEquals(rs.getTimestamp("dateTime643").toInstant().toString(), "1970-01-01T00:00:00Z"); + assertEquals(rs.getTimestamp("dateTime646").toInstant().toString(), "1970-01-01T00:00:00Z"); + assertEquals(rs.getTimestamp("dateTime649").toInstant().toString(), "1970-01-01T00:00:00Z"); assertTrue(rs.next()); - assertEquals(rs.getDate("date"), Date.valueOf("2149-06-06")); - assertEquals(rs.getDate("date32"), Date.valueOf("2299-12-31")); - assertEquals(rs.getTimestamp("dateTime"), java.sql.Timestamp.valueOf("2106-02-07 06:28:15")); - assertEquals(rs.getTimestamp("dateTime32"), java.sql.Timestamp.valueOf("2106-02-07 06:28:15")); - assertEquals(rs.getTimestamp("dateTime643"), java.sql.Timestamp.valueOf("2261-12-31 23:59:59.999")); - assertEquals(rs.getTimestamp("dateTime646"), java.sql.Timestamp.valueOf("2261-12-31 23:59:59.999999")); - assertEquals(rs.getTimestamp("dateTime649"), java.sql.Timestamp.valueOf("2261-12-31 23:59:59.999999999")); + assertEquals(rs.getDate("date", new GregorianCalendar()), Date.valueOf("2149-06-06")); + assertEquals(rs.getDate("date32", new GregorianCalendar()), Date.valueOf("2299-12-31")); + assertEquals(rs.getTimestamp("dateTime").toInstant().toString(), "2106-02-07T06:28:15Z"); + assertEquals(rs.getTimestamp("dateTime32").toInstant().toString(), "2106-02-07T06:28:15Z"); + assertEquals(rs.getTimestamp("dateTime643").toInstant().toString(), "2261-12-31T23:59:59.999Z"); + assertEquals(rs.getTimestamp("dateTime646").toInstant().toString(), "2261-12-31T23:59:59.999999Z"); + assertEquals(rs.getTimestamp("dateTime649").toInstant().toString(), "2261-12-31T23:59:59.999999999Z"); assertTrue(rs.next()); - assertEquals(rs.getDate("date").toLocalDate(), date.toLocalDate()); - assertEquals(rs.getDate("date32").toLocalDate(), date32.toLocalDate()); - assertEquals(rs.getTimestamp("dateTime"), dateTime); - assertEquals(rs.getTimestamp("dateTime32"), dateTime32); - assertEquals(rs.getTimestamp("dateTime643"), dateTime643); - assertEquals(rs.getTimestamp("dateTime646"), dateTime646); - assertEquals(rs.getTimestamp("dateTime649"), dateTime649); + assertEquals(rs.getDate("date", new GregorianCalendar()).toString(), date.toString()); + assertEquals(rs.getDate("date32", new GregorianCalendar()).toString(), date32.toString()); + assertEquals(rs.getTimestamp("dateTime", new GregorianCalendar()).toString(), dateTime.toString()); + assertEquals(rs.getTimestamp("dateTime32", new GregorianCalendar()).toString(), dateTime32.toString()); + assertEquals(rs.getTimestamp("dateTime643", new GregorianCalendar()).toString(), dateTime643.toString()); + assertEquals(rs.getTimestamp("dateTime646", new GregorianCalendar()).toString(), dateTime646.toString()); + assertEquals(rs.getTimestamp("dateTime649", new GregorianCalendar()).toString(), dateTime649.toString()); assertFalse(rs.next()); } @@ -885,7 +887,7 @@ public void testTypeConversions() throws Exception { assertEquals(rs.getObject(3, Double.class), 1.0); assertEquals(String.valueOf(rs.getObject(3, new HashMap>(){{put(JDBCType.FLOAT.getName(), Float.class);}})), "1.0"); - assertEquals(rs.getDate(4), Date.valueOf("2024-12-01")); + assertEquals(rs.getDate(4, new GregorianCalendar()), Date.valueOf("2024-12-01")); assertTrue(rs.getObject(4) instanceof Date); assertEquals(rs.getObject(4), Date.valueOf("2024-12-01")); assertEquals(rs.getString(4), "2024-12-01");//Underlying object is ZonedDateTime @@ -893,7 +895,7 @@ public void testTypeConversions() throws Exception { assertEquals(rs.getObject(4, ZonedDateTime.class), ZonedDateTime.of(2024, 12, 1, 0, 0, 0, 0, ZoneId.of("UTC"))); assertEquals(String.valueOf(rs.getObject(4, new HashMap>(){{put(JDBCType.DATE.getName(), LocalDate.class);}})), "2024-12-01"); - assertEquals(rs.getTimestamp(5), Timestamp.valueOf("2024-12-01 12:34:56")); + assertEquals(rs.getTimestamp(5).toInstant().toString(), "2024-12-01T12:34:56Z"); assertTrue(rs.getObject(5) instanceof Timestamp); assertEquals(rs.getObject(5), Timestamp.valueOf("2024-12-01 12:34:56")); assertEquals(rs.getString(5), "2024-12-01T12:34:56Z[UTC]"); @@ -901,21 +903,21 @@ public void testTypeConversions() throws Exception { assertEquals(rs.getObject(5, ZonedDateTime.class), ZonedDateTime.of(2024, 12, 1, 12, 34, 56, 0, ZoneId.of("UTC"))); assertEquals(String.valueOf(rs.getObject(5, new HashMap>(){{put(JDBCType.TIMESTAMP.getName(), LocalDateTime.class);}})), "2024-12-01T12:34:56"); - assertEquals(rs.getTimestamp(6), Timestamp.valueOf("2024-12-01 12:34:56.789")); + assertEquals(rs.getTimestamp(6).toInstant().toString(), "2024-12-01T12:34:56.789Z"); assertTrue(rs.getObject(6) instanceof Timestamp); assertEquals(rs.getObject(6), Timestamp.valueOf("2024-12-01 12:34:56.789")); assertEquals(rs.getString(6), "2024-12-01T12:34:56.789Z[UTC]"); assertEquals(rs.getObject(6, LocalDateTime.class), LocalDateTime.of(2024, 12, 1, 12, 34, 56, 789000000)); assertEquals(String.valueOf(rs.getObject(6, new HashMap>(){{put(JDBCType.TIMESTAMP.getName(), LocalDateTime.class);}})), "2024-12-01T12:34:56.789"); - assertEquals(rs.getTimestamp(7), Timestamp.valueOf("2024-12-01 12:34:56.789789")); + assertEquals(rs.getTimestamp(7).toInstant().toString(), "2024-12-01T12:34:56.789789Z"); assertTrue(rs.getObject(7) instanceof Timestamp); assertEquals(rs.getObject(7), Timestamp.valueOf("2024-12-01 12:34:56.789789")); assertEquals(rs.getString(7), "2024-12-01T12:34:56.789789Z[UTC]"); assertEquals(rs.getObject(7, LocalDateTime.class), LocalDateTime.of(2024, 12, 1, 12, 34, 56, 789789000)); assertEquals(String.valueOf(rs.getObject(7, new HashMap>(){{put(JDBCType.TIMESTAMP.getName(), OffsetDateTime.class);}})), "2024-12-01T12:34:56.789789Z"); - assertEquals(rs.getTimestamp(8), Timestamp.valueOf("2024-12-01 12:34:56.789789789")); + assertEquals(rs.getTimestamp(8).toInstant().toString(), "2024-12-01T12:34:56.789789789Z"); assertTrue(rs.getObject(8) instanceof Timestamp); assertEquals(rs.getObject(8), Timestamp.valueOf("2024-12-01 12:34:56.789789789")); assertEquals(rs.getString(8), "2024-12-01T12:34:56.789789789Z[UTC]"); diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java index 8a421c097..9de9a6a58 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java @@ -10,8 +10,10 @@ import java.sql.ResultSet; import java.sql.Timestamp; import java.sql.Types; +import java.time.ZoneId; import java.util.Arrays; import java.util.Calendar; +import java.util.GregorianCalendar; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; @@ -170,7 +172,7 @@ public void testSetDate() throws Exception { stmt.setDate(1, java.sql.Date.valueOf("2021-01-01")); try (ResultSet rs = stmt.executeQuery()) { assertTrue(rs.next()); - assertEquals(java.sql.Date.valueOf("2021-01-01"), rs.getDate(1)); + assertEquals(rs.getDate(1, new GregorianCalendar()), java.sql.Date.valueOf("2021-01-01")); assertFalse(rs.next()); } } @@ -180,11 +182,11 @@ public void testSetDate() throws Exception { @Test(groups = { "integration" }) public void testSetTime() throws Exception { try (Connection conn = getJdbcConnection()) { - try (PreparedStatement stmt = conn.prepareStatement("SELECT parseDateTime(?, '%H:%i:%s')")) { + try (PreparedStatement stmt = conn.prepareStatement("SELECT toDateTime(?)")) { stmt.setTime(1, java.sql.Time.valueOf("12:34:56")); try (ResultSet rs = stmt.executeQuery()) { assertTrue(rs.next()); - assertEquals(java.sql.Time.valueOf("12:34:56"), rs.getTime(1)); + assertEquals(rs.getTime(1, new GregorianCalendar()).toString(), "12:34:56"); assertFalse(rs.next()); } } @@ -194,19 +196,13 @@ public void testSetTime() throws Exception { @Test(groups = { "integration" }) public void testSetTimestamp() throws Exception { try (Connection conn = getJdbcConnection()) { - try (PreparedStatement stmt = conn.prepareStatement("SELECT toDateTime64(?, 3), toDateTime64(?, 3), toDateTime64(?, 3)")) { - stmt.setTimestamp(1, java.sql.Timestamp.valueOf("2021-01-01 12:34:56.111")); - stmt.setTimestamp(2, java.sql.Timestamp.valueOf("2021-01-01 12:34:56.123"), java.util.Calendar.getInstance()); - stmt.setTimestamp(3, java.sql.Timestamp.valueOf("2021-01-01 12:34:56.456"), java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"))); + try (PreparedStatement stmt = conn.prepareStatement("SELECT toDateTime64(?, 3), toDateTime64(?, 3)")) { + stmt.setTimestamp(1, java.sql.Timestamp.valueOf("2021-01-01 01:34:56.456")); + stmt.setTimestamp(2, java.sql.Timestamp.valueOf("2021-01-01 01:34:56.456"), java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"))); try (ResultSet rs = stmt.executeQuery()) { assertTrue(rs.next()); - assertEquals(rs.getTimestamp(1), java.sql.Timestamp.valueOf("2021-01-01 12:34:56.111")); - assertEquals(rs.getTimestamp(2), java.sql.Timestamp.valueOf("2021-01-01 12:34:56.123")); - - Timestamp x = java.sql.Timestamp.valueOf("2021-01-01 12:34:56.456"); - Calendar cal = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC")); - cal.setTime(x); - assertEquals(rs.getTimestamp(3).toString(), PreparedStatementImpl.DATETIME_FORMATTER.format(cal.toInstant().atZone(cal.getTimeZone().toZoneId()).withNano(x.getNanos()).toLocalDateTime())); + assertEquals(rs.getTimestamp(1, new GregorianCalendar()).toString(), "2021-01-01 01:34:56.456"); + assertEquals(rs.getTimestamp(2, new GregorianCalendar()).toString(), "2021-01-01 01:34:56.456"); assertFalse(rs.next()); } } diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java index 180c372bb..5512da0d3 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java @@ -21,6 +21,7 @@ import java.sql.Statement; import java.time.LocalDate; import java.util.Arrays; +import java.util.GregorianCalendar; import java.util.List; import java.util.Properties; import java.util.UUID; @@ -123,22 +124,14 @@ public void testExecuteQueryNulls() throws Exception { public void testExecuteQueryDates() throws Exception { try (Connection conn = getJdbcConnection()) { try (Statement stmt = conn.createStatement()) { - try (ResultSet rs = stmt.executeQuery("SELECT toDate('2020-01-01 12:10:07') AS date, toDateTime('2020-01-01 10:11:12', 'Asia/Istanbul') AS datetime")) { + try (ResultSet rs = stmt.executeQuery("SELECT toDate('2020-01-01') AS date, toDateTime('2020-01-01 10:11:12', 'Asia/Istanbul') AS datetime")) { assertTrue(rs.next()); - assertEquals(rs.getDate(1).toString(), "2020-01-01"); - assertEquals(rs.getDate("date").toString(), "2020-01-01"); - assertEquals(rs.getDate(1).toLocalDate().toString(), "2020-01-01"); - assertEquals(rs.getDate("date").toLocalDate().toString(), "2020-01-01"); - assertEquals(rs.getDate(1, null).toLocalDate().toString(), "2020-01-01"); - assertEquals(rs.getDate("date", null).toLocalDate().toString(), "2020-01-01"); + assertEquals(rs.getDate(1, new GregorianCalendar()).toString(), Date.valueOf("2020-01-01").toString()); + assertEquals(rs.getDate("date", new GregorianCalendar()).toString(), Date.valueOf("2020-01-01").toString()); assertEquals(rs.getString(1), "2020-01-01"); assertEquals(rs.getString("date"), "2020-01-01"); - assertEquals(rs.getDate(2).toString(), "2020-01-01"); - assertEquals(rs.getDate("datetime").toString(), "2020-01-01"); - assertEquals(rs.getDate(2).toLocalDate().toString(), "2020-01-01"); - assertEquals(rs.getDate("datetime").toLocalDate().toString(), "2020-01-01"); - assertEquals(rs.getDate(2, null).toLocalDate().toString(), "2020-01-01"); - assertEquals(rs.getDate("datetime", null).toLocalDate().toString(), "2020-01-01"); + assertEquals(rs.getDate(2, new GregorianCalendar()).toString(), "2020-01-01"); + assertEquals(rs.getDate("datetime", new GregorianCalendar()).toString(), "2020-01-01"); assertEquals(rs.getString(2), "2020-01-01T10:11:12+03:00[Asia/Istanbul]"); assertEquals(rs.getString("datetime"), "2020-01-01T10:11:12+03:00[Asia/Istanbul]"); assertFalse(rs.next()); @@ -250,13 +243,13 @@ public void testExecuteUpdateDates() throws Exception { assertEquals(stmt.executeUpdate("INSERT INTO " + getDatabase() + ".dates VALUES (0, '2020-01-01', '2020-01-01 10:11:12'), (1, NULL, '2020-01-01 12:10:07'), (2, '2020-01-01', NULL)"), 3); try (ResultSet rs = stmt.executeQuery("SELECT date, datetime FROM " + getDatabase() + ".dates ORDER BY id")) { assertTrue(rs.next()); - assertEquals(rs.getDate(1).toString(), "2020-01-01"); - assertEquals(rs.getDate(2).toString(), "2020-01-01"); + assertEquals(rs.getDate(1, new GregorianCalendar()).toString(), "2020-01-01"); + assertEquals(rs.getDate(2, new GregorianCalendar()).toString(), "2020-01-01"); assertTrue(rs.next()); assertNull(rs.getDate(1)); - assertEquals(rs.getDate(2).toString(), "2020-01-01"); + assertEquals(rs.getDate(2, new GregorianCalendar()).toString(), "2020-01-01"); assertTrue(rs.next()); - assertEquals(rs.getDate(1).toString(), "2020-01-01"); + assertEquals(rs.getDate(1, new GregorianCalendar()).toString(), "2020-01-01"); assertNull(rs.getDate(2)); assertFalse(rs.next()); } @@ -306,7 +299,7 @@ public void testJdbcEscapeSyntax() throws Exception { "toInt32({fn LENGTH('Hello')}) AS FNLENGTH, toInt32({fn POSITION('Hello', 'l')}) AS FNPOSITION, toInt32({fn MOD(10, 3)}) AS FNMOD, " + "{fn SQRT(9)} AS FNSQRT, {fn SUBSTRING('Hello', 3, 2)} AS FNSUBSTRING")) { assertTrue(rs.next()); - assertEquals(rs.getDate(1), Date.valueOf(LocalDate.of(2021, 11, 1))); + assertEquals(rs.getDate(1, new GregorianCalendar()), Date.valueOf(LocalDate.of(2021, 11, 1))); //assertEquals(rs.getTimestamp(2), java.sql.Timestamp.valueOf(LocalDateTime.of(2021, 11, 1, 12, 34, 56))); assertEquals(rs.getInt(3), 1); assertEquals(rs.getInt("FNABS"), 1);