Search in sources :

Example 1 with ValueDecfloat

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

the class Value method convertToDecfloat.

private ValueDecfloat convertToDecfloat(TypeInfo targetType, int conversionMode) {
    ValueDecfloat v;
    switch(getValueType()) {
        case DECFLOAT:
            v = (ValueDecfloat) this;
            if (v.value == null) {
                return v;
            }
            break;
        case CHAR:
        case VARCHAR:
        case VARCHAR_IGNORECASE:
            {
                String s = getString().trim();
                try {
                    v = ValueDecfloat.get(new BigDecimal(s));
                } catch (NumberFormatException e) {
                    switch(s) {
                        case "-Infinity":
                            return ValueDecfloat.NEGATIVE_INFINITY;
                        case "Infinity":
                        case "+Infinity":
                            return ValueDecfloat.POSITIVE_INFINITY;
                        case "NaN":
                        case "-NaN":
                        case "+NaN":
                            return ValueDecfloat.NAN;
                        default:
                            throw getDataConversionError(DECFLOAT);
                    }
                }
                break;
            }
        case BOOLEAN:
            v = getBoolean() ? ValueDecfloat.ONE : ValueDecfloat.ZERO;
            break;
        case REAL:
            {
                float value = getFloat();
                if (Float.isFinite(value)) {
                    v = ValueDecfloat.get(new BigDecimal(Float.toString(value)));
                } else if (value == Float.POSITIVE_INFINITY) {
                    return ValueDecfloat.POSITIVE_INFINITY;
                } else if (value == Float.NEGATIVE_INFINITY) {
                    return ValueDecfloat.NEGATIVE_INFINITY;
                } else {
                    return ValueDecfloat.NAN;
                }
                break;
            }
        case DOUBLE:
            {
                double value = getDouble();
                if (Double.isFinite(value)) {
                    v = ValueDecfloat.get(new BigDecimal(Double.toString(value)));
                } else if (value == Double.POSITIVE_INFINITY) {
                    return ValueDecfloat.POSITIVE_INFINITY;
                } else if (value == Double.NEGATIVE_INFINITY) {
                    return ValueDecfloat.NEGATIVE_INFINITY;
                } else {
                    return ValueDecfloat.NAN;
                }
                break;
            }
        default:
            try {
                v = ValueDecfloat.get(getBigDecimal());
            } catch (DbException e) {
                if (e.getErrorCode() == ErrorCode.DATA_CONVERSION_ERROR_1) {
                    throw getDataConversionError(DECFLOAT);
                }
                throw e;
            }
            break;
        case NULL:
            throw DbException.getInternalError();
    }
    if (conversionMode != CONVERT_TO) {
        BigDecimal bd = v.value;
        int precision = bd.precision(), targetPrecision = (int) targetType.getPrecision();
        if (precision > targetPrecision) {
            v = ValueDecfloat.get(bd.setScale(bd.scale() - precision + targetPrecision, RoundingMode.HALF_UP));
        }
    }
    return v;
}
Also used : BigDecimal(java.math.BigDecimal) DbException(org.h2.message.DbException)

Example 2 with ValueDecfloat

use of org.h2.value.ValueDecfloat in project SpringStudy by myounghaklee.

the class Value method convertToDecfloat.

private ValueDecfloat convertToDecfloat(TypeInfo targetType, int conversionMode) {
    ValueDecfloat v;
    switch(getValueType()) {
        case DECFLOAT:
            v = (ValueDecfloat) this;
            if (v.value == null) {
                return v;
            }
            break;
        case CHAR:
        case VARCHAR:
        case VARCHAR_IGNORECASE:
            {
                String s = getString().trim();
                try {
                    v = ValueDecfloat.get(new BigDecimal(s));
                } catch (NumberFormatException e) {
                    switch(s) {
                        case "-Infinity":
                            return ValueDecfloat.NEGATIVE_INFINITY;
                        case "Infinity":
                        case "+Infinity":
                            return ValueDecfloat.POSITIVE_INFINITY;
                        case "NaN":
                        case "-NaN":
                        case "+NaN":
                            return ValueDecfloat.NAN;
                        default:
                            throw getDataConversionError(DECFLOAT);
                    }
                }
                break;
            }
        case BOOLEAN:
            v = getBoolean() ? ValueDecfloat.ONE : ValueDecfloat.ZERO;
            break;
        case REAL:
            {
                float value = getFloat();
                if (Float.isFinite(value)) {
                    v = ValueDecfloat.get(new BigDecimal(Float.toString(value)));
                } else if (value == Float.POSITIVE_INFINITY) {
                    return ValueDecfloat.POSITIVE_INFINITY;
                } else if (value == Float.NEGATIVE_INFINITY) {
                    return ValueDecfloat.NEGATIVE_INFINITY;
                } else {
                    return ValueDecfloat.NAN;
                }
                break;
            }
        case DOUBLE:
            {
                double value = getDouble();
                if (Double.isFinite(value)) {
                    v = ValueDecfloat.get(new BigDecimal(Double.toString(value)));
                } else if (value == Double.POSITIVE_INFINITY) {
                    return ValueDecfloat.POSITIVE_INFINITY;
                } else if (value == Double.NEGATIVE_INFINITY) {
                    return ValueDecfloat.NEGATIVE_INFINITY;
                } else {
                    return ValueDecfloat.NAN;
                }
                break;
            }
        default:
            try {
                v = ValueDecfloat.get(getBigDecimal());
            } catch (DbException e) {
                if (e.getErrorCode() == ErrorCode.DATA_CONVERSION_ERROR_1) {
                    throw getDataConversionError(DECFLOAT);
                }
                throw e;
            }
            break;
        case NULL:
            throw DbException.getInternalError();
    }
    if (conversionMode != CONVERT_TO) {
        BigDecimal bd = v.value;
        int precision = bd.precision(), targetPrecision = (int) targetType.getPrecision();
        if (precision > targetPrecision) {
            v = ValueDecfloat.get(bd.setScale(bd.scale() - precision + targetPrecision, RoundingMode.HALF_UP));
        }
    }
    return v;
}
Also used : BigDecimal(java.math.BigDecimal) DbException(org.h2.message.DbException)

Example 3 with ValueDecfloat

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

the class ValueDataType method write.

@Override
public void write(WriteBuffer buff, Value v) {
    if (v == ValueNull.INSTANCE) {
        buff.put((byte) 0);
        return;
    }
    int type = v.getValueType();
    switch(type) {
        case Value.BOOLEAN:
            buff.put(v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE);
            break;
        case Value.TINYINT:
            buff.put(TINYINT).put(v.getByte());
            break;
        case Value.SMALLINT:
            buff.put(SMALLINT).putShort(v.getShort());
            break;
        case Value.ENUM:
        case Value.INTEGER:
            {
                int x = v.getInt();
                if (x < 0) {
                    buff.put(INT_NEG).putVarInt(-x);
                } else if (x < 16) {
                    buff.put((byte) (INT_0_15 + x));
                } else {
                    buff.put(type == Value.INTEGER ? INTEGER : ENUM).putVarInt(x);
                }
                break;
            }
        case Value.BIGINT:
            writeLong(buff, v.getLong());
            break;
        case Value.NUMERIC:
            {
                BigDecimal x = v.getBigDecimal();
                if (BigDecimal.ZERO.equals(x)) {
                    buff.put(NUMERIC_0_1);
                } else if (BigDecimal.ONE.equals(x)) {
                    buff.put((byte) (NUMERIC_0_1 + 1));
                } else {
                    int scale = x.scale();
                    BigInteger b = x.unscaledValue();
                    int bits = b.bitLength();
                    if (bits <= 63) {
                        if (scale == 0) {
                            buff.put(NUMERIC_SMALL_0).putVarLong(b.longValue());
                        } else {
                            buff.put(NUMERIC_SMALL).putVarInt(scale).putVarLong(b.longValue());
                        }
                    } else {
                        byte[] bytes = b.toByteArray();
                        buff.put(NUMERIC).putVarInt(scale).putVarInt(bytes.length).put(bytes);
                    }
                }
                break;
            }
        case Value.DECFLOAT:
            {
                ValueDecfloat d = (ValueDecfloat) v;
                buff.put((byte) DECFLOAT);
                if (d.isFinite()) {
                    BigDecimal x = d.getBigDecimal();
                    byte[] bytes = x.unscaledValue().toByteArray();
                    buff.putVarInt(x.scale()).putVarInt(bytes.length).put(bytes);
                } else {
                    int c;
                    if (d == ValueDecfloat.NEGATIVE_INFINITY) {
                        c = -3;
                    } else if (d == ValueDecfloat.POSITIVE_INFINITY) {
                        c = -2;
                    } else {
                        c = -1;
                    }
                    buff.putVarInt(0).putVarInt(c);
                }
                break;
            }
        case Value.TIME:
            writeTimestampTime(buff.put(TIME), ((ValueTime) v).getNanos());
            break;
        case Value.TIME_TZ:
            {
                ValueTimeTimeZone t = (ValueTimeTimeZone) v;
                long nanosOfDay = t.getNanos();
                buff.put((byte) TIME_TZ).putVarInt((int) (nanosOfDay / DateTimeUtils.NANOS_PER_SECOND)).putVarInt((int) (nanosOfDay % DateTimeUtils.NANOS_PER_SECOND));
                writeTimeZone(buff, t.getTimeZoneOffsetSeconds());
                break;
            }
        case Value.DATE:
            buff.put(DATE).putVarLong(((ValueDate) v).getDateValue());
            break;
        case Value.TIMESTAMP:
            {
                ValueTimestamp ts = (ValueTimestamp) v;
                buff.put(TIMESTAMP).putVarLong(ts.getDateValue());
                writeTimestampTime(buff, ts.getTimeNanos());
                break;
            }
        case Value.TIMESTAMP_TZ:
            {
                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
                buff.put((byte) TIMESTAMP_TZ).putVarLong(ts.getDateValue());
                writeTimestampTime(buff, ts.getTimeNanos());
                writeTimeZone(buff, ts.getTimeZoneOffsetSeconds());
                break;
            }
        case Value.JAVA_OBJECT:
            writeBinary(JAVA_OBJECT, buff, v);
            break;
        case Value.VARBINARY:
            {
                byte[] b = v.getBytesNoCopy();
                int len = b.length;
                if (len < 32) {
                    buff.put((byte) (VARBINARY_0_31 + len)).put(b);
                } else {
                    buff.put(VARBINARY).putVarInt(len).put(b);
                }
                break;
            }
        case Value.BINARY:
            writeBinary((byte) BINARY, buff, v);
            break;
        case Value.UUID:
            {
                ValueUuid uuid = (ValueUuid) v;
                buff.put(UUID).putLong(uuid.getHigh()).putLong(uuid.getLow());
                break;
            }
        case Value.VARCHAR:
            {
                String s = v.getString();
                int len = s.length();
                if (len < 32) {
                    buff.put((byte) (VARCHAR_0_31 + len)).putStringData(s, len);
                } else {
                    writeString(buff.put(VARCHAR), s);
                }
                break;
            }
        case Value.VARCHAR_IGNORECASE:
            writeString(buff.put(VARCHAR_IGNORECASE), v.getString());
            break;
        case Value.CHAR:
            writeString(buff.put(CHAR), v.getString());
            break;
        case Value.DOUBLE:
            {
                double x = v.getDouble();
                if (x == 1.0d) {
                    buff.put((byte) (DOUBLE_0_1 + 1));
                } else {
                    long d = Double.doubleToLongBits(x);
                    if (d == ValueDouble.ZERO_BITS) {
                        buff.put(DOUBLE_0_1);
                    } else {
                        buff.put(DOUBLE).putVarLong(Long.reverse(d));
                    }
                }
                break;
            }
        case Value.REAL:
            {
                float x = v.getFloat();
                if (x == 1.0f) {
                    buff.put((byte) (REAL_0_1 + 1));
                } else {
                    int f = Float.floatToIntBits(x);
                    if (f == ValueReal.ZERO_BITS) {
                        buff.put(REAL_0_1);
                    } else {
                        buff.put(REAL).putVarInt(Integer.reverse(f));
                    }
                }
                break;
            }
        case Value.BLOB:
            {
                buff.put(BLOB);
                ValueBlob lob = (ValueBlob) v;
                LobData lobData = lob.getLobData();
                if (lobData instanceof LobDataDatabase) {
                    LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
                    buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength());
                } else {
                    byte[] small = ((LobDataInMemory) lobData).getSmall();
                    buff.putVarInt(small.length).put(small);
                }
                break;
            }
        case Value.CLOB:
            {
                buff.put(CLOB);
                ValueClob lob = (ValueClob) v;
                LobData lobData = lob.getLobData();
                if (lobData instanceof LobDataDatabase) {
                    LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
                    buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength()).putVarLong(lob.charLength());
                } else {
                    byte[] small = ((LobDataInMemory) lobData).getSmall();
                    buff.putVarInt(small.length).put(small).putVarLong(lob.charLength());
                }
                break;
            }
        case Value.ARRAY:
        case Value.ROW:
            {
                Value[] list = ((ValueCollectionBase) v).getList();
                buff.put(type == Value.ARRAY ? ARRAY : ROW).putVarInt(list.length);
                for (Value x : list) {
                    write(buff, x);
                }
                break;
            }
        case Value.GEOMETRY:
            writeBinary(GEOMETRY, buff, v);
            break;
        case Value.INTERVAL_YEAR:
        case Value.INTERVAL_MONTH:
        case Value.INTERVAL_DAY:
        case Value.INTERVAL_HOUR:
        case Value.INTERVAL_MINUTE:
            {
                ValueInterval interval = (ValueInterval) v;
                int ordinal = type - Value.INTERVAL_YEAR;
                if (interval.isNegative()) {
                    ordinal = ~ordinal;
                }
                buff.put(INTERVAL).put((byte) ordinal).putVarLong(interval.getLeading());
                break;
            }
        case Value.INTERVAL_SECOND:
        case Value.INTERVAL_YEAR_TO_MONTH:
        case Value.INTERVAL_DAY_TO_HOUR:
        case Value.INTERVAL_DAY_TO_MINUTE:
        case Value.INTERVAL_DAY_TO_SECOND:
        case Value.INTERVAL_HOUR_TO_MINUTE:
        case Value.INTERVAL_HOUR_TO_SECOND:
        case Value.INTERVAL_MINUTE_TO_SECOND:
            {
                ValueInterval interval = (ValueInterval) v;
                int ordinal = type - Value.INTERVAL_YEAR;
                if (interval.isNegative()) {
                    ordinal = ~ordinal;
                }
                buff.put(INTERVAL).put((byte) ordinal).putVarLong(interval.getLeading()).putVarLong(interval.getRemaining());
                break;
            }
        case Value.JSON:
            writeBinary((byte) JSON, buff, v);
            break;
        default:
            throw DbException.getInternalError("type=" + v.getValueType());
    }
}
Also used : LobData(org.h2.value.lob.LobData) ValueUuid(org.h2.value.ValueUuid) LobDataDatabase(org.h2.value.lob.LobDataDatabase) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueBlob(org.h2.value.ValueBlob) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) DataUtils.readString(org.h2.mvstore.DataUtils.readString) ValueClob(org.h2.value.ValueClob) ValueInterval(org.h2.value.ValueInterval) ValueBigint(org.h2.value.ValueBigint) ValueTinyint(org.h2.value.ValueTinyint) ValueSmallint(org.h2.value.ValueSmallint) BigDecimal(java.math.BigDecimal) ValueDecfloat(org.h2.value.ValueDecfloat) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) BigInteger(java.math.BigInteger)

Example 4 with ValueDecfloat

use of org.h2.value.ValueDecfloat in project SpringStudy by myounghaklee.

the class ValueDataType method write.

@Override
public void write(WriteBuffer buff, Value v) {
    if (v == ValueNull.INSTANCE) {
        buff.put((byte) 0);
        return;
    }
    int type = v.getValueType();
    switch(type) {
        case Value.BOOLEAN:
            buff.put(v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE);
            break;
        case Value.TINYINT:
            buff.put(TINYINT).put(v.getByte());
            break;
        case Value.SMALLINT:
            buff.put(SMALLINT).putShort(v.getShort());
            break;
        case Value.ENUM:
        case Value.INTEGER:
            {
                int x = v.getInt();
                if (x < 0) {
                    buff.put(INT_NEG).putVarInt(-x);
                } else if (x < 16) {
                    buff.put((byte) (INT_0_15 + x));
                } else {
                    buff.put(type == Value.INTEGER ? INTEGER : ENUM).putVarInt(x);
                }
                break;
            }
        case Value.BIGINT:
            writeLong(buff, v.getLong());
            break;
        case Value.NUMERIC:
            {
                BigDecimal x = v.getBigDecimal();
                if (BigDecimal.ZERO.equals(x)) {
                    buff.put(NUMERIC_0_1);
                } else if (BigDecimal.ONE.equals(x)) {
                    buff.put((byte) (NUMERIC_0_1 + 1));
                } else {
                    int scale = x.scale();
                    BigInteger b = x.unscaledValue();
                    int bits = b.bitLength();
                    if (bits <= 63) {
                        if (scale == 0) {
                            buff.put(NUMERIC_SMALL_0).putVarLong(b.longValue());
                        } else {
                            buff.put(NUMERIC_SMALL).putVarInt(scale).putVarLong(b.longValue());
                        }
                    } else {
                        byte[] bytes = b.toByteArray();
                        buff.put(NUMERIC).putVarInt(scale).putVarInt(bytes.length).put(bytes);
                    }
                }
                break;
            }
        case Value.DECFLOAT:
            {
                ValueDecfloat d = (ValueDecfloat) v;
                buff.put((byte) DECFLOAT);
                if (d.isFinite()) {
                    BigDecimal x = d.getBigDecimal();
                    byte[] bytes = x.unscaledValue().toByteArray();
                    buff.putVarInt(x.scale()).putVarInt(bytes.length).put(bytes);
                } else {
                    int c;
                    if (d == ValueDecfloat.NEGATIVE_INFINITY) {
                        c = -3;
                    } else if (d == ValueDecfloat.POSITIVE_INFINITY) {
                        c = -2;
                    } else {
                        c = -1;
                    }
                    buff.putVarInt(0).putVarInt(c);
                }
                break;
            }
        case Value.TIME:
            writeTimestampTime(buff.put(TIME), ((ValueTime) v).getNanos());
            break;
        case Value.TIME_TZ:
            {
                ValueTimeTimeZone t = (ValueTimeTimeZone) v;
                long nanosOfDay = t.getNanos();
                buff.put((byte) TIME_TZ).putVarInt((int) (nanosOfDay / DateTimeUtils.NANOS_PER_SECOND)).putVarInt((int) (nanosOfDay % DateTimeUtils.NANOS_PER_SECOND));
                writeTimeZone(buff, t.getTimeZoneOffsetSeconds());
                break;
            }
        case Value.DATE:
            buff.put(DATE).putVarLong(((ValueDate) v).getDateValue());
            break;
        case Value.TIMESTAMP:
            {
                ValueTimestamp ts = (ValueTimestamp) v;
                buff.put(TIMESTAMP).putVarLong(ts.getDateValue());
                writeTimestampTime(buff, ts.getTimeNanos());
                break;
            }
        case Value.TIMESTAMP_TZ:
            {
                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
                buff.put((byte) TIMESTAMP_TZ).putVarLong(ts.getDateValue());
                writeTimestampTime(buff, ts.getTimeNanos());
                writeTimeZone(buff, ts.getTimeZoneOffsetSeconds());
                break;
            }
        case Value.JAVA_OBJECT:
            writeBinary(JAVA_OBJECT, buff, v);
            break;
        case Value.VARBINARY:
            {
                byte[] b = v.getBytesNoCopy();
                int len = b.length;
                if (len < 32) {
                    buff.put((byte) (VARBINARY_0_31 + len)).put(b);
                } else {
                    buff.put(VARBINARY).putVarInt(len).put(b);
                }
                break;
            }
        case Value.BINARY:
            writeBinary((byte) BINARY, buff, v);
            break;
        case Value.UUID:
            {
                ValueUuid uuid = (ValueUuid) v;
                buff.put(UUID).putLong(uuid.getHigh()).putLong(uuid.getLow());
                break;
            }
        case Value.VARCHAR:
            {
                String s = v.getString();
                int len = s.length();
                if (len < 32) {
                    buff.put((byte) (VARCHAR_0_31 + len)).putStringData(s, len);
                } else {
                    writeString(buff.put(VARCHAR), s);
                }
                break;
            }
        case Value.VARCHAR_IGNORECASE:
            writeString(buff.put(VARCHAR_IGNORECASE), v.getString());
            break;
        case Value.CHAR:
            writeString(buff.put(CHAR), v.getString());
            break;
        case Value.DOUBLE:
            {
                double x = v.getDouble();
                if (x == 1.0d) {
                    buff.put((byte) (DOUBLE_0_1 + 1));
                } else {
                    long d = Double.doubleToLongBits(x);
                    if (d == ValueDouble.ZERO_BITS) {
                        buff.put(DOUBLE_0_1);
                    } else {
                        buff.put(DOUBLE).putVarLong(Long.reverse(d));
                    }
                }
                break;
            }
        case Value.REAL:
            {
                float x = v.getFloat();
                if (x == 1.0f) {
                    buff.put((byte) (REAL_0_1 + 1));
                } else {
                    int f = Float.floatToIntBits(x);
                    if (f == ValueReal.ZERO_BITS) {
                        buff.put(REAL_0_1);
                    } else {
                        buff.put(REAL).putVarInt(Integer.reverse(f));
                    }
                }
                break;
            }
        case Value.BLOB:
            {
                buff.put(BLOB);
                ValueBlob lob = (ValueBlob) v;
                LobData lobData = lob.getLobData();
                if (lobData instanceof LobDataDatabase) {
                    LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
                    buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength());
                } else {
                    byte[] small = ((LobDataInMemory) lobData).getSmall();
                    buff.putVarInt(small.length).put(small);
                }
                break;
            }
        case Value.CLOB:
            {
                buff.put(CLOB);
                ValueClob lob = (ValueClob) v;
                LobData lobData = lob.getLobData();
                if (lobData instanceof LobDataDatabase) {
                    LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
                    buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength()).putVarLong(lob.charLength());
                } else {
                    byte[] small = ((LobDataInMemory) lobData).getSmall();
                    buff.putVarInt(small.length).put(small).putVarLong(lob.charLength());
                }
                break;
            }
        case Value.ARRAY:
        case Value.ROW:
            {
                Value[] list = ((ValueCollectionBase) v).getList();
                buff.put(type == Value.ARRAY ? ARRAY : ROW).putVarInt(list.length);
                for (Value x : list) {
                    write(buff, x);
                }
                break;
            }
        case Value.GEOMETRY:
            writeBinary(GEOMETRY, buff, v);
            break;
        case Value.INTERVAL_YEAR:
        case Value.INTERVAL_MONTH:
        case Value.INTERVAL_DAY:
        case Value.INTERVAL_HOUR:
        case Value.INTERVAL_MINUTE:
            {
                ValueInterval interval = (ValueInterval) v;
                int ordinal = type - Value.INTERVAL_YEAR;
                if (interval.isNegative()) {
                    ordinal = ~ordinal;
                }
                buff.put(INTERVAL).put((byte) ordinal).putVarLong(interval.getLeading());
                break;
            }
        case Value.INTERVAL_SECOND:
        case Value.INTERVAL_YEAR_TO_MONTH:
        case Value.INTERVAL_DAY_TO_HOUR:
        case Value.INTERVAL_DAY_TO_MINUTE:
        case Value.INTERVAL_DAY_TO_SECOND:
        case Value.INTERVAL_HOUR_TO_MINUTE:
        case Value.INTERVAL_HOUR_TO_SECOND:
        case Value.INTERVAL_MINUTE_TO_SECOND:
            {
                ValueInterval interval = (ValueInterval) v;
                int ordinal = type - Value.INTERVAL_YEAR;
                if (interval.isNegative()) {
                    ordinal = ~ordinal;
                }
                buff.put(INTERVAL).put((byte) (ordinal)).putVarLong(interval.getLeading()).putVarLong(interval.getRemaining());
                break;
            }
        case Value.JSON:
            writeBinary((byte) JSON, buff, v);
            break;
        default:
            throw DbException.getInternalError("type=" + v.getValueType());
    }
}
Also used : LobData(org.h2.value.lob.LobData) ValueUuid(org.h2.value.ValueUuid) LobDataDatabase(org.h2.value.lob.LobDataDatabase) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueBlob(org.h2.value.ValueBlob) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) DataUtils.readString(org.h2.mvstore.DataUtils.readString) ValueClob(org.h2.value.ValueClob) ValueInterval(org.h2.value.ValueInterval) ValueBigint(org.h2.value.ValueBigint) ValueTinyint(org.h2.value.ValueTinyint) ValueSmallint(org.h2.value.ValueSmallint) BigDecimal(java.math.BigDecimal) ValueDecfloat(org.h2.value.ValueDecfloat) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) BigInteger(java.math.BigInteger)

Example 5 with ValueDecfloat

use of org.h2.value.ValueDecfloat in project 468H2Project by lukeunderwood42.

the class ValueDataType method write.

@Override
public void write(WriteBuffer buff, Value v) {
    if (v == ValueNull.INSTANCE) {
        buff.put((byte) 0);
        return;
    }
    int type = v.getValueType();
    switch(type) {
        case Value.BOOLEAN:
            buff.put(v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE);
            break;
        case Value.TINYINT:
            buff.put(TINYINT).put(v.getByte());
            break;
        case Value.SMALLINT:
            buff.put(SMALLINT).putShort(v.getShort());
            break;
        case Value.ENUM:
        case Value.INTEGER:
            {
                int x = v.getInt();
                if (x < 0) {
                    buff.put(INT_NEG).putVarInt(-x);
                } else if (x < 16) {
                    buff.put((byte) (INT_0_15 + x));
                } else {
                    buff.put(type == Value.INTEGER ? INTEGER : ENUM).putVarInt(x);
                }
                break;
            }
        case Value.BIGINT:
            writeLong(buff, v.getLong());
            break;
        case Value.NUMERIC:
            {
                BigDecimal x = v.getBigDecimal();
                if (BigDecimal.ZERO.equals(x)) {
                    buff.put(NUMERIC_0_1);
                } else if (BigDecimal.ONE.equals(x)) {
                    buff.put((byte) (NUMERIC_0_1 + 1));
                } else {
                    int scale = x.scale();
                    BigInteger b = x.unscaledValue();
                    int bits = b.bitLength();
                    if (bits <= 63) {
                        if (scale == 0) {
                            buff.put(NUMERIC_SMALL_0).putVarLong(b.longValue());
                        } else {
                            buff.put(NUMERIC_SMALL).putVarInt(scale).putVarLong(b.longValue());
                        }
                    } else {
                        byte[] bytes = b.toByteArray();
                        buff.put(NUMERIC).putVarInt(scale).putVarInt(bytes.length).put(bytes);
                    }
                }
                break;
            }
        case Value.DECFLOAT:
            {
                ValueDecfloat d = (ValueDecfloat) v;
                buff.put((byte) DECFLOAT);
                if (d.isFinite()) {
                    BigDecimal x = d.getBigDecimal();
                    byte[] bytes = x.unscaledValue().toByteArray();
                    buff.putVarInt(x.scale()).putVarInt(bytes.length).put(bytes);
                } else {
                    int c;
                    if (d == ValueDecfloat.NEGATIVE_INFINITY) {
                        c = -3;
                    } else if (d == ValueDecfloat.POSITIVE_INFINITY) {
                        c = -2;
                    } else {
                        c = -1;
                    }
                    buff.putVarInt(0).putVarInt(c);
                }
                break;
            }
        case Value.TIME:
            writeTimestampTime(buff.put(TIME), ((ValueTime) v).getNanos());
            break;
        case Value.TIME_TZ:
            {
                ValueTimeTimeZone t = (ValueTimeTimeZone) v;
                long nanosOfDay = t.getNanos();
                buff.put((byte) TIME_TZ).putVarInt((int) (nanosOfDay / DateTimeUtils.NANOS_PER_SECOND)).putVarInt((int) (nanosOfDay % DateTimeUtils.NANOS_PER_SECOND));
                writeTimeZone(buff, t.getTimeZoneOffsetSeconds());
                break;
            }
        case Value.DATE:
            buff.put(DATE).putVarLong(((ValueDate) v).getDateValue());
            break;
        case Value.TIMESTAMP:
            {
                ValueTimestamp ts = (ValueTimestamp) v;
                buff.put(TIMESTAMP).putVarLong(ts.getDateValue());
                writeTimestampTime(buff, ts.getTimeNanos());
                break;
            }
        case Value.TIMESTAMP_TZ:
            {
                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
                buff.put((byte) TIMESTAMP_TZ).putVarLong(ts.getDateValue());
                writeTimestampTime(buff, ts.getTimeNanos());
                writeTimeZone(buff, ts.getTimeZoneOffsetSeconds());
                break;
            }
        case Value.JAVA_OBJECT:
            writeBinary(JAVA_OBJECT, buff, v);
            break;
        case Value.VARBINARY:
            {
                byte[] b = v.getBytesNoCopy();
                int len = b.length;
                if (len < 32) {
                    buff.put((byte) (VARBINARY_0_31 + len)).put(b);
                } else {
                    buff.put(VARBINARY).putVarInt(len).put(b);
                }
                break;
            }
        case Value.BINARY:
            writeBinary((byte) BINARY, buff, v);
            break;
        case Value.UUID:
            {
                ValueUuid uuid = (ValueUuid) v;
                buff.put(UUID).putLong(uuid.getHigh()).putLong(uuid.getLow());
                break;
            }
        case Value.VARCHAR:
            {
                String s = v.getString();
                int len = s.length();
                if (len < 32) {
                    buff.put((byte) (VARCHAR_0_31 + len)).putStringData(s, len);
                } else {
                    writeString(buff.put(VARCHAR), s);
                }
                break;
            }
        case Value.VARCHAR_IGNORECASE:
            writeString(buff.put(VARCHAR_IGNORECASE), v.getString());
            break;
        case Value.CHAR:
            writeString(buff.put(CHAR), v.getString());
            break;
        case Value.DOUBLE:
            {
                double x = v.getDouble();
                if (x == 1.0d) {
                    buff.put((byte) (DOUBLE_0_1 + 1));
                } else {
                    long d = Double.doubleToLongBits(x);
                    if (d == ValueDouble.ZERO_BITS) {
                        buff.put(DOUBLE_0_1);
                    } else {
                        buff.put(DOUBLE).putVarLong(Long.reverse(d));
                    }
                }
                break;
            }
        case Value.REAL:
            {
                float x = v.getFloat();
                if (x == 1.0f) {
                    buff.put((byte) (REAL_0_1 + 1));
                } else {
                    int f = Float.floatToIntBits(x);
                    if (f == ValueReal.ZERO_BITS) {
                        buff.put(REAL_0_1);
                    } else {
                        buff.put(REAL).putVarInt(Integer.reverse(f));
                    }
                }
                break;
            }
        case Value.BLOB:
            {
                buff.put(BLOB);
                ValueBlob lob = (ValueBlob) v;
                LobData lobData = lob.getLobData();
                if (lobData instanceof LobDataDatabase) {
                    LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
                    buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength());
                } else {
                    byte[] small = ((LobDataInMemory) lobData).getSmall();
                    buff.putVarInt(small.length).put(small);
                }
                break;
            }
        case Value.CLOB:
            {
                buff.put(CLOB);
                ValueClob lob = (ValueClob) v;
                LobData lobData = lob.getLobData();
                if (lobData instanceof LobDataDatabase) {
                    LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
                    buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength()).putVarLong(lob.charLength());
                } else {
                    byte[] small = ((LobDataInMemory) lobData).getSmall();
                    buff.putVarInt(small.length).put(small).putVarLong(lob.charLength());
                }
                break;
            }
        case Value.ARRAY:
        case Value.ROW:
            {
                Value[] list = ((ValueCollectionBase) v).getList();
                buff.put(type == Value.ARRAY ? ARRAY : ROW).putVarInt(list.length);
                for (Value x : list) {
                    write(buff, x);
                }
                break;
            }
        case Value.GEOMETRY:
            writeBinary(GEOMETRY, buff, v);
            break;
        case Value.INTERVAL_YEAR:
        case Value.INTERVAL_MONTH:
        case Value.INTERVAL_DAY:
        case Value.INTERVAL_HOUR:
        case Value.INTERVAL_MINUTE:
            {
                ValueInterval interval = (ValueInterval) v;
                int ordinal = type - Value.INTERVAL_YEAR;
                if (interval.isNegative()) {
                    ordinal = ~ordinal;
                }
                buff.put(INTERVAL).put((byte) ordinal).putVarLong(interval.getLeading());
                break;
            }
        case Value.INTERVAL_SECOND:
        case Value.INTERVAL_YEAR_TO_MONTH:
        case Value.INTERVAL_DAY_TO_HOUR:
        case Value.INTERVAL_DAY_TO_MINUTE:
        case Value.INTERVAL_DAY_TO_SECOND:
        case Value.INTERVAL_HOUR_TO_MINUTE:
        case Value.INTERVAL_HOUR_TO_SECOND:
        case Value.INTERVAL_MINUTE_TO_SECOND:
            {
                ValueInterval interval = (ValueInterval) v;
                int ordinal = type - Value.INTERVAL_YEAR;
                if (interval.isNegative()) {
                    ordinal = ~ordinal;
                }
                buff.put(INTERVAL).put((byte) ordinal).putVarLong(interval.getLeading()).putVarLong(interval.getRemaining());
                break;
            }
        case Value.JSON:
            writeBinary((byte) JSON, buff, v);
            break;
        default:
            throw DbException.getInternalError("type=" + v.getValueType());
    }
}
Also used : LobData(org.h2.value.lob.LobData) ValueUuid(org.h2.value.ValueUuid) LobDataDatabase(org.h2.value.lob.LobDataDatabase) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueBlob(org.h2.value.ValueBlob) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) DataUtils.readString(org.h2.mvstore.DataUtils.readString) ValueClob(org.h2.value.ValueClob) ValueInterval(org.h2.value.ValueInterval) ValueBigint(org.h2.value.ValueBigint) ValueTinyint(org.h2.value.ValueTinyint) ValueSmallint(org.h2.value.ValueSmallint) BigDecimal(java.math.BigDecimal) ValueDecfloat(org.h2.value.ValueDecfloat) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) BigInteger(java.math.BigInteger)

Aggregations

BigDecimal (java.math.BigDecimal)6 BigInteger (java.math.BigInteger)3 DbException (org.h2.message.DbException)3 DataUtils.readString (org.h2.mvstore.DataUtils.readString)3 Value (org.h2.value.Value)3 ValueBigint (org.h2.value.ValueBigint)3 ValueBlob (org.h2.value.ValueBlob)3 ValueClob (org.h2.value.ValueClob)3 ValueDecfloat (org.h2.value.ValueDecfloat)3 ValueInterval (org.h2.value.ValueInterval)3 ValueSmallint (org.h2.value.ValueSmallint)3 ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)3 ValueTimestamp (org.h2.value.ValueTimestamp)3 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)3 ValueTinyint (org.h2.value.ValueTinyint)3 ValueUuid (org.h2.value.ValueUuid)3 LobData (org.h2.value.lob.LobData)3 LobDataDatabase (org.h2.value.lob.LobDataDatabase)3