Search in sources :

Example 6 with ValueInterval

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

use of org.h2.value.ValueInterval 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 8 with ValueInterval

use of org.h2.value.ValueInterval in project gridgain by gridgain.

the class JdbcResultSet method extractObjectOfType.

private <T> T extractObjectOfType(Class<T> type, Value value) throws SQLException {
    if (value == ValueNull.INSTANCE) {
        return null;
    }
    if (type == BigDecimal.class) {
        return type.cast(value.getBigDecimal());
    } else if (type == BigInteger.class) {
        return type.cast(value.getBigDecimal().toBigInteger());
    } else if (type == String.class) {
        return type.cast(value.getString());
    } else if (type == Boolean.class) {
        return type.cast(value.getBoolean());
    } else if (type == Byte.class) {
        return type.cast(value.getByte());
    } else if (type == Short.class) {
        return type.cast(value.getShort());
    } else if (type == Integer.class) {
        return type.cast(value.getInt());
    } else if (type == Long.class) {
        return type.cast(value.getLong());
    } else if (type == Float.class) {
        return type.cast(value.getFloat());
    } else if (type == Double.class) {
        return type.cast(value.getDouble());
    } else if (type == Date.class) {
        return type.cast(value.getDate());
    } else if (type == Time.class) {
        return type.cast(value.getTime());
    } else if (type == Timestamp.class) {
        return type.cast(value.getTimestamp());
    } else if (type == java.util.Date.class) {
        return type.cast(new java.util.Date(value.getTimestamp().getTime()));
    } else if (type == Calendar.class) {
        Calendar calendar = DateTimeUtils.createGregorianCalendar();
        calendar.setTime(value.getTimestamp());
        return type.cast(calendar);
    } else if (type == UUID.class) {
        return type.cast(value.getObject());
    } else if (type == byte[].class) {
        return type.cast(value.getBytes());
    } else if (type == java.sql.Array.class) {
        int id = getNextId(TraceObject.ARRAY);
        return type.cast(new JdbcArray(conn, value, id));
    } else if (type == Blob.class) {
        int id = getNextId(TraceObject.BLOB);
        return type.cast(new JdbcBlob(conn, value, JdbcLob.State.WITH_VALUE, id));
    } else if (type == Clob.class) {
        int id = getNextId(TraceObject.CLOB);
        return type.cast(new JdbcClob(conn, value, JdbcLob.State.WITH_VALUE, id));
    } else if (type == SQLXML.class) {
        int id = getNextId(TraceObject.SQLXML);
        return type.cast(new JdbcSQLXML(conn, value, JdbcLob.State.WITH_VALUE, id));
    } else if (type == TimestampWithTimeZone.class) {
        ValueTimestampTimeZone v = (ValueTimestampTimeZone) value.convertTo(Value.TIMESTAMP_TZ);
        return type.cast(new TimestampWithTimeZone(v.getDateValue(), v.getTimeNanos(), v.getTimeZoneOffsetMins()));
    } else if (type == Interval.class) {
        if (!(value instanceof ValueInterval)) {
            value = value.convertTo(Value.INTERVAL_DAY_TO_SECOND);
        }
        ValueInterval v = (ValueInterval) value;
        return type.cast(new Interval(v.getQualifier(), false, v.getLeading(), v.getRemaining()));
    } else if (DataType.isGeometryClass(type)) {
        return type.cast(value.convertTo(Value.GEOMETRY).getObject());
    } else if (type == LocalDateTimeUtils.LOCAL_DATE) {
        return type.cast(LocalDateTimeUtils.valueToLocalDate(value));
    } else if (type == LocalDateTimeUtils.LOCAL_TIME) {
        return type.cast(LocalDateTimeUtils.valueToLocalTime(value));
    } else if (type == LocalDateTimeUtils.LOCAL_DATE_TIME) {
        return type.cast(LocalDateTimeUtils.valueToLocalDateTime(value));
    } else if (type == LocalDateTimeUtils.INSTANT) {
        return type.cast(LocalDateTimeUtils.valueToInstant(value));
    } else if (type == LocalDateTimeUtils.OFFSET_DATE_TIME) {
        return type.cast(LocalDateTimeUtils.valueToOffsetDateTime(value));
    } else if (type == LocalDateTimeUtils.PERIOD) {
        return type.cast(LocalDateTimeUtils.valueToPeriod(value));
    } else if (type == LocalDateTimeUtils.DURATION) {
        return type.cast(LocalDateTimeUtils.valueToDuration(value));
    } else {
        throw unsupported(type.getName());
    }
}
Also used : Calendar(java.util.Calendar) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) Time(java.sql.Time) ValueTime(org.gridgain.internal.h2.value.ValueTime) ValueInterval(org.gridgain.internal.h2.value.ValueInterval) ValueDouble(org.gridgain.internal.h2.value.ValueDouble) ValueDate(org.gridgain.internal.h2.value.ValueDate) Date(java.sql.Date) Array(java.sql.Array) TimestampWithTimeZone(org.gridgain.internal.h2.api.TimestampWithTimeZone) ValueLong(org.gridgain.internal.h2.value.ValueLong) BigInteger(java.math.BigInteger) ValueBoolean(org.gridgain.internal.h2.value.ValueBoolean) UUID(java.util.UUID) NClob(java.sql.NClob) Clob(java.sql.Clob) ValueShort(org.gridgain.internal.h2.value.ValueShort) Interval(org.gridgain.internal.h2.api.Interval) ValueInterval(org.gridgain.internal.h2.value.ValueInterval)

Example 9 with ValueInterval

use of org.h2.value.ValueInterval in project gridgain by gridgain.

the class TestDateTimeUtils method testParseIntervalImpl.

private void testParseIntervalImpl(IntervalQualifier qualifier, boolean negative, long leading, long remaining, String s, String full) {
    ValueInterval expected = ValueInterval.from(qualifier, negative, leading, remaining);
    assertEquals(expected, IntervalUtils.parseInterval(qualifier, negative, s));
    StringBuilder b = new StringBuilder();
    b.append("INTERVAL ").append('\'');
    if (negative) {
        b.append('-');
    }
    b.append(full).append("' ").append(qualifier);
    assertEquals(b.toString(), expected.getString());
}
Also used : ValueInterval(org.gridgain.internal.h2.value.ValueInterval)

Example 10 with ValueInterval

use of org.h2.value.ValueInterval in project gridgain by gridgain.

the class DateTimeFunctions method getIntDatePart.

/**
 * Get the specified field of a date, however with years normalized to positive
 * or negative, and month starting with 1.
 *
 * @param date
 *            the date value
 * @param field
 *            the field type, see {@link Function} for constants
 * @param mode
 *            the database mode
 * @return the value
 */
public static int getIntDatePart(Value date, int field, Mode mode) {
    if (date instanceof ValueInterval) {
        ValueInterval interval = (ValueInterval) date;
        IntervalQualifier qualifier = interval.getQualifier();
        boolean negative = interval.isNegative();
        long leading = interval.getLeading(), remaining = interval.getRemaining();
        long v;
        switch(field) {
            case YEAR:
                v = IntervalUtils.yearsFromInterval(qualifier, negative, leading, remaining);
                break;
            case MONTH:
                v = IntervalUtils.monthsFromInterval(qualifier, negative, leading, remaining);
                break;
            case DAY_OF_MONTH:
            case DAY_OF_YEAR:
                v = IntervalUtils.daysFromInterval(qualifier, negative, leading, remaining);
                break;
            case HOUR:
                v = IntervalUtils.hoursFromInterval(qualifier, negative, leading, remaining);
                break;
            case MINUTE:
                v = IntervalUtils.minutesFromInterval(qualifier, negative, leading, remaining);
                break;
            case SECOND:
                v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / NANOS_PER_SECOND;
                break;
            case MILLISECOND:
                v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000_000 % 1_000;
                break;
            case MICROSECOND:
                v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) / 1_000 % 1_000_000;
                break;
            case NANOSECOND:
                v = IntervalUtils.nanosFromInterval(qualifier, negative, leading, remaining) % NANOS_PER_SECOND;
                break;
            default:
                throw DbException.getUnsupportedException("getDatePart(" + date + ", " + field + ')');
        }
        return (int) v;
    } else {
        long[] a = DateTimeUtils.dateAndTimeFromValue(date);
        long dateValue = a[0];
        long timeNanos = a[1];
        switch(field) {
            case YEAR:
                return DateTimeUtils.yearFromDateValue(dateValue);
            case MONTH:
                return DateTimeUtils.monthFromDateValue(dateValue);
            case DAY_OF_MONTH:
                return DateTimeUtils.dayFromDateValue(dateValue);
            case HOUR:
                return (int) (timeNanos / NANOS_PER_HOUR % 24);
            case MINUTE:
                return (int) (timeNanos / NANOS_PER_MINUTE % 60);
            case SECOND:
                return (int) (timeNanos / NANOS_PER_SECOND % 60);
            case MILLISECOND:
                return (int) (timeNanos / 1_000_000 % 1_000);
            case MICROSECOND:
                return (int) (timeNanos / 1_000 % 1_000_000);
            case NANOSECOND:
                return (int) (timeNanos % NANOS_PER_SECOND);
            case DAY_OF_YEAR:
                return DateTimeUtils.getDayOfYear(dateValue);
            case DAY_OF_WEEK:
                return DateTimeUtils.getSundayDayOfWeek(dateValue);
            case DOW:
                {
                    int dow = DateTimeUtils.getSundayDayOfWeek(dateValue);
                    if (mode.getEnum() == ModeEnum.PostgreSQL) {
                        dow--;
                    }
                    return dow;
                }
            case WEEK:
                GregorianCalendar gc = DateTimeUtils.getCalendar();
                return DateTimeUtils.getWeekOfYear(dateValue, gc.getFirstDayOfWeek() - 1, gc.getMinimalDaysInFirstWeek());
            case QUARTER:
                return (DateTimeUtils.monthFromDateValue(dateValue) - 1) / 3 + 1;
            case ISO_YEAR:
                return DateTimeUtils.getIsoWeekYear(dateValue);
            case ISO_WEEK:
                return DateTimeUtils.getIsoWeekOfYear(dateValue);
            case ISO_DAY_OF_WEEK:
                return DateTimeUtils.getIsoDayOfWeek(dateValue);
            case TIMEZONE_HOUR:
            case TIMEZONE_MINUTE:
                {
                    int offsetMinutes;
                    if (date instanceof ValueTimestampTimeZone) {
                        offsetMinutes = ((ValueTimestampTimeZone) date).getTimeZoneOffsetMins();
                    } else {
                        offsetMinutes = DateTimeUtils.getTimeZoneOffsetMillis(null, dateValue, timeNanos);
                    }
                    if (field == TIMEZONE_HOUR) {
                        return offsetMinutes / 60;
                    }
                    return offsetMinutes % 60;
                }
        }
    }
    throw DbException.getUnsupportedException("getDatePart(" + date + ", " + field + ')');
}
Also used : GregorianCalendar(java.util.GregorianCalendar) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueInterval(org.gridgain.internal.h2.value.ValueInterval) IntervalQualifier(org.gridgain.internal.h2.api.IntervalQualifier)

Aggregations

ValueInterval (org.h2.value.ValueInterval)18 BigInteger (java.math.BigInteger)14 BigDecimal (java.math.BigDecimal)13 ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)11 ValueInterval (org.gridgain.internal.h2.value.ValueInterval)10 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)7 ValueTime (org.gridgain.internal.h2.value.ValueTime)6 IntervalQualifier (org.h2.api.IntervalQualifier)6 ValueDate (org.h2.value.ValueDate)6 ValueTime (org.h2.value.ValueTime)6 Value (org.gridgain.internal.h2.value.Value)5 ValueTimestampTimeZone (org.gridgain.internal.h2.value.ValueTimestampTimeZone)5 Value (org.h2.value.Value)5 ValueTimestamp (org.h2.value.ValueTimestamp)4 ResultInterface (org.gridgain.internal.h2.result.ResultInterface)3 TypeInfo (org.gridgain.internal.h2.value.TypeInfo)3 ValueDate (org.gridgain.internal.h2.value.ValueDate)3 ValueLobDb (org.gridgain.internal.h2.value.ValueLobDb)3 ValueString (org.gridgain.internal.h2.value.ValueString)3 ValueTimestamp (org.gridgain.internal.h2.value.ValueTimestamp)3