Search in sources :

Example 6 with ValueTimestampTimeZone

use of org.h2.value.ValueTimestampTimeZone in project h2database by h2database.

the class LocalDateTimeUtils method valueToOffsetDateTime.

/**
 * 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");
    }
}
Also used : ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with ValueTimestampTimeZone

use of org.h2.value.ValueTimestampTimeZone in project h2database by h2database.

the class DateTimeFunctions method getIntDatePart.

/**
 * Get the specified field of a date, however with years normalized to positive
 * or negative, and month starting with 1.
 *
 * @param date
 *            the date value
 * @param field
 *            the field type, see {@link Function} for constants
 * @return the value
 */
public static int getIntDatePart(Value date, int field) {
    long[] a = DateTimeUtils.dateAndTimeFromValue(date);
    long dateValue = a[0];
    long timeNanos = a[1];
    switch(field) {
        case YEAR:
            return DateTimeUtils.yearFromDateValue(dateValue);
        case MONTH:
            return DateTimeUtils.monthFromDateValue(dateValue);
        case DAY_OF_MONTH:
            return DateTimeUtils.dayFromDateValue(dateValue);
        case HOUR:
            return (int) (timeNanos / 3_600_000_000_000L % 24);
        case MINUTE:
            return (int) (timeNanos / 60_000_000_000L % 60);
        case SECOND:
            return (int) (timeNanos / 1_000_000_000 % 60);
        case MILLISECOND:
            return (int) (timeNanos / 1_000_000 % 1_000);
        case MICROSECOND:
            return (int) (timeNanos / 1_000 % 1_000_000);
        case NANOSECOND:
            return (int) (timeNanos % 1_000_000_000);
        case DAY_OF_YEAR:
            return DateTimeUtils.getDayOfYear(dateValue);
        case DAY_OF_WEEK:
            return DateTimeUtils.getSundayDayOfWeek(dateValue);
        case WEEK:
            GregorianCalendar gc = DateTimeUtils.getCalendar();
            return DateTimeUtils.getWeekOfYear(dateValue, gc.getFirstDayOfWeek() - 1, gc.getMinimalDaysInFirstWeek());
        case QUARTER:
            return (DateTimeUtils.monthFromDateValue(dateValue) - 1) / 3 + 1;
        case ISO_YEAR:
            return DateTimeUtils.getIsoWeekYear(dateValue);
        case ISO_WEEK:
            return DateTimeUtils.getIsoWeekOfYear(dateValue);
        case ISO_DAY_OF_WEEK:
            return DateTimeUtils.getIsoDayOfWeek(dateValue);
        case TIMEZONE_HOUR:
        case TIMEZONE_MINUTE:
            {
                int offsetMinutes;
                if (date instanceof ValueTimestampTimeZone) {
                    offsetMinutes = ((ValueTimestampTimeZone) date).getTimeZoneOffsetMins();
                } else {
                    offsetMinutes = DateTimeUtils.getTimeZoneOffsetMillis(null, dateValue, timeNanos);
                }
                if (field == TIMEZONE_HOUR) {
                    return offsetMinutes / 60;
                }
                return offsetMinutes % 60;
            }
    }
    throw DbException.getUnsupportedException("getDatePart(" + date + ", " + field + ')');
}
Also used : GregorianCalendar(java.util.GregorianCalendar) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone)

Example 8 with ValueTimestampTimeZone

use of org.h2.value.ValueTimestampTimeZone in project h2database by h2database.

the class Data method writeValue.

/**
 * Append a value.
 *
 * @param v the value
 */
public void writeValue(Value v) {
    int start = pos;
    if (v == ValueNull.INSTANCE) {
        data[pos++] = 0;
        return;
    }
    int type = v.getType();
    switch(type) {
        case Value.BOOLEAN:
            writeByte((byte) (v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE));
            break;
        case Value.BYTE:
            writeByte((byte) type);
            writeByte(v.getByte());
            break;
        case Value.SHORT:
            writeByte((byte) type);
            writeShortInt(v.getShort());
            break;
        case Value.ENUM:
        case Value.INT:
            {
                int x = v.getInt();
                if (x < 0) {
                    writeByte((byte) INT_NEG);
                    writeVarInt(-x);
                } else if (x < 16) {
                    writeByte((byte) (INT_0_15 + x));
                } else {
                    writeByte((byte) type);
                    writeVarInt(x);
                }
                break;
            }
        case Value.LONG:
            {
                long x = v.getLong();
                if (x < 0) {
                    writeByte((byte) LONG_NEG);
                    writeVarLong(-x);
                } else if (x < 8) {
                    writeByte((byte) (LONG_0_7 + x));
                } else {
                    writeByte((byte) type);
                    writeVarLong(x);
                }
                break;
            }
        case Value.DECIMAL:
            {
                BigDecimal x = v.getBigDecimal();
                if (BigDecimal.ZERO.equals(x)) {
                    writeByte((byte) DECIMAL_0_1);
                } else if (BigDecimal.ONE.equals(x)) {
                    writeByte((byte) (DECIMAL_0_1 + 1));
                } else {
                    int scale = x.scale();
                    BigInteger b = x.unscaledValue();
                    int bits = b.bitLength();
                    if (bits <= 63) {
                        if (scale == 0) {
                            writeByte((byte) DECIMAL_SMALL_0);
                            writeVarLong(b.longValue());
                        } else {
                            writeByte((byte) DECIMAL_SMALL);
                            writeVarInt(scale);
                            writeVarLong(b.longValue());
                        }
                    } else {
                        writeByte((byte) type);
                        writeVarInt(scale);
                        byte[] bytes = b.toByteArray();
                        writeVarInt(bytes.length);
                        write(bytes, 0, bytes.length);
                    }
                }
                break;
            }
        case Value.TIME:
            if (STORE_LOCAL_TIME) {
                writeByte((byte) LOCAL_TIME);
                ValueTime t = (ValueTime) v;
                long nanos = t.getNanos();
                long millis = nanos / 1_000_000;
                nanos -= millis * 1_000_000;
                writeVarLong(millis);
                writeVarLong(nanos);
            } else {
                writeByte((byte) type);
                writeVarLong(DateTimeUtils.getTimeLocalWithoutDst(v.getTime()));
            }
            break;
        case Value.DATE:
            {
                if (STORE_LOCAL_TIME) {
                    writeByte((byte) LOCAL_DATE);
                    long x = ((ValueDate) v).getDateValue();
                    writeVarLong(x);
                } else {
                    writeByte((byte) type);
                    long x = DateTimeUtils.getTimeLocalWithoutDst(v.getDate());
                    writeVarLong(x / MILLIS_PER_MINUTE);
                }
                break;
            }
        case Value.TIMESTAMP:
            {
                if (STORE_LOCAL_TIME) {
                    writeByte((byte) LOCAL_TIMESTAMP);
                    ValueTimestamp ts = (ValueTimestamp) v;
                    long dateValue = ts.getDateValue();
                    writeVarLong(dateValue);
                    long nanos = ts.getTimeNanos();
                    long millis = nanos / 1_000_000;
                    nanos -= millis * 1_000_000;
                    writeVarLong(millis);
                    writeVarLong(nanos);
                } else {
                    Timestamp ts = v.getTimestamp();
                    writeByte((byte) type);
                    writeVarLong(DateTimeUtils.getTimeLocalWithoutDst(ts));
                    writeVarInt(ts.getNanos() % 1_000_000);
                }
                break;
            }
        case Value.TIMESTAMP_TZ:
            {
                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
                writeByte((byte) type);
                writeVarLong(ts.getDateValue());
                writeVarLong(ts.getTimeNanos());
                writeVarInt(ts.getTimeZoneOffsetMins());
                break;
            }
        case Value.GEOMETRY:
        // fall though
        case Value.JAVA_OBJECT:
            {
                writeByte((byte) type);
                byte[] b = v.getBytesNoCopy();
                int len = b.length;
                writeVarInt(len);
                write(b, 0, len);
                break;
            }
        case Value.BYTES:
            {
                byte[] b = v.getBytesNoCopy();
                int len = b.length;
                if (len < 32) {
                    writeByte((byte) (BYTES_0_31 + len));
                    write(b, 0, len);
                } else {
                    writeByte((byte) type);
                    writeVarInt(len);
                    write(b, 0, len);
                }
                break;
            }
        case Value.UUID:
            {
                writeByte((byte) type);
                ValueUuid uuid = (ValueUuid) v;
                writeLong(uuid.getHigh());
                writeLong(uuid.getLow());
                break;
            }
        case Value.STRING:
            {
                String s = v.getString();
                int len = s.length();
                if (len < 32) {
                    writeByte((byte) (STRING_0_31 + len));
                    writeStringWithoutLength(s, len);
                } else {
                    writeByte((byte) type);
                    writeString(s);
                }
                break;
            }
        case Value.STRING_IGNORECASE:
        case Value.STRING_FIXED:
            writeByte((byte) type);
            writeString(v.getString());
            break;
        case Value.DOUBLE:
            {
                double x = v.getDouble();
                if (x == 1.0d) {
                    writeByte((byte) (DOUBLE_0_1 + 1));
                } else {
                    long d = Double.doubleToLongBits(x);
                    if (d == ValueDouble.ZERO_BITS) {
                        writeByte((byte) DOUBLE_0_1);
                    } else {
                        writeByte((byte) type);
                        writeVarLong(Long.reverse(d));
                    }
                }
                break;
            }
        case Value.FLOAT:
            {
                float x = v.getFloat();
                if (x == 1.0f) {
                    writeByte((byte) (FLOAT_0_1 + 1));
                } else {
                    int f = Float.floatToIntBits(x);
                    if (f == ValueFloat.ZERO_BITS) {
                        writeByte((byte) FLOAT_0_1);
                    } else {
                        writeByte((byte) type);
                        writeVarInt(Integer.reverse(f));
                    }
                }
                break;
            }
        case Value.BLOB:
        case Value.CLOB:
            {
                writeByte((byte) type);
                if (v instanceof ValueLob) {
                    ValueLob lob = (ValueLob) v;
                    lob.convertToFileIfRequired(handler);
                    byte[] small = lob.getSmall();
                    if (small == null) {
                        int t = -1;
                        if (!lob.isLinkedToTable()) {
                            t = -2;
                        }
                        writeVarInt(t);
                        writeVarInt(lob.getTableId());
                        writeVarInt(lob.getObjectId());
                        writeVarLong(lob.getPrecision());
                        writeByte((byte) (lob.isCompressed() ? 1 : 0));
                        if (t == -2) {
                            writeString(lob.getFileName());
                        }
                    } else {
                        writeVarInt(small.length);
                        write(small, 0, small.length);
                    }
                } else {
                    ValueLobDb lob = (ValueLobDb) v;
                    byte[] small = lob.getSmall();
                    if (small == null) {
                        writeVarInt(-3);
                        writeVarInt(lob.getTableId());
                        writeVarLong(lob.getLobId());
                        writeVarLong(lob.getPrecision());
                    } else {
                        writeVarInt(small.length);
                        write(small, 0, small.length);
                    }
                }
                break;
            }
        case Value.ARRAY:
            {
                writeByte((byte) type);
                Value[] list = ((ValueArray) v).getList();
                writeVarInt(list.length);
                for (Value x : list) {
                    writeValue(x);
                }
                break;
            }
        case Value.RESULT_SET:
            {
                writeByte((byte) type);
                try {
                    ResultSet rs = ((ValueResultSet) v).getResultSet();
                    rs.beforeFirst();
                    ResultSetMetaData meta = rs.getMetaData();
                    int columnCount = meta.getColumnCount();
                    writeVarInt(columnCount);
                    for (int i = 0; i < columnCount; i++) {
                        writeString(meta.getColumnName(i + 1));
                        writeVarInt(meta.getColumnType(i + 1));
                        writeVarInt(meta.getPrecision(i + 1));
                        writeVarInt(meta.getScale(i + 1));
                    }
                    while (rs.next()) {
                        writeByte((byte) 1);
                        for (int i = 0; i < columnCount; i++) {
                            int t = DataType.getValueTypeFromResultSet(meta, i + 1);
                            Value val = DataType.readValue(null, rs, i + 1, t);
                            writeValue(val);
                        }
                    }
                    writeByte((byte) 0);
                    rs.beforeFirst();
                } catch (SQLException e) {
                    throw DbException.convert(e);
                }
                break;
            }
        default:
            DbException.throwInternalError("type=" + v.getType());
    }
    if (SysProperties.CHECK2) {
        if (pos - start != getValueLen(v, handler)) {
            throw DbException.throwInternalError("value size error: got " + (pos - start) + " expected " + getValueLen(v, handler));
        }
    }
}
Also used : ValueUuid(org.h2.value.ValueUuid) ValueLob(org.h2.value.ValueLob) ValueLobDb(org.h2.value.ValueLobDb) SQLException(java.sql.SQLException) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueString(org.h2.value.ValueString) Timestamp(java.sql.Timestamp) ValueTimestamp(org.h2.value.ValueTimestamp) BigDecimal(java.math.BigDecimal) ValueTime(org.h2.value.ValueTime) ResultSetMetaData(java.sql.ResultSetMetaData) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) ValueResultSet(org.h2.value.ValueResultSet) BigInteger(java.math.BigInteger)

Example 9 with ValueTimestampTimeZone

use of org.h2.value.ValueTimestampTimeZone in project h2database by h2database.

the class ToDateParser method getResultingValueWithTimeZone.

private ValueTimestampTimeZone getResultingValueWithTimeZone() {
    ValueTimestamp ts = getResultingValue();
    long dateValue = ts.getDateValue();
    short offset;
    if (timeZoneHMValid) {
        offset = (short) (timeZoneHour * 60 + ((timeZoneHour >= 0) ? timeZoneMinute : -timeZoneMinute));
    } else {
        TimeZone timeZone = this.timeZone;
        if (timeZone == null) {
            timeZone = TimeZone.getDefault();
        }
        long millis = DateTimeUtils.convertDateTimeValueToMillis(timeZone, dateValue, nanos / 1_000_000);
        offset = (short) (timeZone.getOffset(millis) / 60_000);
    }
    return ValueTimestampTimeZone.fromDateValueAndNanos(dateValue, ts.getTimeNanos(), offset);
}
Also used : TimeZone(java.util.TimeZone) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueTimestamp(org.h2.value.ValueTimestamp)

Example 10 with ValueTimestampTimeZone

use of org.h2.value.ValueTimestampTimeZone in project h2database by h2database.

the class Value method convertTo.

/**
 * Compare a value to the specified type.
 *
 * @param targetType the type of the returned value
 * @param precision the precision of the column to convert this value to.
 *        The special constant <code>-1</code> is used to indicate that
 *        the precision plays no role when converting the value
 * @param mode the conversion mode
 * @param column the column (if any), used for to improve the error message if conversion fails
 * @param enumerators the ENUM datatype enumerators (if any),
 *        for dealing with ENUM conversions
 * @return the converted value
 */
public Value convertTo(int targetType, int precision, Mode mode, Object column, String[] enumerators) {
    // converting BLOB to CLOB and vice versa is done in ValueLob
    if (getType() == targetType) {
        return this;
    }
    try {
        // decimal conversion
        switch(targetType) {
            case BOOLEAN:
                {
                    switch(getType()) {
                        case BYTE:
                        case SHORT:
                        case INT:
                        case LONG:
                        case DECIMAL:
                        case DOUBLE:
                        case FLOAT:
                            return ValueBoolean.get(getSignum() != 0);
                        case TIME:
                        case DATE:
                        case TIMESTAMP:
                        case TIMESTAMP_TZ:
                        case BYTES:
                        case JAVA_OBJECT:
                        case UUID:
                        case ENUM:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case BYTE:
                {
                    switch(getType()) {
                        case BOOLEAN:
                            return ValueByte.get(getBoolean() ? (byte) 1 : (byte) 0);
                        case SHORT:
                        case ENUM:
                        case INT:
                            return ValueByte.get(convertToByte(getInt(), column));
                        case LONG:
                            return ValueByte.get(convertToByte(getLong(), column));
                        case DECIMAL:
                            return ValueByte.get(convertToByte(convertToLong(getBigDecimal(), column), column));
                        case DOUBLE:
                            return ValueByte.get(convertToByte(convertToLong(getDouble(), column), column));
                        case FLOAT:
                            return ValueByte.get(convertToByte(convertToLong(getFloat(), column), column));
                        case BYTES:
                            return ValueByte.get((byte) Integer.parseInt(getString(), 16));
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case SHORT:
                {
                    switch(getType()) {
                        case BOOLEAN:
                            return ValueShort.get(getBoolean() ? (short) 1 : (short) 0);
                        case BYTE:
                            return ValueShort.get(getByte());
                        case ENUM:
                        case INT:
                            return ValueShort.get(convertToShort(getInt(), column));
                        case LONG:
                            return ValueShort.get(convertToShort(getLong(), column));
                        case DECIMAL:
                            return ValueShort.get(convertToShort(convertToLong(getBigDecimal(), column), column));
                        case DOUBLE:
                            return ValueShort.get(convertToShort(convertToLong(getDouble(), column), column));
                        case FLOAT:
                            return ValueShort.get(convertToShort(convertToLong(getFloat(), column), column));
                        case BYTES:
                            return ValueShort.get((short) Integer.parseInt(getString(), 16));
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case INT:
                {
                    switch(getType()) {
                        case BOOLEAN:
                            return ValueInt.get(getBoolean() ? 1 : 0);
                        case BYTE:
                        case ENUM:
                        case SHORT:
                            return ValueInt.get(getInt());
                        case LONG:
                            return ValueInt.get(convertToInt(getLong(), column));
                        case DECIMAL:
                            return ValueInt.get(convertToInt(convertToLong(getBigDecimal(), column), column));
                        case DOUBLE:
                            return ValueInt.get(convertToInt(convertToLong(getDouble(), column), column));
                        case FLOAT:
                            return ValueInt.get(convertToInt(convertToLong(getFloat(), column), column));
                        case BYTES:
                            return ValueInt.get((int) Long.parseLong(getString(), 16));
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case LONG:
                {
                    switch(getType()) {
                        case BOOLEAN:
                            return ValueLong.get(getBoolean() ? 1 : 0);
                        case BYTE:
                        case SHORT:
                        case ENUM:
                        case INT:
                            return ValueLong.get(getInt());
                        case DECIMAL:
                            return ValueLong.get(convertToLong(getBigDecimal(), column));
                        case DOUBLE:
                            return ValueLong.get(convertToLong(getDouble(), column));
                        case FLOAT:
                            return ValueLong.get(convertToLong(getFloat(), column));
                        case BYTES:
                            {
                                // parseLong doesn't work for ffffffffffffffff
                                byte[] d = getBytes();
                                if (d.length == 8) {
                                    return ValueLong.get(Bits.readLong(d, 0));
                                }
                                return ValueLong.get(Long.parseLong(getString(), 16));
                            }
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case DECIMAL:
                {
                    switch(getType()) {
                        case BOOLEAN:
                            return ValueDecimal.get(BigDecimal.valueOf(getBoolean() ? 1 : 0));
                        case BYTE:
                        case SHORT:
                        case ENUM:
                        case INT:
                            return ValueDecimal.get(BigDecimal.valueOf(getInt()));
                        case LONG:
                            return ValueDecimal.get(BigDecimal.valueOf(getLong()));
                        case DOUBLE:
                            {
                                double d = getDouble();
                                if (Double.isInfinite(d) || Double.isNaN(d)) {
                                    throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, "" + d);
                                }
                                return ValueDecimal.get(BigDecimal.valueOf(d));
                            }
                        case FLOAT:
                            {
                                float f = getFloat();
                                if (Float.isInfinite(f) || Float.isNaN(f)) {
                                    throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, "" + f);
                                }
                                // better rounding behavior than BigDecimal.valueOf(f)
                                return ValueDecimal.get(new BigDecimal(Float.toString(f)));
                            }
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case DOUBLE:
                {
                    switch(getType()) {
                        case BOOLEAN:
                            return ValueDouble.get(getBoolean() ? 1 : 0);
                        case BYTE:
                        case SHORT:
                        case INT:
                            return ValueDouble.get(getInt());
                        case LONG:
                            return ValueDouble.get(getLong());
                        case DECIMAL:
                            return ValueDouble.get(getBigDecimal().doubleValue());
                        case FLOAT:
                            return ValueDouble.get(getFloat());
                        case ENUM:
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case FLOAT:
                {
                    switch(getType()) {
                        case BOOLEAN:
                            return ValueFloat.get(getBoolean() ? 1 : 0);
                        case BYTE:
                        case SHORT:
                        case INT:
                            return ValueFloat.get(getInt());
                        case LONG:
                            return ValueFloat.get(getLong());
                        case DECIMAL:
                            return ValueFloat.get(getBigDecimal().floatValue());
                        case DOUBLE:
                            return ValueFloat.get((float) getDouble());
                        case ENUM:
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case DATE:
                {
                    switch(getType()) {
                        case TIME:
                            // this will be the result
                            return ValueDate.fromDateValue(DateTimeUtils.EPOCH_DATE_VALUE);
                        case TIMESTAMP:
                            return ValueDate.fromDateValue(((ValueTimestamp) this).getDateValue());
                        case TIMESTAMP_TZ:
                            {
                                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this;
                                long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos();
                                long millis = DateTimeUtils.getMillis(dateValue, timeNanos, ts.getTimeZoneOffsetMins());
                                return ValueDate.fromMillis(millis);
                            }
                        case ENUM:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case TIME:
                {
                    switch(getType()) {
                        case DATE:
                            // has the time set to 0, the result will be 0
                            return ValueTime.fromNanos(0);
                        case TIMESTAMP:
                            return ValueTime.fromNanos(((ValueTimestamp) this).getTimeNanos());
                        case TIMESTAMP_TZ:
                            {
                                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this;
                                long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos();
                                long millis = DateTimeUtils.getMillis(dateValue, timeNanos, ts.getTimeZoneOffsetMins());
                                return ValueTime.fromNanos(DateTimeUtils.nanosFromDate(millis) + timeNanos % 1_000_000);
                            }
                        case ENUM:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case TIMESTAMP:
                {
                    switch(getType()) {
                        case TIME:
                            return DateTimeUtils.normalizeTimestamp(0, ((ValueTime) this).getNanos());
                        case DATE:
                            return ValueTimestamp.fromDateValueAndNanos(((ValueDate) this).getDateValue(), 0);
                        case TIMESTAMP_TZ:
                            {
                                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) this;
                                long dateValue = ts.getDateValue(), timeNanos = ts.getTimeNanos();
                                long millis = DateTimeUtils.getMillis(dateValue, timeNanos, ts.getTimeZoneOffsetMins());
                                return ValueTimestamp.fromMillisNanos(millis, (int) (timeNanos % 1_000_000));
                            }
                        case ENUM:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case TIMESTAMP_TZ:
                {
                    switch(getType()) {
                        case TIME:
                            {
                                ValueTimestamp ts = DateTimeUtils.normalizeTimestamp(0, ((ValueTime) this).getNanos());
                                return DateTimeUtils.timestampTimeZoneFromLocalDateValueAndNanos(ts.getDateValue(), ts.getTimeNanos());
                            }
                        case DATE:
                            return DateTimeUtils.timestampTimeZoneFromLocalDateValueAndNanos(((ValueDate) this).getDateValue(), 0);
                        case TIMESTAMP:
                            {
                                ValueTimestamp ts = (ValueTimestamp) this;
                                return DateTimeUtils.timestampTimeZoneFromLocalDateValueAndNanos(ts.getDateValue(), ts.getTimeNanos());
                            }
                        case ENUM:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case BYTES:
                {
                    switch(getType()) {
                        case JAVA_OBJECT:
                        case BLOB:
                            return ValueBytes.getNoCopy(getBytesNoCopy());
                        case UUID:
                        case GEOMETRY:
                            return ValueBytes.getNoCopy(getBytes());
                        case BYTE:
                            return ValueBytes.getNoCopy(new byte[] { getByte() });
                        case SHORT:
                            {
                                int x = getShort();
                                return ValueBytes.getNoCopy(new byte[] { (byte) (x >> 8), (byte) x });
                            }
                        case INT:
                            {
                                byte[] b = new byte[4];
                                Bits.writeInt(b, 0, getInt());
                                return ValueBytes.getNoCopy(b);
                            }
                        case LONG:
                            {
                                byte[] b = new byte[8];
                                Bits.writeLong(b, 0, getLong());
                                return ValueBytes.getNoCopy(b);
                            }
                        case ENUM:
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case JAVA_OBJECT:
                {
                    switch(getType()) {
                        case BYTES:
                        case BLOB:
                            return ValueJavaObject.getNoCopy(null, getBytesNoCopy(), getDataHandler());
                        case ENUM:
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case ENUM:
                {
                    switch(getType()) {
                        case BYTE:
                        case SHORT:
                        case INT:
                        case LONG:
                        case DECIMAL:
                            return ValueEnum.get(enumerators, getInt());
                        case STRING:
                        case STRING_IGNORECASE:
                        case STRING_FIXED:
                            return ValueEnum.get(enumerators, getString());
                        case JAVA_OBJECT:
                            Object object = JdbcUtils.deserialize(getBytesNoCopy(), getDataHandler());
                            if (object instanceof String) {
                                return ValueEnum.get(enumerators, (String) object);
                            } else if (object instanceof Integer) {
                                return ValueEnum.get(enumerators, (int) object);
                            }
                        // $FALL-THROUGH$
                        default:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                }
            case BLOB:
                {
                    switch(getType()) {
                        case BYTES:
                            return ValueLobDb.createSmallLob(Value.BLOB, getBytesNoCopy());
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case UUID:
                {
                    switch(getType()) {
                        case BYTES:
                            return ValueUuid.get(getBytesNoCopy());
                        case JAVA_OBJECT:
                            Object object = JdbcUtils.deserialize(getBytesNoCopy(), getDataHandler());
                            if (object instanceof java.util.UUID) {
                                return ValueUuid.get((java.util.UUID) object);
                            }
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
            case GEOMETRY:
                {
                    switch(getType()) {
                        case BYTES:
                            return ValueGeometry.get(getBytesNoCopy());
                        case JAVA_OBJECT:
                            Object object = JdbcUtils.deserialize(getBytesNoCopy(), getDataHandler());
                            if (DataType.isGeometry(object)) {
                                return ValueGeometry.getFromGeometry(object);
                            }
                        // $FALL-THROUGH$
                        case TIMESTAMP_TZ:
                            throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
                    }
                    break;
                }
        }
        // conversion by parsing the string value
        String s = getString();
        switch(targetType) {
            case NULL:
                return ValueNull.INSTANCE;
            case BOOLEAN:
                {
                    if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t") || s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y")) {
                        return ValueBoolean.TRUE;
                    } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("f") || s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n")) {
                        return ValueBoolean.FALSE;
                    } else {
                        // convert to a number, and if it is not 0 then it is true
                        return ValueBoolean.get(new BigDecimal(s).signum() != 0);
                    }
                }
            case BYTE:
                return ValueByte.get(Byte.parseByte(s.trim()));
            case SHORT:
                return ValueShort.get(Short.parseShort(s.trim()));
            case INT:
                return ValueInt.get(Integer.parseInt(s.trim()));
            case LONG:
                return ValueLong.get(Long.parseLong(s.trim()));
            case DECIMAL:
                return ValueDecimal.get(new BigDecimal(s.trim()));
            case TIME:
                return ValueTime.parse(s.trim());
            case DATE:
                return ValueDate.parse(s.trim());
            case TIMESTAMP:
                return ValueTimestamp.parse(s.trim(), mode);
            case TIMESTAMP_TZ:
                return ValueTimestampTimeZone.parse(s.trim());
            case BYTES:
                return ValueBytes.getNoCopy(StringUtils.convertHexToBytes(s.trim()));
            case JAVA_OBJECT:
                return ValueJavaObject.getNoCopy(null, StringUtils.convertHexToBytes(s.trim()), getDataHandler());
            case STRING:
                return ValueString.get(s);
            case STRING_IGNORECASE:
                return ValueStringIgnoreCase.get(s);
            case STRING_FIXED:
                return ValueStringFixed.get(s, precision, mode);
            case DOUBLE:
                return ValueDouble.get(Double.parseDouble(s.trim()));
            case FLOAT:
                return ValueFloat.get(Float.parseFloat(s.trim()));
            case CLOB:
                return ValueLobDb.createSmallLob(CLOB, s.getBytes(StandardCharsets.UTF_8));
            case BLOB:
                return ValueLobDb.createSmallLob(BLOB, StringUtils.convertHexToBytes(s.trim()));
            case ARRAY:
                return ValueArray.get(new Value[] { ValueString.get(s) });
            case RESULT_SET:
                {
                    SimpleResultSet rs = new SimpleResultSet();
                    rs.setAutoClose(false);
                    rs.addColumn("X", Types.VARCHAR, s.length(), 0);
                    rs.addRow(s);
                    return ValueResultSet.get(rs);
                }
            case UUID:
                return ValueUuid.get(s);
            case GEOMETRY:
                return ValueGeometry.get(s);
            default:
                if (JdbcUtils.customDataTypesHandler != null) {
                    return JdbcUtils.customDataTypesHandler.convert(this, targetType);
                }
                throw DbException.throwInternalError("type=" + targetType);
        }
    } catch (NumberFormatException e) {
        throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, getString());
    }
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) BigDecimal(java.math.BigDecimal)

Aggregations

ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)14 ValueTimestamp (org.h2.value.ValueTimestamp)6 Value (org.h2.value.Value)5 ValueTime (org.h2.value.ValueTime)5 BigDecimal (java.math.BigDecimal)4 ValueDate (org.h2.value.ValueDate)4 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 SimpleResultSet (org.h2.tools.SimpleResultSet)3 ValueResultSet (org.h2.value.ValueResultSet)3 ValueString (org.h2.value.ValueString)3 BigInteger (java.math.BigInteger)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 TimeZone (java.util.TimeZone)2 ValueLobDb (org.h2.value.ValueLobDb)2 ValueUuid (org.h2.value.ValueUuid)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1