Search in sources :

Example 1 with MySqlDeclareHandlerStatement

use of com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareHandlerStatement in project druid by alibaba.

the class MySqlStatementParser method parseDeclareHandler.

/**
     * 定义异常处理程序
     * @author zhujun [455910092@qq.com]
     * 2016-04-16
     * @return
     */
public MySqlDeclareHandlerStatement parseDeclareHandler() {
    //DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement
    //handler_type 取值为 CONTINUE | EXIT | UNDO 
    //condition_value 取值为 SQLWARNING | NOT FOUND | SQLEXCEPTION | SQLSTATE value(异常码 e.g 1062)
    MySqlDeclareHandlerStatement stmt = new MySqlDeclareHandlerStatement();
    accept(Token.DECLARE);
    //String handlerType = exprParser.name().getSimpleName();
    if (lexer.token() == Token.CONTINUE) {
        stmt.setHandleType(MySqlHandlerType.CONTINUE);
    } else if (lexer.token() == Token.EXIT) {
        stmt.setHandleType(MySqlHandlerType.CONTINUE);
    } else if (lexer.token() == Token.UNDO) {
        stmt.setHandleType(MySqlHandlerType.CONTINUE);
    } else {
        throw new ParserException("unkown handle type");
    }
    lexer.nextToken();
    acceptIdentifier("HANDLER");
    accept(Token.FOR);
    for (; ; ) {
        String tokenName = lexer.stringVal();
        ConditionValue condition = new ConditionValue();
        if (tokenName.equalsIgnoreCase("NOT")) {
            //for 'NOT FOUND'
            lexer.nextToken();
            acceptIdentifier("HANDLE");
            condition.setType(ConditionType.SYSTEM);
            condition.setValue("NOT FOUND");
        } else if (tokenName.equalsIgnoreCase("SQLSTATE")) {
            //for SQLSTATE (SQLSTATE '10001') 
            condition.setType(ConditionType.SQLSTATE);
            lexer.nextToken();
            //condition.setValue(lexer.stringVal());
            //lexer.nextToken();
            condition.setValue(exprParser.name().toString());
        } else if (identifierEquals("SQLEXCEPTION")) {
            //for SQLEXCEPTION
            condition.setType(ConditionType.SYSTEM);
            condition.setValue(lexer.stringVal());
            lexer.nextToken();
        } else if (identifierEquals("SQLWARNING")) {
            //for SQLWARNING
            condition.setType(ConditionType.SYSTEM);
            condition.setValue(lexer.stringVal());
            lexer.nextToken();
        } else {
            //for condition_name or mysql_error_code
            if (lexer.token() == Token.LITERAL_INT) {
                condition.setType(ConditionType.MYSQL_ERROR_CODE);
                condition.setValue(lexer.integerValue().toString());
            } else {
                condition.setType(ConditionType.SELF);
                condition.setValue(tokenName);
            }
            lexer.nextToken();
        }
        stmt.getConditionValues().add(condition);
        if (lexer.token() == Token.COMMA) {
            accept(Token.COMMA);
            continue;
        } else if (lexer.token() != Token.EOF) {
            break;
        } else {
            throw new ParserException("declare handle not eof");
        }
    }
    stmt.setSpStatement(parseSpStatement());
    if (!(stmt.getSpStatement() instanceof SQLBlockStatement)) {
        accept(Token.SEMI);
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) ConditionValue(com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue) MySqlDeclareHandlerStatement(com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareHandlerStatement)

Aggregations

ConditionValue (com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue)1 MySqlDeclareHandlerStatement (com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareHandlerStatement)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1