Search in sources :

Example 1 with SQLRequest

use of io.mycat.sqlhandler.SQLRequest in project Mycat2 by MyCATApache.

the class CreateTableSQLHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<MySqlCreateTableStatement> request, MycatDataContext dataContext, Response response) {
    LockService lockService = MetaClusterCurrent.wrapper(LockService.class);
    return lockService.lock(DDL_LOCK, () -> {
        try {
            Map hint = Optional.ofNullable(request.getAst().getHeadHintsDirect()).map(i -> i.get(0)).map(i -> i.getText()).filter(i -> {
                i = i.replaceAll(" ", "");
                return i.contains("+mycat:createTable{");
            }).map(i -> i.substring(i.indexOf("{"))).map(i -> JsonUtil.from(i, Map.class)).orElse(null);
            MySqlCreateTableStatement ast = request.getAst();
            String schemaName = ast.getSchema() == null ? dataContext.getDefaultSchema() : SQLUtils.normalize(ast.getSchema());
            String tableName = ast.getTableName() == null ? null : SQLUtils.normalize(ast.getTableName());
            if (ast.getSchema() == null) {
                ast.setSchema(schemaName);
            }
            if (tableName == null) {
                return response.sendError(new MycatException("CreateTableSQL need tableName"));
            }
            if (schemaName == null) {
                return response.sendError("No database selected", 1046);
            }
            createTable(hint, schemaName, tableName, ast);
            return response.sendOk();
        } catch (Throwable throwable) {
            return Future.failedFuture(throwable);
        }
    });
}
Also used : ClusterConfig(io.mycat.config.ClusterConfig) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) io.mycat(io.mycat) SQLRequest(io.mycat.sqlhandler.SQLRequest) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) Vertx(io.vertx.core.Vertx) LoggerFactory(org.slf4j.LoggerFactory) AbstractSQLHandler(io.mycat.sqlhandler.AbstractSQLHandler) MycatRouterConfigOps.getAutoHashProperties(io.mycat.config.MycatRouterConfigOps.getAutoHashProperties) ConfigUpdater(io.mycat.sqlhandler.ConfigUpdater) Future(io.vertx.core.Future) Objects(java.util.Objects) Lock(io.vertx.core.shareddata.Lock) MycatRouterConfigOps(io.mycat.config.MycatRouterConfigOps) Map(java.util.Map) Optional(java.util.Optional) JsonUtil(io.mycat.util.JsonUtil) Map(java.util.Map) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)

Example 2 with SQLRequest

use of io.mycat.sqlhandler.SQLRequest 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

SQLUtils (com.alibaba.druid.sql.SQLUtils)2 io.mycat (io.mycat)2 MycatRouterConfigOps (io.mycat.config.MycatRouterConfigOps)2 AbstractSQLHandler (io.mycat.sqlhandler.AbstractSQLHandler)2 ConfigUpdater (io.mycat.sqlhandler.ConfigUpdater)2 SQLRequest (io.mycat.sqlhandler.SQLRequest)2 Future (io.vertx.core.Future)2 SQLParameter (com.alibaba.druid.sql.ast.SQLParameter)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1 SQLBlockStatement (com.alibaba.druid.sql.ast.statement.SQLBlockStatement)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 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)1 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)1 ClusterConfig (io.mycat.config.ClusterConfig)1 MycatRouterConfigOps.getAutoHashProperties (io.mycat.config.MycatRouterConfigOps.getAutoHashProperties)1 NormalBackEndProcedureInfoConfig (io.mycat.config.NormalBackEndProcedureInfoConfig)1