1111import java .sql .*;
1212import java .time .LocalDate ;
1313import java .time .LocalDateTime ;
14+ import java .time .ZoneId ;
15+ import java .time .ZonedDateTime ;
1416import java .util .Calendar ;
1517import java .util .GregorianCalendar ;
1618import java .util .Map ;
@@ -179,12 +181,12 @@ public byte[] getBytes(int columnIndex) throws SQLException {
179181
180182 @ Override
181183 public Date getDate (int columnIndex ) throws SQLException {
182- return getDate (columnIndexToName ( columnIndex ) );
184+ return getDate (columnIndex , null );
183185 }
184186
185187 @ Override
186188 public Time getTime (int columnIndex ) throws SQLException {
187- return getTime (columnIndexToName ( columnIndex ) );
189+ return getTime (columnIndex , null );
188190 }
189191
190192 @ Override
@@ -369,37 +371,12 @@ public byte[] getBytes(String columnLabel) throws SQLException {
369371
370372 @ Override
371373 public Date getDate (String columnLabel ) throws SQLException {
372- checkClosed ();
373- try {
374- //TODO: Add this to ClickHouseBinaryFormatReader
375- LocalDate localDate = reader .getLocalDate (columnLabel );
376- if (localDate == null ) {
377- wasNull = true ;
378- return null ;
379- }
380-
381- wasNull = false ;
382- return Date .valueOf (localDate );
383- } catch (Exception e ) {
384- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getDate(%s)" , parentStatement .getLastSql (), columnLabel ), e );
385- }
374+ return getDate (columnLabel , null );
386375 }
387376
388377 @ Override
389378 public Time getTime (String columnLabel ) throws SQLException {
390- checkClosed ();
391- try {
392- LocalDateTime localDateTime = reader .getLocalDateTime (columnLabel );
393- if (localDateTime == null ) {
394- wasNull = true ;
395- return null ;
396- }
397-
398- wasNull = false ;
399- return Time .valueOf (localDateTime .toLocalTime ());
400- } catch (Exception e ) {
401- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getTime(%s)" , parentStatement .getLastSql (), columnLabel ), e );
402- }
379+ return getTime (columnLabel , null );
403380 }
404381
405382 @ Override
@@ -1068,15 +1045,18 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException {
10681045 @ Override
10691046 public Date getDate (String columnLabel , Calendar cal ) throws SQLException {
10701047 checkClosed ();
1071- Date date = getDate (columnLabel );
1072- if (date == null ) {
1073- return null ;
1048+ try {
1049+ ZonedDateTime zdt = reader .getZonedDateTime (columnLabel );
1050+ if (zdt == null ) {
1051+ wasNull = true ;
1052+ return null ;
1053+ }
1054+ wasNull = false ;
1055+ ZoneId zoneId = cal != null ? cal .getTimeZone ().toZoneId () : zdt .getZone ();
1056+ return Date .valueOf (zdt .withZoneSameInstant (zoneId ).toLocalDate ());
1057+ } catch (Exception e ) {
1058+ throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getDate(%s, %s)" , parentStatement .getLastSql (), columnLabel , cal ), e );
10741059 }
1075- LocalDate d = date .toLocalDate ();
1076- Calendar c = (Calendar )( cal != null ? cal : defaultCalendar ).clone ();
1077- c .clear ();
1078- c .set (d .getYear (), d .getMonthValue () - 1 , d .getDayOfMonth (), 0 , 0 , 0 );
1079- return new Date (c .getTimeInMillis ());
10801060 }
10811061
10821062 @ Override
@@ -1087,7 +1067,18 @@ public Time getTime(int columnIndex, Calendar cal) throws SQLException {
10871067 @ Override
10881068 public Time getTime (String columnLabel , Calendar cal ) throws SQLException {
10891069 checkClosed ();
1090- return getTime (columnLabel );
1070+ try {
1071+ ZonedDateTime zdt = reader .getZonedDateTime (columnLabel );
1072+ if (zdt == null ) {
1073+ wasNull = true ;
1074+ return null ;
1075+ }
1076+ wasNull = false ;
1077+ ZoneId zoneId = cal != null ? cal .getTimeZone ().toZoneId () : zdt .getZone ();
1078+ return Time .valueOf (zdt .withZoneSameInstant (zoneId ).toLocalTime ());
1079+ } catch (Exception e ) {
1080+ throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getTime(%s, %s)" , parentStatement .getLastSql (), columnLabel , cal ), e );
1081+ }
10911082 }
10921083
10931084 @ Override
@@ -1099,20 +1090,16 @@ public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException
10991090 public Timestamp getTimestamp (String columnLabel , Calendar cal ) throws SQLException {
11001091 checkClosed ();
11011092 try {
1102- LocalDateTime localDateTime = reader .getLocalDateTime (columnLabel );
1103- if (localDateTime == null ) {
1093+ ZonedDateTime zdt = reader .getZonedDateTime (columnLabel );
1094+ ZoneId zoneId = cal != null ? cal .getTimeZone ().toZoneId () : zdt .getZone ();
1095+ if (zdt == null ) {
11041096 wasNull = true ;
11051097 return null ;
11061098 }
1107- Calendar c = (Calendar ) (cal != null ? cal : defaultCalendar ).clone ();
1108- c .set (localDateTime .getYear (), localDateTime .getMonthValue () - 1 , localDateTime .getDayOfMonth (), localDateTime .getHour (), localDateTime .getMinute (),
1109- localDateTime .getSecond ());
1110- Timestamp timestamp = new Timestamp (c .getTimeInMillis ());
1111- timestamp .setNanos (localDateTime .getNano ());
11121099 wasNull = false ;
1113- return timestamp ;
1100+ return Timestamp . valueOf ( zdt . withZoneSameInstant ( zoneId ). toLocalDateTime ()) ;
11141101 } catch (Exception e ) {
1115- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getTimestamp(%s)" , parentStatement .getLastSql (), columnLabel ), e );
1102+ throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getTimestamp(%s, %s )" , parentStatement .getLastSql (), columnLabel , cal ), e );
11161103 }
11171104
11181105 }
0 commit comments