/** * Converts a LocalTime to a Value. * * @param localTime the LocalTime to convert, not {@code null} * @return the value */ public static Value localTimeToTimeValue(Object localTime) { try { return ValueTime.fromNanos((Long) LOCAL_TIME_TO_NANO.invoke(localTime)); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "time conversion failed"); } }
/** * Converts a value to a Instant. * * <p>This method should only called from Java 8 or later.</p> * * @param value the value to convert * @return the Instant */ public static Object valueToInstant(Value value) { try { return TIMESTAMP_TO_INSTANT.invoke(value.getTimestamp()); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "timestamp conversion failed"); } }
/** * Converts a value to a LocalTime. * * <p>This method should only called from Java 8 or later.</p> * * @param value the value to convert * @return the LocalTime */ public static Object valueToLocalTime(Value value) { try { return LOCAL_TIME_OF_NANO.invoke(null, ((ValueTime) value.convertTo(Value.TIME)).getNanos()); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "time conversion failed"); } }
/** * Converts a LocalDate to a Value. * * @param localDate the LocalDate to convert, not {@code null} * @return the value */ public static Value localDateToDateValue(Object localDate) { try { return ValueDate.fromDateValue(dateValueFromLocalDate(localDate)); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "date conversion failed"); } }
/** * Converts a OffsetDateTime to a Value. * * @param offsetDateTime the OffsetDateTime to convert, not {@code null} * @return the value */ public static Value offsetDateTimeToValue(Object offsetDateTime) { try { Object localDateTime = OFFSET_DATE_TIME_TO_LOCAL_DATE_TIME.invoke(offsetDateTime); Object localDate = LOCAL_DATE_TIME_TO_LOCAL_DATE.invoke(localDateTime); Object zoneOffset = OFFSET_DATE_TIME_GET_OFFSET.invoke(offsetDateTime); long dateValue = dateValueFromLocalDate(localDate); long timeNanos = timeNanosFromLocalDateTime(localDateTime); short timeZoneOffsetMins = zoneOffsetToOffsetMinute(zoneOffset); return ValueTimestampTimeZone.fromDateValueAndNanos(dateValue, timeNanos, timeZoneOffsetMins); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "time conversion failed"); } }
/** * Converts a LocalDateTime to a Value. * * @param localDateTime the LocalDateTime to convert, not {@code null} * @return the value */ public static Value localDateTimeToValue(Object localDateTime) { try { Object localDate = LOCAL_DATE_TIME_TO_LOCAL_DATE.invoke(localDateTime); long dateValue = dateValueFromLocalDate(localDate); long timeNanos = timeNanosFromLocalDateTime(localDateTime); return ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "local date time conversion failed"); } }
/** * Convert a throwable to an SQL exception using the default mapping. All * errors except the following are re-thrown: StackOverflowError, * LinkageError. * * @param e the root cause * @return the exception object */ public static DbException convert(Throwable e) { if (e instanceof DbException) { return (DbException) e; } else if (e instanceof SQLException) { return new DbException((SQLException) e); } else if (e instanceof InvocationTargetException) { return convertInvocation((InvocationTargetException) e, null); } else if (e instanceof IOException) { return get(ErrorCode.IO_EXCEPTION_1, e, e.toString()); } else if (e instanceof OutOfMemoryError) { return get(ErrorCode.OUT_OF_MEMORY, e); } else if (e instanceof StackOverflowError || e instanceof LinkageError) { return get(ErrorCode.GENERAL_ERROR_1, e, e.toString()); } else if (e instanceof Error) { throw (Error) e; } return get(ErrorCode.GENERAL_ERROR_1, e, e.toString()); }
/** * Converts a value to a OffsetDateTime. * * <p>This method should only called from Java 8 or later.</p> * * @param value the value to convert * @return the OffsetDateTime */ public static Object valueToOffsetDateTime(Value value) { ValueTimestampTimeZone valueTimestampTimeZone = (ValueTimestampTimeZone) value.convertTo(Value.TIMESTAMP_TZ); long dateValue = valueTimestampTimeZone.getDateValue(); long timeNanos = valueTimestampTimeZone.getTimeNanos(); try { Object localDateTime = localDateTimeFromDateNanos(dateValue, timeNanos); short timeZoneOffsetMins = valueTimestampTimeZone.getTimeZoneOffsetMins(); int offsetSeconds = (int) TimeUnit.MINUTES.toSeconds(timeZoneOffsetMins); Object offset = ZONE_OFFSET_OF_TOTAL_SECONDS.invoke(null, offsetSeconds); return OFFSET_DATE_TIME_OF_LOCAL_DATE_TIME_ZONE_OFFSET.invoke(null, localDateTime, offset); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "timestamp with time zone conversion failed"); } }
/** * Converts a value to a LocalDate. * * <p>This method should only called from Java 8 or later.</p> * * @param value the value to convert * @return the LocalDate */ public static Object valueToLocalDate(Value value) { try { return localDateFromDateValue(((ValueDate) value.convertTo(Value.DATE)).getDateValue()); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "date conversion failed"); } }
/** * Converts a value to a LocalDateTime. * * <p>This method should only called from Java 8 or later.</p> * * @param value the value to convert * @return the LocalDateTime */ public static Object valueToLocalDateTime(Value value) { ValueTimestamp valueTimestamp = (ValueTimestamp) value.convertTo(Value.TIMESTAMP); long dateValue = valueTimestamp.getDateValue(); long timeNanos = valueTimestamp.getTimeNanos(); try { return localDateTimeFromDateNanos(dateValue, timeNanos); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "timestamp conversion failed"); } }
throw DbException.convertInvocation(e, buff.toString()); } catch (Exception e) { throw DbException.convert(e);
private static Object dateToLocalDate(Date date) { try { return TO_LOCAL_DATE.invoke(date); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "date conversion failed"); } }
/** * Converts a LocalTime to a Value. * * @param localTime the LocalTime to convert, not {@code null} * @return the value */ public static Value localTimeToTimeValue(Object localTime) { try { return ValueTime.fromNanos((Long) LOCAL_TIME_TO_NANO.invoke(localTime)); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "time conversion failed"); } }
/** * Converts a LocalDate to a Value. * * @param localDate the LocalDate to convert, not {@code null} * @return the value */ public static Value localDateToDateValue(Object localDate) { try { Date date = (Date) DATE_VALUE_OF.invoke(null, localDate); return ValueDate.get(date); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "date conversion failed"); } }
/** * Converts a value to a LocalTime. * * <p>This method should only called from Java 8 or later.</p> * * @param value the value to convert * @return the LocalTime */ public static Object valueToLocalTime(Value value) { try { return LOCAL_TIME_OF_NANO.invoke(null, ((ValueTime) value.convertTo(Value.TIME)).getNanos()); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "time conversion failed"); } }
private static Object timestampWithTimeZoneToOffsetDateTime( TimestampWithTimeZone timestampWithTimeZone) { long dateValue = timestampWithTimeZone.getYMD(); long timeNanos = timestampWithTimeZone.getNanosSinceMidnight(); try { Object localDateTime = localDateTimeFromDateNanos(dateValue, timeNanos); short timeZoneOffsetMins = timestampWithTimeZone.getTimeZoneOffsetMins(); int offsetSeconds = (int) TimeUnit.MINUTES.toSeconds(timeZoneOffsetMins); Object offset = ZONE_OFFSET_OF_TOTAL_SECONDS.invoke(null, offsetSeconds); return OFFSET_DATE_TIME_OF_LOCAL_DATE_TIME_ZONE_OFFSET.invoke(null, localDateTime, offset); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "timestamp with time zone conversion failed"); } }
/** * Converts a value to a LocalDateTime. * * <p>This method should only called from Java 8 or later.</p> * * @param value the value to convert * @return the LocalDateTime */ public static Object valueToLocalDateTime(ValueTimestamp value) { long dateValue = value.getDateValue(); long timeNanos = value.getTimeNanos(); try { Object localDate = localDateFromDateValue(dateValue); Object localDateTime = LOCAL_DATE_AT_START_OF_DAY.invoke(localDate); return LOCAL_DATE_TIME_PLUS_NANOS.invoke(localDateTime, timeNanos); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "timestamp conversion failed"); } }
/** * Converts a OffsetDateTime to a Value. * * @param offsetDateTime the OffsetDateTime to convert, not {@code null} * @return the value */ public static Value offsetDateTimeToValue(Object offsetDateTime) { try { Object localDateTime = OFFSET_DATE_TIME_TO_LOCAL_DATE_TIME.invoke(offsetDateTime); Object localDate = LOCAL_DATE_TIME_TO_LOCAL_DATE.invoke(localDateTime); Object zoneOffset = OFFSET_DATE_TIME_GET_OFFSET.invoke(offsetDateTime); long dateValue = dateValueFromLocalDate(localDate); long timeNanos = timeNanosFromLocalDate(localDateTime); short timeZoneOffsetMins = zoneOffsetToOffsetMinute(zoneOffset); return ValueTimestampTimeZone.fromDateValueAndNanos(dateValue, timeNanos, timeZoneOffsetMins); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "time conversion failed"); } }
/** * Converts a LocalDateTime to a Value. * * @param localDateTime the LocalDateTime to convert, not {@code null} * @return the value */ public static Value localDateTimeToValue(Object localDateTime) { try { Object localDate = LOCAL_DATE_TIME_TO_LOCAL_DATE.invoke(localDateTime); long dateValue = dateValueFromLocalDate(localDate); long timeNanos = timeNanosFromLocalDate(localDateTime); return ValueTimestamp.fromDateValueAndNanos(dateValue, timeNanos); } catch (IllegalAccessException e) { throw DbException.convert(e); } catch (InvocationTargetException e) { throw DbException.convertInvocation(e, "local date time conversion failed"); } }
/** * Convert a throwable to an SQL exception using the default mapping. All * errors except the following are re-thrown: StackOverflowError, * LinkageError. * * @param e the root cause * @return the exception object */ public static DbException convert(Throwable e) { if (e instanceof DbException) { return (DbException) e; } else if (e instanceof SQLException) { return new DbException((SQLException) e); } else if (e instanceof InvocationTargetException) { return convertInvocation((InvocationTargetException) e, null); } else if (e instanceof IOException) { return get(ErrorCode.IO_EXCEPTION_1, e, e.toString()); } else if (e instanceof OutOfMemoryError) { return get(ErrorCode.OUT_OF_MEMORY, e); } else if (e instanceof StackOverflowError || e instanceof LinkageError) { return get(ErrorCode.GENERAL_ERROR_1, e, e.toString()); } else if (e instanceof Error) { throw (Error) e; } return get(ErrorCode.GENERAL_ERROR_1, e, e.toString()); }