Search in sources :

Example 1 with ValueTime

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

the class TestTimeStampWithTimeZone method testConversionsImpl.

private void testConversionsImpl(String timeStr, boolean testReverse) {
    ValueTimestamp ts = ValueTimestamp.parse(timeStr);
    ValueDate d = (ValueDate) ts.convertTo(Value.DATE);
    ValueTime t = (ValueTime) ts.convertTo(Value.TIME);
    ValueTimestampTimeZone tstz = ValueTimestampTimeZone.parse(timeStr);
    assertEquals(ts, tstz.convertTo(Value.TIMESTAMP));
    assertEquals(d, tstz.convertTo(Value.DATE));
    assertEquals(t, tstz.convertTo(Value.TIME));
    assertEquals(ts.getTimestamp(), tstz.getTimestamp());
    if (testReverse) {
        assertEquals(0, tstz.compareTo(ts.convertTo(Value.TIMESTAMP_TZ), null));
        assertEquals(d.convertTo(Value.TIMESTAMP).convertTo(Value.TIMESTAMP_TZ), d.convertTo(Value.TIMESTAMP_TZ));
        assertEquals(t.convertTo(Value.TIMESTAMP).convertTo(Value.TIMESTAMP_TZ), t.convertTo(Value.TIMESTAMP_TZ));
    }
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueDate(org.h2.value.ValueDate)

Example 2 with ValueTime

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

the class TestDate method testDateTimeUtils.

private void testDateTimeUtils() {
    ValueTimestamp ts1 = ValueTimestamp.parse("-999-08-07 13:14:15.16");
    ValueTimestamp ts2 = ValueTimestamp.parse("19999-08-07 13:14:15.16");
    ValueTime t1 = (ValueTime) ts1.convertTo(Value.TIME);
    ValueTime t2 = (ValueTime) ts2.convertTo(Value.TIME);
    ValueDate d1 = (ValueDate) ts1.convertTo(Value.DATE);
    ValueDate d2 = (ValueDate) ts2.convertTo(Value.DATE);
    assertEquals("-999-08-07 13:14:15.16", ts1.getString());
    assertEquals("-999-08-07", d1.getString());
    assertEquals("13:14:15.16", t1.getString());
    assertEquals("19999-08-07 13:14:15.16", ts2.getString());
    assertEquals("19999-08-07", d2.getString());
    assertEquals("13:14:15.16", t2.getString());
    ValueTimestamp ts1a = DateTimeUtils.convertTimestamp(ts1.getTimestamp(), DateTimeUtils.createGregorianCalendar());
    ValueTimestamp ts2a = DateTimeUtils.convertTimestamp(ts2.getTimestamp(), DateTimeUtils.createGregorianCalendar());
    assertEquals("-999-08-07 13:14:15.16", ts1a.getString());
    assertEquals("19999-08-07 13:14:15.16", ts2a.getString());
    // test for bug on Java 1.8.0_60 in "Europe/Moscow" timezone.
    // Doesn't affect most other timezones
    long millis = 1407437460000L;
    long result1 = DateTimeUtils.nanosFromDate(DateTimeUtils.getTimeUTCWithoutDst(millis));
    long result2 = DateTimeUtils.nanosFromDate(DateTimeUtils.getTimeUTCWithoutDst(millis));
    assertEquals(result1, result2);
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueDate(org.h2.value.ValueDate)

Example 3 with ValueTime

use of org.h2.value.ValueTime in project ignite by apache.

the class InlineIndexHelper method compareAsDateTime.

/**
 * @param pageAddr Page address.
 * @param off Offset.
 * @param v Value to compare.
 * @param type Highest value type.
 * @return Compare result ({@code -2} means we can't compare).
 */
private int compareAsDateTime(long pageAddr, int off, Value v, int type) {
    // only compatible types are supported now.
    if (PageUtils.getByte(pageAddr, off) == type) {
        switch(type) {
            case Value.TIME:
                long nanos1 = PageUtils.getLong(pageAddr, off + 1);
                long nanos2 = ((ValueTime) v.convertTo(type)).getNanos();
                return fixSort(Long.signum(nanos1 - nanos2), sortType());
            case Value.DATE:
                long date1 = PageUtils.getLong(pageAddr, off + 1);
                long date2 = ((ValueDate) v.convertTo(type)).getDateValue();
                return fixSort(Long.signum(date1 - date2), sortType());
            case Value.TIMESTAMP:
                ValueTimestamp v0 = (ValueTimestamp) v.convertTo(type);
                date1 = PageUtils.getLong(pageAddr, off + 1);
                date2 = v0.getDateValue();
                int c = Long.signum(date1 - date2);
                if (c == 0) {
                    nanos1 = PageUtils.getLong(pageAddr, off + 9);
                    nanos2 = v0.getTimeNanos();
                    c = Long.signum(nanos1 - nanos2);
                }
                return fixSort(c, sortType());
        }
    }
    return Integer.MIN_VALUE;
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueDate(org.h2.value.ValueDate)

Example 4 with ValueTime

use of org.h2.value.ValueTime 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 5 with ValueTime

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

the class PgServerThread method writeDataColumn.

private void writeDataColumn(ResultSet rs, int column, int pgType, boolean text) throws Exception {
    Value v = ((JdbcResultSet) rs).get(column);
    if (v == ValueNull.INSTANCE) {
        writeInt(-1);
        return;
    }
    if (text) {
        // plain text
        switch(pgType) {
            case PgServer.PG_TYPE_BOOL:
                writeInt(1);
                dataOut.writeByte(v.getBoolean() ? 't' : 'f');
                break;
            default:
                byte[] data = v.getString().getBytes(getEncoding());
                writeInt(data.length);
                write(data);
        }
    } else {
        // binary
        switch(pgType) {
            case PgServer.PG_TYPE_INT2:
                writeInt(2);
                writeShort(v.getShort());
                break;
            case PgServer.PG_TYPE_INT4:
                writeInt(4);
                writeInt(v.getInt());
                break;
            case PgServer.PG_TYPE_INT8:
                writeInt(8);
                dataOut.writeLong(v.getLong());
                break;
            case PgServer.PG_TYPE_FLOAT4:
                writeInt(4);
                dataOut.writeFloat(v.getFloat());
                break;
            case PgServer.PG_TYPE_FLOAT8:
                writeInt(8);
                dataOut.writeDouble(v.getDouble());
                break;
            case PgServer.PG_TYPE_BYTEA:
                {
                    byte[] data = v.getBytesNoCopy();
                    writeInt(data.length);
                    write(data);
                    break;
                }
            case PgServer.PG_TYPE_DATE:
                {
                    ValueDate d = (ValueDate) v.convertTo(Value.DATE);
                    writeInt(4);
                    writeInt((int) (toPostgreDays(d.getDateValue())));
                    break;
                }
            case PgServer.PG_TYPE_TIME:
                {
                    ValueTime t = (ValueTime) v.convertTo(Value.TIME);
                    writeInt(8);
                    long m = t.getNanos();
                    if (INTEGER_DATE_TYPES) {
                        // long format
                        m /= 1_000;
                    } else {
                        // double format
                        m = Double.doubleToLongBits(m * 0.000_000_001);
                    }
                    dataOut.writeLong(m);
                    break;
                }
            case PgServer.PG_TYPE_TIMESTAMP_NO_TMZONE:
                {
                    ValueTimestamp t = (ValueTimestamp) v.convertTo(Value.TIMESTAMP);
                    writeInt(8);
                    long m = toPostgreDays(t.getDateValue()) * 86_400;
                    long nanos = t.getTimeNanos();
                    if (INTEGER_DATE_TYPES) {
                        // long format
                        m = m * 1_000_000 + nanos / 1_000;
                    } else {
                        // double format
                        m = Double.doubleToLongBits(m + nanos * 0.000_000_001);
                    }
                    dataOut.writeLong(m);
                    break;
                }
            default:
                throw new IllegalStateException("output binary format is undefined");
        }
    }
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) ValueDate(org.h2.value.ValueDate) JdbcResultSet(org.h2.jdbc.JdbcResultSet)

Aggregations

ValueTime (org.h2.value.ValueTime)10 ValueTimestamp (org.h2.value.ValueTimestamp)7 ValueDate (org.h2.value.ValueDate)6 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)5 BigDecimal (java.math.BigDecimal)4 Value (org.h2.value.Value)4 SimpleResultSet (org.h2.tools.SimpleResultSet)3 BigInteger (java.math.BigInteger)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 ValueLobDb (org.h2.value.ValueLobDb)2 ValueResultSet (org.h2.value.ValueResultSet)2 ValueString (org.h2.value.ValueString)2 ValueUuid (org.h2.value.ValueUuid)2 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 JdbcResultSet (org.h2.jdbc.JdbcResultSet)1