Search in sources :

Example 26 with ParserException

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

the class OracleExprParser method primary.

public SQLExpr primary() {
    final Token tok = lexer.token();
    SQLExpr sqlExpr = null;
    switch(tok) {
        case SYSDATE:
            lexer.nextToken();
            OracleSysdateExpr sysdate = new OracleSysdateExpr();
            if (lexer.token() == Token.MONKEYS_AT) {
                lexer.nextToken();
                accept(Token.BANG);
                sysdate.setOption("!");
            }
            sqlExpr = sysdate;
            return primaryRest(sqlExpr);
        case PRIOR:
            lexer.nextToken();
            sqlExpr = expr();
            sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Prior, sqlExpr);
            return primaryRest(sqlExpr);
        case COLON:
            lexer.nextToken();
            if (lexer.token() == Token.LITERAL_INT) {
                String name = ":" + lexer.numberString();
                lexer.nextToken();
                return new SQLVariantRefExpr(name);
            } else if (lexer.token() == Token.IDENTIFIER) {
                String name = lexer.stringVal();
                if (name.charAt(0) == 'B' || name.charAt(0) == 'b') {
                    lexer.nextToken();
                    return new SQLVariantRefExpr(":" + name);
                }
                throw new ParserException("syntax error : " + lexer.token() + " " + lexer.stringVal());
            } else {
                throw new ParserException("syntax error : " + lexer.token());
            }
        case LITERAL_ALIAS:
            String alias = '"' + lexer.stringVal() + '"';
            lexer.nextToken();
            return primaryRest(new SQLIdentifierExpr(alias));
        case BINARY_FLOAT:
            OracleBinaryFloatExpr floatExpr = new OracleBinaryFloatExpr();
            floatExpr.setValue(Float.parseFloat(lexer.numberString()));
            lexer.nextToken();
            return primaryRest(floatExpr);
        case BINARY_DOUBLE:
            OracleBinaryDoubleExpr doubleExpr = new OracleBinaryDoubleExpr();
            doubleExpr.setValue(Double.parseDouble(lexer.numberString()));
            lexer.nextToken();
            return primaryRest(doubleExpr);
        case TABLE:
            lexer.nextToken();
            return primaryRest(new SQLIdentifierExpr("TABLE"));
        case PLUS:
            lexer.nextToken();
            switch(lexer.token()) {
                case LITERAL_INT:
                    sqlExpr = new SQLIntegerExpr(lexer.integerValue());
                    lexer.nextToken();
                    break;
                case LITERAL_FLOAT:
                    sqlExpr = new SQLNumberExpr(lexer.decimalValue());
                    lexer.nextToken();
                    break;
                case BINARY_FLOAT:
                    sqlExpr = new OracleBinaryFloatExpr(Float.parseFloat(lexer.numberString()));
                    lexer.nextToken();
                    break;
                case BINARY_DOUBLE:
                    sqlExpr = new OracleBinaryDoubleExpr(Double.parseDouble(lexer.numberString()));
                    lexer.nextToken();
                    break;
                case LPAREN:
                    lexer.nextToken();
                    sqlExpr = expr();
                    accept(Token.RPAREN);
                    sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Plus, sqlExpr);
                    break;
                default:
                    throw new ParserException("TODO");
            }
            return primaryRest(sqlExpr);
        case SUB:
            lexer.nextToken();
            switch(lexer.token()) {
                case LITERAL_INT:
                    Number integerValue = lexer.integerValue();
                    if (integerValue instanceof Integer) {
                        int intVal = ((Integer) integerValue).intValue();
                        if (intVal == Integer.MIN_VALUE) {
                            integerValue = Long.valueOf(((long) intVal) * -1);
                        } else {
                            integerValue = Integer.valueOf(intVal * -1);
                        }
                    } else if (integerValue instanceof Long) {
                        long longVal = ((Long) integerValue).longValue();
                        if (longVal == 2147483648L) {
                            integerValue = Integer.valueOf((int) (((long) longVal) * -1));
                        } else {
                            integerValue = Long.valueOf(longVal * -1);
                        }
                    } else {
                        integerValue = ((BigInteger) integerValue).negate();
                    }
                    sqlExpr = new SQLIntegerExpr(integerValue);
                    lexer.nextToken();
                    break;
                case LITERAL_FLOAT:
                    sqlExpr = new SQLNumberExpr(lexer.decimalValue().negate());
                    lexer.nextToken();
                    break;
                case BINARY_FLOAT:
                    sqlExpr = new OracleBinaryFloatExpr(Float.parseFloat(lexer.numberString()) * -1);
                    lexer.nextToken();
                    break;
                case BINARY_DOUBLE:
                    sqlExpr = new OracleBinaryDoubleExpr(Double.parseDouble(lexer.numberString()) * -1);
                    lexer.nextToken();
                    break;
                case VARIANT:
                case IDENTIFIER:
                    sqlExpr = expr();
                    sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
                    break;
                case LPAREN:
                    lexer.nextToken();
                    sqlExpr = expr();
                    accept(Token.RPAREN);
                    sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
                    break;
                default:
                    throw new ParserException("TODO " + lexer.token());
            }
            return primaryRest(sqlExpr);
        case CURSOR:
            lexer.nextToken();
            accept(Token.LPAREN);
            OracleSelect select = createSelectParser().select();
            OracleCursorExpr cursorExpr = new OracleCursorExpr(select);
            accept(Token.RPAREN);
            sqlExpr = cursorExpr;
            return primaryRest(sqlExpr);
        case MODEL:
        case PCTFREE:
        case INITRANS:
        case MAXTRANS:
        case SEGMENT:
        case CREATION:
        case IMMEDIATE:
        case DEFERRED:
        case STORAGE:
        case NEXT:
        case MINEXTENTS:
        case MAXEXTENTS:
        case MAXSIZE:
        case PCTINCREASE:
        case FLASH_CACHE:
        case CELL_FLASH_CACHE:
        case KEEP:
        case NONE:
        case LOB:
        case STORE:
        case ROW:
        case CHUNK:
        case CACHE:
        case NOCACHE:
        case LOGGING:
        case NOCOMPRESS:
        case KEEP_DUPLICATES:
        case EXCEPTIONS:
        case PURGE:
            sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
            lexer.nextToken();
            return primaryRest(sqlExpr);
        default:
            return super.primary();
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) OracleSysdateExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSysdateExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) OracleBinaryFloatExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleBinaryFloatExpr) Token(com.alibaba.druid.sql.parser.Token) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) 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) BigInteger(java.math.BigInteger) OracleCursorExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleCursorExpr) OracleSelect(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) BigInteger(java.math.BigInteger) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) OracleBinaryDoubleExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleBinaryDoubleExpr)

Example 27 with ParserException

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

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

the class OracleExprParser method name.

public SQLName name() {
    SQLName name = super.name();
    if (lexer.token() == Token.MONKEYS_AT) {
        lexer.nextToken();
        if (lexer.token() != Token.IDENTIFIER) {
            throw new ParserException("syntax error, expect identifier, but " + lexer.token());
        }
        OracleDbLinkExpr dbLink = new OracleDbLinkExpr();
        dbLink.setExpr(name);
        dbLink.setDbLink(lexer.stringVal());
        lexer.nextToken();
        return dbLink;
    }
    return name;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) OracleDbLinkExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleDbLinkExpr)

Example 29 with ParserException

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

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

the class OracleExprParser method parseConstaint.

public OracleConstraint parseConstaint() {
    OracleConstraint constraint = (OracleConstraint) super.parseConstaint();
    for (; ; ) {
        if (lexer.token() == Token.EXCEPTIONS) {
            lexer.nextToken();
            accept(Token.INTO);
            SQLName exceptionsInto = this.name();
            constraint.setExceptionsInto(exceptionsInto);
            continue;
        }
        if (lexer.token() == Token.DISABLE) {
            lexer.nextToken();
            constraint.setEnable(false);
            continue;
        }
        if (lexer.token() == Token.ENABLE) {
            lexer.nextToken();
            constraint.setEnable(true);
            continue;
        }
        if (lexer.token() == Token.INITIALLY) {
            lexer.nextToken();
            if (lexer.token() == Token.IMMEDIATE) {
                lexer.nextToken();
                constraint.setInitially(Initially.IMMEDIATE);
            } else {
                accept(Token.DEFERRED);
                constraint.setInitially(Initially.DEFERRED);
            }
            continue;
        }
        if (lexer.token() == Token.NOT) {
            lexer.nextToken();
            if (identifierEquals("DEFERRABLE")) {
                lexer.nextToken();
                constraint.setDeferrable(false);
                continue;
            }
            throw new ParserException("TODO " + lexer.token());
        }
        if (identifierEquals("DEFERRABLE")) {
            lexer.nextToken();
            constraint.setDeferrable(true);
            continue;
        }
        break;
    }
    return constraint;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) OracleConstraint(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint)

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