Search in sources :

Example 1 with NormalBackEndProcedureInfoConfig

use of io.mycat.config.NormalBackEndProcedureInfoConfig in project Mycat2 by MyCATApache.

the class SQLCallStatementHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<SQLCallStatement> request, MycatDataContext dataContext, Response response) {
    SQLCallStatement ast = request.getAst();
    SQLName procedureExpr = ast.getProcedureName();
    if (procedureExpr instanceof SQLIdentifierExpr && dataContext.getDefaultSchema() != null) {
        String name = ((SQLIdentifierExpr) procedureExpr).getName();
        procedureExpr = new SQLPropertyExpr(new SQLIdentifierExpr(dataContext.getDefaultSchema()), name);
    }
    SQLPropertyExpr sqlPropertyExpr = (SQLPropertyExpr) procedureExpr;
    String schemaName = SQLUtils.normalize(sqlPropertyExpr.getOwnerName(), true);
    String pName = SQLUtils.normalize(sqlPropertyExpr.getName(), true);
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    Optional<ProcedureHandler> procedureHandlerOptional = metadataManager.getProcedure(schemaName, pName);
    if (procedureHandlerOptional.isPresent()) {
        ProcedureHandler procedureHandler = procedureHandlerOptional.get();
        switch(procedureHandler.getType()) {
            case NORMAL:
                NormalProcedureHandler normalProcedureHandler = (NormalProcedureHandler) procedureHandler;
                NormalProcedureConfig config = normalProcedureHandler.getConfig();
                NormalBackEndProcedureInfoConfig locality = config.getLocality();
                String targetName = dataContext.resolveDatasourceTargetName(locality.getTargetName(), true);
                return response.proxyProcedure(ast.toString(), targetName);
        }
        LOGGER.error("unknown call {}", ast);
    }
    return response.proxyProcedure(ast.toString(), MetadataManager.getPrototype());
}
Also used : NormalBackEndProcedureInfoConfig(io.mycat.config.NormalBackEndProcedureInfoConfig) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLCallStatement(com.alibaba.druid.sql.ast.statement.SQLCallStatement) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) NormalProcedureConfig(io.mycat.config.NormalProcedureConfig)

Example 2 with NormalBackEndProcedureInfoConfig

use of io.mycat.config.NormalBackEndProcedureInfoConfig in project Mycat2 by MyCATApache.

the class SQLCreateProcedureHandler method onExecute.

@Override
@SneakyThrows
protected Future<Void> onExecute(SQLRequest<SQLCreateProcedureStatement> request, MycatDataContext dataContext, Response response) {
    SQLCreateProcedureStatement ast = request.getAst();
    if (ast.getName() instanceof SQLIdentifierExpr) {
        String defaultSchema = dataContext.getDefaultSchema();
        if (defaultSchema != null) {
            ast.setName(new SQLPropertyExpr(defaultSchema, ((SQLIdentifierExpr) ast.getName()).getName()));
        }
    }
    if (!(ast.getName() instanceof SQLPropertyExpr)) {
        throw new IllegalArgumentException("unknown schema:");
    }
    SQLPropertyExpr pNameExpr = (SQLPropertyExpr) ast.getName();
    String schemaName = SQLUtils.normalize(pNameExpr.getOwnerName().toLowerCase());
    String pName = SQLUtils.normalize(pNameExpr.getName().toLowerCase());
    List<SQLParameter> sqlParameters = Optional.ofNullable(ast.getParameters()).orElse(Collections.emptyList());
    Map<SQLParameter.ParameterType, List<SQLParameter>> parameterTypeListMap = sqlParameters.stream().collect(Collectors.groupingBy(k -> k.getParamType()));
    SQLBlockStatement block = (SQLBlockStatement) ast.getBlock();
    if (dataContext.getDefaultSchema() != null) {
        block.accept(new MySqlASTVisitorAdapter() {

            @Override
            public void endVisit(SQLExprTableSource x) {
                resolveSQLExprTableSource(x, dataContext);
            }
        });
    }
    Map<String, Collection<String>> collect = TableCollector.collect(dataContext.getDefaultSchema(), block);
    int resultSetCount = getResultSetCount(block);
    List<TableHandler> tableHandlers = getTableHandlers(block);
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    NormalProcedureConfig normalProcedureConfig = new NormalProcedureConfig();
    normalProcedureConfig.setCreateProcedureSQL(ast.toString());
    NormalBackEndProcedureInfoConfig normalBackEndProcedureInfoConfig = new NormalBackEndProcedureInfoConfig();
    normalBackEndProcedureInfoConfig.setProcedureName(pName);
    normalBackEndProcedureInfoConfig.setSchemaName(schemaName);
    normalBackEndProcedureInfoConfig.setTargetName(MetadataManager.getPrototype());
    normalProcedureConfig.setLocality(normalBackEndProcedureInfoConfig);
    try (MycatRouterConfigOps ops = ConfigUpdater.getOps()) {
        ops.addProcedure(schemaName, pName, normalProcedureConfig);
        ops.commit();
    }
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
        connection.executeUpdate(ast.toString(), false);
    }
    return response.sendOk();
}
Also used : java.util(java.util) SQLUtils(com.alibaba.druid.sql.SQLUtils) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) io.mycat(io.mycat) SneakyThrows(lombok.SneakyThrows) SQLRequest(io.mycat.sqlhandler.SQLRequest) NormalBackEndProcedureInfoConfig(io.mycat.config.NormalBackEndProcedureInfoConfig) AbstractSQLHandler(io.mycat.sqlhandler.AbstractSQLHandler) SQLBlockStatement(com.alibaba.druid.sql.ast.statement.SQLBlockStatement) SQLCreateProcedureStatement(com.alibaba.druid.sql.ast.statement.SQLCreateProcedureStatement) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ConfigUpdater(io.mycat.sqlhandler.ConfigUpdater) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) MycatRouterConfigOps(io.mycat.config.MycatRouterConfigOps) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SQLParameter(com.alibaba.druid.sql.ast.SQLParameter) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) NormalProcedureConfig(io.mycat.config.NormalProcedureConfig) NotNull(org.jetbrains.annotations.NotNull) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) NormalBackEndProcedureInfoConfig(io.mycat.config.NormalBackEndProcedureInfoConfig) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) MycatRouterConfigOps(io.mycat.config.MycatRouterConfigOps) SQLCreateProcedureStatement(com.alibaba.druid.sql.ast.statement.SQLCreateProcedureStatement) SQLBlockStatement(com.alibaba.druid.sql.ast.statement.SQLBlockStatement) NormalProcedureConfig(io.mycat.config.NormalProcedureConfig) SQLParameter(com.alibaba.druid.sql.ast.SQLParameter) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Aggregations

SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)2 NormalBackEndProcedureInfoConfig (io.mycat.config.NormalBackEndProcedureInfoConfig)2 NormalProcedureConfig (io.mycat.config.NormalProcedureConfig)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLParameter (com.alibaba.druid.sql.ast.SQLParameter)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLBlockStatement (com.alibaba.druid.sql.ast.statement.SQLBlockStatement)1 SQLCallStatement (com.alibaba.druid.sql.ast.statement.SQLCallStatement)1 SQLCreateProcedureStatement (com.alibaba.druid.sql.ast.statement.SQLCreateProcedureStatement)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)1 io.mycat (io.mycat)1 MycatRouterConfigOps (io.mycat.config.MycatRouterConfigOps)1 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)1 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)1 AbstractSQLHandler (io.mycat.sqlhandler.AbstractSQLHandler)1 ConfigUpdater (io.mycat.sqlhandler.ConfigUpdater)1