Search in sources :

Example 16 with ParserException

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

the class MySqlLexer method scanVariable.

public void scanVariable() {
    if (ch != '@' && ch != ':' && ch != '#' && ch != '$') {
        throw new ParserException("illegal variable");
    }
    mark = pos;
    bufPos = 1;
    if (charAt(pos + 1) == '@') {
        ch = charAt(++pos);
        bufPos++;
    }
    if (charAt(pos + 1) == '`') {
        ++pos;
        ++bufPos;
        char ch;
        for (; ; ) {
            ch = charAt(++pos);
            if (ch == '`') {
                bufPos++;
                ch = charAt(++pos);
                break;
            } else if (ch == EOI) {
                throw new ParserException("illegal identifier");
            }
            bufPos++;
            continue;
        }
        this.ch = charAt(pos);
        stringVal = subString(mark, bufPos);
        token = Token.VARIANT;
    } else if (charAt(pos + 1) == '{') {
        ++pos;
        ++bufPos;
        char ch;
        for (; ; ) {
            ch = charAt(++pos);
            if (ch == '}') {
                bufPos++;
                ch = charAt(++pos);
                break;
            } else if (ch == EOI) {
                throw new ParserException("illegal identifier");
            }
            bufPos++;
            continue;
        }
        this.ch = charAt(pos);
        stringVal = subString(mark, bufPos);
        token = Token.VARIANT;
    } else {
        for (; ; ) {
            ch = charAt(++pos);
            if (!isIdentifierChar(ch)) {
                break;
            }
            bufPos++;
            continue;
        }
    }
    this.ch = charAt(pos);
    stringVal = subString(mark, bufPos);
    token = Token.VARIANT;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException)

Example 17 with ParserException

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

the class DB2SelectParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    accept(Token.SELECT);
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
    }
    DB2SelectQueryBlock queryBlock = new DB2SelectQueryBlock();
    if (lexer.token() == Token.DISTINCT) {
        queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
        lexer.nextToken();
    } else if (lexer.token() == Token.UNIQUE) {
        queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE);
        lexer.nextToken();
    } else if (lexer.token() == Token.ALL) {
        queryBlock.setDistionOption(SQLSetQuantifier.ALL);
        lexer.nextToken();
    }
    parseSelectList(queryBlock);
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    if (lexer.token() == Token.ORDER) {
        SQLOrderBy orderBy = parseOrderBy();
        queryBlock.setOrderBy(orderBy);
    }
    for (; ; ) {
        if (lexer.token() == Token.FETCH) {
            lexer.nextToken();
            accept(Token.FIRST);
            SQLExpr first = this.exprParser.primary();
            queryBlock.setFirst(first);
            if (identifierEquals("ROW") || identifierEquals("ROWS")) {
                lexer.nextToken();
            }
            accept(Token.ONLY);
            continue;
        }
        if (lexer.token() == Token.WITH) {
            lexer.nextToken();
            if (identifierEquals("RR")) {
                queryBlock.setIsolation(Isolation.RR);
            } else if (identifierEquals("RS")) {
                queryBlock.setIsolation(Isolation.RS);
            } else if (identifierEquals("CS")) {
                queryBlock.setIsolation(Isolation.CS);
            } else if (identifierEquals("UR")) {
                queryBlock.setIsolation(Isolation.UR);
            } else {
                throw new ParserException("TODO");
            }
            lexer.nextToken();
            continue;
        }
        if (lexer.token() == Token.FOR) {
            lexer.nextToken();
            if (lexer.token() == Token.UPDATE) {
                queryBlock.setForUpdate(true);
                lexer.nextToken();
            } else {
                acceptIdentifier("READ");
                accept(Token.ONLY);
                queryBlock.setForReadOnly(true);
            }
        }
        if (lexer.token() == Token.OPTIMIZE) {
            lexer.nextToken();
            accept(Token.FOR);
            queryBlock.setOptimizeFor(this.expr());
            if (identifierEquals("ROW")) {
                lexer.nextToken();
            } else {
                acceptIdentifier("ROWS");
            }
        }
        break;
    }
    return queryRest(queryBlock);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 18 with ParserException

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

the class OracleExprParser method parseAggregateExpr.

protected SQLAggregateExpr parseAggregateExpr(String methodName) {
    methodName = methodName.toUpperCase();
    SQLAggregateExpr aggregateExpr;
    if (lexer.token() == Token.UNIQUE) {
        aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateOption.UNIQUE);
        lexer.nextToken();
    } else if (lexer.token() == (Token.ALL)) {
        aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateOption.ALL);
        lexer.nextToken();
    } else if (lexer.token() == (Token.DISTINCT)) {
        aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateOption.DISTINCT);
        lexer.nextToken();
    } else {
        aggregateExpr = new SQLAggregateExpr(methodName);
    }
    exprList(aggregateExpr.getArguments(), aggregateExpr);
    if (lexer.stringVal().equalsIgnoreCase("IGNORE")) {
        lexer.nextToken();
        identifierEquals("NULLS");
        aggregateExpr.setIgnoreNulls(true);
    }
    accept(Token.RPAREN);
    if (identifierEquals("WITHIN")) {
        lexer.nextToken();
        accept(Token.GROUP);
        accept(Token.LPAREN);
        SQLOrderBy withinGroup = this.parseOrderBy();
        aggregateExpr.setWithinGroup(withinGroup);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.KEEP) {
        lexer.nextToken();
        SQLKeep keep = new SQLKeep();
        accept(Token.LPAREN);
        acceptIdentifier("DENSE_RANK");
        if (identifierEquals("FIRST")) {
            lexer.nextToken();
            keep.setDenseRank(DenseRank.FIRST);
        } else {
            acceptIdentifier("LAST");
            keep.setDenseRank(DenseRank.LAST);
        }
        SQLOrderBy orderBy = this.parseOrderBy();
        keep.setOrderBy(orderBy);
        aggregateExpr.setKeep(keep);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.OVER) {
        OracleAnalytic over = new OracleAnalytic();
        lexer.nextToken();
        accept(Token.LPAREN);
        if (identifierEquals("PARTITION")) {
            lexer.nextToken();
            accept(Token.BY);
            if (lexer.token() == (Token.LPAREN)) {
                lexer.nextToken();
                exprList(over.getPartitionBy(), over);
                accept(Token.RPAREN);
            } else {
                exprList(over.getPartitionBy(), over);
            }
        }
        over.setOrderBy(parseOrderBy());
        if (over.getOrderBy() != null) {
            OracleAnalyticWindowing windowing = null;
            if (lexer.stringVal().equalsIgnoreCase("ROWS")) {
                lexer.nextToken();
                windowing = new OracleAnalyticWindowing();
                windowing.setType(OracleAnalyticWindowing.Type.ROWS);
            } else if (lexer.stringVal().equalsIgnoreCase("RANGE")) {
                lexer.nextToken();
                windowing = new OracleAnalyticWindowing();
                windowing.setType(OracleAnalyticWindowing.Type.RANGE);
            }
            if (windowing != null) {
                if (lexer.stringVal().equalsIgnoreCase("CURRENT")) {
                    lexer.nextToken();
                    if (lexer.stringVal().equalsIgnoreCase("ROW")) {
                        lexer.nextToken();
                        windowing.setExpr(new SQLIdentifierExpr("CURRENT ROW"));
                        over.setWindowing(windowing);
                    }
                    throw new ParserException("syntax error");
                }
                if (lexer.stringVal().equalsIgnoreCase("UNBOUNDED")) {
                    lexer.nextToken();
                    if (lexer.stringVal().equalsIgnoreCase("PRECEDING")) {
                        lexer.nextToken();
                        windowing.setExpr(new SQLIdentifierExpr("UNBOUNDED PRECEDING"));
                    } else {
                        throw new ParserException("syntax error");
                    }
                }
                over.setWindowing(windowing);
            }
        }
        accept(Token.RPAREN);
        aggregateExpr.setOver(over);
    }
    return aggregateExpr;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLKeep(com.alibaba.druid.sql.ast.SQLKeep) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) OracleAnalytic(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalytic) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr) OracleAnalyticWindowing(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalyticWindowing)

Example 19 with ParserException

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

the class OracleSelectParser method withSubquery.

protected void withSubquery(SQLSelect select) {
    if (lexer.token() == Token.WITH) {
        lexer.nextToken();
        SQLWithSubqueryClause subqueryFactoringClause = new SQLWithSubqueryClause();
        for (; ; ) {
            OracleWithSubqueryEntry entry = new OracleWithSubqueryEntry();
            entry.setName((SQLIdentifierExpr) this.exprParser.name());
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                exprParser.names(entry.getColumns());
                accept(Token.RPAREN);
            }
            accept(Token.AS);
            accept(Token.LPAREN);
            entry.setSubQuery(select());
            accept(Token.RPAREN);
            if (identifierEquals("SEARCH")) {
                lexer.nextToken();
                SearchClause searchClause = new SearchClause();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax erorr : " + lexer.token());
                }
                searchClause.setType(SearchClause.Type.valueOf(lexer.stringVal()));
                lexer.nextToken();
                acceptIdentifier("FIRST");
                accept(Token.BY);
                searchClause.addItem(exprParser.parseSelectOrderByItem());
                while (lexer.token() == (Token.COMMA)) {
                    lexer.nextToken();
                    searchClause.addItem(exprParser.parseSelectOrderByItem());
                }
                accept(Token.SET);
                searchClause.setOrderingColumn((SQLIdentifierExpr) exprParser.name());
                entry.setSearchClause(searchClause);
            }
            if (identifierEquals("CYCLE")) {
                lexer.nextToken();
                CycleClause cycleClause = new CycleClause();
                exprParser.exprList(cycleClause.getAliases(), cycleClause);
                accept(Token.SET);
                cycleClause.setMark(exprParser.expr());
                accept(Token.TO);
                cycleClause.setValue(exprParser.expr());
                accept(Token.DEFAULT);
                cycleClause.setDefaultValue(exprParser.expr());
                entry.setCycleClause(cycleClause);
            }
            subqueryFactoringClause.addEntry(entry);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
        select.setWithSubQuery(subqueryFactoringClause);
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) CycleClause(com.alibaba.druid.sql.dialect.oracle.ast.clause.CycleClause) SearchClause(com.alibaba.druid.sql.dialect.oracle.ast.clause.SearchClause) OracleWithSubqueryEntry(com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry)

Example 20 with ParserException

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

the class OracleSelectParser method select.

public OracleSelect select() {
    OracleSelect select = new OracleSelect();
    withSubquery(select);
    SQLSelectQuery query = query();
    select.setQuery(query);
    SQLOrderBy orderBy = this.parseOrderBy();
    select.setOrderBy(orderBy);
    if (orderBy != null && query instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
        parseFetchClause(queryBlock);
    }
    if (lexer.token() == (Token.FOR)) {
        lexer.nextToken();
        accept(Token.UPDATE);
        OracleSelectForUpdate forUpdate = new OracleSelectForUpdate();
        if (lexer.token() == Token.OF) {
            lexer.nextToken();
            this.exprParser.exprList(forUpdate.getOf(), forUpdate);
        }
        if (lexer.token() == Token.NOWAIT) {
            lexer.nextToken();
            forUpdate.setNotWait(true);
        } else if (lexer.token() == Token.WAIT) {
            lexer.nextToken();
            forUpdate.setWait(this.exprParser.primary());
        } else if (identifierEquals("SKIP")) {
            lexer.nextToken();
            acceptIdentifier("LOCKED");
            forUpdate.setSkipLocked(true);
        }
        select.setForUpdate(forUpdate);
    }
    if (select.getOrderBy() == null) {
        select.setOrderBy(this.exprParser.parseOrderBy());
    }
    if (lexer.token() == Token.WITH) {
        lexer.nextToken();
        if (identifierEquals("READ")) {
            lexer.nextToken();
            if (identifierEquals("ONLY")) {
                lexer.nextToken();
            } else {
                throw new ParserException("syntax error");
            }
            select.setRestriction(new OracleSelectRestriction.ReadOnly());
        } else if (lexer.token() == (Token.CHECK)) {
            lexer.nextToken();
            if (identifierEquals("OPTION")) {
                lexer.nextToken();
            } else {
                throw new ParserException("syntax error");
            }
            OracleSelectRestriction.CheckOption checkOption = new OracleSelectRestriction.CheckOption();
            if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                throw new ParserException("TODO");
            }
            select.setRestriction(checkOption);
        } else {
            throw new ParserException("syntax error");
        }
    }
    return select;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) OracleSelect(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect) OracleSelectForUpdate(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectForUpdate) OracleSelectRestriction(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectRestriction)

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