Search in sources :

Example 1 with MySqlDeclareConditionStatement

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

the class MySqlStatementParser method parseDeclareCondition.

/**
     * zhujun [455910092@qq.com]
     * 2016-04-17
     * 定义条件
     * @return
     */
public MySqlDeclareConditionStatement parseDeclareCondition() {
    /*
    	DECLARE condition_name CONDITION FOR condition_value

    	condition_value:
    	    SQLSTATE [VALUE] sqlstate_value
    	  | mysql_error_code
    	*/
    MySqlDeclareConditionStatement stmt = new MySqlDeclareConditionStatement();
    accept(Token.DECLARE);
    stmt.setConditionName(exprParser.name().toString());
    accept(Token.CONDITION);
    accept(Token.FOR);
    String tokenName = lexer.stringVal();
    ConditionValue condition = new ConditionValue();
    if (tokenName.equalsIgnoreCase("SQLSTATE")) {
        //for SQLSTATE (SQLSTATE '10001') 
        condition.setType(ConditionType.SQLSTATE);
        lexer.nextToken();
        condition.setValue(exprParser.name().toString());
    } else if (lexer.token() == Token.LITERAL_INT) {
        condition.setType(ConditionType.MYSQL_ERROR_CODE);
        condition.setValue(lexer.integerValue().toString());
        lexer.nextToken();
    } else {
        throw new ParserException("declare condition grammer error.");
    }
    stmt.setConditionValue(condition);
    accept(Token.SEMI);
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) ConditionValue(com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue) MySqlDeclareConditionStatement(com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareConditionStatement)

Aggregations

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