Search in sources :

Example 16 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class DruidUpdateParser method visitorParse.

public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    MySqlUpdateStatement update = (MySqlUpdateStatement) stmt;
    SQLTableSource tableSource = update.getTableSource();
    String schemaName = schema == null ? null : schema.getName();
    if (tableSource instanceof SQLJoinTableSource) {
        StringPtr sqlSchema = new StringPtr(null);
        if (!SchemaUtil.isNoSharding(sc, (SQLJoinTableSource) tableSource, stmt, schemaName, sqlSchema)) {
            String msg = "UPDATE query with multiple tables is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        } else {
            if (update.getWhere() != null && !SchemaUtil.isNoSharding(sc, update.getWhere(), schemaName, sqlSchema)) {
                String msg = "UPDATE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            String realSchema = sqlSchema.get() == null ? schemaName : sqlSchema.get();
            SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(realSchema);
            rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), realSchema));
            RouterUtil.routeToSingleNode(rrs, schemaConfig.getDataNode());
            rrs.setFinishedRoute(true);
            return schema;
        }
    } else {
        SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, (SQLExprTableSource) tableSource);
        if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), ServerPrivileges.CheckType.UPDATE)) {
            String msg = "The statement DML privilege check is not passed, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        schema = schemaInfo.getSchemaConfig();
        String tableName = schemaInfo.getTable();
        rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
        if (RouterUtil.isNoSharding(schema, tableName)) {
            if (update.getWhere() != null && !SchemaUtil.isNoSharding(sc, update.getWhere(), schemaName, new StringPtr(schemaInfo.getSchema()))) {
                String msg = "UPDATE query with sub-query is not supported, sql:" + stmt;
                throw new SQLNonTransientException(msg);
            }
            RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
            rrs.setFinishedRoute(true);
            return schema;
        }
        super.visitorParse(schema, rrs, stmt, visitor, sc);
        if (visitor.isHasSubQuery()) {
            String msg = "UPDATE query with sub-query  is not supported, sql:" + stmt;
            throw new SQLNonTransientException(msg);
        }
        TableConfig tc = schema.getTables().get(tableName);
        if (tc.isGlobalTable()) {
            if (GlobalTableUtil.useGlobalTableCheck()) {
                String sql = convertUpdateSQL(schemaInfo, update, rrs.getStatement());
                rrs.setStatement(sql);
            }
            RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
            rrs.setFinishedRoute(true);
            return schema;
        }
        String partitionColumn = tc.getPartitionColumn();
        String joinKey = tc.getJoinKey();
        confirmShardColumnNotUpdated(update, schema, tableName, partitionColumn, joinKey, rrs);
        confirmChildColumnNotUpdated(update, schema, tableName);
        if (schema.getTables().get(tableName).isGlobalTable() && ctx.getRouteCalculateUnit().getTablesAndConditions().size() > 1) {
            throw new SQLNonTransientException("global table is not supported in multi table related update " + tableName);
        }
        if (ctx.getTables().size() == 0) {
            ctx.addTable(schemaInfo.getTable());
        }
    }
    return schema;
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) StringPtr(com.actiontech.dble.plan.common.ptr.StringPtr) TableConfig(com.actiontech.dble.config.model.TableConfig) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 17 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class DruidAlterTableParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    SQLAlterTableStatement alterTable = (SQLAlterTableStatement) stmt;
    String schemaName = schema == null ? null : schema.getName();
    SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, alterTable.getTableSource());
    boolean support = false;
    String msg = "The DDL is not supported, sql:";
    for (SQLAlterTableItem alterItem : alterTable.getItems()) {
        if (alterItem instanceof SQLAlterTableAddColumn || alterItem instanceof SQLAlterTableAddIndex || alterItem instanceof SQLAlterTableDropIndex || alterItem instanceof SQLAlterTableDropKey || alterItem instanceof SQLAlterTableDropPrimaryKey) {
            support = true;
        } else if (alterItem instanceof SQLAlterTableAddConstraint) {
            SQLConstraint constraint = ((SQLAlterTableAddConstraint) alterItem).getConstraint();
            if (constraint instanceof MySqlPrimaryKey) {
                support = true;
            }
        } else if (alterItem instanceof MySqlAlterTableChangeColumn || alterItem instanceof MySqlAlterTableModifyColumn || alterItem instanceof SQLAlterTableDropColumnItem) {
            List<SQLName> columnList = new ArrayList<>();
            if (alterItem instanceof MySqlAlterTableChangeColumn) {
                columnList.add(((MySqlAlterTableChangeColumn) alterItem).getColumnName());
            } else if (alterItem instanceof MySqlAlterTableModifyColumn) {
                columnList.add(((MySqlAlterTableModifyColumn) alterItem).getNewColumnDefinition().getName());
            } else if (alterItem instanceof SQLAlterTableDropColumnItem) {
                columnList = ((SQLAlterTableDropColumnItem) alterItem).getColumns();
            }
            support = !this.columnInfluenceCheck(columnList, schemaInfo.getSchemaConfig(), schemaInfo.getTable());
            if (!support) {
                msg = "The columns may be sharding keys or ER keys, are not allowed to alter sql:";
            }
        }
    }
    if (!support) {
        msg = msg + stmt;
        throw new SQLNonTransientException(msg);
    }
    String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
    rrs.setStatement(statement);
    if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
        RouterUtil.routeToSingleDDLNode(schemaInfo, rrs);
        return schemaInfo.getSchemaConfig();
    }
    if (GlobalTableUtil.useGlobalTableCheck() && GlobalTableUtil.isGlobalTable(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
        String sql = modifyColumnIfAlter(schemaInfo, rrs.getStatement(), alterTable);
        rrs.setSrcStatement(sql);
        sql = RouterUtil.removeSchema(sql, schemaInfo.getSchema());
        rrs.setStatement(sql);
    }
    RouterUtil.routeToDDLNode(schemaInfo, rrs);
    return schemaInfo.getSchemaConfig();
}
Also used : MySqlAlterTableModifyColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn) SQLName(com.alibaba.druid.sql.ast.SQLName) ArrayList(java.util.ArrayList) SQLNonTransientException(java.sql.SQLNonTransientException) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey) MySqlAlterTableChangeColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 18 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class DruidCreateTableParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    MySqlCreateTableStatement createStmt = (MySqlCreateTableStatement) stmt;
    // disable create table select from
    if (createStmt.getSelect() != null) {
        String msg = "create table from other table not supported :" + stmt;
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    // disable create table select from
    if (createStmt.getLike() != null) {
        String msg = "create table like other table not supported :" + stmt;
        LOGGER.info(msg);
        throw new SQLNonTransientException(msg);
    }
    String schemaName = schema == null ? null : schema.getName();
    SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, createStmt.getTableSource());
    String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
    rrs.setStatement(statement);
    if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
        RouterUtil.routeToSingleDDLNode(schemaInfo, rrs);
        return schemaInfo.getSchemaConfig();
    }
    sharingTableCheck(createStmt);
    if (GlobalTableUtil.useGlobalTableCheck() && GlobalTableUtil.isGlobalTable(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
        String sql = addColumnIfCreate(createStmt);
        rrs.setSrcStatement(sql);
        sql = RouterUtil.removeSchema(sql, schemaInfo.getSchema());
        rrs.setStatement(sql);
    }
    try {
        RouterUtil.routeToDDLNode(schemaInfo, rrs);
    } catch (SQLException e) {
        String msg = "Table '" + schemaInfo.getSchema() + "." + schemaInfo.getTable() + "' doesn't exist in the config of schema";
        throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
    }
    return schemaInfo.getSchemaConfig();
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 19 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class DruidDropIndexParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    String schemaName = schema == null ? null : schema.getName();
    SQLDropIndexStatement dropStmt = (SQLDropIndexStatement) stmt;
    SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, dropStmt.getTableName());
    String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
    rrs.setStatement(statement);
    if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
        RouterUtil.routeToSingleDDLNode(schemaInfo, rrs);
        return schemaInfo.getSchemaConfig();
    }
    RouterUtil.routeToDDLNode(schemaInfo, rrs);
    return schemaInfo.getSchemaConfig();
}
Also used : SQLDropIndexStatement(com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Example 20 with SchemaInfo

use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.

the class DescribeHandler method handle.

public static void handle(String stmt, ServerConnection c) {
    try {
        SQLStatement statement = RouteStrategyFactory.getRouteStrategy().parserSQL(stmt);
        MySqlExplainStatement describeStatement = (MySqlExplainStatement) statement;
        SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(c.getUser(), c.getSchema(), describeStatement.getTableName(), null);
        c.routeSystemInfoAndExecuteSQL(RouterUtil.removeSchema(stmt, schemaInfo.getSchema()), schemaInfo, ServerParse.DESCRIBE);
    } catch (Exception e) {
        c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
        return;
    }
}
Also used : MySqlExplainStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExplainStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Aggregations

SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)20 SQLNonTransientException (java.sql.SQLNonTransientException)10 SQLException (java.sql.SQLException)9 TableConfig (com.actiontech.dble.config.model.TableConfig)5 StructureMeta (com.actiontech.dble.meta.protocol.StructureMeta)4 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)3 StringPtr (com.actiontech.dble.plan.common.ptr.StringPtr)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)2 MySqlPrimaryKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)2 MySqlAlterTableChangeColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)2 MySqlAlterTableModifyColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 MySqlUnionQuery (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLCreateIndexStatement (com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement)1 SQLDropIndexStatement (com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement)1