Search in sources :

Example 1 with OracleIntervalType

use of com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIntervalType in project druid by alibaba.

the class OracleExprParser method parseInterval.

protected SQLExpr parseInterval() {
    accept(Token.INTERVAL);
    OracleIntervalExpr interval = new OracleIntervalExpr();
    if (lexer.token() != Token.LITERAL_CHARS) {
        return new SQLIdentifierExpr("INTERVAL");
    }
    interval.setValue(new SQLCharExpr(lexer.stringVal()));
    lexer.nextToken();
    OracleIntervalType type = OracleIntervalType.valueOf(lexer.stringVal());
    interval.setType(type);
    lexer.nextToken();
    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();
        if (lexer.token() == Token.COMMA) {
            lexer.nextToken();
            if (lexer.token() != Token.LITERAL_INT) {
                throw new ParserException("syntax error");
            }
            interval.setFactionalSecondsPrecision(lexer.integerValue().intValue());
            lexer.nextToken();
        }
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.TO) {
        lexer.nextToken();
        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.setToFactionalSecondsPrecision(lexer.integerValue().intValue());
                lexer.nextToken();
                accept(Token.RPAREN);
            }
        } else {
            interval.setToType(OracleIntervalType.MONTH);
            lexer.nextToken();
        }
    }
    return interval;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) OracleIntervalExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIntervalExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) OracleIntervalType(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIntervalType)

Example 2 with OracleIntervalType

use of com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIntervalType 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)

Aggregations

SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 OracleIntervalExpr (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIntervalExpr)2 OracleIntervalType (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleIntervalType)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1 SQLTimestampExpr (com.alibaba.druid.sql.ast.expr.SQLTimestampExpr)1 SQLForeignKeyConstraint (com.alibaba.druid.sql.ast.statement.SQLForeignKeyConstraint)1 OracleDatetimeExpr (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleDatetimeExpr)1 OracleDbLinkExpr (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleDbLinkExpr)1 OracleOuterExpr (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleOuterExpr)1 OracleRangeExpr (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleRangeExpr)1 OracleSizeExpr (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSizeExpr)1 OracleConstraint (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint)1