Search in sources :

Example 1 with SQLBlockStatement

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

the class SqlAstParser method parseStatementDialect.

private boolean parseStatementDialect(List<StatementHolder> statementList) {
    if (lexer.token() == Token.KILL) {
        SQLStatement stmt = parseKill();
        statementList.add(new StatementHolder(Type.KILL, stmt));
        return true;
    }
    if (identifierEquals("PREPARE")) {
        MySqlPrepareStatement stmt = parsePrepare();
        statementList.add(new StatementHolder(Type.PREPARE, stmt));
        return true;
    }
    if (identifierEquals("EXECUTE")) {
        MySqlExecuteStatement stmt = parseExecute();
        statementList.add(new StatementHolder(Type.EXECUTE, stmt));
        return true;
    }
    if (identifierEquals("DEALLOCATE")) {
        MysqlDeallocatePrepareStatement stmt = parseDeallocatePrepare();
        statementList.add(new StatementHolder(Type.DEALLOCATE, stmt));
        return true;
    }
    if (identifierEquals("LOAD")) {
        SQLStatement stmt = parseLoad();
        statementList.add(new StatementHolder(Type.LOAD_DATA_INFILE_SQL, stmt));
        return true;
    }
    if (lexer.token() == Token.REPLACE) {
        MySqlReplaceStatement stmt = parseReplicate();
        statementList.add(new StatementHolder(Type.REPLACE, stmt));
        return true;
    }
    if (identifierEquals("START")) {
        SQLStartTransactionStatement stmt = parseStart();
        statementList.add(new StatementHolder(Type.START, stmt));
        return true;
    }
    if (lexer.token() == Token.SHOW) {
        SQLStatement stmt = parseShow();
        statementList.add(new StatementHolder(Type.SHOW, stmt));
        return true;
    }
    if (lexer.token() == Token.EXPLAIN) {
        SQLStatement stmt = this.parseExplain();
        statementList.add(new StatementHolder(Type.EXPLAIN, stmt));
        return true;
    }
    if (identifierEquals("BINLOG")) {
        SQLStatement stmt = parseBinlog();
        statementList.add(new StatementHolder(Type.BINLOG, stmt));
        return true;
    }
    if (identifierEquals("RESET")) {
        SQLStatement stmt = parseReset();
        statementList.add(new StatementHolder(Type.RESET, stmt));
        return true;
    }
    if (lexer.token() == Token.ANALYZE) {
        SQLStatement stmt = parseAnalyze();
        statementList.add(new StatementHolder(Type.ANALYZE, stmt));
        return true;
    }
    if (lexer.token() == Token.OPTIMIZE) {
        SQLStatement stmt = parseOptimize();
        statementList.add(new StatementHolder(Type.OPTIMIZE, stmt));
        return true;
    }
    if (identifierEquals("HELP")) {
        lexer.nextToken();
        MySqlHelpStatement stmt = new MySqlHelpStatement();
        stmt.setContent(this.exprParser.primary());
        statementList.add(new StatementHolder(Type.HELP, stmt));
        return true;
    }
    if (lexer.token() == Token.DESC || identifierEquals("DESCRIBE")) {
        SQLStatement stmt = parseDescribe();
        statementList.add(new StatementHolder(Type.DESCRIBE, stmt));
        return true;
    }
    if (lexer.token() == Token.LOCK) {
        lexer.nextToken();
        String val = lexer.stringVal();
        boolean isLockTables = "TABLES".equalsIgnoreCase(val) && lexer.token() == Token.IDENTIFIER;
        boolean isLockTable = "TABLE".equalsIgnoreCase(val) && lexer.token() == Token.TABLE;
        if (isLockTables || isLockTable) {
            lexer.nextToken();
        } else {
            setErrorEndPos(lexer.pos());
            throw new ParserException("syntax error, expect TABLES or TABLE, actual " + lexer.token());
        }
        MySqlLockTableStatement stmt = new MySqlLockTableStatement();
        stmt.setTableSource(this.exprParser.name());
        if (identifierEquals("READ")) {
            lexer.nextToken();
            if (identifierEquals("LOCAL")) {
                lexer.nextToken();
                stmt.setLockType(LockType.READ_LOCAL);
            } else {
                stmt.setLockType(LockType.READ);
            }
        } else if (identifierEquals("WRITE")) {
            stmt.setLockType(LockType.WRITE);
        } else if (identifierEquals("LOW_PRIORITY")) {
            lexer.nextToken();
            acceptIdentifier("WRITE");
            stmt.setLockType(LockType.LOW_PRIORITY_WRITE);
        } else {
            throw new ParserException("syntax error, expect READ or WRITE, actual " + lexer.token());
        }
        if (lexer.token() == Token.HINT) {
            stmt.setHints(this.exprParser.parseHints());
        }
        statementList.add(new StatementHolder(Type.LOCK, stmt));
        return true;
    }
    if (identifierEquals("UNLOCK")) {
        lexer.nextToken();
        String val = lexer.stringVal();
        boolean isUnLockTables = "TABLE".equalsIgnoreCase(val) && lexer.token() == Token.IDENTIFIER;
        boolean isUnLockTable = "TABLE".equalsIgnoreCase(val) && lexer.token() == Token.TABLE;
        MySqlUnlockTablesStatement stmt = new MySqlUnlockTablesStatement();
        statementList.add(new StatementHolder(Type.UNLOCK, stmt));
        if (isUnLockTables || isUnLockTable) {
            lexer.nextToken();
        } else {
            setErrorEndPos(lexer.pos());
            throw new ParserException("syntax error, expect TABLES or TABLE, actual " + lexer.token());
        }
        return true;
    }
    if (lexer.token() == Token.HINT) {
        List<SQLCommentHint> hints = this.exprParser.parseHints();
        boolean tddlSelectHints = false;
        if (hints.size() == 1 && statementList.size() == 0 && lexer.token() == Token.SELECT) {
            SQLCommentHint hint = hints.get(0);
            String hintText = hint.getText();
            if (hintText.startsWith("+TDDL")) {
                tddlSelectHints = true;
            }
        }
        if (tddlSelectHints) {
            SQLSelectStatement stmt = (SQLSelectStatement) this.parseStatement();
            stmt.setHeadHints(hints);
            statementList.add(new StatementHolder(Type.SELECT_WITH_HINT, stmt));
            return true;
        }
        MySqlHintStatement stmt = new MySqlHintStatement();
        stmt.setHints(hints);
        statementList.add(new StatementHolder(Type.HINT, stmt));
        return true;
    }
    if (lexer.token() == Token.BEGIN) {
        SQLBlockStatement stmt = this.parseBlock();
        statementList.add(new StatementHolder(Type.BEGIN, stmt));
        return true;
    }
    return false;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlUnlockTablesStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement) MySqlLockTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement) MySqlHintStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement) MySqlExecuteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlReplaceStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement) SQLBlockStatement(com.alibaba.druid.sql.ast.statement.SQLBlockStatement) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) MysqlDeallocatePrepareStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MysqlDeallocatePrepareStatement) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLStartTransactionStatement(com.alibaba.druid.sql.ast.statement.SQLStartTransactionStatement) MySqlHelpStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHelpStatement) MySqlPrepareStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement)

Example 2 with SQLBlockStatement

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

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLBlockStatement (com.alibaba.druid.sql.ast.statement.SQLBlockStatement)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLParameter (com.alibaba.druid.sql.ast.SQLParameter)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1 SQLCreateProcedureStatement (com.alibaba.druid.sql.ast.statement.SQLCreateProcedureStatement)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLStartTransactionStatement (com.alibaba.druid.sql.ast.statement.SQLStartTransactionStatement)1 MySqlExecuteStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement)1 MySqlHelpStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHelpStatement)1 MySqlHintStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement)1 MySqlLockTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement)1 MySqlPrepareStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement)1 MySqlReplaceStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement)1 MySqlUnlockTablesStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement)1 MysqlDeallocatePrepareStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MysqlDeallocatePrepareStatement)1 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)1