Search in sources :

Example 21 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr 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 22 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr 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 23 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class PGExprParser method primary.

public SQLExpr primary() {
    if (lexer.token() == Token.ARRAY) {
        SQLArrayExpr array = new SQLArrayExpr();
        array.setExpr(new SQLIdentifierExpr(lexer.stringVal()));
        lexer.nextToken();
        accept(Token.LBRACKET);
        this.exprList(array.getValues(), array);
        accept(Token.RBRACKET);
        return primaryRest(array);
    } else if (lexer.token() == Token.POUND) {
        lexer.nextToken();
        if (lexer.token() == Token.LBRACE) {
            lexer.nextToken();
            String varName = lexer.stringVal();
            lexer.nextToken();
            accept(Token.RBRACE);
            SQLVariantRefExpr expr = new SQLVariantRefExpr("#{" + varName + "}");
            return primaryRest(expr);
        } else {
            SQLExpr value = this.primary();
            SQLUnaryExpr expr = new SQLUnaryExpr(SQLUnaryOperator.Pound, value);
            return primaryRest(expr);
        }
    }
    return super.primary();
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLArrayExpr(com.alibaba.druid.sql.ast.expr.SQLArrayExpr)

Example 24 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class OracleSchemaStatVisitor method visit.

@Override
public boolean visit(OracleSelectJoin x) {
    super.visit(x);
    for (SQLExpr item : x.getUsing()) {
        if (item instanceof SQLIdentifierExpr) {
            String columnName = ((SQLIdentifierExpr) item).getName();
            String leftTable = (String) x.getLeft().getAttribute(ATTR_TABLE);
            String rightTable = (String) x.getRight().getAttribute(ATTR_TABLE);
            if (leftTable != null && rightTable != null) {
                Relationship relationship = new Relationship();
                relationship.setLeft(new Column(leftTable, columnName));
                relationship.setRight(new Column(rightTable, columnName));
                relationship.setOperator("USING");
                relationships.add(relationship);
            }
            if (leftTable != null) {
                addColumn(leftTable, columnName);
            }
            if (rightTable != null) {
                addColumn(rightTable, columnName);
            }
        }
    }
    return false;
}
Also used : ModelColumn(com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.ModelColumn) Column(com.alibaba.druid.stat.TableStat.Column) Relationship(com.alibaba.druid.stat.TableStat.Relationship) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 25 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class SQLServerStatementParser method parseSet.

public SQLStatement parseSet() {
    accept(Token.SET);
    if (identifierEquals("TRANSACTION")) {
        lexer.nextToken();
        acceptIdentifier("ISOLATION");
        acceptIdentifier("LEVEL");
        SQLServerSetTransactionIsolationLevelStatement stmt = new SQLServerSetTransactionIsolationLevelStatement();
        if (identifierEquals("READ")) {
            lexer.nextToken();
            if (identifierEquals("UNCOMMITTED")) {
                stmt.setLevel("READ UNCOMMITTED");
                lexer.nextToken();
            } else if (identifierEquals("COMMITTED")) {
                stmt.setLevel("READ COMMITTED");
                lexer.nextToken();
            } else {
                throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
            }
        } else if (identifierEquals("SERIALIZABLE")) {
            stmt.setLevel("SERIALIZABLE");
            lexer.nextToken();
        } else if (identifierEquals("SNAPSHOT")) {
            stmt.setLevel("SNAPSHOT");
            lexer.nextToken();
        } else if (identifierEquals("REPEATABLE")) {
            lexer.nextToken();
            if (identifierEquals("READ")) {
                stmt.setLevel("REPEATABLE READ");
                lexer.nextToken();
            } else {
                throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
            }
        } else {
            throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
        }
        return stmt;
    }
    if (identifierEquals("STATISTICS")) {
        lexer.nextToken();
        SQLServerSetStatement stmt = new SQLServerSetStatement();
        if (identifierEquals("IO") || identifierEquals("XML") || identifierEquals("PROFILE") || identifierEquals("TIME")) {
            stmt.getItem().setTarget(new SQLIdentifierExpr("STATISTICS " + lexer.stringVal().toUpperCase()));
            lexer.nextToken();
            if (lexer.token() == Token.ON) {
                stmt.getItem().setValue(new SQLIdentifierExpr("ON"));
                lexer.nextToken();
            } else if (identifierEquals("OFF")) {
                stmt.getItem().setValue(new SQLIdentifierExpr("OFF"));
                lexer.nextToken();
            }
        }
        return stmt;
    }
    if (lexer.token() == Token.VARIANT) {
        SQLSetStatement stmt = new SQLSetStatement(getDbType());
        parseAssignItems(stmt.getItems(), stmt);
        return stmt;
    } else {
        SQLServerSetStatement stmt = new SQLServerSetStatement();
        stmt.getItem().setTarget(this.exprParser.expr());
        if (lexer.token() == Token.ON) {
            stmt.getItem().setValue(new SQLIdentifierExpr("ON"));
            lexer.nextToken();
        } else if (identifierEquals("OFF")) {
            stmt.getItem().setValue(new SQLIdentifierExpr("OFF"));
            lexer.nextToken();
        } else {
            stmt.getItem().setValue(this.exprParser.expr());
        }
        return stmt;
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLServerSetTransactionIsolationLevelStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerSetTransactionIsolationLevelStatement) SQLServerSetStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerSetStatement) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)

Aggregations

SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)73 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)42 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)24 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)10 ParserException (com.alibaba.druid.sql.parser.ParserException)10 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)9 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)9 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)9 SQLName (com.alibaba.druid.sql.ast.SQLName)8 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)8 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)7 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)6 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)5 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)4 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)4 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)4 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)4