Search in sources :

Example 1 with CastTarget

use of com.actiontech.dble.plan.common.CastTarget in project dble by actiontech.

the class ItemCreate method createFuncCast.

public ItemFunc createFuncCast(Item a, CastType type) {
    CastTarget castType = type.getTarget();
    ItemFunc res = null;
    if (castType == CastTarget.ITEM_CAST_BINARY) {
        res = new ItemFuncBinary(a, type.getLength());
    } else if (castType == CastTarget.ITEM_CAST_SIGNED_INT) {
        res = new ItemFuncSigned(a);
    } else if (castType == CastTarget.ITEM_CAST_UNSIGNED_INT) {
        res = new ItemFuncUnsigned(a);
    } else if (castType == CastTarget.ITEM_CAST_DATE) {
        res = new ItemDateTypecast(a);
    } else if (castType == CastTarget.ITEM_CAST_TIME || castType == CastTarget.ITEM_CAST_DATETIME) {
        if (type.getLength() > MyTime.DATETIME_MAX_DECIMALS) {
            throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "too big precision in cast time/datetime,max 6,current:" + type.getLength());
        }
        if (type.getLength() == -1) {
            res = (castType == CastTarget.ITEM_CAST_TIME) ? new ItemTimeTypecast(a) : new ItemDatetimeTypecast(a);
        } else {
            res = (castType == CastTarget.ITEM_CAST_TIME) ? new ItemTimeTypecast(a, type.getLength()) : new ItemDatetimeTypecast(a, type.getLength());
        }
    } else if (castType == CastTarget.ITEM_CAST_DECIMAL) {
        if (type.getLength() < type.getDec()) {
            throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "For float(m,d), double(m,d) or decimal(m,d), M must be >= d");
        }
        if (type.getLength() > MySQLcom.DECIMAL_MAX_PRECISION) {
            throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "Too big precision " + type.getLength() + " max is " + MySQLcom.DECIMAL_MAX_PRECISION);
        }
        if (type.getDec() > MySQLcom.DECIMAL_MAX_SCALE) {
            throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "Too big scale " + type.getDec() + " max is " + MySQLcom.DECIMAL_MAX_SCALE);
        }
        res = new ItemDecimalTypecast(a, type.getLength(), type.getDec());
    } else if (castType == CastTarget.ITEM_CAST_NCHAR) {
        int len = -1;
        if (type.getLength() > 0)
            len = type.getLength();
        res = new ItemNCharTypecast(a, len);
    } else {
        assert (false);
    }
    return res;
}
Also used : CastTarget(com.actiontech.dble.plan.common.CastTarget) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Aggregations

CastTarget (com.actiontech.dble.plan.common.CastTarget)1 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)1