Search in sources :

Example 1 with SQLPropertyExpr

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

the class PagerUtils method limitDB2.

private static String limitDB2(SQLSelect select, String dbType, int offset, int count) {
    SQLSelectQuery query = select.getQuery();
    SQLBinaryOpExpr gt = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.GreaterThan, //
    new SQLNumberExpr(offset), JdbcConstants.DB2);
    SQLBinaryOpExpr lteq = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.LessThanOrEqual, //
    new SQLNumberExpr(count + offset), JdbcConstants.DB2);
    SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq, JdbcConstants.DB2);
    if (query instanceof SQLSelectQueryBlock) {
        DB2SelectQueryBlock queryBlock = (DB2SelectQueryBlock) query;
        if (offset <= 0) {
            queryBlock.setFirst(new SQLNumberExpr(count));
            return SQLUtils.toSQLString(select, dbType);
        }
        SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
        SQLOrderBy orderBy = select.getOrderBy();
        if (orderBy == null && select.getQuery() instanceof SQLSelectQueryBlock) {
            SQLSelectQueryBlock selectQueryBlcok = (SQLSelectQueryBlock) select.getQuery();
            orderBy = selectQueryBlcok.getOrderBy();
            selectQueryBlcok.setOrderBy(null);
        } else {
            select.setOrderBy(null);
        }
        aggregateExpr.setOver(new SQLOver(orderBy));
        queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
        DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
        countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
        countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
        countQueryBlock.setWhere(pageCondition);
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    DB2SelectQueryBlock countQueryBlock = new DB2SelectQueryBlock();
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
    SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
    SQLOrderBy orderBy = select.getOrderBy();
    aggregateExpr.setOver(new SQLOver(orderBy));
    select.setOrderBy(null);
    countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
    countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
    if (offset <= 0) {
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    DB2SelectQueryBlock offsetQueryBlock = new DB2SelectQueryBlock();
    offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
    offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
    offsetQueryBlock.setWhere(pageCondition);
    return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 2 with SQLPropertyExpr

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

the class MySqlShowIndexesStatement method setTable.

public void setTable(SQLName table) {
    if (table instanceof SQLPropertyExpr) {
        SQLPropertyExpr propExpr = (SQLPropertyExpr) table;
        this.setDatabase((SQLName) propExpr.getOwner());
        this.table = new SQLIdentifierExpr(propExpr.getName());
        return;
    }
    this.table = table;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)

Example 3 with SQLPropertyExpr

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

the class MySqlShowColumnsStatement method setTable.

public void setTable(SQLName table) {
    if (table instanceof SQLPropertyExpr) {
        SQLPropertyExpr propExpr = (SQLPropertyExpr) table;
        this.setDatabase((SQLName) propExpr.getOwner());
        this.table = new SQLIdentifierExpr(propExpr.getName());
        return;
    }
    this.table = table;
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)

Example 4 with SQLPropertyExpr

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

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

the class OracleSchemaStatVisitor method visit.

public boolean visit(OracleSelectTableReference x) {
    SQLExpr expr = x.getExpr();
    if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvoke = (SQLMethodInvokeExpr) expr;
        if ("TABLE".equalsIgnoreCase(methodInvoke.getMethodName()) && methodInvoke.getParameters().size() == 1) {
            expr = methodInvoke.getParameters().get(0);
        }
    }
    Map<String, String> aliasMap = getAliasMap();
    if (expr instanceof SQLName) {
        String ident;
        if (expr instanceof SQLPropertyExpr) {
            String owner = ((SQLPropertyExpr) expr).getOwner().toString();
            String name = ((SQLPropertyExpr) expr).getName();
            if (aliasMap.containsKey(owner)) {
                owner = aliasMap.get(owner);
            }
            ident = owner + "." + name;
        } else {
            ident = expr.toString();
        }
        if (containsSubQuery(ident)) {
            return false;
        }
        if ("DUAL".equalsIgnoreCase(ident)) {
            return false;
        }
        x.putAttribute(ATTR_TABLE, ident);
        TableStat stat = getTableStat(ident);
        Mode mode = getMode();
        switch(mode) {
            case Delete:
                stat.incrementDeleteCount();
                break;
            case Insert:
                stat.incrementInsertCount();
                break;
            case Update:
                stat.incrementUpdateCount();
                break;
            case Select:
                stat.incrementSelectCount();
                break;
            case Merge:
                stat.incrementMergeCount();
                break;
            default:
                break;
        }
        putAliasMap(aliasMap, x.getAlias(), ident);
        putAliasMap(aliasMap, ident, ident);
        return false;
    }
    accept(x.getExpr());
    return false;
}
Also used : SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) Mode(com.alibaba.druid.stat.TableStat.Mode) SQLName(com.alibaba.druid.sql.ast.SQLName) TableStat(com.alibaba.druid.stat.TableStat) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)27 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)24 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)17 SQLName (com.alibaba.druid.sql.ast.SQLName)6 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 TenantCallBack (com.alibaba.druid.wall.WallConfig.TenantCallBack)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)3 Column (com.alibaba.druid.stat.TableStat.Column)3 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)2 SQLOver (com.alibaba.druid.sql.ast.SQLOver)2