Search in sources :

Example 1 with CastType

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

the class MySQLItemVisitor method endVisit.

@Override
public void endVisit(SQLCastExpr x) {
    Item a = getItem(x.getExpr());
    SQLDataType dataType = x.getDataType();
    if (dataType instanceof SQLCharacterDataType) {
        SQLCharacterDataType charType = (SQLCharacterDataType) dataType;
        String upType = charType.getName().toUpperCase();
        List<Integer> args = changeExprListToInt(charType.getArguments());
        String charSetName = charType.getCharSetName();
        if (upType.equals("CHAR")) {
            int len = -1;
            if (args.size() > 0) {
                len = args.get(0);
            }
            item = new ItemCharTypecast(a, len, charSetName);
        } else if (charSetName == null) {
            int len = -1;
            if (args.size() > 0) {
                len = args.get(0);
            }
            item = new ItemNCharTypecast(a, len);
        } else {
            throw new MySQLOutPutException(ErrorCode.ER_PARSE_ERROR, "", "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'character set " + charSetName + ")'");
        }
    } else {
        CastType castType = getCastType((SQLDataTypeImpl) dataType);
        item = ItemCreate.getInstance().createFuncCast(a, castType);
    }
    initName(x);
}
Also used : ItemNCharTypecast(com.actiontech.dble.plan.common.item.function.castfunc.ItemNCharTypecast) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) ItemCharTypecast(com.actiontech.dble.plan.common.item.function.castfunc.ItemCharTypecast) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) CastType(com.actiontech.dble.plan.common.CastType) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 2 with CastType

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

the class MySQLItemVisitor method getCastType.

private CastType getCastType(SQLDataTypeImpl dataTypeImpl) {
    CastType castType = new CastType();
    String upType = dataTypeImpl.getName().toUpperCase();
    List<Integer> args = changeExprListToInt(dataTypeImpl.getArguments());
    switch(upType) {
        case "BINARY":
            castType.setTarget(CastTarget.ITEM_CAST_BINARY);
            if (args.size() > 0) {
                castType.setLength(args.get(0));
            }
            break;
        case "DATE":
            castType.setTarget(CastTarget.ITEM_CAST_DATE);
            break;
        case "DATETIME":
            castType.setTarget(CastTarget.ITEM_CAST_DATETIME);
            if (args.size() > 0) {
                castType.setLength(args.get(0));
            }
            break;
        case "DECIMAL":
            castType.setTarget(CastTarget.ITEM_CAST_DECIMAL);
            if (args.size() > 0) {
                castType.setLength(args.get(0));
            }
            if (args.size() > 1) {
                castType.setDec(args.get(1));
            }
            break;
        case "NCHAR":
            castType.setTarget(CastTarget.ITEM_CAST_NCHAR);
            if (args.size() > 0) {
                castType.setLength(args.get(0));
            }
            break;
        case "SIGNED":
            castType.setTarget(CastTarget.ITEM_CAST_SIGNED_INT);
            break;
        case "UNSIGNED":
            castType.setTarget(CastTarget.ITEM_CAST_UNSIGNED_INT);
            break;
        case "TIME":
            castType.setTarget(CastTarget.ITEM_CAST_TIME);
            if (args.size() > 0) {
                castType.setLength(args.get(0));
            }
            break;
        default:
            // not support SIGNED INT /UNSIGNED INT/JSON
            throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not supported cast as:" + upType);
    }
    return castType;
}
Also used : CastType(com.actiontech.dble.plan.common.CastType) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Aggregations

CastType (com.actiontech.dble.plan.common.CastType)2 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)2 ItemCharTypecast (com.actiontech.dble.plan.common.item.function.castfunc.ItemCharTypecast)1 ItemNCharTypecast (com.actiontech.dble.plan.common.item.function.castfunc.ItemNCharTypecast)1 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)1 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)1