Search in sources :

Example 6 with ValueDate

use of org.gridgain.internal.h2.value.ValueDate in project SpringStudy by myounghaklee.

the class DateTimeFunction method extractEpoch.

private static ValueNumeric extractEpoch(SessionLocal session, Value value) {
    ValueNumeric result;
    if (value instanceof ValueInterval) {
        ValueInterval interval = (ValueInterval) value;
        if (interval.getQualifier().isYearMonth()) {
            interval = (ValueInterval) interval.convertTo(TypeInfo.TYPE_INTERVAL_YEAR_TO_MONTH);
            long leading = interval.getLeading();
            long remaining = interval.getRemaining();
            BigInteger bi = BigInteger.valueOf(leading).multiply(BigInteger.valueOf(31557600)).add(BigInteger.valueOf(remaining * 2592000));
            if (interval.isNegative()) {
                bi = bi.negate();
            }
            return ValueNumeric.get(bi);
        } else {
            return ValueNumeric.get(new BigDecimal(IntervalUtils.intervalToAbsolute(interval)).divide(BD_NANOS_PER_SECOND));
        }
    }
    long[] a = DateTimeUtils.dateAndTimeFromValue(value, session);
    long dateValue = a[0];
    long timeNanos = a[1];
    if (value instanceof ValueTime) {
        result = ValueNumeric.get(BigDecimal.valueOf(timeNanos).divide(BD_NANOS_PER_SECOND));
    } else if (value instanceof ValueDate) {
        result = ValueNumeric.get(// 
        BigInteger.valueOf(DateTimeUtils.absoluteDayFromDateValue(dateValue)).multiply(BI_SECONDS_PER_DAY));
    } else {
        BigDecimal bd = BigDecimal.valueOf(timeNanos).divide(BD_NANOS_PER_SECOND).add(// 
        BigDecimal.valueOf(DateTimeUtils.absoluteDayFromDateValue(dateValue)).multiply(BD_SECONDS_PER_DAY));
        if (value instanceof ValueTimestampTimeZone) {
            result = ValueNumeric.get(bd.subtract(BigDecimal.valueOf(((ValueTimestampTimeZone) value).getTimeZoneOffsetSeconds())));
        } else if (value instanceof ValueTimeTimeZone) {
            result = ValueNumeric.get(bd.subtract(BigDecimal.valueOf(((ValueTimeTimeZone) value).getTimeZoneOffsetSeconds())));
        } else {
            result = ValueNumeric.get(bd);
        }
    }
    return result;
}
Also used : ValueTime(org.h2.value.ValueTime) ValueNumeric(org.h2.value.ValueNumeric) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) BigInteger(java.math.BigInteger) ValueDate(org.h2.value.ValueDate) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) ValueInterval(org.h2.value.ValueInterval) BigDecimal(java.math.BigDecimal)

Example 7 with ValueDate

use of org.gridgain.internal.h2.value.ValueDate in project gridgain by gridgain.

the class ValueDataType method writeValue.

private void writeValue(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.BYTE:
            buff.put(BYTE).put(v.getByte());
            break;
        case Value.SHORT:
            buff.put(SHORT).putShort(v.getShort());
            break;
        case Value.ENUM:
        case Value.INT:
            {
                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.INT ? INT : ENUM).putVarInt(x);
                }
                break;
            }
        case Value.LONG:
            {
                long x = v.getLong();
                if (x < 0) {
                    buff.put(LONG_NEG).putVarLong(-x);
                } else if (x < 8) {
                    buff.put((byte) (LONG_0_7 + x));
                } else {
                    buff.put(LONG).putVarLong(x);
                }
                break;
            }
        case Value.DECIMAL:
            {
                BigDecimal x = v.getBigDecimal();
                if (BigDecimal.ZERO.equals(x)) {
                    buff.put(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(DECIMAL_SMALL_0).putVarLong(b.longValue());
                        } else {
                            buff.put(DECIMAL_SMALL).putVarInt(scale).putVarLong(b.longValue());
                        }
                    } else {
                        byte[] bytes = b.toByteArray();
                        buff.put(DECIMAL).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(TIME).putVarLong(millis).putVarLong(nanos);
                break;
            }
        case Value.DATE:
            {
                long x = ((ValueDate) v).getDateValue();
                buff.put(DATE).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(TIMESTAMP).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(TIMESTAMP_TZ).putVarLong(dateValue).putVarLong(millis).putVarLong(nanos).putVarInt(ts.getTimeZoneOffsetMins());
                break;
            }
        case Value.JAVA_OBJECT:
            {
                byte[] b = v.getBytesNoCopy();
                buff.put(JAVA_OBJECT).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(BYTES).putVarInt(b.length).put(b);
                }
                break;
            }
        case Value.UUID:
            {
                ValueUuid uuid = (ValueUuid) v;
                buff.put(UUID).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(STRING);
                    writeString(buff, s);
                }
                break;
            }
        case Value.STRING_IGNORECASE:
            buff.put(STRING_IGNORECASE);
            writeString(buff, v.getString());
            break;
        case Value.STRING_FIXED:
            buff.put(STRING_FIXED);
            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(DOUBLE_0_1);
                    } else {
                        buff.put(DOUBLE).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(FLOAT_0_1);
                    } else {
                        buff.put(FLOAT).putVarInt(Integer.reverse(f));
                    }
                }
                break;
            }
        case Value.BLOB:
        case Value.CLOB:
            {
                buff.put(type == Value.BLOB ? BLOB : CLOB);
                ValueLobDb lob = (ValueLobDb) v;
                byte[] small = lob.getSmall();
                if (small == null) {
                    buff.putVarInt(-3).putVarInt(lob.getTableId()).putVarLong(lob.getLobId()).putVarLong(lob.getType().getPrecision());
                } else {
                    buff.putVarInt(small.length).put(small);
                }
                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) {
                    writeValue(buff, x);
                }
                break;
            }
        case Value.RESULT_SET:
            {
                buff.put(RESULT_SET);
                ResultInterface result = ((ValueResultSet) v).getResult();
                int columnCount = result.getVisibleColumnCount();
                buff.putVarInt(columnCount);
                for (int i = 0; i < columnCount; i++) {
                    writeString(buff, result.getAlias(i));
                    writeString(buff, result.getColumnName(i));
                    TypeInfo columnType = result.getColumnType(i);
                    buff.putVarInt(columnType.getValueType()).putVarLong(columnType.getPrecision()).putVarInt(columnType.getScale());
                }
                while (result.next()) {
                    buff.put((byte) 1);
                    Value[] row = result.currentRow();
                    for (int i = 0; i < columnCount; i++) {
                        writeValue(buff, row[i]);
                    }
                }
                buff.put((byte) 0);
                break;
            }
        case Value.GEOMETRY:
            {
                byte[] b = v.getBytes();
                int len = b.length;
                buff.put(GEOMETRY).putVarInt(len).put(b);
                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;
            }
        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.getValueType());
    }
}
Also used : ValueUuid(org.gridgain.internal.h2.value.ValueUuid) ValueLobDb(org.gridgain.internal.h2.value.ValueLobDb) ResultInterface(org.gridgain.internal.h2.result.ResultInterface) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueString(org.gridgain.internal.h2.value.ValueString) ValueInterval(org.gridgain.internal.h2.value.ValueInterval) TypeInfo(org.gridgain.internal.h2.value.TypeInfo) BigDecimal(java.math.BigDecimal) ValueTime(org.gridgain.internal.h2.value.ValueTime) ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) Value(org.gridgain.internal.h2.value.Value) BigInteger(java.math.BigInteger)

Example 8 with ValueDate

use of org.gridgain.internal.h2.value.ValueDate in project gridgain by gridgain.

the class Data method getValueLen.

/**
 * Calculate the number of bytes required to encode the given value.
 *
 * @param v the value
 * @param storeLocalTime
 *            calculate size of DATE, TIME, and TIMESTAMP values with local
 *            time storage format
 * @return the number of bytes required to store this value
 */
public static int getValueLen(Object o, boolean storeLocalTime) {
    if (o instanceof Value) {
        Value v = (Value) o;
        if (v == ValueNull.INSTANCE) {
            return 1;
        }
        switch(v.getValueType()) {
            case Value.BOOLEAN:
                return 1;
            case Value.BYTE:
                return 2;
            case Value.SHORT:
                return 3;
            case Value.ENUM:
            case Value.INT:
                {
                    int x = v.getInt();
                    if (x < 0) {
                        return 1 + getVarIntLen(-x);
                    } else if (x < 16) {
                        return 1;
                    } else {
                        return 1 + getVarIntLen(x);
                    }
                }
            case Value.LONG:
                {
                    long x = v.getLong();
                    if (x < 0) {
                        return 1 + getVarLongLen(-x);
                    } else if (x < 8) {
                        return 1;
                    } else {
                        return 1 + getVarLongLen(x);
                    }
                }
            case Value.DOUBLE:
                {
                    double x = v.getDouble();
                    if (x == 1.0d) {
                        return 1;
                    }
                    long d = Double.doubleToLongBits(x);
                    if (d == ValueDouble.ZERO_BITS) {
                        return 1;
                    }
                    return 1 + getVarLongLen(Long.reverse(d));
                }
            case Value.FLOAT:
                {
                    float x = v.getFloat();
                    if (x == 1.0f) {
                        return 1;
                    }
                    int f = Float.floatToIntBits(x);
                    if (f == ValueFloat.ZERO_BITS) {
                        return 1;
                    }
                    return 1 + getVarIntLen(Integer.reverse(f));
                }
            case Value.STRING:
                {
                    String s = v.getString();
                    int len = s.length();
                    if (len < 32) {
                        return 1 + getStringWithoutLengthLen(s, len);
                    }
                    return 1 + getStringLen(s);
                }
            case Value.STRING_IGNORECASE:
            case Value.STRING_FIXED:
                return 1 + getStringLen(v.getString());
            case Value.DECIMAL:
                {
                    BigDecimal x = v.getBigDecimal();
                    if (BigDecimal.ZERO.equals(x)) {
                        return 1;
                    } else if (BigDecimal.ONE.equals(x)) {
                        return 1;
                    }
                    int scale = x.scale();
                    BigInteger b = x.unscaledValue();
                    int bits = b.bitLength();
                    if (bits <= 63) {
                        if (scale == 0) {
                            return 1 + getVarLongLen(b.longValue());
                        }
                        return 1 + getVarIntLen(scale) + getVarLongLen(b.longValue());
                    }
                    byte[] bytes = b.toByteArray();
                    return 1 + getVarIntLen(scale) + getVarIntLen(bytes.length) + bytes.length;
                }
            case Value.TIME:
                if (storeLocalTime) {
                    long nanos = ((ValueTime) v).getNanos();
                    long millis = nanos / 1_000_000;
                    nanos -= millis * 1_000_000;
                    return 1 + getVarLongLen(millis) + getVarLongLen(nanos);
                }
                return 1 + getVarLongLen(DateTimeUtils.getTimeLocalWithoutDst(v.getTime()));
            case Value.DATE:
                {
                    if (storeLocalTime) {
                        long dateValue = ((ValueDate) v).getDateValue();
                        return 1 + getVarLongLen(dateValue);
                    }
                    long x = DateTimeUtils.getTimeLocalWithoutDst(v.getDate());
                    return 1 + getVarLongLen(x / MILLIS_PER_MINUTE);
                }
            case Value.TIMESTAMP:
                {
                    if (storeLocalTime) {
                        ValueTimestamp ts = (ValueTimestamp) v;
                        long dateValue = ts.getDateValue();
                        long nanos = ts.getTimeNanos();
                        long millis = nanos / 1_000_000;
                        nanos -= millis * 1_000_000;
                        return 1 + getVarLongLen(dateValue) + getVarLongLen(millis) + getVarLongLen(nanos);
                    }
                    Timestamp ts = v.getTimestamp();
                    return 1 + getVarLongLen(DateTimeUtils.getTimeLocalWithoutDst(ts)) + getVarIntLen(ts.getNanos() % 1_000_000);
                }
            case Value.TIMESTAMP_TZ:
                {
                    ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
                    long dateValue = ts.getDateValue();
                    long nanos = ts.getTimeNanos();
                    short tz = ts.getTimeZoneOffsetMins();
                    return 1 + getVarLongLen(dateValue) + getVarLongLen(nanos) + getVarIntLen(tz);
                }
            case Value.GEOMETRY:
            case Value.JAVA_OBJECT:
                {
                    byte[] b = v.getBytesNoCopy();
                    return 1 + getVarIntLen(b.length) + b.length;
                }
            case Value.BYTES:
                {
                    byte[] b = v.getBytesNoCopy();
                    int len = b.length;
                    if (len < 32) {
                        return 1 + b.length;
                    }
                    return 1 + getVarIntLen(b.length) + b.length;
                }
            case Value.UUID:
                return 1 + LENGTH_LONG + LENGTH_LONG;
            case Value.BLOB:
            case Value.CLOB:
                {
                    int len = 1;
                    if (v instanceof ValueLob) {
                        ValueLob lob = (ValueLob) v;
                        byte[] small = lob.getSmall();
                        if (small == null) {
                            int t = -1;
                            if (!lob.isLinkedToTable()) {
                                t = -2;
                            }
                            len += getVarIntLen(t);
                            len += getVarIntLen(lob.getTableId());
                            len += getVarIntLen(lob.getObjectId());
                            len += getVarLongLen(lob.getType().getPrecision());
                            len += 1;
                            if (t == -2) {
                                len += getStringLen(lob.getFileName());
                            }
                        } else {
                            len += getVarIntLen(small.length);
                            len += small.length;
                        }
                    } else {
                        ValueLobDb lob = (ValueLobDb) v;
                        byte[] small = lob.getSmall();
                        if (small == null) {
                            len += getVarIntLen(-3);
                            len += getVarIntLen(lob.getTableId());
                            len += getVarLongLen(lob.getLobId());
                            len += getVarLongLen(lob.getType().getPrecision());
                        } else {
                            len += getVarIntLen(small.length);
                            len += small.length;
                        }
                    }
                    return len;
                }
            case Value.ARRAY:
            case Value.ROW:
                {
                    Value[] list = ((ValueCollectionBase) v).getList();
                    int len = 1 + getVarIntLen(list.length);
                    for (Value x : list) {
                        len += getValueLen(x, storeLocalTime);
                    }
                    return len;
                }
            case Value.RESULT_SET:
                {
                    int len = 1;
                    ResultInterface result = ((ValueResultSet) v).getResult();
                    int columnCount = result.getVisibleColumnCount();
                    len += getVarIntLen(columnCount);
                    for (int i = 0; i < columnCount; i++) {
                        len += getStringLen(result.getAlias(i));
                        len += getStringLen(result.getColumnName(i));
                        TypeInfo columnType = result.getColumnType(i);
                        len += getVarIntLen(columnType.getValueType());
                        len += getVarLongLen(columnType.getPrecision());
                        len += getVarIntLen(columnType.getScale());
                    }
                    while (result.next()) {
                        len++;
                        Value[] row = result.currentRow();
                        for (int i = 0; i < columnCount; i++) {
                            Value val = row[i];
                            len += getValueLen(val, storeLocalTime);
                        }
                    }
                    len++;
                    return len;
                }
            case Value.INTERVAL_YEAR:
            case Value.INTERVAL_MONTH:
            case Value.INTERVAL_DAY:
            case Value.INTERVAL_HOUR:
            case Value.INTERVAL_MINUTE:
                {
                    ValueInterval interval = (ValueInterval) v;
                    return 2 + getVarLongLen(interval.getLeading());
                }
            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;
                    return 2 + getVarLongLen(interval.getLeading()) + getVarLongLen(interval.getRemaining());
                }
            default:
                if (JdbcUtils.customDataTypesHandler != null) {
                    byte[] b = v.getBytesNoCopy();
                    return 1 + getVarIntLen(v.getValueType()) + getVarIntLen(b.length) + b.length;
                }
                throw DbException.throwInternalError("type=" + v.getValueType());
        }
    } else if (o instanceof AggregateDataCount) {
        // 2 x Byte + Long.
        return 2 * 2 + 10;
    } else if (o instanceof AggregateDataDefault) {
        AggregateDataDefault a = (AggregateDataDefault) o;
        // Byte + 2 x Int + 3 x Long + ValueLen.
        return 2 + 2 * 6 + 3 * 10 + getValueLen(a.value() == null ? ValueNull.INSTANCE : a.value(), storeLocalTime);
    } else if (o instanceof AggregateDataCollecting) {
        AggregateDataCollecting a = (AggregateDataCollecting) o;
        // 2 x Byte + Int
        int len = 2 * 2 + 6;
        len += getValueLen(a.getSharedArgument() == null ? ValueNull.INSTANCE : a.getSharedArgument(), storeLocalTime);
        Collection<Value> values = a.values();
        if (values != null) {
            for (Value v : values) len += getValueLen(v, storeLocalTime);
        }
        return len;
    } else
        throw DbException.throwInternalError("unknown type=" + o.getClass());
}
Also used : AggregateDataCount(org.gridgain.internal.h2.expression.aggregate.AggregateDataCount) ValueLob(org.gridgain.internal.h2.value.ValueLob) ValueLobDb(org.gridgain.internal.h2.value.ValueLobDb) ResultInterface(org.gridgain.internal.h2.result.ResultInterface) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueString(org.gridgain.internal.h2.value.ValueString) ValueInterval(org.gridgain.internal.h2.value.ValueInterval) Timestamp(java.sql.Timestamp) ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) TypeInfo(org.gridgain.internal.h2.value.TypeInfo) BigDecimal(java.math.BigDecimal) ValueTime(org.gridgain.internal.h2.value.ValueTime) AggregateDataCollecting(org.gridgain.internal.h2.expression.aggregate.AggregateDataCollecting) ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) Value(org.gridgain.internal.h2.value.Value) BigInteger(java.math.BigInteger) Collection(java.util.Collection) AggregateDataDefault(org.gridgain.internal.h2.expression.aggregate.AggregateDataDefault)

Example 9 with ValueDate

use of org.gridgain.internal.h2.value.ValueDate in project gridgain by gridgain.

the class PgServerThread method writeDataColumn.

private void writeDataColumn(ResultSet rs, int column, int pgType, boolean text) throws IOException {
    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.gridgain.internal.h2.value.ValueTime) ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) Value(org.gridgain.internal.h2.value.Value) ValueDate(org.gridgain.internal.h2.value.ValueDate) JdbcResultSet(org.gridgain.internal.h2.jdbc.JdbcResultSet)

Example 10 with ValueDate

use of org.gridgain.internal.h2.value.ValueDate in project gridgain by gridgain.

the class TestDate method testValueDate.

private void testValueDate() {
    assertEquals("2000-01-01", ValueDate.get(Date.valueOf("2000-01-01")).getString());
    assertEquals("0-00-00", ValueDate.fromDateValue(0).getString());
    assertEquals("9999-12-31", ValueDate.parse("9999-12-31").getString());
    assertEquals("-9999-12-31", ValueDate.parse("-9999-12-31").getString());
    assertEquals(Integer.MAX_VALUE + "-12-31", ValueDate.parse(Integer.MAX_VALUE + "-12-31").getString());
    assertEquals(Integer.MIN_VALUE + "-12-31", ValueDate.parse(Integer.MIN_VALUE + "-12-31").getString());
    ValueDate d1 = ValueDate.parse("2001-01-01");
    assertEquals("2001-01-01", d1.getDate().toString());
    assertEquals("DATE '2001-01-01'", d1.getSQL());
    assertEquals("DATE '2001-01-01'", d1.toString());
    assertEquals(Value.DATE, d1.getValueType());
    long dv = d1.getDateValue();
    assertEquals((int) ((dv >>> 32) ^ dv), d1.hashCode());
    TypeInfo type = d1.getType();
    assertEquals(d1.getString().length(), type.getDisplaySize());
    assertEquals(ValueDate.PRECISION, type.getPrecision());
    assertEquals("java.sql.Date", d1.getObject().getClass().getName());
    ValueDate d1b = ValueDate.parse("2001-01-01");
    assertTrue(d1 == d1b);
    Value.clearCache();
    d1b = ValueDate.parse("2001-01-01");
    assertFalse(d1 == d1b);
    assertTrue(d1.equals(d1));
    assertTrue(d1.equals(d1b));
    assertTrue(d1b.equals(d1));
    assertEquals(0, d1.compareTo(d1b, null, null));
    assertEquals(0, d1b.compareTo(d1, null, null));
    ValueDate d2 = ValueDate.parse("2002-02-02");
    assertFalse(d1.equals(d2));
    assertFalse(d2.equals(d1));
    assertEquals(-1, d1.compareTo(d2, null, null));
    assertEquals(1, d2.compareTo(d1, null, null));
    // can't convert using java.util.Date
    assertEquals(Integer.MAX_VALUE + "-12-31 00:00:00", ValueDate.parse(Integer.MAX_VALUE + "-12-31").convertTo(Value.TIMESTAMP).getString());
    assertEquals(Integer.MIN_VALUE + "-12-31 00:00:00", ValueDate.parse(Integer.MIN_VALUE + "-12-31").convertTo(Value.TIMESTAMP).getString());
    assertEquals("00:00:00", ValueDate.parse(Integer.MAX_VALUE + "-12-31").convertTo(Value.TIME).getString());
    assertEquals("00:00:00", ValueDate.parse(Integer.MIN_VALUE + "-12-31").convertTo(Value.TIME).getString());
}
Also used : ValueDate(org.gridgain.internal.h2.value.ValueDate) TypeInfo(org.gridgain.internal.h2.value.TypeInfo)

Aggregations

ValueDate (org.h2.value.ValueDate)23 ValueTime (org.h2.value.ValueTime)18 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)16 ValueTimestamp (org.h2.value.ValueTimestamp)15 BigDecimal (java.math.BigDecimal)10 ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)10 BigInteger (java.math.BigInteger)9 ValueDate (org.gridgain.internal.h2.value.ValueDate)9 ValueTime (org.gridgain.internal.h2.value.ValueTime)9 Value (org.gridgain.internal.h2.value.Value)8 ValueTimestamp (org.gridgain.internal.h2.value.ValueTimestamp)8 ValueTimestampTimeZone (org.gridgain.internal.h2.value.ValueTimestampTimeZone)8 ValueInterval (org.gridgain.internal.h2.value.ValueInterval)6 TypeInfo (org.gridgain.internal.h2.value.TypeInfo)4 ValueInterval (org.h2.value.ValueInterval)4 ResultInterface (org.gridgain.internal.h2.result.ResultInterface)3 ValueLobDb (org.gridgain.internal.h2.value.ValueLobDb)3 ValueString (org.gridgain.internal.h2.value.ValueString)3 Value (org.h2.value.Value)3 Date (java.sql.Date)2