Search in sources :

Example 1 with MySqlPrepareStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement in project druid by alibaba.

the class MySqlStatementParser method parseStatementListDialect.

public boolean parseStatementListDialect(List<SQLStatement> statementList) {
    if (lexer.token() == Token.KILL) {
        SQLStatement stmt = parseKill();
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals("PREPARE")) {
        MySqlPrepareStatement stmt = parsePrepare();
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals("EXECUTE")) {
        MySqlExecuteStatement stmt = parseExecute();
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals("DEALLOCATE")) {
        MysqlDeallocatePrepareStatement stmt = parseDeallocatePrepare();
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals("LOAD")) {
        SQLStatement stmt = parseLoad();
        statementList.add(stmt);
        return true;
    }
    if (lexer.token() == Token.REPLACE) {
        MySqlReplaceStatement stmt = parseReplicate();
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals("START")) {
        SQLStartTransactionStatement stmt = parseStart();
        statementList.add(stmt);
        return true;
    }
    if (lexer.token() == Token.SHOW) {
        SQLStatement stmt = parseShow();
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals(BINLOG)) {
        SQLStatement stmt = parseBinlog();
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals(RESET)) {
        SQLStatement stmt = parseReset();
        statementList.add(stmt);
        return true;
    }
    if (lexer.token() == Token.ANALYZE) {
        SQLStatement stmt = parseAnalyze();
        statementList.add(stmt);
        return true;
    }
    if (lexer.token() == Token.OPTIMIZE) {
        SQLStatement stmt = parseOptimize();
        statementList.add(stmt);
        return true;
    }
    if (identifierEquals("HELP")) {
        lexer.nextToken();
        MySqlHelpStatement stmt = new MySqlHelpStatement();
        stmt.setContent(this.exprParser.primary());
        statementList.add(stmt);
        return true;
    }
    if (lexer.token() == Token.DESC || identifierEquals(DESCRIBE)) {
        SQLStatement stmt = parseDescribe();
        statementList.add(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(stmt);
        return true;
    }
    if (identifierEquals("UNLOCK")) {
        lexer.nextToken();
        String val = lexer.stringVal();
        boolean isUnLockTables = TABLES.equalsIgnoreCase(val) && lexer.token() == Token.IDENTIFIER;
        boolean isUnLockTable = "TABLE".equalsIgnoreCase(val) && lexer.token() == Token.TABLE;
        statementList.add(new MySqlUnlockTablesStatement());
        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(stmt);
            return true;
        }
        MySqlHintStatement stmt = new MySqlHintStatement();
        stmt.setHints(hints);
        statementList.add(stmt);
        return true;
    }
    if (lexer.token() == Token.BEGIN) {
        statementList.add(this.parseBlock());
        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) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) MysqlDeallocatePrepareStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MysqlDeallocatePrepareStatement) MySqlHelpStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHelpStatement) MySqlPrepareStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement)

Example 2 with MySqlPrepareStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement in project druid by alibaba.

the class MySqlStatementParser method parsePrepare.

public MySqlPrepareStatement parsePrepare() {
    acceptIdentifier("PREPARE");
    SQLName name = exprParser.name();
    accept(Token.FROM);
    SQLExpr from = exprParser.expr();
    return new MySqlPrepareStatement(name, from);
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MySqlPrepareStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement)

Aggregations

MySqlPrepareStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement)2 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)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 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 ParserException (com.alibaba.druid.sql.parser.ParserException)1