Search in sources :

Example 61 with SQLExpr

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

the class OracleExprParser method primaryRest.

public SQLExpr primaryRest(SQLExpr expr) {
    if (expr.getClass() == SQLIdentifierExpr.class) {
        String ident = ((SQLIdentifierExpr) expr).getName();
        if ("TIMESTAMP".equalsIgnoreCase(ident)) {
            if (lexer.token() != Token.LITERAL_ALIAS && lexer.token() != Token.LITERAL_CHARS) {
                return new SQLIdentifierExpr("TIMESTAMP");
            }
            SQLTimestampExpr timestamp = new SQLTimestampExpr();
            String literal = lexer.stringVal();
            timestamp.setLiteral(literal);
            accept(Token.LITERAL_CHARS);
            if (identifierEquals("AT")) {
                lexer.nextToken();
                acceptIdentifier("TIME");
                acceptIdentifier("ZONE");
                String timezone = lexer.stringVal();
                timestamp.setTimeZone(timezone);
                accept(Token.LITERAL_CHARS);
            }
            return primaryRest(timestamp);
        }
    }
    if (lexer.token() == Token.IDENTIFIER && expr instanceof SQLNumericLiteralExpr) {
        String ident = lexer.stringVal();
        if (ident.length() == 1) {
            char unit = ident.charAt(0);
            switch(unit) {
                case 'K':
                case 'M':
                case 'G':
                case 'T':
                case 'P':
                case 'E':
                case 'k':
                case 'm':
                case 'g':
                case 't':
                case 'p':
                case 'e':
                    expr = new OracleSizeExpr(expr, OracleSizeExpr.Unit.valueOf(ident.toUpperCase()));
                    lexer.nextToken();
                    break;
                default:
                    break;
            }
        }
    }
    if (lexer.token() == Token.DOTDOT) {
        lexer.nextToken();
        SQLExpr upBound = expr();
        return new OracleRangeExpr(expr, upBound);
    }
    if (lexer.token() == Token.MONKEYS_AT) {
        lexer.nextToken();
        OracleDbLinkExpr dblink = new OracleDbLinkExpr();
        dblink.setExpr(expr);
        if (lexer.token() == Token.BANG) {
            dblink.setDbLink("!");
            lexer.nextToken();
        } else {
            String link = lexer.stringVal();
            accept(Token.IDENTIFIER);
            dblink.setDbLink(link);
        }
        expr = dblink;
    }
    if (identifierEquals("DAY") || identifierEquals("YEAR")) {
        lexer.mark();
        String name = lexer.stringVal();
        lexer.nextToken();
        if (lexer.token() == Token.COMMA) {
            lexer.reset();
            return expr;
        }
        OracleIntervalExpr interval = new OracleIntervalExpr();
        interval.setValue(expr);
        OracleIntervalType type = OracleIntervalType.valueOf(name);
        interval.setType(type);
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            if (lexer.token() != Token.LITERAL_INT) {
                throw new ParserException("syntax error");
            }
            interval.setPrecision(lexer.integerValue().intValue());
            lexer.nextToken();
            accept(Token.RPAREN);
        }
        accept(Token.TO);
        if (identifierEquals("SECOND")) {
            lexer.nextToken();
            interval.setToType(OracleIntervalType.SECOND);
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                if (lexer.token() != Token.LITERAL_INT) {
                    throw new ParserException("syntax error");
                }
                interval.setFactionalSecondsPrecision(lexer.integerValue().intValue());
                lexer.nextToken();
                accept(Token.RPAREN);
            }
        } else {
            interval.setToType(OracleIntervalType.MONTH);
            lexer.nextToken();
        }
        expr = interval;
    }
    if (identifierEquals("AT")) {
        char markChar = lexer.current();
        int markBp = lexer.bp();
        lexer.nextToken();
        if (identifierEquals("LOCAL")) {
            lexer.nextToken();
            expr = new OracleDatetimeExpr(expr, new SQLIdentifierExpr("LOCAL"));
        } else {
            if (identifierEquals("TIME")) {
                lexer.nextToken();
            } else {
                lexer.reset(markBp, markChar, Token.IDENTIFIER);
                return expr;
            }
            acceptIdentifier("ZONE");
            SQLExpr timeZone = primary();
            expr = new OracleDatetimeExpr(expr, timeZone);
        }
    }
    SQLExpr restExpr = super.primaryRest(expr);
    if (restExpr != expr && restExpr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvoke = (SQLMethodInvokeExpr) restExpr;
        if (methodInvoke.getParameters().size() == 1) {
            SQLExpr paramExpr = methodInvoke.getParameters().get(0);
            if (paramExpr instanceof SQLIdentifierExpr && "+".equals(((SQLIdentifierExpr) paramExpr).getName())) {
                OracleOuterExpr outerExpr = new OracleOuterExpr();
                if (methodInvoke.getOwner() == null) {
                    outerExpr.setExpr(new SQLIdentifierExpr(methodInvoke.getMethodName()));
                } else {
                    outerExpr.setExpr(new SQLPropertyExpr(methodInvoke.getOwner(), methodInvoke.getMethodName()));
                }
                return outerExpr;
            }
        }
    }
    return restExpr;
}
Also used : OracleSizeExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSizeExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) OracleIntervalExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIntervalExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLForeignKeyConstraint(com.alibaba.druid.sql.ast.statement.SQLForeignKeyConstraint) OracleConstraint(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint) SQLTimestampExpr(com.alibaba.druid.sql.ast.expr.SQLTimestampExpr) OracleOuterExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleOuterExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) OracleRangeExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleRangeExpr) OracleIntervalType(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIntervalType) OracleDbLinkExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleDbLinkExpr) OracleDatetimeExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleDatetimeExpr)

Example 62 with SQLExpr

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

the class OdpsExprParser method parseSelectItem.

@Override
public SQLSelectItem parseSelectItem() {
    SQLExpr expr;
    if (lexer.token() == Token.IDENTIFIER) {
        expr = new SQLIdentifierExpr(lexer.stringVal());
        lexer.nextTokenComma();
        if (lexer.token() != Token.COMMA) {
            expr = this.primaryRest(expr);
            expr = this.exprRest(expr);
        }
    } else {
        expr = expr();
    }
    String alias = null;
    if (lexer.token() == Token.AS) {
        lexer.nextToken();
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            OdpsUDTFSQLSelectItem selectItem = new OdpsUDTFSQLSelectItem();
            selectItem.setExpr(expr);
            for (; ; ) {
                alias = lexer.stringVal();
                lexer.nextToken();
                selectItem.getAliasList().add(alias);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            accept(Token.RPAREN);
            return selectItem;
        } else {
            alias = alias();
        }
    } else {
        alias = as();
    }
    SQLSelectItem item = new SQLSelectItem(expr, alias);
    if (lexer.hasComment() && lexer.isKeepComments()) {
        item.addAfterComment(lexer.readAndResetComments());
    }
    return item;
}
Also used : OdpsUDTFSQLSelectItem(com.alibaba.druid.sql.dialect.odps.ast.OdpsUDTFSQLSelectItem) OdpsUDTFSQLSelectItem(com.alibaba.druid.sql.dialect.odps.ast.OdpsUDTFSQLSelectItem) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 63 with SQLExpr

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

the class OdpsSelectParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    OdpsSelectQueryBlock queryBlock = new OdpsSelectQueryBlock();
    if (lexer.hasComment() && lexer.isKeepComments()) {
        queryBlock.addBeforeComment(lexer.readAndResetComments());
    }
    accept(Token.SELECT);
    if (lexer.token() == Token.HINT) {
        this.exprParser.parseHints(queryBlock.getHints());
    }
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
    }
    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);
    queryBlock.setOrderBy(this.exprParser.parseOrderBy());
    if (lexer.token() == Token.DISTRIBUTE) {
        lexer.nextToken();
        accept(Token.BY);
        this.exprParser.exprList(queryBlock.getDistributeBy(), queryBlock);
        if (identifierEquals("SORT")) {
            lexer.nextToken();
            accept(Token.BY);
            for (; ; ) {
                SQLExpr expr = this.expr();
                SQLSelectOrderByItem sortByItem = new SQLSelectOrderByItem(expr);
                if (lexer.token() == Token.ASC) {
                    sortByItem.setType(SQLOrderingSpecification.ASC);
                    lexer.nextToken();
                } else if (lexer.token() == Token.DESC) {
                    sortByItem.setType(SQLOrderingSpecification.DESC);
                    lexer.nextToken();
                }
                queryBlock.getSortBy().add(sortByItem);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                } else {
                    break;
                }
            }
        }
    }
    if (lexer.token() == Token.LIMIT) {
        lexer.nextToken();
        queryBlock.setLimit(new SQLLimit(this.expr()));
    }
    return queryRest(queryBlock);
}
Also used : SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 64 with SQLExpr

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

the class OdpsStatementParser method parseShow.

public SQLStatement parseShow() {
    accept(Token.SHOW);
    if (identifierEquals("PARTITIONS")) {
        lexer.nextToken();
        OdpsShowPartitionsStmt stmt = new OdpsShowPartitionsStmt();
        SQLExpr expr = this.exprParser.expr();
        stmt.setTableSource(new SQLExprTableSource(expr));
        return stmt;
    }
    if (identifierEquals("STATISTIC")) {
        lexer.nextToken();
        OdpsShowStatisticStmt stmt = new OdpsShowStatisticStmt();
        SQLExpr expr = this.exprParser.expr();
        stmt.setTableSource(new SQLExprTableSource(expr));
        return stmt;
    }
    if (identifierEquals("TABLES")) {
        lexer.nextToken();
        SQLShowTablesStatement stmt = new SQLShowTablesStatement();
        if (lexer.token() == Token.FROM) {
            lexer.nextToken();
            stmt.setDatabase(this.exprParser.name());
        }
        if (lexer.token() == Token.LIKE) {
            lexer.nextToken();
            stmt.setLike(this.exprParser.expr());
        }
        return stmt;
    }
    if (identifierEquals("GRANTS")) {
        lexer.nextToken();
        OdpsShowGrantsStmt stmt = new OdpsShowGrantsStmt();
        if (lexer.token() == Token.FOR) {
            lexer.nextToken();
            stmt.setUser(this.exprParser.expr());
        }
        if (lexer.token() == Token.ON) {
            lexer.nextToken();
            acceptIdentifier("type");
            stmt.setObjectType(this.exprParser.expr());
        }
        return stmt;
    }
    throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OdpsShowStatisticStmt(com.alibaba.druid.sql.dialect.odps.ast.OdpsShowStatisticStmt) SQLShowTablesStatement(com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) OdpsShowGrantsStmt(com.alibaba.druid.sql.dialect.odps.ast.OdpsShowGrantsStmt) OdpsShowPartitionsStmt(com.alibaba.druid.sql.dialect.odps.ast.OdpsShowPartitionsStmt) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 65 with SQLExpr

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

the class OdpsStatementParser method parseSet.

public SQLStatement parseSet() {
    List<String> comments = null;
    if (lexer.isKeepComments() && lexer.hasComment()) {
        comments = lexer.readAndResetComments();
    }
    accept(Token.SET);
    if (identifierEquals("LABEL")) {
        OdpsSetLabelStatement stmt = new OdpsSetLabelStatement();
        if (comments != null) {
            stmt.addBeforeComment(comments);
        }
        lexer.nextToken();
        stmt.setLabel(lexer.stringVal());
        lexer.nextToken();
        accept(Token.TO);
        if (lexer.token() == Token.USER) {
            lexer.nextToken();
            SQLName name = this.exprParser.name();
            stmt.setUser(name);
            return stmt;
        }
        accept(Token.TABLE);
        SQLExpr expr = this.exprParser.name();
        stmt.setTable(new SQLExprTableSource(expr));
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            this.exprParser.names(stmt.getColumns(), stmt);
            accept(Token.RPAREN);
        }
        return stmt;
    } else {
        SQLSetStatement stmt = new SQLSetStatement(getDbType());
        if (comments != null) {
            stmt.addBeforeComment(comments);
        }
        parseAssignItems(stmt.getItems(), stmt);
        return stmt;
    }
}
Also used : SQLSetStatement(com.alibaba.druid.sql.ast.statement.SQLSetStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) OdpsSetLabelStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsSetLabelStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)225 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)42 SQLName (com.alibaba.druid.sql.ast.SQLName)33 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)30 ParserException (com.alibaba.druid.sql.parser.ParserException)23 SQLObject (com.alibaba.druid.sql.ast.SQLObject)22 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)17 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)13 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)13 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)12 ArrayList (java.util.ArrayList)12 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)10 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)10 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)8 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)8 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)7 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)7