Skip to content

Commit e46aac9

Browse files
author
Paultagoras
committed
Tweaking handling
1 parent 94821de commit e46aac9

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,10 +1053,10 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
10531053
wasNull = false;
10541054

10551055
if (cal == null) {
1056-
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
1056+
cal = new GregorianCalendar();
10571057
}
10581058

1059-
return new Date(zdt.withZoneSameLocal(cal.getTimeZone().toZoneId()).toInstant().toEpochMilli());//This assumes the response is in cal.getTimeZone()
1059+
return Date.valueOf(zdt.withZoneSameInstant(cal.getTimeZone().toZoneId()).toLocalDate());
10601060
} catch (Exception e) {
10611061
throw ExceptionUtils.toSqlState(String.format("Method: getDate(\"%s\") encountered an exception.", columnLabel), String.format("SQL: [%s]", parentStatement.getLastSql()), e);
10621062
}
@@ -1079,10 +1079,10 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
10791079
wasNull = false;
10801080

10811081
if (cal == null) {
1082-
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
1082+
cal = new GregorianCalendar();
10831083
}
10841084

1085-
return new Time(zdt.withZoneSameLocal(cal.getTimeZone().toZoneId()).toInstant().toEpochMilli());//This assumes the response is in cal.getTimeZone()
1085+
return Time.valueOf(zdt.withZoneSameInstant(cal.getTimeZone().toZoneId()).toLocalTime());
10861086
} catch (Exception e) {
10871087
throw ExceptionUtils.toSqlState(String.format("Method: getTime(\"%s\") encountered an exception.", columnLabel), String.format("SQL: [%s]", parentStatement.getLastSql()), e);
10881088
}
@@ -1105,10 +1105,10 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept
11051105
wasNull = false;
11061106

11071107
if (cal == null) {
1108-
cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
1108+
cal = new GregorianCalendar();
11091109
}
11101110

1111-
Timestamp ts = new Timestamp(zdt.withZoneSameLocal(cal.getTimeZone().toZoneId()).toInstant().toEpochMilli());
1111+
Timestamp ts = Timestamp.valueOf(zdt.withZoneSameInstant(cal.getTimeZone().toZoneId()).toLocalDateTime());
11121112
ts.setNanos(zdt.getNano());
11131113
return ts;//This assumes the response is in cal.getTimeZone()
11141114
} catch (Exception e) {

jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,26 +281,26 @@ public void testDateTypes() throws SQLException {
281281
try (Statement stmt = conn.createStatement()) {
282282
try (ResultSet rs = stmt.executeQuery("SELECT * FROM test_dates ORDER BY order")) {
283283
assertTrue(rs.next());
284-
assertEquals(rs.getDate("date", new GregorianCalendar()), Date.valueOf("1970-01-01"));
285-
assertEquals(rs.getDate("date32", new GregorianCalendar()), Date.valueOf("1970-01-01"));
284+
assertEquals(rs.getDate("date", new GregorianCalendar(TimeZone.getTimeZone("UTC"))), Date.valueOf("1970-01-01"));
285+
assertEquals(rs.getDate("date32", new GregorianCalendar(TimeZone.getTimeZone("UTC"))), Date.valueOf("1970-01-01"));
286286
assertEquals(rs.getTimestamp("dateTime").toInstant().toString(), "1970-01-01T00:00:00Z");
287287
assertEquals(rs.getTimestamp("dateTime32").toInstant().toString(), "1970-01-01T00:00:00Z");
288288
assertEquals(rs.getTimestamp("dateTime643").toInstant().toString(), "1970-01-01T00:00:00Z");
289289
assertEquals(rs.getTimestamp("dateTime646").toInstant().toString(), "1970-01-01T00:00:00Z");
290290
assertEquals(rs.getTimestamp("dateTime649").toInstant().toString(), "1970-01-01T00:00:00Z");
291291

292292
assertTrue(rs.next());
293-
assertEquals(rs.getDate("date", new GregorianCalendar()), Date.valueOf("2149-06-06"));
294-
assertEquals(rs.getDate("date32", new GregorianCalendar()), Date.valueOf("2299-12-31"));
293+
assertEquals(rs.getDate("date", new GregorianCalendar(TimeZone.getTimeZone("UTC"))), Date.valueOf("2149-06-06"));
294+
assertEquals(rs.getDate("date32", new GregorianCalendar(TimeZone.getTimeZone("UTC"))), Date.valueOf("2299-12-31"));
295295
assertEquals(rs.getTimestamp("dateTime").toInstant().toString(), "2106-02-07T06:28:15Z");
296296
assertEquals(rs.getTimestamp("dateTime32").toInstant().toString(), "2106-02-07T06:28:15Z");
297297
assertEquals(rs.getTimestamp("dateTime643").toInstant().toString(), "2261-12-31T23:59:59.999Z");
298298
assertEquals(rs.getTimestamp("dateTime646").toInstant().toString(), "2261-12-31T23:59:59.999999Z");
299299
assertEquals(rs.getTimestamp("dateTime649").toInstant().toString(), "2261-12-31T23:59:59.999999999Z");
300300

301301
assertTrue(rs.next());
302-
assertEquals(rs.getDate("date", new GregorianCalendar()).toString(), date.toString());
303-
assertEquals(rs.getDate("date32", new GregorianCalendar()).toString(), date32.toString());
302+
assertEquals(rs.getDate("date", new GregorianCalendar(TimeZone.getTimeZone("UTC"))).toString(), date.toString());
303+
assertEquals(rs.getDate("date32", new GregorianCalendar(TimeZone.getTimeZone("UTC"))).toString(), date32.toString());
304304
assertEquals(rs.getTimestamp("dateTime").toString(), dateTime.toString());
305305
assertEquals(rs.getTimestamp("dateTime32").toString(), dateTime32.toString());
306306
assertEquals(rs.getTimestamp("dateTime643").toString(), dateTime643.toString());
@@ -887,7 +887,7 @@ public void testTypeConversions() throws Exception {
887887
assertEquals(rs.getObject(3, Double.class), 1.0);
888888
assertEquals(String.valueOf(rs.getObject(3, new HashMap<String, Class<?>>(){{put(JDBCType.FLOAT.getName(), Float.class);}})), "1.0");
889889

890-
assertEquals(rs.getDate(4, new GregorianCalendar()), Date.valueOf("2024-12-01"));
890+
assertEquals(rs.getDate(4, new GregorianCalendar(TimeZone.getTimeZone("UTC"))), Date.valueOf("2024-12-01"));
891891
assertTrue(rs.getObject(4) instanceof Date);
892892
assertEquals(rs.getObject(4), Date.valueOf("2024-12-01"));
893893
assertEquals(rs.getString(4), "2024-12-01");//Underlying object is ZonedDateTime

jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Arrays;
1515
import java.util.Calendar;
1616
import java.util.GregorianCalendar;
17+
import java.util.TimeZone;
1718

1819
import static org.testng.Assert.assertEquals;
1920
import static org.testng.Assert.assertFalse;
@@ -172,7 +173,7 @@ public void testSetDate() throws Exception {
172173
stmt.setDate(1, java.sql.Date.valueOf("2021-01-01"));
173174
try (ResultSet rs = stmt.executeQuery()) {
174175
assertTrue(rs.next());
175-
assertEquals(rs.getDate(1, new GregorianCalendar()), java.sql.Date.valueOf("2021-01-01"));
176+
assertEquals(rs.getDate(1, new GregorianCalendar(TimeZone.getTimeZone("UTC"))), java.sql.Date.valueOf("2021-01-01"));
176177
assertFalse(rs.next());
177178
}
178179
}
@@ -202,7 +203,7 @@ public void testSetTimestamp() throws Exception {
202203
try (ResultSet rs = stmt.executeQuery()) {
203204
assertTrue(rs.next());
204205
assertEquals(rs.getTimestamp(1).toString(), "2021-01-01 01:34:56.456");
205-
assertEquals(rs.getTimestamp(2, new GregorianCalendar()).toString(), "2021-01-01 01:34:56.456");
206+
assertEquals(rs.getTimestamp(2, java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"))).toString(), "2021-01-01 01:34:56.456");
206207
assertFalse(rs.next());
207208
}
208209
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.GregorianCalendar;
2525
import java.util.List;
2626
import java.util.Properties;
27+
import java.util.TimeZone;
2728
import java.util.UUID;
2829
import java.util.concurrent.CountDownLatch;
2930
import java.util.concurrent.atomic.AtomicReference;
@@ -126,12 +127,12 @@ public void testExecuteQueryDates() throws Exception {
126127
try (Statement stmt = conn.createStatement()) {
127128
try (ResultSet rs = stmt.executeQuery("SELECT toDate('2020-01-01') AS date, toDateTime('2020-01-01 10:11:12', 'Asia/Istanbul') AS datetime")) {
128129
assertTrue(rs.next());
129-
assertEquals(rs.getDate(1, new GregorianCalendar()).toString(), Date.valueOf("2020-01-01").toString());
130-
assertEquals(rs.getDate("date", new GregorianCalendar()).toString(), Date.valueOf("2020-01-01").toString());
130+
assertEquals(rs.getDate(1, new GregorianCalendar(TimeZone.getTimeZone("UTC"))).toString(), Date.valueOf("2020-01-01").toString());
131+
assertEquals(rs.getDate("date", new GregorianCalendar(TimeZone.getTimeZone("UTC"))).toString(), Date.valueOf("2020-01-01").toString());
131132
assertEquals(rs.getString(1), "2020-01-01");
132133
assertEquals(rs.getString("date"), "2020-01-01");
133-
assertEquals(rs.getDate(2, new GregorianCalendar()).toString(), "2020-01-01");
134-
assertEquals(rs.getDate("datetime", new GregorianCalendar()).toString(), "2020-01-01");
134+
assertEquals(rs.getDate(2, new GregorianCalendar(TimeZone.getTimeZone("UTC"))).toString(), "2020-01-01");
135+
assertEquals(rs.getDate("datetime", new GregorianCalendar(TimeZone.getTimeZone("UTC"))).toString(), "2020-01-01");
135136
assertEquals(rs.getString(2), "2020-01-01T10:11:12+03:00[Asia/Istanbul]");
136137
assertEquals(rs.getString("datetime"), "2020-01-01T10:11:12+03:00[Asia/Istanbul]");
137138
assertFalse(rs.next());
@@ -243,13 +244,13 @@ public void testExecuteUpdateDates() throws Exception {
243244
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);
244245
try (ResultSet rs = stmt.executeQuery("SELECT date, datetime FROM " + getDatabase() + ".dates ORDER BY id")) {
245246
assertTrue(rs.next());
246-
assertEquals(rs.getDate(1, new GregorianCalendar()).toString(), "2020-01-01");
247-
assertEquals(rs.getDate(2, new GregorianCalendar()).toString(), "2020-01-01");
247+
assertEquals(rs.getDate(1, new GregorianCalendar(TimeZone.getTimeZone("UTC"))).toString(), "2020-01-01");
248+
assertEquals(rs.getDate(2, new GregorianCalendar(TimeZone.getTimeZone("UTC"))).toString(), "2020-01-01");
248249
assertTrue(rs.next());
249250
assertNull(rs.getDate(1));
250-
assertEquals(rs.getDate(2, new GregorianCalendar()).toString(), "2020-01-01");
251+
assertEquals(rs.getDate(2).toString(), "2020-01-01");
251252
assertTrue(rs.next());
252-
assertEquals(rs.getDate(1, new GregorianCalendar()).toString(), "2020-01-01");
253+
assertEquals(rs.getDate(1, new GregorianCalendar(TimeZone.getTimeZone("UTC"))).toString(), "2020-01-01");
253254
assertNull(rs.getDate(2));
254255
assertFalse(rs.next());
255256
}
@@ -299,7 +300,7 @@ public void testJdbcEscapeSyntax() throws Exception {
299300
"toInt32({fn LENGTH('Hello')}) AS FNLENGTH, toInt32({fn POSITION('Hello', 'l')}) AS FNPOSITION, toInt32({fn MOD(10, 3)}) AS FNMOD, " +
300301
"{fn SQRT(9)} AS FNSQRT, {fn SUBSTRING('Hello', 3, 2)} AS FNSUBSTRING")) {
301302
assertTrue(rs.next());
302-
assertEquals(rs.getDate(1, new GregorianCalendar()), Date.valueOf(LocalDate.of(2021, 11, 1)));
303+
assertEquals(rs.getDate(1, new GregorianCalendar(TimeZone.getTimeZone("UTC"))), Date.valueOf(LocalDate.of(2021, 11, 1)));
303304
//assertEquals(rs.getTimestamp(2), java.sql.Timestamp.valueOf(LocalDateTime.of(2021, 11, 1, 12, 34, 56)));
304305
assertEquals(rs.getInt(3), 1);
305306
assertEquals(rs.getInt("FNABS"), 1);

0 commit comments

Comments
 (0)