Search in sources :

Example 1 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project Mycat_plus by coderczp.

the class DruidCreateTableParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement) stmt;
    if (createStmt.getQuery() != null) {
        String msg = "create table from other table not supported :" + stmt;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String tableName = StringUtil.removeBackquote(createStmt.getTableSource().toString().toUpperCase());
    if (schema.getTables().containsKey(tableName)) {
        TableConfig tableConfig = schema.getTables().get(tableName);
        AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
        if (algorithm instanceof SlotFunction) {
            SQLColumnDefinition column = new SQLColumnDefinition();
            column.setDataType(new SQLCharacterDataType("int"));
            column.setName(new SQLIdentifierExpr("_slot"));
            column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
            ((SQLCreateTableStatement) stmt).getTableElementList().add(column);
            String sql = createStmt.toString();
            rrs.setStatement(sql);
            ctx.setSql(sql);
        }
    }
    ctx.addTable(tableName);
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLNonTransientException(java.sql.SQLNonTransientException) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) TableConfig(io.mycat.config.model.TableConfig) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SlotFunction(io.mycat.route.function.SlotFunction)

Example 2 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project Mycat_plus by coderczp.

the class RouterUtil method changeCreateTable.

private static String changeCreateTable(SchemaConfig schema, String tableName, String sql) {
    if (schema.getTables().containsKey(tableName)) {
        MySqlStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement insertStatement = parser.parseStatement();
        if (insertStatement instanceof MySqlCreateTableStatement) {
            TableConfig tableConfig = schema.getTables().get(tableName);
            AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
            if (algorithm instanceof SlotFunction) {
                SQLColumnDefinition column = new SQLColumnDefinition();
                column.setDataType(new SQLCharacterDataType("int"));
                column.setName(new SQLIdentifierExpr("_slot"));
                column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
                ((SQLCreateTableStatement) insertStatement).getTableElementList().add(column);
                return insertStatement.toString();
            }
        }
    }
    return sql;
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) TableConfig(io.mycat.config.model.TableConfig) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SlotFunction(io.mycat.route.function.SlotFunction)

Example 3 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project Mycat_plus by coderczp.

the class GlobalTableUtil method addColumnIfCreate.

static String addColumnIfCreate(String sql, SQLStatement statement) {
    if (isCreate(statement) && sql.trim().toUpperCase().startsWith("CREATE TABLE ") && !hasGlobalColumn(statement)) {
        SQLColumnDefinition column = new SQLColumnDefinition();
        column.setDataType(new SQLCharacterDataType("bigint"));
        column.setName(new SQLIdentifierExpr(GLOBAL_TABLE_MYCAT_COLUMN));
        column.setComment(new SQLCharExpr("全局表保存修改时间戳的字段名"));
        ((SQLCreateTableStatement) statement).getTableElementList().add(column);
    }
    return statement.toString();
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 4 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType in project dble by actiontech.

the class ItemCharTypecast method toExpression.

@Override
public SQLExpr toExpression() {
    SQLCastExpr cast = new SQLCastExpr();
    cast.setExpr(args.get(0).toExpression());
    SQLCharacterDataType dataType = new SQLCharacterDataType(SQLCharacterDataType.CHAR_TYPE_CHAR);
    cast.setDataType(dataType);
    if (castLength >= 0) {
        dataType.addArgument(new SQLIntegerExpr(castLength));
    }
    if (charSetName != null) {
        dataType.setName(charSetName);
    }
    cast.setDataType(dataType);
    return cast;
}
Also used : SQLCastExpr(com.alibaba.druid.sql.ast.expr.SQLCastExpr) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)

Example 5 with SQLCharacterDataType

use of com.alibaba.druid.sql.ast.statement.SQLCharacterDataType 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)

Aggregations

SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)15 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)7 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)7 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)7 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)5 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)5 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)4 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4 TableConfig (io.mycat.config.model.TableConfig)4 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)4 SlotFunction (io.mycat.route.function.SlotFunction)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 CastType (com.actiontech.dble.plan.common.CastType)1 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)1