Search in sources :

Example 56 with ParserException

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

the class MySqlLexer method scanSharp.

public void scanSharp() {
    if (ch != '#') {
        throw new ParserException("illegal stat");
    }
    if (charAt(pos + 1) == '{') {
        scanVariable();
        return;
    }
    Token lastToken = this.token;
    scanChar();
    mark = pos;
    bufPos = 0;
    for (; ; ) {
        if (ch == '\r') {
            if (charAt(pos + 1) == '\n') {
                bufPos += 2;
                scanChar();
                break;
            }
            bufPos++;
            break;
        } else if (ch == EOI) {
            break;
        }
        if (ch == '\n') {
            scanChar();
            bufPos++;
            break;
        }
        scanChar();
        bufPos++;
    }
    stringVal = subString(mark - 1, bufPos + 1);
    token = Token.LINE_COMMENT;
    commentCount++;
    if (keepComments) {
        addComment(stringVal);
    }
    if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
        return;
    }
    endOfComment = isEOF();
    if (!isAllowComment() && (isEOF() || !isSafeComment(stringVal))) {
        throw new NotAllowCommentException();
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) NotAllowCommentException(com.alibaba.druid.sql.parser.NotAllowCommentException) Token(com.alibaba.druid.sql.parser.Token)

Example 57 with ParserException

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

the class PGSelectParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == Token.VALUES) {
        lexer.nextToken();
        accept(Token.LPAREN);
        PGValuesQuery valuesQuery = new PGValuesQuery();
        this.exprParser.exprList(valuesQuery.getValues(), valuesQuery);
        accept(Token.RPAREN);
        return queryRest(valuesQuery);
    }
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        if (select instanceof SQLSelectQueryBlock) {
            ((SQLSelectQueryBlock) select).setParenthesized(true);
        }
        accept(Token.RPAREN);
        return queryRest(select);
    }
    PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
    if (lexer.token() == Token.SELECT) {
        lexer.nextToken();
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == Token.DISTINCT) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
            lexer.nextToken();
            if (lexer.token() == Token.ON) {
                lexer.nextToken();
                for (; ; ) {
                    SQLExpr expr = this.createExprParser().expr();
                    queryBlock.getDistinctOn().add(expr);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    } else {
                        break;
                    }
                }
            }
        } else if (lexer.token() == Token.ALL) {
            queryBlock.setDistionOption(SQLSetQuantifier.ALL);
            lexer.nextToken();
        }
        parseSelectList(queryBlock);
        if (lexer.token() == Token.INTO) {
            lexer.nextToken();
            if (lexer.token() == Token.TEMPORARY) {
                lexer.nextToken();
                queryBlock.setIntoOption(IntoOption.TEMPORARY);
            } else if (lexer.token() == Token.TEMP) {
                lexer.nextToken();
                queryBlock.setIntoOption(IntoOption.TEMP);
            } else if (lexer.token() == Token.UNLOGGED) {
                lexer.nextToken();
                queryBlock.setIntoOption(IntoOption.UNLOGGED);
            }
            if (lexer.token() == Token.TABLE) {
                lexer.nextToken();
            }
            SQLExpr name = this.createExprParser().name();
            queryBlock.setInto(new SQLExprTableSource(name));
        }
    }
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    if (lexer.token() == Token.WINDOW) {
        lexer.nextToken();
        PGSelectQueryBlock.WindowClause window = new PGSelectQueryBlock.WindowClause();
        window.setName(this.expr());
        accept(Token.AS);
        for (; ; ) {
            SQLExpr expr = this.createExprParser().expr();
            window.getDefinition().add(expr);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            } else {
                break;
            }
        }
        queryBlock.setWindow(window);
    }
    queryBlock.setOrderBy(this.createExprParser().parseOrderBy());
    for (; ; ) {
        if (lexer.token() == Token.LIMIT) {
            SQLLimit limit = new SQLLimit();
            lexer.nextToken();
            if (lexer.token() == Token.ALL) {
                limit.setRowCount(new SQLIdentifierExpr("ALL"));
                lexer.nextToken();
            } else {
                limit.setRowCount(expr());
            }
            queryBlock.setLimit(limit);
        } else if (lexer.token() == Token.OFFSET) {
            SQLLimit limit = queryBlock.getLimit();
            if (limit == null) {
                limit = new SQLLimit();
                queryBlock.setLimit(limit);
            }
            lexer.nextToken();
            SQLExpr offset = expr();
            limit.setOffset(offset);
            if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
                lexer.nextToken();
            }
        } else {
            break;
        }
    }
    if (lexer.token() == Token.FETCH) {
        lexer.nextToken();
        PGSelectQueryBlock.FetchClause fetch = new PGSelectQueryBlock.FetchClause();
        if (lexer.token() == Token.FIRST) {
            fetch.setOption(PGSelectQueryBlock.FetchClause.Option.FIRST);
        } else if (lexer.token() == Token.NEXT) {
            fetch.setOption(PGSelectQueryBlock.FetchClause.Option.NEXT);
        } else {
            throw new ParserException("expect 'FIRST' or 'NEXT'");
        }
        SQLExpr count = expr();
        fetch.setCount(count);
        if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
            lexer.nextToken();
        } else {
            throw new ParserException("expect 'ROW' or 'ROWS'");
        }
        if (lexer.token() == Token.ONLY) {
            lexer.nextToken();
        } else {
            throw new ParserException("expect 'ONLY'");
        }
        queryBlock.setFetch(fetch);
    }
    if (lexer.token() == Token.FOR) {
        lexer.nextToken();
        PGSelectQueryBlock.ForClause forClause = new PGSelectQueryBlock.ForClause();
        if (lexer.token() == Token.UPDATE) {
            forClause.setOption(PGSelectQueryBlock.ForClause.Option.UPDATE);
            lexer.nextToken();
        } else if (lexer.token() == Token.SHARE) {
            forClause.setOption(PGSelectQueryBlock.ForClause.Option.SHARE);
            lexer.nextToken();
        } else {
            throw new ParserException("expect 'FIRST' or 'NEXT'");
        }
        if (lexer.token() == Token.OF) {
            for (; ; ) {
                SQLExpr expr = this.createExprParser().expr();
                forClause.getOf().add(expr);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else {
                    break;
                }
            }
        }
        if (lexer.token() == Token.NOWAIT) {
            lexer.nextToken();
            forClause.setNoWait(true);
        }
        queryBlock.setForClause(forClause);
    }
    return queryRest(queryBlock);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) PGValuesQuery(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGValuesQuery) PGSelectQueryBlock(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Example 58 with ParserException

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

the class OracleSelectParser method parsePivot.

private void parsePivot(OracleSelectTableSource tableSource) {
    OracleSelectPivot.Item item;
    if (identifierEquals("PIVOT")) {
        lexer.nextToken();
        OracleSelectPivot pivot = new OracleSelectPivot();
        if (identifierEquals("XML")) {
            lexer.nextToken();
            pivot.setXml(true);
        }
        accept(Token.LPAREN);
        while (true) {
            item = new OracleSelectPivot.Item();
            item.setExpr((SQLAggregateExpr) this.exprParser.expr());
            item.setAlias(as());
            pivot.addItem(item);
            if (!(lexer.token() == (Token.COMMA))) {
                break;
            }
            lexer.nextToken();
        }
        accept(Token.FOR);
        if (lexer.token() == (Token.LPAREN)) {
            lexer.nextToken();
            while (true) {
                pivot.getPivotFor().add(new SQLIdentifierExpr(lexer.stringVal()));
                lexer.nextToken();
                if (!(lexer.token() == (Token.COMMA))) {
                    break;
                }
                lexer.nextToken();
            }
            accept(Token.RPAREN);
        } else {
            pivot.getPivotFor().add(new SQLIdentifierExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        accept(Token.IN);
        accept(Token.LPAREN);
        if (lexer.token() == (Token.LPAREN)) {
            throw new ParserException("TODO");
        }
        if (lexer.token() == (Token.SELECT)) {
            throw new ParserException("TODO");
        }
        for (; ; ) {
            item = new OracleSelectPivot.Item();
            item.setExpr(this.exprParser.expr());
            item.setAlias(as());
            pivot.getPivotIn().add(item);
            if (lexer.token() != Token.COMMA) {
                break;
            }
            lexer.nextToken();
        }
        accept(Token.RPAREN);
        accept(Token.RPAREN);
        tableSource.setPivot(pivot);
    } else if (identifierEquals("UNPIVOT")) {
        lexer.nextToken();
        OracleSelectUnPivot unPivot = new OracleSelectUnPivot();
        if (identifierEquals("INCLUDE")) {
            lexer.nextToken();
            acceptIdentifier("NULLS");
            unPivot.setNullsIncludeType(OracleSelectUnPivot.NullsIncludeType.INCLUDE_NULLS);
        } else if (identifierEquals("EXCLUDE")) {
            lexer.nextToken();
            acceptIdentifier("NULLS");
            unPivot.setNullsIncludeType(OracleSelectUnPivot.NullsIncludeType.EXCLUDE_NULLS);
        }
        accept(Token.LPAREN);
        if (lexer.token() == (Token.LPAREN)) {
            lexer.nextToken();
            this.exprParser.exprList(unPivot.getItems(), unPivot);
            accept(Token.RPAREN);
        } else {
            unPivot.addItem(this.exprParser.expr());
        }
        accept(Token.FOR);
        if (lexer.token() == (Token.LPAREN)) {
            lexer.nextToken();
            while (true) {
                unPivot.getPivotFor().add(new SQLIdentifierExpr(lexer.stringVal()));
                lexer.nextToken();
                if (!(lexer.token() == (Token.COMMA))) {
                    break;
                }
                lexer.nextToken();
            }
            accept(Token.RPAREN);
        } else {
            unPivot.getPivotFor().add(new SQLIdentifierExpr(lexer.stringVal()));
            lexer.nextToken();
        }
        accept(Token.IN);
        accept(Token.LPAREN);
        if (lexer.token() == (Token.LPAREN)) {
            throw new ParserException("TODO");
        }
        if (lexer.token() == (Token.SELECT)) {
            throw new ParserException("TODO");
        }
        for (; ; ) {
            item = new OracleSelectPivot.Item();
            item.setExpr(this.exprParser.expr());
            item.setAlias(as());
            unPivot.getPivotIn().add(item);
            if (lexer.token() != Token.COMMA) {
                break;
            }
            lexer.nextToken();
        }
        accept(Token.RPAREN);
        accept(Token.RPAREN);
        tableSource.setPivot(unPivot);
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) CellAssignmentItem(com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.CellAssignmentItem) OracleSelectPivot(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectPivot) OracleSelectUnPivot(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectUnPivot) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Example 59 with ParserException

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

the class OracleSelectParser method parseTableSourceQueryTableExpr.

private void parseTableSourceQueryTableExpr(OracleSelectTableReference tableReference) {
    tableReference.setExpr(this.exprParser.expr());
    {
        FlashbackQueryClause clause = flashback();
        tableReference.setFlashback(clause);
    }
    if (identifierEquals("SAMPLE")) {
        lexer.nextToken();
        SampleClause sample = new SampleClause();
        if (identifierEquals("BLOCK")) {
            sample.setBlock(true);
            lexer.nextToken();
        }
        accept(Token.LPAREN);
        this.exprParser.exprList(sample.getPercent(), sample);
        accept(Token.RPAREN);
        if (identifierEquals("SEED")) {
            lexer.nextToken();
            accept(Token.LPAREN);
            sample.setSeedValue(expr());
            accept(Token.RPAREN);
        }
        tableReference.setSampleClause(sample);
    }
    if (identifierEquals("PARTITION")) {
        lexer.nextToken();
        PartitionExtensionClause partition = new PartitionExtensionClause();
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            partition.setPartition(exprParser.name());
            accept(Token.RPAREN);
        } else {
            accept(Token.FOR);
            accept(Token.LPAREN);
            exprParser.names(partition.getFor());
            accept(Token.RPAREN);
        }
        tableReference.setPartition(partition);
    }
    if (identifierEquals("SUBPARTITION")) {
        lexer.nextToken();
        PartitionExtensionClause partition = new PartitionExtensionClause();
        partition.setSubPartition(true);
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            partition.setPartition(exprParser.name());
            accept(Token.RPAREN);
        } else {
            accept(Token.FOR);
            accept(Token.LPAREN);
            exprParser.names(partition.getFor());
            accept(Token.RPAREN);
        }
        tableReference.setPartition(partition);
    }
    if (identifierEquals("VERSIONS")) {
        lexer.nextToken();
        if (lexer.token() == Token.BETWEEN) {
            lexer.nextToken();
            VersionsFlashbackQueryClause clause = new VersionsFlashbackQueryClause();
            if (identifierEquals("SCN")) {
                clause.setType(AsOfFlashbackQueryClause.Type.SCN);
                lexer.nextToken();
            } else {
                acceptIdentifier("TIMESTAMP");
                clause.setType(AsOfFlashbackQueryClause.Type.TIMESTAMP);
            }
            SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) exprParser.expr();
            if (binaryExpr.getOperator() != SQLBinaryOperator.BooleanAnd) {
                throw new ParserException("syntax error : " + binaryExpr.getOperator());
            }
            clause.setBegin(binaryExpr.getLeft());
            clause.setEnd(binaryExpr.getRight());
            tableReference.setFlashback(clause);
        } else {
            throw new ParserException("TODO");
        }
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) FlashbackQueryClause(com.alibaba.druid.sql.dialect.oracle.ast.clause.FlashbackQueryClause) AsOfFlashbackQueryClause(com.alibaba.druid.sql.dialect.oracle.ast.clause.FlashbackQueryClause.AsOfFlashbackQueryClause) VersionsFlashbackQueryClause(com.alibaba.druid.sql.dialect.oracle.ast.clause.FlashbackQueryClause.VersionsFlashbackQueryClause) SampleClause(com.alibaba.druid.sql.dialect.oracle.ast.clause.SampleClause) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) VersionsFlashbackQueryClause(com.alibaba.druid.sql.dialect.oracle.ast.clause.FlashbackQueryClause.VersionsFlashbackQueryClause) PartitionExtensionClause(com.alibaba.druid.sql.dialect.oracle.ast.clause.PartitionExtensionClause)

Example 60 with ParserException

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

the class OracleStatementParser method parseAlterDrop.

public void parseAlterDrop(SQLAlterTableStatement stmt) {
    lexer.nextToken();
    if (lexer.token() == Token.CONSTRAINT) {
        lexer.nextToken();
        SQLAlterTableDropConstraint item = new SQLAlterTableDropConstraint();
        item.setConstraintName(this.exprParser.name());
        stmt.addItem(item);
    } else if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLAlterTableDropColumnItem item = new SQLAlterTableDropColumnItem();
        this.exprParser.names(item.getColumns());
        stmt.addItem(item);
        accept(Token.RPAREN);
    } else if (lexer.token() == Token.COLUMN) {
        lexer.nextToken();
        SQLAlterTableDropColumnItem item = new SQLAlterTableDropColumnItem();
        this.exprParser.names(item.getColumns());
        stmt.addItem(item);
    } else if (identifierEquals("PARTITION")) {
        lexer.nextToken();
        OracleAlterTableDropPartition item = new OracleAlterTableDropPartition();
        item.setName(this.exprParser.name());
        stmt.addItem(item);
    } else if (lexer.token() == Token.INDEX) {
        lexer.nextToken();
        SQLName indexName = this.exprParser.name();
        SQLAlterTableDropIndex item = new SQLAlterTableDropIndex();
        item.setIndexName(indexName);
        stmt.addItem(item);
    } else {
        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) OracleAlterTableDropPartition(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleAlterTableDropPartition)

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