Search in sources :

Example 11 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class MySqlStatementParser method parseShowTabless.

private SQLShowTablesStatement parseShowTabless() {
    SQLShowTablesStatement stmt = new SQLShowTablesStatement();
    if (lexer.token() == Token.FROM) {
        lexer.nextToken();
        SQLName database = exprParser.name();
        stmt.setDatabase(database);
    }
    if (lexer.token() == Token.LIKE) {
        lexer.nextToken();
        SQLExpr like = exprParser.expr();
        stmt.setLike(like);
    }
    if (lexer.token() == Token.WHERE) {
        lexer.nextToken();
        SQLExpr where = exprParser.expr();
        stmt.setWhere(where);
    }
    return stmt;
}
Also used : SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 12 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr 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 13 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class MySqlStatementParser method parseDeleteStatement.

public MySqlDeleteStatement parseDeleteStatement() {
    MySqlDeleteStatement deleteStatement = new MySqlDeleteStatement();
    if (lexer.token() == Token.DELETE) {
        lexer.nextToken();
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == Token.HINT) {
            this.getExprParser().parseHints(deleteStatement.getHints());
        }
        if (identifierEquals(LOW_PRIORITY)) {
            deleteStatement.setLowPriority(true);
            lexer.nextToken();
        }
        if (identifierEquals("QUICK")) {
            deleteStatement.setQuick(true);
            lexer.nextToken();
        }
        if (identifierEquals(IGNORE)) {
            deleteStatement.setIgnore(true);
            lexer.nextToken();
        }
        if (lexer.token() == Token.IDENTIFIER) {
            deleteStatement.setTableSource(createSQLSelectParser().parseTableSource());
            if (lexer.token() == Token.FROM) {
                lexer.nextToken();
                SQLTableSource tableSource = createSQLSelectParser().parseTableSource();
                deleteStatement.setFrom(tableSource);
            }
        } else if (lexer.token() == Token.FROM) {
            lexer.nextToken();
            deleteStatement.setTableSource(createSQLSelectParser().parseTableSource());
        } else {
            throw new ParserException("syntax error");
        }
        if (identifierEquals("USING")) {
            lexer.nextToken();
            SQLTableSource tableSource = createSQLSelectParser().parseTableSource();
            deleteStatement.setUsing(tableSource);
        }
    }
    if (lexer.token() == (Token.WHERE)) {
        lexer.nextToken();
        SQLExpr where = this.exprParser.expr();
        deleteStatement.setWhere(where);
    }
    if (lexer.token() == (Token.ORDER)) {
        SQLOrderBy orderBy = exprParser.parseOrderBy();
        deleteStatement.setOrderBy(orderBy);
    }
    deleteStatement.setLimit(this.exprParser.parseLimit());
    return deleteStatement;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 14 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class MySqlStatementParser method parseDeclare.

/**
     * parse declare statement
     */
public MySqlDeclareStatement parseDeclare() {
    MySqlDeclareStatement stmt = new MySqlDeclareStatement();
    accept(Token.DECLARE);
    // lexer.nextToken();
    for (; ; ) {
        SQLDeclareItem item = new SQLDeclareItem();
        item.setName(exprParser.name());
        stmt.addVar(item);
        if (lexer.token() == Token.COMMA) {
            accept(Token.COMMA);
            continue;
        } else if (lexer.token() != Token.EOF) {
            // var type
            item.setDataType(exprParser.parseDataType());
            if (lexer.token() == Token.DEFAULT) {
                lexer.nextToken();
                SQLExpr defaultValue = this.exprParser.primary();
                item.setValue(defaultValue);
            }
            break;
        } else {
            throw new ParserException("TODO");
        }
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlDeclareStatement(com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLDeclareItem(com.alibaba.druid.sql.ast.SQLDeclareItem)

Example 15 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class SQLUtils method toSQLExpr.

public static SQLExpr toSQLExpr(String sql, String dbType) {
    SQLExprParser parser = SQLParserUtils.createExprParser(sql, dbType);
    SQLExpr expr = parser.expr();
    if (parser.getLexer().token() != Token.EOF) {
        throw new ParserException("illegal sql expr : " + sql);
    }
    return expr;
}
Also used : SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)422 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)71 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)68 SQLName (com.alibaba.druid.sql.ast.SQLName)47 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)33 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)32 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)30 ArrayList (java.util.ArrayList)30 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)28 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)25 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)25 ParserException (com.alibaba.druid.sql.parser.ParserException)25 SQLObject (com.alibaba.druid.sql.ast.SQLObject)24 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)23 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)20 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)17 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)16 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)15 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)15