Search in sources :

Example 11 with ValueDate

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

the class CompatibilityDatePlusTimeOperation method getValue.

@Override
public Value getValue(SessionLocal session) {
    Value l = left.getValue(session);
    Value r = right.getValue(session);
    if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
        return ValueNull.INSTANCE;
    }
    switch(l.getValueType()) {
        case Value.TIME:
            if (r.getValueType() == Value.DATE) {
                return // 
                ValueTimestamp.fromDateValueAndNanos(// 
                ((ValueDate) r).getDateValue(), ((ValueTime) l).getNanos());
            }
            break;
        case Value.TIME_TZ:
            if (r.getValueType() == Value.DATE) {
                ValueTimeTimeZone t = (ValueTimeTimeZone) l;
                return ValueTimestampTimeZone.fromDateValueAndNanos(((ValueDate) r).getDateValue(), t.getNanos(), t.getTimeZoneOffsetSeconds());
            }
            break;
        case Value.TIMESTAMP:
            {
                if (r.getValueType() == Value.TIME_TZ) {
                    ValueTimestamp ts = (ValueTimestamp) l;
                    l = ValueTimestampTimeZone.fromDateValueAndNanos(ts.getDateValue(), ts.getTimeNanos(), ((ValueTimeTimeZone) r).getTimeZoneOffsetSeconds());
                }
                break;
            }
    }
    long[] a = DateTimeUtils.dateAndTimeFromValue(l, session);
    long dateValue = a[0], timeNanos = a[1] + (r instanceof ValueTime ? ((ValueTime) r).getNanos() : ((ValueTimeTimeZone) r).getNanos());
    if (timeNanos >= NANOS_PER_DAY) {
        timeNanos -= NANOS_PER_DAY;
        dateValue = DateTimeUtils.incrementDateValue(dateValue);
    }
    return DateTimeUtils.dateTimeToValue(l, dateValue, timeNanos);
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone)

Example 12 with ValueDate

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

the class IntervalOperation method getValue.

@Override
public Value getValue(SessionLocal session) {
    Value l = left.getValue(session);
    Value r = right.getValue(session);
    if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
        return ValueNull.INSTANCE;
    }
    int lType = l.getValueType(), rType = r.getValueType();
    switch(opType) {
        case INTERVAL_PLUS_INTERVAL:
        case INTERVAL_MINUS_INTERVAL:
            {
                BigInteger a1 = IntervalUtils.intervalToAbsolute((ValueInterval) l);
                BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
                return IntervalUtils.intervalFromAbsolute(IntervalQualifier.valueOf(Value.getHigherOrder(lType, rType) - Value.INTERVAL_YEAR), opType == IntervalOpType.INTERVAL_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2));
            }
        case INTERVAL_DIVIDE_INTERVAL:
            return ValueNumeric.get(IntervalUtils.intervalToAbsolute((ValueInterval) l)).divide(ValueNumeric.get(IntervalUtils.intervalToAbsolute((ValueInterval) r)), type);
        case DATETIME_PLUS_INTERVAL:
        case DATETIME_MINUS_INTERVAL:
            return getDateTimeWithInterval(session, l, r, lType, rType);
        case INTERVAL_MULTIPLY_NUMERIC:
        case INTERVAL_DIVIDE_NUMERIC:
            {
                BigDecimal a1 = new BigDecimal(IntervalUtils.intervalToAbsolute((ValueInterval) l));
                BigDecimal a2 = r.getBigDecimal();
                return IntervalUtils.intervalFromAbsolute(IntervalQualifier.valueOf(lType - Value.INTERVAL_YEAR), (opType == IntervalOpType.INTERVAL_MULTIPLY_NUMERIC ? a1.multiply(a2) : a1.divide(a2)).toBigInteger());
            }
        case DATETIME_MINUS_DATETIME:
            {
                Value result;
                if ((lType == Value.TIME || lType == Value.TIME_TZ) && (rType == Value.TIME || rType == Value.TIME_TZ)) {
                    long diff;
                    if (lType == Value.TIME && rType == Value.TIME) {
                        diff = ((ValueTime) l).getNanos() - ((ValueTime) r).getNanos();
                    } else {
                        ValueTimeTimeZone left = (ValueTimeTimeZone) l.convertTo(TypeInfo.TYPE_TIME_TZ, session), right = (ValueTimeTimeZone) r.convertTo(TypeInfo.TYPE_TIME_TZ, session);
                        diff = left.getNanos() - right.getNanos() + (right.getTimeZoneOffsetSeconds() - left.getTimeZoneOffsetSeconds()) * DateTimeUtils.NANOS_PER_SECOND;
                    }
                    boolean negative = diff < 0;
                    if (negative) {
                        diff = -diff;
                    }
                    result = ValueInterval.from(IntervalQualifier.HOUR_TO_SECOND, negative, diff / NANOS_PER_HOUR, diff % NANOS_PER_HOUR);
                } else if (forcedType != null && DataType.isYearMonthIntervalType(forcedType.getValueType())) {
                    long[] dt1 = dateAndTimeFromValue(l, session), dt2 = dateAndTimeFromValue(r, session);
                    long dateValue1 = lType == Value.TIME || lType == Value.TIME_TZ ? session.currentTimestamp().getDateValue() : dt1[0];
                    long dateValue2 = rType == Value.TIME || rType == Value.TIME_TZ ? session.currentTimestamp().getDateValue() : dt2[0];
                    long leading = 12L * (DateTimeUtils.yearFromDateValue(dateValue1) - DateTimeUtils.yearFromDateValue(dateValue2)) + DateTimeUtils.monthFromDateValue(dateValue1) - DateTimeUtils.monthFromDateValue(dateValue2);
                    int d1 = DateTimeUtils.dayFromDateValue(dateValue1);
                    int d2 = DateTimeUtils.dayFromDateValue(dateValue2);
                    if (leading >= 0) {
                        if (d1 < d2 || d1 == d2 && dt1[1] < dt2[1]) {
                            leading--;
                        }
                    } else if (d1 > d2 || d1 == d2 && dt1[1] > dt2[1]) {
                        leading++;
                    }
                    boolean negative;
                    if (leading < 0) {
                        negative = true;
                        leading = -leading;
                    } else {
                        negative = false;
                    }
                    result = ValueInterval.from(IntervalQualifier.MONTH, negative, leading, 0L);
                } else if (lType == Value.DATE && rType == Value.DATE) {
                    long diff = absoluteDayFromDateValue(((ValueDate) l).getDateValue()) - absoluteDayFromDateValue(((ValueDate) r).getDateValue());
                    boolean negative = diff < 0;
                    if (negative) {
                        diff = -diff;
                    }
                    result = ValueInterval.from(IntervalQualifier.DAY, negative, diff, 0L);
                } else {
                    BigInteger diff = nanosFromValue(session, l).subtract(nanosFromValue(session, r));
                    if (lType == Value.TIMESTAMP_TZ || rType == Value.TIMESTAMP_TZ) {
                        l = l.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, session);
                        r = r.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, session);
                        diff = diff.add(BigInteger.valueOf((((ValueTimestampTimeZone) r).getTimeZoneOffsetSeconds() - ((ValueTimestampTimeZone) l).getTimeZoneOffsetSeconds()) * NANOS_PER_SECOND));
                    }
                    result = IntervalUtils.intervalFromAbsolute(IntervalQualifier.DAY_TO_SECOND, diff);
                }
                if (forcedType != null) {
                    result = result.castTo(forcedType, session);
                }
                return result;
            }
    }
    throw DbException.getInternalError("type=" + opType);
}
Also used : DateTimeUtils.dateTimeToValue(org.h2.util.DateTimeUtils.dateTimeToValue) DateTimeUtils.absoluteDayFromDateValue(org.h2.util.DateTimeUtils.absoluteDayFromDateValue) Value(org.h2.value.Value) DateTimeUtils.dateAndTimeFromValue(org.h2.util.DateTimeUtils.dateAndTimeFromValue) BigInteger(java.math.BigInteger) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) ValueInterval(org.h2.value.ValueInterval) BigDecimal(java.math.BigDecimal)

Example 13 with ValueDate

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

the class IntervalOperation method getDateTimeWithInterval.

private Value getDateTimeWithInterval(SessionLocal session, Value l, Value r, int lType, int rType) {
    switch(lType) {
        case Value.TIME:
            if (DataType.isYearMonthIntervalType(rType)) {
                throw DbException.getInternalError("type=" + rType);
            }
            return ValueTime.fromNanos(getTimeWithInterval(r, ((ValueTime) l).getNanos()));
        case Value.TIME_TZ:
            {
                if (DataType.isYearMonthIntervalType(rType)) {
                    throw DbException.getInternalError("type=" + rType);
                }
                ValueTimeTimeZone t = (ValueTimeTimeZone) l;
                return ValueTimeTimeZone.fromNanos(getTimeWithInterval(r, t.getNanos()), t.getTimeZoneOffsetSeconds());
            }
        case Value.DATE:
        case Value.TIMESTAMP:
        case Value.TIMESTAMP_TZ:
            if (DataType.isYearMonthIntervalType(rType)) {
                long m = IntervalUtils.intervalToAbsolute((ValueInterval) r).longValue();
                if (opType == IntervalOpType.DATETIME_MINUS_INTERVAL) {
                    m = -m;
                }
                return DateTimeFunction.dateadd(session, DateTimeFunction.MONTH, m, l);
            } else {
                BigInteger a2 = IntervalUtils.intervalToAbsolute((ValueInterval) r);
                if (lType == Value.DATE) {
                    BigInteger a1 = BigInteger.valueOf(absoluteDayFromDateValue(((ValueDate) l).getDateValue()));
                    a2 = a2.divide(NANOS_PER_DAY_BI);
                    BigInteger n = opType == IntervalOpType.DATETIME_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2);
                    return ValueDate.fromDateValue(dateValueFromAbsoluteDay(n.longValue()));
                } else {
                    long[] a = dateAndTimeFromValue(l, session);
                    long absoluteDay = absoluteDayFromDateValue(a[0]);
                    long timeNanos = a[1];
                    BigInteger[] dr = a2.divideAndRemainder(NANOS_PER_DAY_BI);
                    if (opType == IntervalOpType.DATETIME_PLUS_INTERVAL) {
                        absoluteDay += dr[0].longValue();
                        timeNanos += dr[1].longValue();
                    } else {
                        absoluteDay -= dr[0].longValue();
                        timeNanos -= dr[1].longValue();
                    }
                    if (timeNanos >= NANOS_PER_DAY) {
                        timeNanos -= NANOS_PER_DAY;
                        absoluteDay++;
                    } else if (timeNanos < 0) {
                        timeNanos += NANOS_PER_DAY;
                        absoluteDay--;
                    }
                    return dateTimeToValue(l, dateValueFromAbsoluteDay(absoluteDay), timeNanos);
                }
            }
    }
    throw DbException.getInternalError("type=" + opType);
}
Also used : ValueTime(org.h2.value.ValueTime) BigInteger(java.math.BigInteger) ValueDate(org.h2.value.ValueDate) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) ValueInterval(org.h2.value.ValueInterval)

Example 14 with ValueDate

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

the class TestTimeStampWithTimeZone method testConversionsImpl.

private void testConversionsImpl(String timeStr, boolean testReverse, CastDataProvider provider) {
    ValueTimestamp ts = ValueTimestamp.parse(timeStr, null);
    ValueDate d = ts.convertToDate(provider);
    ValueTime t = (ValueTime) ts.convertTo(TypeInfo.TYPE_TIME, provider);
    ValueTimestampTimeZone tstz = ValueTimestampTimeZone.parse(timeStr, null);
    assertEquals(ts, tstz.convertTo(TypeInfo.TYPE_TIMESTAMP, provider));
    assertEquals(d, tstz.convertToDate(provider));
    assertEquals(t, tstz.convertTo(TypeInfo.TYPE_TIME, provider));
    assertEquals(LegacyDateTimeUtils.toTimestamp(provider, null, ts), LegacyDateTimeUtils.toTimestamp(provider, null, tstz));
    if (testReverse) {
        assertEquals(0, tstz.compareTo(ts.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider), null, null));
        assertEquals(d.convertTo(TypeInfo.TYPE_TIMESTAMP, provider).convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider), d.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider));
        assertEquals(t.convertTo(TypeInfo.TYPE_TIMESTAMP, provider).convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider), t.convertTo(TypeInfo.TYPE_TIMESTAMP_TZ, provider));
    }
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueDate(org.h2.value.ValueDate)

Example 15 with ValueDate

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

the class TestDate method testDateTimeUtils.

private void testDateTimeUtils() {
    TimeZone old = TimeZone.getDefault();
    /*
         * java.util.TimeZone doesn't support LMT, so perform this test with
         * fixed time zone offset
         */
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
    DateTimeUtils.resetCalendar();
    try {
        ValueTimestamp ts1 = ValueTimestamp.parse("-999-08-07 13:14:15.16", null);
        ValueTimestamp ts2 = ValueTimestamp.parse("19999-08-07 13:14:15.16", null);
        ValueTime t1 = (ValueTime) ts1.convertTo(TypeInfo.TYPE_TIME);
        ValueTime t2 = (ValueTime) ts2.convertTo(TypeInfo.TYPE_TIME);
        ValueDate d1 = ts1.convertToDate(null);
        ValueDate d2 = ts2.convertToDate(null);
        assertEquals("-0999-08-07 13:14:15.16", ts1.getString());
        assertEquals("-0999-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());
        TimeZone timeZone = TimeZone.getDefault();
        ValueTimestamp ts1a = LegacyDateTimeUtils.fromTimestamp(null, timeZone, LegacyDateTimeUtils.toTimestamp(null, null, ts1));
        ValueTimestamp ts2a = LegacyDateTimeUtils.fromTimestamp(null, timeZone, LegacyDateTimeUtils.toTimestamp(null, null, ts2));
        assertEquals("-0999-08-07 13:14:15.16", ts1a.getString());
        assertEquals("19999-08-07 13:14:15.16", ts2a.getString());
    } finally {
        TimeZone.setDefault(old);
        DateTimeUtils.resetCalendar();
    }
}
Also used : ValueTime(org.h2.value.ValueTime) TimeZone(java.util.TimeZone) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueTimestamp(org.h2.value.ValueTimestamp) ValueDate(org.h2.value.ValueDate)

Aggregations

ValueDate (org.h2.value.ValueDate)23 ValueTimestamp (org.h2.value.ValueTimestamp)23 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)23 ValueTime (org.h2.value.ValueTime)22 ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)18 Value (org.h2.value.Value)15 BigDecimal (java.math.BigDecimal)13 BigInteger (java.math.BigInteger)11 ValueDate (org.gridgain.internal.h2.value.ValueDate)9 ValueInterval (org.h2.value.ValueInterval)9 ValueTime (org.gridgain.internal.h2.value.ValueTime)6 ValueTimestamp (org.gridgain.internal.h2.value.ValueTimestamp)5 ValueBigint (org.h2.value.ValueBigint)5 ValueTimestampTimeZone (org.gridgain.internal.h2.value.ValueTimestampTimeZone)4 ValueSmallint (org.h2.value.ValueSmallint)4 ValueUuid (org.h2.value.ValueUuid)4 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Value (org.gridgain.internal.h2.value.Value)3 SimpleResultSet (org.h2.tools.SimpleResultSet)3