Search in sources :

Example 1 with MySqlCheckTableStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCheckTableStatement in project Mycat2 by MyCATApache.

the class MySQLCheckHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<MySqlCheckTableStatement> request, MycatDataContext dataContext, Response response) {
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("TABLE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("OP", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("MSG_TYPE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("MSG_TEXT", JDBCType.VARCHAR);
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    List<Throwable> throwables = Collections.synchronizedList(new LinkedList<>());
    MySqlCheckTableStatement ast = request.getAst();
    for (SQLExprTableSource table : ast.getTables()) {
        resolveSQLExprTableSource(table, dataContext);
        String schemaName = SQLUtils.normalize(table.getSchema());
        String tableName = SQLUtils.normalize(table.getTableName());
        TableHandler tableHandler = metadataManager.getTable(schemaName, tableName);
        Set<String> errorInfo = new HashSet<>();
        switch(tableHandler.getType()) {
            case SHARDING:
                {
                    ShardingTableHandler shardingTableHandler = (ShardingTableHandler) tableHandler;
                    errorInfo = check(metadataManager, jdbcConnectionManager, throwables, shardingTableHandler.dataNodes().parallelStream());
                    break;
                }
            case GLOBAL:
                {
                    GlobalTableHandler globalTableHandler = (GlobalTableHandler) tableHandler;
                    errorInfo = check(metadataManager, jdbcConnectionManager, throwables, globalTableHandler.getGlobalDataNode().parallelStream());
                    break;
                }
            case NORMAL:
                {
                    break;
                }
            case CUSTOM:
                {
                    break;
                }
            default:
                throw new IllegalStateException("Unexpected value: " + tableHandler.getType());
        }
        resultSetBuilder.addObjectRowPayload(Arrays.asList(table.toString(), "check", errorInfo.isEmpty() ? "Ok" : "Error", String.join(",", errorInfo)));
    }
    return response.sendResultSet(resultSetBuilder.build());
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) ShardingTableHandler(io.mycat.router.ShardingTableHandler) GlobalTableHandler(io.mycat.calcite.table.GlobalTableHandler) GlobalTableHandler(io.mycat.calcite.table.GlobalTableHandler) ShardingTableHandler(io.mycat.router.ShardingTableHandler) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) MySqlCheckTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCheckTableStatement)

Aggregations

SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 MySqlCheckTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCheckTableStatement)1 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)1 GlobalTableHandler (io.mycat.calcite.table.GlobalTableHandler)1 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)1 ShardingTableHandler (io.mycat.router.ShardingTableHandler)1