Search in sources :

Example 6 with ValueDecfloat

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

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)

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