Search in sources :

Example 6 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class MySqlStatementParser method parseProcedureStatementList.

/**
     * parse procedure statement block
     */
private void parseProcedureStatementList(List<SQLStatement> statementList, int max) {
    for (; ; ) {
        if (max != -1) {
            if (statementList.size() >= max) {
                return;
            }
        }
        if (lexer.token() == Token.EOF) {
            return;
        }
        if (lexer.token() == Token.END) {
            return;
        }
        if (lexer.token() == Token.ELSE) {
            return;
        }
        if (lexer.token() == (Token.SEMI)) {
            lexer.nextToken();
            continue;
        }
        if (lexer.token() == Token.WHEN) {
            return;
        }
        if (lexer.token() == Token.UNTIL) {
            return;
        }
        // select into
        if (lexer.token() == (Token.SELECT)) {
            statementList.add(this.parseSelectInto());
            continue;
        }
        // update
        if (lexer.token() == (Token.UPDATE)) {
            statementList.add(parseUpdateStatement());
            continue;
        }
        // create
        if (lexer.token() == (Token.CREATE)) {
            statementList.add(parseCreate());
            continue;
        }
        // insert
        if (lexer.token() == Token.INSERT) {
            statementList.add(parseInsert());
            continue;
        }
        // delete
        if (lexer.token() == (Token.DELETE)) {
            statementList.add(parseDeleteStatement());
            continue;
        }
        // call
        if (lexer.token() == Token.LBRACE || identifierEquals("CALL")) {
            statementList.add(this.parseCall());
            continue;
        }
        // begin
        if (lexer.token() == Token.BEGIN) {
            statementList.add(this.parseBlock());
            continue;
        }
        if (lexer.token() == Token.VARIANT) {
            SQLExpr variant = this.exprParser.primary();
            if (variant instanceof SQLBinaryOpExpr) {
                SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) variant;
                if (binaryOpExpr.getOperator() == SQLBinaryOperator.Assignment) {
                    SQLSetStatement stmt = new SQLSetStatement(binaryOpExpr.getLeft(), binaryOpExpr.getRight(), getDbType());
                    statementList.add(stmt);
                    continue;
                }
            }
            accept(Token.COLONEQ);
            SQLExpr value = this.exprParser.expr();
            SQLSetStatement stmt = new SQLSetStatement(variant, value, getDbType());
            statementList.add(stmt);
            continue;
        }
        // select
        if (lexer.token() == Token.LPAREN) {
            char ch = lexer.current();
            int bp = lexer.bp();
            lexer.nextToken();
            if (lexer.token() == Token.SELECT) {
                lexer.reset(bp, ch, Token.LPAREN);
                statementList.add(this.parseSelect());
                continue;
            } else {
                throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
            }
        }
        // assign statement
        if (lexer.token() == Token.SET) {
            statementList.add(this.parseAssign());
            continue;
        }
        // while statement
        if (lexer.token() == Token.WHILE) {
            statementList.add(this.parseWhile());
            continue;
        }
        // loop statement
        if (lexer.token() == Token.LOOP) {
            statementList.add(this.parseLoop());
            continue;
        }
        // if statement
        if (lexer.token() == Token.IF) {
            statementList.add(this.parseIf());
            continue;
        }
        // case statement
        if (lexer.token() == Token.CASE) {
            statementList.add(this.parseCase());
            continue;
        }
        // declare statement
        if (lexer.token() == Token.DECLARE) {
            char markChar = lexer.current();
            int markBp = lexer.bp();
            lexer.nextToken();
            lexer.nextToken();
            if (// cursor declare statement
            lexer.token() == Token.CURSOR) {
                lexer.reset(markBp, markChar, Token.DECLARE);
                statementList.add(this.parseCursorDeclare());
            } else if (identifierEquals("HANDLER")) {
                //DECLARE异常处理程序 [add by zhujun 2016-04-16]
                lexer.reset(markBp, markChar, Token.DECLARE);
                statementList.add(this.parseDeclareHandler());
            } else if (lexer.token() == Token.CONDITION) {
                //DECLARE异常 [add by zhujun 2016-04-17]
                lexer.reset(markBp, markChar, Token.DECLARE);
                statementList.add(this.parseDeclareCondition());
            } else {
                lexer.reset(markBp, markChar, Token.DECLARE);
                statementList.add(this.parseDeclare());
            }
            continue;
        }
        // leave statement
        if (lexer.token() == Token.LEAVE) {
            statementList.add(this.parseLeave());
            continue;
        }
        // iterate statement
        if (lexer.token() == Token.ITERATE) {
            statementList.add(this.parseIterate());
            continue;
        }
        // repeat statement
        if (lexer.token() == Token.REPEAT) {
            statementList.add(this.parseRepeat());
            continue;
        }
        // open cursor
        if (lexer.token() == Token.OPEN) {
            statementList.add(this.parseOpen());
            continue;
        }
        // close cursor
        if (lexer.token() == Token.CLOSE) {
            statementList.add(this.parseClose());
            continue;
        }
        // fetch cursor into
        if (lexer.token() == Token.FETCH) {
            statementList.add(this.parseFetch());
            continue;
        }
        if (lexer.token() == Token.IDENTIFIER) {
            String label = lexer.stringVal();
            char ch = lexer.current();
            int bp = lexer.bp();
            lexer.nextToken();
            if (lexer.token() == Token.VARIANT && lexer.stringVal().equals(":")) {
                lexer.nextToken();
                if (lexer.token() == Token.LOOP) {
                    // parse loop statement
                    statementList.add(this.parseLoop(label));
                } else if (lexer.token() == Token.WHILE) {
                    // parse while statement with label
                    statementList.add(this.parseWhile(label));
                } else if (lexer.token() == Token.BEGIN) {
                    // parse begin-end statement with label
                    statementList.add(this.parseBlock(label));
                } else if (lexer.token() == Token.REPEAT) {
                    // parse repeat statement with label
                    statementList.add(this.parseRepeat(label));
                }
                continue;
            } else {
                lexer.reset(bp, ch, Token.IDENTIFIER);
            }
        }
        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint)

Example 7 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class MySqlExprParser method additiveRest.

public SQLExpr additiveRest(SQLExpr expr) {
    if (lexer.token() == Token.PLUS) {
        lexer.nextToken();
        SQLExpr rightExp = multiplicative();
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Add, rightExp, JdbcConstants.MYSQL);
        expr = additiveRest(expr);
    } else if (lexer.token() == Token.SUB) {
        lexer.nextToken();
        SQLExpr rightExp = multiplicative();
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Subtract, rightExp, JdbcConstants.MYSQL);
        expr = additiveRest(expr);
    }
    return expr;
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 8 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class MySqlExprParser method notRationalRest.

public SQLExpr notRationalRest(SQLExpr expr) {
    if (identifierEquals("REGEXP")) {
        lexer.nextToken();
        SQLExpr rightExp = primary();
        rightExp = relationalRest(rightExp);
        return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRegExp, rightExp, JdbcConstants.MYSQL);
    }
    return super.notRationalRest(expr);
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 9 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class OracleExprParser method exprRest.

public SQLExpr exprRest(SQLExpr expr) {
    expr = super.exprRest(expr);
    if (lexer.token() == Token.COLONEQ) {
        lexer.nextToken();
        SQLExpr right = expr();
        expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Assignment, right, getDbType());
    }
    return expr;
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 10 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.

the class OracleExprParser method relationalRest.

public SQLExpr relationalRest(SQLExpr expr) {
    if (lexer.token() == Token.IS) {
        lexer.nextToken();
        if (lexer.token() == Token.NOT) {
            lexer.nextToken();
            SQLExpr rightExpr = primary();
            expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.IsNot, rightExpr, getDbType());
        } else if (identifierEquals("A")) {
            lexer.nextToken();
            accept(Token.SET);
            expr = new OracleIsSetExpr(expr);
        } else {
            SQLExpr rightExpr = primary();
            expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Is, rightExpr, getDbType());
        }
        return expr;
    }
    return super.relationalRest(expr);
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) OracleIsSetExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIsSetExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)46 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)30 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)9 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)8 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)8 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)7 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)7 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)6 SQLObject (com.alibaba.druid.sql.ast.SQLObject)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)5 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)4 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)4 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 SQLOver (com.alibaba.druid.sql.ast.SQLOver)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3