Search in sources :

Example 1 with ValueDecimal

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

the class TestValue method testModulusDecimal.

private void testModulusDecimal() {
    final ValueDecimal vd1 = ValueDecimal.get(new BigDecimal(12));
    new AssertThrows(ErrorCode.DIVISION_BY_ZERO_1) {

        @Override
        public void test() {
            vd1.modulus(ValueDecimal.get(new BigDecimal(0)));
        }
    };
    ValueDecimal vd2 = ValueDecimal.get(new BigDecimal(10));
    ValueDecimal vd3 = vd1.modulus(vd2);
    assertEquals(2, vd3.getDouble());
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) ValueDecimal(org.h2.value.ValueDecimal) BigDecimal(java.math.BigDecimal)

Example 2 with ValueDecimal

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

the class Data method readValue.

/**
 * Read a value.
 *
 * @return the value
 */
public Value readValue() {
    int type = data[pos++] & 255;
    switch(type) {
        case Value.NULL:
            return ValueNull.INSTANCE;
        case BOOLEAN_TRUE:
            return ValueBoolean.TRUE;
        case BOOLEAN_FALSE:
            return ValueBoolean.FALSE;
        case INT_NEG:
            return ValueInt.get(-readVarInt());
        case Value.ENUM:
        case Value.INT:
            return ValueInt.get(readVarInt());
        case LONG_NEG:
            return ValueLong.get(-readVarLong());
        case Value.LONG:
            return ValueLong.get(readVarLong());
        case Value.BYTE:
            return ValueByte.get(readByte());
        case Value.SHORT:
            return ValueShort.get(readShortInt());
        case DECIMAL_0_1:
            return (ValueDecimal) ValueDecimal.ZERO;
        case DECIMAL_0_1 + 1:
            return (ValueDecimal) ValueDecimal.ONE;
        case DECIMAL_SMALL_0:
            return ValueDecimal.get(BigDecimal.valueOf(readVarLong()));
        case DECIMAL_SMALL:
            {
                int scale = readVarInt();
                return ValueDecimal.get(BigDecimal.valueOf(readVarLong(), scale));
            }
        case Value.DECIMAL:
            {
                int scale = readVarInt();
                int len = readVarInt();
                byte[] buff = Utils.newBytes(len);
                read(buff, 0, len);
                BigInteger b = new BigInteger(buff);
                return ValueDecimal.get(new BigDecimal(b, scale));
            }
        case LOCAL_DATE:
            {
                return ValueDate.fromDateValue(readVarLong());
            }
        case Value.DATE:
            {
                long x = readVarLong() * MILLIS_PER_MINUTE;
                return ValueDate.fromMillis(DateTimeUtils.getTimeUTCWithoutDst(x));
            }
        case LOCAL_TIME:
            {
                long nanos = readVarLong() * 1_000_000 + readVarLong();
                return ValueTime.fromNanos(nanos);
            }
        case Value.TIME:
            // need to normalize the year, month and day
            return ValueTime.fromMillis(DateTimeUtils.getTimeUTCWithoutDst(readVarLong()));
        case LOCAL_TIMESTAMP:
            {
                long dateValue = readVarLong();
                long nanos = readVarLong() * 1_000_000 + readVarLong();
                return ValueTimestamp.fromDateValueAndNanos(dateValue, nanos);
            }
        case Value.TIMESTAMP:
            {
                return ValueTimestamp.fromMillisNanos(DateTimeUtils.getTimeUTCWithoutDst(readVarLong()), readVarInt());
            }
        case Value.TIMESTAMP_TZ:
            {
                long dateValue = readVarLong();
                long nanos = readVarLong();
                short tz = (short) readVarInt();
                return ValueTimestampTimeZone.fromDateValueAndNanos(dateValue, nanos, tz);
            }
        case Value.BYTES:
            {
                int len = readVarInt();
                byte[] b = Utils.newBytes(len);
                read(b, 0, len);
                return ValueBytes.getNoCopy(b);
            }
        case Value.GEOMETRY:
            {
                int len = readVarInt();
                byte[] b = Utils.newBytes(len);
                read(b, 0, len);
                return ValueGeometry.get(b);
            }
        case Value.JAVA_OBJECT:
            {
                int len = readVarInt();
                byte[] b = Utils.newBytes(len);
                read(b, 0, len);
                return ValueJavaObject.getNoCopy(null, b, handler);
            }
        case Value.UUID:
            return ValueUuid.get(readLong(), readLong());
        case Value.STRING:
            return ValueString.get(readString());
        case Value.STRING_IGNORECASE:
            return ValueStringIgnoreCase.get(readString());
        case Value.STRING_FIXED:
            return ValueStringFixed.get(readString());
        case FLOAT_0_1:
            return ValueFloat.get(0);
        case FLOAT_0_1 + 1:
            return ValueFloat.get(1);
        case DOUBLE_0_1:
            return ValueDouble.get(0);
        case DOUBLE_0_1 + 1:
            return ValueDouble.get(1);
        case Value.DOUBLE:
            return ValueDouble.get(Double.longBitsToDouble(Long.reverse(readVarLong())));
        case Value.FLOAT:
            return ValueFloat.get(Float.intBitsToFloat(Integer.reverse(readVarInt())));
        case Value.BLOB:
        case Value.CLOB:
            {
                int smallLen = readVarInt();
                if (smallLen >= 0) {
                    byte[] small = Utils.newBytes(smallLen);
                    read(small, 0, smallLen);
                    return ValueLobDb.createSmallLob(type, small);
                } else if (smallLen == -3) {
                    int tableId = readVarInt();
                    long lobId = readVarLong();
                    long precision = readVarLong();
                    return ValueLobDb.create(type, handler, tableId, lobId, null, precision);
                } else {
                    int tableId = readVarInt();
                    int objectId = readVarInt();
                    long precision = 0;
                    boolean compression = false;
                    // including file name)
                    if (smallLen == -1 || smallLen == -2) {
                        precision = readVarLong();
                        compression = readByte() == 1;
                    }
                    if (smallLen == -2) {
                        String filename = readString();
                        return ValueLob.openUnlinked(type, handler, tableId, objectId, precision, compression, filename);
                    }
                    return ValueLob.openLinked(type, handler, tableId, objectId, precision, compression);
                }
            }
        case Value.ARRAY:
            {
                int len = readVarInt();
                Value[] list = new Value[len];
                for (int i = 0; i < len; i++) {
                    list[i] = readValue();
                }
                return ValueArray.get(list);
            }
        case Value.RESULT_SET:
            {
                SimpleResultSet rs = new SimpleResultSet();
                rs.setAutoClose(false);
                int columns = readVarInt();
                for (int i = 0; i < columns; i++) {
                    rs.addColumn(readString(), readVarInt(), readVarInt(), readVarInt());
                }
                while (readByte() != 0) {
                    Object[] o = new Object[columns];
                    for (int i = 0; i < columns; i++) {
                        o[i] = readValue().getObject();
                    }
                    rs.addRow(o);
                }
                return ValueResultSet.get(rs);
            }
        default:
            if (type >= INT_0_15 && type < INT_0_15 + 16) {
                return ValueInt.get(type - INT_0_15);
            } else if (type >= LONG_0_7 && type < LONG_0_7 + 8) {
                return ValueLong.get(type - LONG_0_7);
            } else if (type >= BYTES_0_31 && type < BYTES_0_31 + 32) {
                int len = type - BYTES_0_31;
                byte[] b = Utils.newBytes(len);
                read(b, 0, len);
                return ValueBytes.getNoCopy(b);
            } else if (type >= STRING_0_31 && type < STRING_0_31 + 32) {
                return ValueString.get(readString(type - STRING_0_31));
            }
            throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "type: " + type);
    }
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ValueDecimal(org.h2.value.ValueDecimal) BigInteger(java.math.BigInteger) ValueString(org.h2.value.ValueString) BigDecimal(java.math.BigDecimal)

Example 3 with ValueDecimal

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

the class TestValueMemory method testCompare.

private void testCompare() {
    ValueDecimal a = ValueDecimal.get(new BigDecimal("0.0"));
    ValueDecimal b = ValueDecimal.get(new BigDecimal("-0.00"));
    assertTrue(a.hashCode() != b.hashCode());
    assertFalse(a.equals(b));
}
Also used : ValueDecimal(org.h2.value.ValueDecimal) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)3 ValueDecimal (org.h2.value.ValueDecimal)3 BigInteger (java.math.BigInteger)1 AssertThrows (org.h2.test.utils.AssertThrows)1 SimpleResultSet (org.h2.tools.SimpleResultSet)1 ValueString (org.h2.value.ValueString)1