Search in sources :

Example 1 with MySQLTime

use of com.actiontech.dble.plan.common.time.MySQLTime in project dble by actiontech.

the class ItemFuncMinMax method valStr.

@Override
public String valStr() {
    if (compareAsDates) {
        if (isTemporal()) {
            /*
                 * In case of temporal data types, we always return string value
                 * according the format of the data type. For example, in case
                 * of LEAST(time_column, datetime_column) the result date type
                 * is DATETIME, so we return a 'YYYY-MM-DD hh:mm:ss' string even
                 * if time_column wins (conversion from TIME to DATETIME happens
                 * in this case).
                 */
            LongPtr result = new LongPtr(0);
            cmpDatetimes(result);
            if (nullValue)
                return null;
            MySQLTime ltime = new MySQLTime();
            MyTime.timeFromLonglongPacked(ltime, fieldType(), result.get());
            return MyTime.myTimeToStrL(ltime, decimals);
        } else {
            /*
                 * In case of VARCHAR result type we just return val_str() value
                 * of the winning item AS IS, without conversion.
                 */
            long minMaxIdx = cmpDatetimes(new LongPtr(0));
            if (nullValue)
                return null;
            String strRes = args.get((int) minMaxIdx).valStr();
            if (args.get((int) minMaxIdx).isNullValue()) {
                // check if the call to val_str() above returns a NULL value
                nullValue = true;
                return null;
            }
            return strRes;
        }
    }
    if (cmpType == ItemResult.INT_RESULT) {
        BigInteger nr = valInt();
        if (nullValue)
            return null;
        return nr.toString();
    } else if (cmpType == ItemResult.DECIMAL_RESULT) {
        BigDecimal bd = valDecimal();
        if (nullValue)
            return null;
        return bd.toString();
    } else if (cmpType == ItemResult.REAL_RESULT) {
        BigDecimal nr = valReal();
        if (nullValue)
            return null;
        /* purecov: inspected */
        return nr.toString();
    } else if (cmpType == ItemResult.STRING_RESULT) {
        String res = null;
        for (int i = 0; i < args.size(); i++) {
            if (i == 0)
                res = args.get(i).valStr();
            else {
                String res2 = args.get(i).valStr();
                if (res2 != null) {
                    int cmp = res.compareTo(res2);
                    if ((cmpSign < 0 ? cmp : -1 * cmp) < 0)
                        res = res2;
                }
            }
            if ((nullValue = args.get(i).isNull()))
                return null;
        }
        return res;
    } else {
        // This case should never be chosen
        return null;
    }
}
Also used : LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr) MySQLTime(com.actiontech.dble.plan.common.time.MySQLTime) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 2 with MySQLTime

use of com.actiontech.dble.plan.common.time.MySQLTime in project dble by actiontech.

the class ItemFuncTimeToSec method valInt.

@Override
public BigInteger valInt() {
    MySQLTime ltime = new MySQLTime();
    if (getArg0Time(ltime))
        return BigInteger.ZERO;
    long seconds = ltime.getHour() * 3600L + ltime.getMinute() * 60 + ltime.getSecond();
    return BigInteger.valueOf(ltime.isNeg() ? -seconds : seconds);
}
Also used : MySQLTime(com.actiontech.dble.plan.common.time.MySQLTime)

Example 3 with MySQLTime

use of com.actiontech.dble.plan.common.time.MySQLTime in project dble by actiontech.

the class ItemFuncTimestampDiff method valInt.

@Override
public BigInteger valInt() {
    MySQLTime ltime1 = new MySQLTime();
    MySQLTime ltime2 = new MySQLTime();
    nullValue = false;
    int neg = 1;
    long months = 0;
    if (args.get(0).getDate(ltime1, MyTime.TIME_NO_ZERO_DATE) || args.get(1).getDate(ltime2, MyTime.TIME_NO_ZERO_DATE)) {
        nullValue = true;
        return BigInteger.ZERO;
    }
    LongPtr lpseconds = new LongPtr(0);
    LongPtr lpmicroseconds = new LongPtr(0);
    if (MyTime.calcTimeDiff(ltime2, ltime1, 1, lpseconds, lpmicroseconds))
        neg = -1;
    long seconds = lpseconds.get(), microseconds = lpmicroseconds.get();
    if (intType == MySqlIntervalUnit.YEAR || intType == MySqlIntervalUnit.QUARTER || intType == MySqlIntervalUnit.MONTH) {
        long yearBeg, yearEnd, monthBeg, monthEnd, dayBeg, dayEnd;
        long years = 0;
        long secondBeg, secondEnd, microsecondBeg, microsecondEnd;
        if (neg == -1) {
            yearBeg = ltime2.getYear();
            yearEnd = ltime1.getYear();
            monthBeg = ltime2.getMonth();
            monthEnd = ltime1.getMonth();
            dayBeg = ltime2.getDay();
            dayEnd = ltime1.getDay();
            secondBeg = ltime2.getHour() * 3600 + ltime2.getMinute() * 60 + ltime2.getSecond();
            secondEnd = ltime1.getHour() * 3600 + ltime1.getMinute() * 60 + ltime1.getSecond();
            microsecondBeg = ltime2.getSecondPart();
            microsecondEnd = ltime1.getSecondPart();
        } else {
            yearBeg = ltime1.getYear();
            yearEnd = ltime2.getYear();
            monthBeg = ltime1.getMonth();
            monthEnd = ltime2.getMonth();
            dayBeg = ltime1.getDay();
            dayEnd = ltime2.getDay();
            secondBeg = ltime1.getHour() * 3600 + ltime1.getMinute() * 60 + ltime1.getSecond();
            secondEnd = ltime2.getHour() * 3600 + ltime2.getMinute() * 60 + ltime2.getSecond();
            microsecondBeg = ltime1.getSecondPart();
            microsecondEnd = ltime2.getSecondPart();
        }
        /* calc years */
        years = yearEnd - yearBeg;
        if (monthEnd < monthBeg || (monthEnd == monthBeg && dayEnd < dayBeg))
            years -= 1;
        /* calc months */
        months = 12 * years;
        if (monthEnd < monthBeg || (monthEnd == monthBeg && dayEnd < dayBeg))
            months += 12 - (monthBeg - monthEnd);
        else
            months += (monthEnd - monthBeg);
        if (dayEnd < dayBeg)
            months -= 1;
        else if ((dayEnd == dayBeg) && ((secondEnd < secondBeg) || (secondEnd == secondBeg && microsecondEnd < microsecondBeg)))
            months -= 1;
    }
    switch(intType) {
        case YEAR:
            return BigInteger.valueOf(months / 12 * neg);
        case QUARTER:
            return BigInteger.valueOf(months / 3 * neg);
        case MONTH:
            return BigInteger.valueOf(months * neg);
        case WEEK:
            return BigInteger.valueOf(seconds / MyTime.SECONDS_IN_24H / 7L * neg);
        case DAY:
            return BigInteger.valueOf(seconds / MyTime.SECONDS_IN_24H * neg);
        case HOUR:
            return BigInteger.valueOf(seconds / 3600L * neg);
        case MINUTE:
            return BigInteger.valueOf(seconds / 60L * neg);
        case SECOND:
            return BigInteger.valueOf(seconds * neg);
        case MICROSECOND:
            /*
                 * In MySQL difference between any two valid datetime values in
                 * microseconds fits into longlong.
                 */
            return BigInteger.valueOf((seconds * 1000000L + microseconds) * neg);
        default:
            nullValue = true;
            return BigInteger.ZERO;
    }
}
Also used : LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr) MySQLTime(com.actiontech.dble.plan.common.time.MySQLTime)

Example 4 with MySQLTime

use of com.actiontech.dble.plan.common.time.MySQLTime in project dble by actiontech.

the class ItemFuncDayname method valStr.

@Override
public String valStr() {
    MySQLTime ltime = new MySQLTime();
    if (getArg0Date(ltime, MyTime.TIME_NO_ZERO_DATE))
        return null;
    long weekday = MyTime.calcWeekday(MyTime.calcDaynr(ltime.getYear(), ltime.getMonth(), ltime.getDay()), false);
    return MyTime.DAY_NAMES[(int) weekday];
}
Also used : MySQLTime(com.actiontech.dble.plan.common.time.MySQLTime)

Example 5 with MySQLTime

use of com.actiontech.dble.plan.common.time.MySQLTime in project dble by actiontech.

the class ItemFuncAddTime method valDatetime.

@Override
protected boolean valDatetime(MySQLTime time, long fuzzyDate) {
    MySQLTime lTime1 = new MySQLTime();
    MySQLTime lTime2 = new MySQLTime();
    boolean isTime = false;
    int lSign = sign;
    nullValue = false;
    if (cachedFieldType == FieldTypes.MYSQL_TYPE_DATETIME) /* TIMESTAMP function */
    {
        if (getArg0Date(lTime1, fuzzyDate) || args.get(1).getTime(lTime2) || lTime1.getTimeType() == MySQLTimestampType.MYSQL_TIMESTAMP_TIME || lTime2.getTimeType() != MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
            nullValue = true;
            return true;
        }
    } else /* ADDTIME function */
    {
        if (args.get(0).getTime(lTime1) || args.get(1).getTime(lTime2) || lTime2.getTimeType() == MySQLTimestampType.MYSQL_TIMESTAMP_DATETIME) {
            nullValue = true;
            return true;
        }
        isTime = (lTime1.getTimeType() == MySQLTimestampType.MYSQL_TIMESTAMP_TIME);
    }
    if (lTime1.isNeg() != lTime2.isNeg())
        lSign = -lSign;
    time.setZeroTime(time.getTimeType());
    LongPtr seconds = new LongPtr(0);
    LongPtr microseconds = new LongPtr(0);
    time.setNeg(MyTime.calcTimeDiff(lTime1, lTime2, -lSign, seconds, microseconds));
    /*
         * If first argument was negative and diff between arguments is non-zero
         * we need to swap sign to get proper result.
         */
    if (lTime1.isNeg() && (seconds.get() != 0 || microseconds.get() != 0))
        // Swap sign of result
        time.setNeg(!time.isNeg());
    if (!isTime && time.isNeg()) {
        nullValue = true;
        return true;
    }
    long days = seconds.get() / MyTime.SECONDS_IN_24H;
    MyTime.calcTimeFromSec(time, seconds.get() % MyTime.SECONDS_IN_24H, microseconds.get());
    if (!isTime) {
        LongPtr lpyear = new LongPtr(0);
        LongPtr lpmonth = new LongPtr(0);
        LongPtr lpday = new LongPtr(0);
        MyTime.getDateFromDaynr(days, lpyear, lpmonth, lpday);
        time.setYear(lpyear.get());
        time.setMonth(lpmonth.get());
        time.setDay(lpday.get());
        time.setTimeType(MySQLTimestampType.MYSQL_TIMESTAMP_DATETIME);
        if (time.getDay() != 0)
            return false;
        nullValue = true;
        return true;
    }
    time.setTimeType(MySQLTimestampType.MYSQL_TIMESTAMP_TIME);
    time.setHour(time.getHour() + days * 24);
    return false;
}
Also used : LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr) MySQLTime(com.actiontech.dble.plan.common.time.MySQLTime)

Aggregations

MySQLTime (com.actiontech.dble.plan.common.time.MySQLTime)16 LongPtr (com.actiontech.dble.plan.common.ptr.LongPtr)6 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)1 DateTimeFormat (com.actiontech.dble.plan.common.time.DateTimeFormat)1 MySQLTimeStatus (com.actiontech.dble.plan.common.time.MySQLTimeStatus)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1