|
28 | 28 | import java.sql.Time; |
29 | 29 | import java.sql.Timestamp; |
30 | 30 | import java.sql.Types; |
| 31 | +import java.time.Instant; |
31 | 32 | import java.time.LocalDate; |
32 | 33 | import java.time.LocalDateTime; |
33 | 34 | import java.time.LocalTime; |
34 | 35 | import java.time.ZoneId; |
| 36 | +import java.time.ZonedDateTime; |
35 | 37 | import java.time.format.DateTimeFormatter; |
36 | 38 | import java.time.format.DateTimeFormatterBuilder; |
37 | 39 | import java.time.temporal.ChronoField; |
38 | 40 | import java.util.Calendar; |
39 | 41 | import java.util.Collection; |
40 | 42 | import java.util.GregorianCalendar; |
41 | 43 | import java.util.Map; |
| 44 | +import java.util.TimeZone; |
42 | 45 |
|
43 | 46 | public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcV2Wrapper { |
44 | 47 | private static final Logger LOG = LoggerFactory.getLogger(PreparedStatementImpl.class); |
@@ -159,19 +162,16 @@ public void setBytes(int parameterIndex, byte[] x) throws SQLException { |
159 | 162 |
|
160 | 163 | @Override |
161 | 164 | public void setDate(int parameterIndex, Date x) throws SQLException { |
162 | | - checkClosed(); |
163 | 165 | setDate(parameterIndex, x, null); |
164 | 166 | } |
165 | 167 |
|
166 | 168 | @Override |
167 | 169 | public void setTime(int parameterIndex, Time x) throws SQLException { |
168 | | - checkClosed(); |
169 | 170 | setTime(parameterIndex, x, null); |
170 | 171 | } |
171 | 172 |
|
172 | 173 | @Override |
173 | 174 | public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { |
174 | | - checkClosed(); |
175 | 175 | setTimestamp(parameterIndex, x, null); |
176 | 176 | } |
177 | 177 |
|
@@ -269,42 +269,42 @@ public ResultSetMetaData getMetaData() throws SQLException { |
269 | 269 | public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { |
270 | 270 | checkClosed(); |
271 | 271 | if (cal == null) { |
272 | | - cal = new GregorianCalendar(); |
273 | | - cal.setTime(x); |
| 272 | + cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));//This says whatever date is in UTC |
274 | 273 | } |
275 | 274 |
|
276 | | - ZoneId tz = cal.getTimeZone().toZoneId(); |
| 275 | + LocalDate d = x.toLocalDate(); |
277 | 276 | Calendar c = (Calendar) cal.clone(); |
278 | | - c.setTime(x); |
279 | | - parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(tz).toLocalDate()); |
| 277 | + c.clear(); |
| 278 | + c.set(d.getYear(), d.getMonthValue() - 1, d.getDayOfMonth(), 0, 0, 0); |
| 279 | + parameters[parameterIndex - 1] = encodeObject(c.toInstant()); |
280 | 280 | } |
281 | 281 |
|
282 | 282 | @Override |
283 | 283 | public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { |
284 | 284 | checkClosed(); |
285 | 285 | if (cal == null) { |
286 | | - cal = new GregorianCalendar(); |
287 | | - cal.setTime(x); |
| 286 | + cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); |
288 | 287 | } |
289 | 288 |
|
290 | | - ZoneId tz = cal.getTimeZone().toZoneId(); |
| 289 | + LocalTime t = x.toLocalTime(); |
291 | 290 | Calendar c = (Calendar) cal.clone(); |
292 | | - c.setTime(x); |
293 | | - parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(tz).toLocalTime()); |
| 291 | + c.clear(); |
| 292 | + c.set(1970, Calendar.JANUARY, 1, t.getHour(), t.getMinute(), t.getSecond()); |
| 293 | + parameters[parameterIndex - 1] = encodeObject(c.toInstant()); |
294 | 294 | } |
295 | 295 |
|
296 | 296 | @Override |
297 | 297 | public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { |
298 | 298 | checkClosed(); |
299 | 299 | if (cal == null) { |
300 | | - cal = new GregorianCalendar(); |
301 | | - cal.setTime(x); |
| 300 | + cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); |
302 | 301 | } |
303 | 302 |
|
304 | | - ZoneId tz = cal.getTimeZone().toZoneId(); |
| 303 | + LocalDateTime ldt = x.toLocalDateTime(); |
305 | 304 | Calendar c = (Calendar) cal.clone(); |
306 | | - c.setTime(x); |
307 | | - parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(tz).withNano(x.getNanos()).toLocalDateTime()); |
| 305 | + c.clear(); |
| 306 | + c.set(ldt.getYear(), ldt.getMonthValue() - 1, ldt.getDayOfMonth(), ldt.getHour(), ldt.getMinute(), ldt.getSecond()); |
| 307 | + parameters[parameterIndex - 1] = encodeObject(c.toInstant().atZone(ZoneId.of("UTC")).withNano(x.getNanos())); |
308 | 308 | } |
309 | 309 |
|
310 | 310 | @Override |
@@ -479,6 +479,10 @@ private static String encodeObject(Object x) throws SQLException { |
479 | 479 | return "'" + DATETIME_FORMATTER.format(((Timestamp) x).toLocalDateTime()) + "'"; |
480 | 480 | } else if (x instanceof LocalDateTime) { |
481 | 481 | return "'" + DATETIME_FORMATTER.format((LocalDateTime) x) + "'"; |
| 482 | + } else if (x instanceof ZonedDateTime) { |
| 483 | + return encodeObject(((ZonedDateTime) x).toInstant()); |
| 484 | + } else if (x instanceof Instant) { |
| 485 | + return "fromUnixTimestamp64Nano(" + (((Instant) x).getEpochSecond() * 1_000_000_000L + ((Instant) x).getNano())+ ")"; |
482 | 486 | } else if (x instanceof Array) { |
483 | 487 | StringBuilder listString = new StringBuilder(); |
484 | 488 | listString.append("["); |
@@ -571,6 +575,7 @@ private static String encodeObject(Object x) throws SQLException { |
571 | 575 | } |
572 | 576 | } |
573 | 577 |
|
| 578 | + |
574 | 579 | private static String escapeString(String x) { |
575 | 580 | return x.replace("\\", "\\\\").replace("'", "\\'");//Escape single quotes |
576 | 581 | } |
|
0 commit comments