Search in sources :

Example 1 with SQLSubqueryTableSource

use of com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource 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 SQLSubqueryTableSource

use of com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource in project Mycat_plus by coderczp.

the class DruidSelectOracleParser method parseThreeLevelPageSql.

private void parseThreeLevelPageSql(SQLStatement stmt, RouteResultset rrs, SchemaConfig schema, SQLSubqueryTableSource from, SQLBinaryOpExpr one, SQLBinaryOperator operator) {
    SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
    int firstrownum = right.getNumber().intValue();
    if (operator == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
        firstrownum = firstrownum - 1;
    }
    SQLSelectQuery subSelect = from.getSelect().getQuery();
    if (subSelect instanceof OracleSelectQueryBlock) {
        // 第二层子查询
        OracleSelectQueryBlock twoSubSelect = (OracleSelectQueryBlock) subSelect;
        if (twoSubSelect.getWhere() instanceof SQLBinaryOpExpr && twoSubSelect.getFrom() instanceof SQLSubqueryTableSource) {
            SQLBinaryOpExpr twoWhere = (SQLBinaryOpExpr) twoSubSelect.getWhere();
            boolean isRowNum = "rownum".equalsIgnoreCase(twoWhere.getLeft().toString());
            boolean isLess = twoWhere.getOperator() == SQLBinaryOperator.LessThanOrEqual || twoWhere.getOperator() == SQLBinaryOperator.LessThan;
            if (isRowNum && twoWhere.getRight() instanceof SQLIntegerExpr && isLess) {
                int lastrownum = ((SQLIntegerExpr) twoWhere.getRight()).getNumber().intValue();
                if (operator == SQLBinaryOperator.LessThan && lastrownum != 0) {
                    lastrownum = lastrownum - 1;
                }
                SQLSelectQuery finalQuery = ((SQLSubqueryTableSource) twoSubSelect.getFrom()).getSelect().getQuery();
                if (finalQuery instanceof OracleSelectQueryBlock) {
                    setLimitIFChange(stmt, rrs, schema, one, firstrownum, lastrownum);
                    parseOrderAggGroupOracle(stmt, rrs, (OracleSelectQueryBlock) finalQuery, schema);
                    isNeedParseOrderAgg = false;
                }
            }
        }
    }
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 3 with SQLSubqueryTableSource

use of com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource in project Mycat-Server by MyCATApache.

the class DruidSelectOracleParser method parseThreeLevelPageSql.

private void parseThreeLevelPageSql(SQLStatement stmt, RouteResultset rrs, SchemaConfig schema, SQLSubqueryTableSource from, SQLBinaryOpExpr one, SQLBinaryOperator operator) {
    SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
    int firstrownum = right.getNumber().intValue();
    if (operator == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
        firstrownum = firstrownum - 1;
    }
    SQLSelectQuery subSelect = from.getSelect().getQuery();
    if (subSelect instanceof OracleSelectQueryBlock) {
        // 第二层子查询
        OracleSelectQueryBlock twoSubSelect = (OracleSelectQueryBlock) subSelect;
        if (twoSubSelect.getWhere() instanceof SQLBinaryOpExpr && twoSubSelect.getFrom() instanceof SQLSubqueryTableSource) {
            SQLBinaryOpExpr twoWhere = (SQLBinaryOpExpr) twoSubSelect.getWhere();
            boolean isRowNum = "rownum".equalsIgnoreCase(twoWhere.getLeft().toString());
            boolean isLess = twoWhere.getOperator() == SQLBinaryOperator.LessThanOrEqual || twoWhere.getOperator() == SQLBinaryOperator.LessThan;
            if (isRowNum && twoWhere.getRight() instanceof SQLIntegerExpr && isLess) {
                int lastrownum = ((SQLIntegerExpr) twoWhere.getRight()).getNumber().intValue();
                if (operator == SQLBinaryOperator.LessThan && lastrownum != 0) {
                    lastrownum = lastrownum - 1;
                }
                SQLSelectQuery finalQuery = ((SQLSubqueryTableSource) twoSubSelect.getFrom()).getSelect().getQuery();
                if (finalQuery instanceof OracleSelectQueryBlock) {
                    setLimitIFChange(stmt, rrs, schema, one, firstrownum, lastrownum);
                    parseOrderAggGroupOracle(stmt, rrs, (OracleSelectQueryBlock) finalQuery, schema);
                    isNeedParseOrderAgg = false;
                }
            }
        }
    }
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 4 with SQLSubqueryTableSource

use of com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource in project druid by alibaba.

the class SQLTransformUtils method transformOracleToPostgresql.

public static SQLTableSource transformOracleToPostgresql(SQLTableSource x) {
    if (x instanceof OracleSelectTableReference) {
        OracleSelectTableReference xx = (OracleSelectTableReference) x;
        SQLExprTableSource y = new SQLExprTableSource();
        xx.cloneTo(y);
        y.setParent(x.getParent());
        return y;
    }
    if (x instanceof OracleSelectJoin) {
        OracleSelectJoin xx = (OracleSelectJoin) x;
        SQLJoinTableSource y = new SQLJoinTableSource();
        xx.cloneTo(y);
        y.setLeft(transformOracleToPostgresql(y.getLeft()));
        y.setRight(transformOracleToPostgresql(y.getRight()));
        y.setParent(x.getParent());
        return y;
    }
    if (x instanceof OracleSelectSubqueryTableSource) {
        OracleSelectSubqueryTableSource xx = (OracleSelectSubqueryTableSource) x;
        SQLSubqueryTableSource y = new SQLSubqueryTableSource();
        xx.cloneTo(y);
        y.setParent(x.getParent());
        return y;
    }
    if (x instanceof OracleWithSubqueryEntry) {
        SQLWithSubqueryClause.Entry entry = new SQLWithSubqueryClause.Entry();
        ((OracleWithSubqueryEntry) x).cloneTo(entry);
        entry.setParent(x.getParent());
        return entry;
    }
    return x;
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) OracleWithSubqueryEntry(com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry) SQLWithSubqueryClause(com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause) OracleSelectTableReference(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectTableReference) OracleSelectJoin(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectJoin) OracleSelectSubqueryTableSource(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectSubqueryTableSource) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLJoinTableSource(com.alibaba.druid.sql.ast.statement.SQLJoinTableSource) OracleWithSubqueryEntry(com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry)

Example 5 with SQLSubqueryTableSource

use of com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource in project druid by alibaba.

the class NameResolveVisitor method isAliasColumn.

/**
 * 是否是 select item 字段的别名
 *
 * @param x      x 是否是 select item 字段的别名
 * @param source 从 source 数据中查找 and 判断
 * @return true:是、false:不是
 */
public boolean isAliasColumn(SQLExpr x, SQLSelectQueryBlock source) {
    if (x instanceof SQLIdentifierExpr) {
        SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) x;
        long nameHashCode64 = identifierExpr.nameHashCode64();
        SQLSelectQueryBlock queryBlock = source;
        SQLSelectItem selectItem = queryBlock.findSelectItem(nameHashCode64);
        if (selectItem != null) {
            return true;
        }
        if (queryBlock.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
            SQLSelectQueryBlock subQueryBlock = ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQueryBlock();
            if (isAliasColumn(x, subQueryBlock)) {
                return true;
            }
        }
    }
    return false;
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

Aggregations

SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)19 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)12 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)12 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)12 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)9 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)8 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)8 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)8 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)8 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)7 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)7 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)7 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)6 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)5 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)4 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 OracleSelect (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect)4 List (java.util.List)4 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3