Search in sources :

Example 11 with ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlStatementParser method parseLoadDataInFile.

protected MySqlLoadDataInFileStatement parseLoadDataInFile() {
    acceptIdentifier("DATA");
    MySqlLoadDataInFileStatement stmt = new MySqlLoadDataInFileStatement();
    if (identifierEquals(LOW_PRIORITY)) {
        stmt.setLowPriority(true);
        lexer.nextToken();
    }
    if (identifierEquals("CONCURRENT")) {
        stmt.setConcurrent(true);
        lexer.nextToken();
    }
    if (identifierEquals(LOCAL)) {
        stmt.setLocal(true);
        lexer.nextToken();
    }
    acceptIdentifier("INFILE");
    SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr();
    stmt.setFileName(fileName);
    if (lexer.token() == Token.REPLACE) {
        stmt.setReplicate(true);
        lexer.nextToken();
    }
    if (identifierEquals(IGNORE)) {
        stmt.setIgnore(true);
        lexer.nextToken();
    }
    accept(Token.INTO);
    accept(Token.TABLE);
    SQLName tableName = exprParser.name();
    stmt.setTableName(tableName);
    if (identifierEquals(CHARACTER)) {
        lexer.nextToken();
        accept(Token.SET);
        if (lexer.token() != Token.LITERAL_CHARS) {
            throw new ParserException("syntax error, illegal charset");
        }
        String charset = lexer.stringVal();
        lexer.nextToken();
        stmt.setCharset(charset);
    }
    if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) {
        lexer.nextToken();
        if (identifierEquals("TERMINATED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsTerminatedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("OPTIONALLY")) {
            stmt.setColumnsEnclosedOptionally(true);
            lexer.nextToken();
        }
        if (identifierEquals("ENCLOSED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsEnclosedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("ESCAPED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setColumnsEscaped(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
    }
    if (identifierEquals("LINES")) {
        lexer.nextToken();
        if (identifierEquals("STARTING")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setLinesStartingBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        if (identifierEquals("TERMINATED")) {
            lexer.nextToken();
            accept(Token.BY);
            stmt.setLinesTerminatedBy(new SQLCharExpr(lexer.stringVal()));
            lexer.nextToken();
        }
    }
    if (identifierEquals(IGNORE)) {
        lexer.nextToken();
        stmt.setIgnoreLinesNumber(this.exprParser.expr());
        acceptIdentifier("LINES");
    }
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getColumns(), stmt);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.SET) {
        lexer.nextToken();
        this.exprParser.exprList(stmt.getSetList(), stmt);
    }
    return stmt;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) MySqlLoadDataInFileStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement)

Example 12 with ParserException

use of com.alibaba.druid.sql.parser.ParserException 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 ParserException

use of com.alibaba.druid.sql.parser.ParserException 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 ParserException

use of com.alibaba.druid.sql.parser.ParserException 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 ParserException

use of com.alibaba.druid.sql.parser.ParserException in project druid by alibaba.

the class MySqlExprParser method primary.

public SQLExpr primary() {
    final Token tok = lexer.token();
    if (identifierEquals("outfile")) {
        lexer.nextToken();
        SQLExpr file = primary();
        SQLExpr expr = new MySqlOutFileExpr(file);
        return primaryRest(expr);
    }
    switch(tok) {
        case LITERAL_ALIAS:
            String aliasValue = lexer.stringVal();
            lexer.nextToken();
            return primaryRest(new SQLCharExpr(aliasValue));
        case VARIANT:
            SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(lexer.stringVal());
            lexer.nextToken();
            if (varRefExpr.getName().equalsIgnoreCase("@@global")) {
                accept(Token.DOT);
                varRefExpr = new SQLVariantRefExpr(lexer.stringVal(), true);
                lexer.nextToken();
            } else if (varRefExpr.getName().equals("@") && lexer.token() == Token.LITERAL_CHARS) {
                varRefExpr.setName("@'" + lexer.stringVal() + "'");
                lexer.nextToken();
            } else if (varRefExpr.getName().equals("@@") && lexer.token() == Token.LITERAL_CHARS) {
                varRefExpr.setName("@@'" + lexer.stringVal() + "'");
                lexer.nextToken();
            }
            return primaryRest(varRefExpr);
        case VALUES:
            lexer.nextToken();
            if (lexer.token() != Token.LPAREN) {
                throw new ParserException("syntax error, illegal values clause");
            }
            return this.methodRest(new SQLIdentifierExpr("VALUES"), true);
        case BINARY:
            lexer.nextToken();
            if (lexer.token() == Token.COMMA || lexer.token() == Token.SEMI || lexer.token() == Token.EOF) {
                return new SQLIdentifierExpr("BINARY");
            } else {
                SQLUnaryExpr binaryExpr = new SQLUnaryExpr(SQLUnaryOperator.BINARY, expr());
                return primaryRest(binaryExpr);
            }
        case CACHE:
        case GROUP:
            lexer.nextToken();
            return primaryRest(new SQLIdentifierExpr(lexer.stringVal()));
        default:
            return super.primary();
    }
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) Token(com.alibaba.druid.sql.parser.Token) MySqlOutFileExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

ParserException (com.alibaba.druid.sql.parser.ParserException)98 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)25 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLName (com.alibaba.druid.sql.ast.SQLName)16 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)9 Token (com.alibaba.druid.sql.parser.Token)9 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)5 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)5 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)4 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)4 OracleConstraint (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint)4 DbType (com.alibaba.druid.DbType)3 SQLDeclareItem (com.alibaba.druid.sql.ast.SQLDeclareItem)3 SQLPartitionByHash (com.alibaba.druid.sql.ast.SQLPartitionByHash)3