Search in sources :

Example 1 with ValueUuid

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

the class TestValue method testUUID.

private void testUUID() {
    long maxHigh = 0, maxLow = 0, minHigh = -1L, minLow = -1L;
    for (int i = 0; i < 100; i++) {
        ValueUuid uuid = ValueUuid.getNewRandom();
        maxHigh |= uuid.getHigh();
        maxLow |= uuid.getLow();
        minHigh &= uuid.getHigh();
        minLow &= uuid.getLow();
    }
    ValueUuid max = ValueUuid.get(maxHigh, maxLow);
    assertEquals("ffffffff-ffff-4fff-bfff-ffffffffffff", max.getString());
    ValueUuid min = ValueUuid.get(minHigh, minLow);
    assertEquals("00000000-0000-4000-8000-000000000000", min.getString());
    // Test conversion from ValueJavaObject to ValueUuid
    String uuidStr = "12345678-1234-4321-8765-123456789012";
    UUID origUUID = UUID.fromString(uuidStr);
    ValueJavaObject valObj = ValueJavaObject.getNoCopy(origUUID, null, null);
    Value valUUID = valObj.convertTo(Value.UUID);
    assertTrue(valUUID instanceof ValueUuid);
    assertTrue(valUUID.getString().equals(uuidStr));
    assertTrue(valUUID.getObject().equals(origUUID));
    ValueJavaObject voString = ValueJavaObject.getNoCopy(new String("This is not a ValueUuid object"), null, null);
    assertThrows(DbException.class, voString).convertTo(Value.UUID);
}
Also used : ValueUuid(org.h2.value.ValueUuid) ValueJavaObject(org.h2.value.ValueJavaObject) Value(org.h2.value.Value) ValueString(org.h2.value.ValueString) UUID(java.util.UUID) DbException(org.h2.message.DbException)

Example 2 with ValueUuid

use of org.h2.value.ValueUuid 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 3 with ValueUuid

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

the class Column method convertAutoIncrementToSequence.

/**
 * Convert the auto-increment flag to a sequence that is linked with this
 * table.
 *
 * @param session the session
 * @param schema the schema where the sequence should be generated
 * @param id the object id
 * @param temporary true if the sequence is temporary and does not need to
 *            be stored
 */
public void convertAutoIncrementToSequence(Session session, Schema schema, int id, boolean temporary) {
    if (!autoIncrement) {
        DbException.throwInternalError();
    }
    if ("IDENTITY".equals(originalSQL)) {
        originalSQL = "BIGINT";
    } else if ("SERIAL".equals(originalSQL)) {
        originalSQL = "INT";
    }
    String sequenceName;
    do {
        ValueUuid uuid = ValueUuid.getNewRandom();
        String s = uuid.getString();
        s = StringUtils.toUpperEnglish(s.replace('-', '_'));
        sequenceName = "SYSTEM_SEQUENCE_" + s;
    } while (schema.findSequence(sequenceName) != null);
    Sequence seq = new Sequence(schema, id, sequenceName, start, increment);
    seq.setTemporary(temporary);
    session.getDatabase().addSchemaObject(session, seq);
    setAutoIncrement(false, 0, 0);
    SequenceValue seqValue = new SequenceValue(seq);
    setDefaultExpression(session, seqValue);
    setSequence(seq);
}
Also used : ValueUuid(org.h2.value.ValueUuid) SequenceValue(org.h2.expression.SequenceValue) ValueString(org.h2.value.ValueString) Sequence(org.h2.schema.Sequence)

Example 4 with ValueUuid

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

the class ValueDataType method writeValue.

private void writeValue(WriteBuffer buff, Value v) {
    if (v == ValueNull.INSTANCE) {
        buff.put((byte) 0);
        return;
    }
    int type = v.getType();
    switch(type) {
        case Value.BOOLEAN:
            buff.put((byte) (v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE));
            break;
        case Value.BYTE:
            buff.put((byte) type).put(v.getByte());
            break;
        case Value.SHORT:
            buff.put((byte) type).putShort(v.getShort());
            break;
        case Value.ENUM:
        case Value.INT:
            {
                int x = v.getInt();
                if (x < 0) {
                    buff.put((byte) INT_NEG).putVarInt(-x);
                } else if (x < 16) {
                    buff.put((byte) (INT_0_15 + x));
                } else {
                    buff.put((byte) type).putVarInt(x);
                }
                break;
            }
        case Value.LONG:
            {
                long x = v.getLong();
                if (x < 0) {
                    buff.put((byte) LONG_NEG).putVarLong(-x);
                } else if (x < 8) {
                    buff.put((byte) (LONG_0_7 + x));
                } else {
                    buff.put((byte) type).putVarLong(x);
                }
                break;
            }
        case Value.DECIMAL:
            {
                BigDecimal x = v.getBigDecimal();
                if (BigDecimal.ZERO.equals(x)) {
                    buff.put((byte) DECIMAL_0_1);
                } else if (BigDecimal.ONE.equals(x)) {
                    buff.put((byte) (DECIMAL_0_1 + 1));
                } else {
                    int scale = x.scale();
                    BigInteger b = x.unscaledValue();
                    int bits = b.bitLength();
                    if (bits <= 63) {
                        if (scale == 0) {
                            buff.put((byte) DECIMAL_SMALL_0).putVarLong(b.longValue());
                        } else {
                            buff.put((byte) DECIMAL_SMALL).putVarInt(scale).putVarLong(b.longValue());
                        }
                    } else {
                        byte[] bytes = b.toByteArray();
                        buff.put((byte) type).putVarInt(scale).putVarInt(bytes.length).put(bytes);
                    }
                }
                break;
            }
        case Value.TIME:
            {
                ValueTime t = (ValueTime) v;
                long nanos = t.getNanos();
                long millis = nanos / 1000000;
                nanos -= millis * 1000000;
                buff.put((byte) type).putVarLong(millis).putVarLong(nanos);
                break;
            }
        case Value.DATE:
            {
                long x = ((ValueDate) v).getDateValue();
                buff.put((byte) type).putVarLong(x);
                break;
            }
        case Value.TIMESTAMP:
            {
                ValueTimestamp ts = (ValueTimestamp) v;
                long dateValue = ts.getDateValue();
                long nanos = ts.getTimeNanos();
                long millis = nanos / 1000000;
                nanos -= millis * 1000000;
                buff.put((byte) type).putVarLong(dateValue).putVarLong(millis).putVarLong(nanos);
                break;
            }
        case Value.TIMESTAMP_TZ:
            {
                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
                long dateValue = ts.getDateValue();
                long nanos = ts.getTimeNanos();
                long millis = nanos / 1000000;
                nanos -= millis * 1000000;
                buff.put((byte) type).putVarLong(dateValue).putVarLong(millis).putVarLong(nanos).putVarInt(ts.getTimeZoneOffsetMins());
                break;
            }
        case Value.JAVA_OBJECT:
            {
                byte[] b = v.getBytesNoCopy();
                buff.put((byte) type).putVarInt(b.length).put(b);
                break;
            }
        case Value.BYTES:
            {
                byte[] b = v.getBytesNoCopy();
                int len = b.length;
                if (len < 32) {
                    buff.put((byte) (BYTES_0_31 + len)).put(b);
                } else {
                    buff.put((byte) type).putVarInt(b.length).put(b);
                }
                break;
            }
        case Value.UUID:
            {
                ValueUuid uuid = (ValueUuid) v;
                buff.put((byte) type).putLong(uuid.getHigh()).putLong(uuid.getLow());
                break;
            }
        case Value.STRING:
            {
                String s = v.getString();
                int len = s.length();
                if (len < 32) {
                    buff.put((byte) (STRING_0_31 + len)).putStringData(s, len);
                } else {
                    buff.put((byte) type);
                    writeString(buff, s);
                }
                break;
            }
        case Value.STRING_IGNORECASE:
        case Value.STRING_FIXED:
            buff.put((byte) type);
            writeString(buff, 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((byte) DOUBLE_0_1);
                    } else {
                        buff.put((byte) type).putVarLong(Long.reverse(d));
                    }
                }
                break;
            }
        case Value.FLOAT:
            {
                float x = v.getFloat();
                if (x == 1.0f) {
                    buff.put((byte) (FLOAT_0_1 + 1));
                } else {
                    int f = Float.floatToIntBits(x);
                    if (f == ValueFloat.ZERO_BITS) {
                        buff.put((byte) FLOAT_0_1);
                    } else {
                        buff.put((byte) type).putVarInt(Integer.reverse(f));
                    }
                }
                break;
            }
        case Value.BLOB:
        case Value.CLOB:
            {
                buff.put((byte) type);
                ValueLobDb lob = (ValueLobDb) v;
                byte[] small = lob.getSmall();
                if (small == null) {
                    buff.putVarInt(-3).putVarInt(lob.getTableId()).putVarLong(lob.getLobId()).putVarLong(lob.getPrecision());
                } else {
                    buff.putVarInt(small.length).put(small);
                }
                break;
            }
        case Value.ARRAY:
            {
                Value[] list = ((ValueArray) v).getList();
                buff.put((byte) type).putVarInt(list.length);
                for (Value x : list) {
                    writeValue(buff, x);
                }
                break;
            }
        case Value.RESULT_SET:
            {
                buff.put((byte) type);
                try {
                    ResultSet rs = ((ValueResultSet) v).getResultSet();
                    rs.beforeFirst();
                    ResultSetMetaData meta = rs.getMetaData();
                    int columnCount = meta.getColumnCount();
                    buff.putVarInt(columnCount);
                    for (int i = 0; i < columnCount; i++) {
                        writeString(buff, meta.getColumnName(i + 1));
                        buff.putVarInt(meta.getColumnType(i + 1)).putVarInt(meta.getPrecision(i + 1)).putVarInt(meta.getScale(i + 1));
                    }
                    while (rs.next()) {
                        buff.put((byte) 1);
                        for (int i = 0; i < columnCount; i++) {
                            int t = org.h2.value.DataType.getValueTypeFromResultSet(meta, i + 1);
                            Value val = org.h2.value.DataType.readValue(null, rs, i + 1, t);
                            writeValue(buff, val);
                        }
                    }
                    buff.put((byte) 0);
                    rs.beforeFirst();
                } catch (SQLException e) {
                    throw DbException.convert(e);
                }
                break;
            }
        case Value.GEOMETRY:
            {
                byte[] b = v.getBytes();
                int len = b.length;
                buff.put((byte) type).putVarInt(len).put(b);
                break;
            }
        default:
            if (JdbcUtils.customDataTypesHandler != null) {
                byte[] b = v.getBytesNoCopy();
                buff.put((byte) CUSTOM_DATA_TYPE).putVarInt(type).putVarInt(b.length).put(b);
                break;
            }
            DbException.throwInternalError("type=" + v.getType());
    }
}
Also used : ValueUuid(org.h2.value.ValueUuid) ValueLobDb(org.h2.value.ValueLobDb) SQLException(java.sql.SQLException) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueString(org.h2.value.ValueString) 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)

Aggregations

ValueString (org.h2.value.ValueString)4 ValueUuid (org.h2.value.ValueUuid)4 Value (org.h2.value.Value)3 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 SimpleResultSet (org.h2.tools.SimpleResultSet)2 ValueLobDb (org.h2.value.ValueLobDb)2 ValueResultSet (org.h2.value.ValueResultSet)2 ValueTime (org.h2.value.ValueTime)2 ValueTimestamp (org.h2.value.ValueTimestamp)2 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)2 Timestamp (java.sql.Timestamp)1 UUID (java.util.UUID)1 SequenceValue (org.h2.expression.SequenceValue)1 DbException (org.h2.message.DbException)1 Sequence (org.h2.schema.Sequence)1 ValueJavaObject (org.h2.value.ValueJavaObject)1