Search in sources :

Example 1 with ConditionValue

use of com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue 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)

Example 2 with ConditionValue

use of com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue 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)

Example 3 with ConditionValue

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

the class MySqlOutputVisitor method visit.

@Override
public boolean visit(MySqlDeclareHandlerStatement x) {
    print0(ucase ? "DECLARE " : "declare ");
    print0(ucase ? x.getHandleType().toString().toUpperCase() : x.getHandleType().toString().toLowerCase());
    print0(ucase ? " HANDLER FOR " : " handler for ");
    for (int i = 0; i < x.getConditionValues().size(); i++) {
        ConditionValue cv = x.getConditionValues().get(i);
        if (cv.getType() == ConditionType.SQLSTATE) {
            print0(ucase ? " SQLSTATE " : " sqlstate ");
            print0(cv.getValue());
        } else if (cv.getType() == ConditionType.MYSQL_ERROR_CODE) {
            print0(cv.getValue());
        } else if (cv.getType() == ConditionType.SELF) {
            print0(cv.getValue());
        } else if (cv.getType() == ConditionType.SYSTEM) {
            print0(ucase ? cv.getValue().toUpperCase() : cv.getValue().toLowerCase());
        }
        if (i != x.getConditionValues().size() - 1) {
            print0(", ");
        }
    }
    println();
    return true;
}
Also used : ConditionValue(com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue) MySqlForceIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) MySqlUseIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint) MySqlIgnoreIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint)

Aggregations

ConditionValue (com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue)3 ParserException (com.alibaba.druid.sql.parser.ParserException)2 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 MySqlForceIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint)1 MySqlIgnoreIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint)1 MySqlUseIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint)1 MySqlDeclareConditionStatement (com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareConditionStatement)1 MySqlDeclareHandlerStatement (com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareHandlerStatement)1