Search in sources :

Example 1 with SQLInSubQueryExpr

use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat_plus by coderczp.

the class BinaryOpResultHandler method dohandler.

@Override
public String dohandler(SQLStatement statement, SQLSelect sqlselect, SQLObject parent, List param) {
    SQLBinaryOpExpr pp = (SQLBinaryOpExpr) parent;
    if (pp.getLeft() instanceof SQLQueryExpr) {
        SQLQueryExpr left = (SQLQueryExpr) pp.getLeft();
        if (left.getSubQuery().equals(sqlselect)) {
            SQLExprImpl listExpr = null;
            if (null == param || param.isEmpty()) {
                listExpr = new SQLNullExpr();
            } else {
                listExpr = new SQLListExpr();
                listExpr.setParent(left.getParent());
                ((SQLListExpr) listExpr).getItems().addAll(param);
            }
            pp.setLeft(listExpr);
        }
    } else if (pp.getRight() instanceof SQLQueryExpr) {
        SQLQueryExpr right = (SQLQueryExpr) pp.getRight();
        if (right.getSubQuery().equals(sqlselect)) {
            SQLExprImpl listExpr = null;
            if (null == param || param.isEmpty()) {
                listExpr = new SQLNullExpr();
            } else {
                listExpr = new SQLListExpr();
                listExpr.setParent(right.getParent());
                ((SQLListExpr) listExpr).getItems().addAll(param);
            }
            pp.setRight(listExpr);
        }
    } else if (pp.getLeft() instanceof SQLInSubQueryExpr) {
        SQLInSubQueryExpr left = (SQLInSubQueryExpr) pp.getLeft();
        if (left.getSubQuery().equals(sqlselect)) {
            SQLExprImpl inlistExpr = null;
            if (null == param || param.isEmpty()) {
                inlistExpr = new SQLNullExpr();
            } else {
                inlistExpr = new SQLInListExpr();
                ((SQLInListExpr) inlistExpr).setTargetList(param);
                ((SQLInListExpr) inlistExpr).setExpr(pp.getRight());
                ((SQLInListExpr) inlistExpr).setNot(left.isNot());
                ((SQLInListExpr) inlistExpr).setParent(left.getParent());
            }
            pp.setLeft(inlistExpr);
        }
    } else if (pp.getRight() instanceof SQLInSubQueryExpr) {
        SQLInSubQueryExpr right = (SQLInSubQueryExpr) pp.getRight();
        if (right.getSubQuery().equals(sqlselect)) {
            SQLExprImpl listExpr = null;
            if (null == param || param.isEmpty()) {
                listExpr = new SQLNullExpr();
            } else {
                listExpr = new SQLListExpr();
                ((SQLListExpr) listExpr).getItems().addAll(param);
            }
            pp.setRight(listExpr);
        }
    }
    return statement.toString();
}
Also used : SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExprImpl(com.alibaba.druid.sql.ast.SQLExprImpl) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLListExpr(com.alibaba.druid.sql.ast.expr.SQLListExpr) SQLInSubQueryExpr(com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)

Example 2 with SQLInSubQueryExpr

use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat_plus by coderczp.

the class MycatSchemaStatVisitor method visit.

/* 
     *  遇到 all 将子查询改写成  SELECT MAX(name) FROM subtest1
     *  例如:
     *        select * from subtest where id > all (select name from subtest1);
     *    		>/>= all ----> >/>= max
     *    		</<= all ----> </<= min
     *    		<>   all ----> not in
     *          =    all ----> id = 1 and id = 2
     *          other  不改写
     */
@Override
public boolean visit(SQLAllExpr x) {
    setSubQueryRelationOrFlag(x);
    List<SQLSelectItem> itemlist = ((SQLSelectQueryBlock) (x.getSubQuery().getQuery())).getSelectList();
    SQLExpr sexpr = itemlist.get(0).getExpr();
    if (x.getParent() instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr parentExpr = (SQLBinaryOpExpr) x.getParent();
        SQLAggregateExpr saexpr = null;
        switch(parentExpr.getOperator()) {
            case GreaterThan:
            case GreaterThanOrEqual:
            case NotLessThan:
                this.hasChange = true;
                if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
                    saexpr = new SQLAggregateExpr("MAX");
                    saexpr.getArguments().add(sexpr);
                    saexpr.setParent(itemlist.get(0));
                    itemlist.get(0).setExpr(saexpr);
                }
                SQLQueryExpr maxSubQuery = new SQLQueryExpr(x.getSubQuery());
                x.getSubQuery().setParent(x.getParent());
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setLeft(maxSubQuery);
                    } else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setRight(maxSubQuery);
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(x.getSubQuery());
            case LessThan:
            case LessThanOrEqual:
            case NotGreaterThan:
                this.hasChange = true;
                if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
                    saexpr = new SQLAggregateExpr("MIN");
                    saexpr.getArguments().add(sexpr);
                    saexpr.setParent(itemlist.get(0));
                    itemlist.get(0).setExpr(saexpr);
                    x.subQuery.setParent(x.getParent());
                }
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                SQLQueryExpr minSubQuery = new SQLQueryExpr(x.getSubQuery());
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setLeft(minSubQuery);
                    } else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setRight(minSubQuery);
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(x.getSubQuery());
            case LessThanOrGreater:
            case NotEqual:
                this.hasChange = true;
                SQLInSubQueryExpr notInSubQueryExpr = new SQLInSubQueryExpr(x.getSubQuery());
                x.getSubQuery().setParent(notInSubQueryExpr);
                notInSubQueryExpr.setNot(true);
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    SQLBinaryOpExpr xp = (SQLBinaryOpExpr) x.getParent();
                    if (xp.getLeft().equals(x)) {
                        notInSubQueryExpr.setExpr(xp.getRight());
                    } else if (xp.getRight().equals(x)) {
                        notInSubQueryExpr.setExpr(xp.getLeft());
                    }
                    if (xp.getParent() instanceof MySqlSelectQueryBlock) {
                        ((MySqlSelectQueryBlock) xp.getParent()).setWhere(notInSubQueryExpr);
                    } else if (xp.getParent() instanceof SQLBinaryOpExpr) {
                        SQLBinaryOpExpr pp = ((SQLBinaryOpExpr) xp.getParent());
                        if (pp.getLeft().equals(xp)) {
                            pp.setLeft(notInSubQueryExpr);
                        } else if (pp.getRight().equals(xp)) {
                            pp.setRight(notInSubQueryExpr);
                        }
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(notInSubQueryExpr);
            default:
                break;
        }
    }
    addSubQuerys(x.getSubQuery());
    return super.visit(x);
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLInSubQueryExpr(com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 3 with SQLInSubQueryExpr

use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat_plus by coderczp.

the class GlobalTableUtil method convertUpdateSQL.

/**
 * UPDATE [LOW_PRIORITY] [IGNORE] table_reference
 *    	SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
 *    	[WHERE where_condition]
 *    	[ORDER BY ...]
 *    	[LIMIT row_count]
 *
 *		Multiple-table syntax:
 *
 *		UPDATE [LOW_PRIORITY] [IGNORE] table_references
 *    	SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
 *    	[WHERE where_condition]
 *
 *    	update user, tuser set user.name='dddd',tuser.pwd='aaa'
 *    	where user.id=2 and tuser.id=0;
 * @param sql update tuser set pwd='aaa', name='digdee' where id=0;
 * @return
 */
public static String convertUpdateSQL(String sql) {
    try {
        MySqlStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement stmt = parser.parseStatement();
        MySqlUpdateStatement update = (MySqlUpdateStatement) stmt;
        SQLTableSource ts = update.getTableSource();
        if (ts != null && ts.toString().contains(",")) {
            System.out.println(ts.toString());
            LOGGER.warn("Do not support Multiple-table udpate syntax...");
            return sql;
        }
        String tableName = StringUtil.removeBackquote(update.getTableName().getSimpleName());
        if (!isGlobalTable(tableName))
            return sql;
        if (!isInnerColExist(tableName))
            // 没有内部列
            return sql;
        StringBuilder sb = new StringBuilder(150);
        SQLExpr se = update.getWhere();
        // where中有子查询: update company set name='com' where id in (select id from xxx where ...)
        if (se instanceof SQLInSubQueryExpr) {
            // return sql;
            int idx = sql.toUpperCase().indexOf(" SET ") + 5;
            sb.append(sql.substring(0, idx)).append(GLOBAL_TABLE_MYCAT_COLUMN).append("=").append(operationTimestamp).append(",").append(sql.substring(idx));
            return sb.toString();
        }
        String where = null;
        if (update.getWhere() != null)
            where = update.getWhere().toString();
        SQLOrderBy orderBy = update.getOrderBy();
        Limit limit = update.getLimit();
        sb.append("update ").append(tableName).append(" set ");
        List<SQLUpdateSetItem> items = update.getItems();
        boolean flag = false;
        for (int i = 0; i < items.size(); i++) {
            SQLUpdateSetItem item = items.get(i);
            String col = item.getColumn().toString();
            String val = item.getValue().toString();
            if (StringUtil.removeBackquote(col).equalsIgnoreCase(GLOBAL_TABLE_MYCAT_COLUMN)) {
                flag = true;
                sb.append(col).append("=");
                if (i != items.size() - 1)
                    sb.append(operationTimestamp).append(",");
                else
                    sb.append(operationTimestamp);
            } else {
                sb.append(col).append("=");
                if (i != items.size() - 1)
                    sb.append(val).append(",");
                else
                    sb.append(val);
            }
        }
        if (!flag) {
            sb.append(",").append(GLOBAL_TABLE_MYCAT_COLUMN).append("=").append(operationTimestamp);
        }
        sb.append(" where ").append(where);
        if (orderBy != null && orderBy.getItems() != null && orderBy.getItems().size() > 0) {
            sb.append(" order by ");
            for (int i = 0; i < orderBy.getItems().size(); i++) {
                SQLSelectOrderByItem item = orderBy.getItems().get(i);
                SQLOrderingSpecification os = item.getType();
                sb.append(item.getExpr().toString());
                if (i < orderBy.getItems().size() - 1) {
                    if (os != null)
                        sb.append(" ").append(os.toString());
                    sb.append(",");
                } else {
                    if (os != null)
                        sb.append(" ").append(os.toString());
                }
            }
        }
        if (limit != null) {
            // 分为两种情况: limit 10;   limit 10,10;
            sb.append(" limit ");
            if (limit.getOffset() != null)
                sb.append(limit.getOffset().toString()).append(",");
            sb.append(limit.getRowCount().toString());
        }
        return sb.toString();
    } catch (Exception e) {
        LOGGER.warn(e.getMessage());
        return sql;
    }
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLOrderingSpecification(com.alibaba.druid.sql.ast.SQLOrderingSpecification) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLInSubQueryExpr(com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLConstraint(com.alibaba.druid.sql.ast.statement.SQLConstraint) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) SQLUpdateSetItem(com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) Limit(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Example 4 with SQLInSubQueryExpr

use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method visit.

/* 
     *  遇到 all 将子查询改写成  SELECT MAX(name) FROM subtest1
     *  例如:
     *        select * from subtest where id > all (select name from subtest1);
     *    		>/>= all ----> >/>= max
     *    		</<= all ----> </<= min
     *    		<>   all ----> not in
     *          =    all ----> id = 1 and id = 2
     *          other  不改写
     */
@Override
public boolean visit(SQLAllExpr x) {
    setSubQueryRelationOrFlag(x);
    List<SQLSelectItem> itemlist = ((SQLSelectQueryBlock) (x.getSubQuery().getQuery())).getSelectList();
    SQLExpr sexpr = itemlist.get(0).getExpr();
    if (x.getParent() instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr parentExpr = (SQLBinaryOpExpr) x.getParent();
        SQLAggregateExpr saexpr = null;
        switch(parentExpr.getOperator()) {
            case GreaterThan:
            case GreaterThanOrEqual:
            case NotLessThan:
                this.hasChange = true;
                if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
                    saexpr = new SQLAggregateExpr("MAX");
                    saexpr.getArguments().add(sexpr);
                    saexpr.setParent(itemlist.get(0));
                    itemlist.get(0).setExpr(saexpr);
                }
                SQLQueryExpr maxSubQuery = new SQLQueryExpr(x.getSubQuery());
                x.getSubQuery().setParent(x.getParent());
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setLeft(maxSubQuery);
                    } else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setRight(maxSubQuery);
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(x.getSubQuery());
            case LessThan:
            case LessThanOrEqual:
            case NotGreaterThan:
                this.hasChange = true;
                if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
                    saexpr = new SQLAggregateExpr("MIN");
                    saexpr.getArguments().add(sexpr);
                    saexpr.setParent(itemlist.get(0));
                    itemlist.get(0).setExpr(saexpr);
                    x.subQuery.setParent(x.getParent());
                }
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                SQLQueryExpr minSubQuery = new SQLQueryExpr(x.getSubQuery());
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setLeft(minSubQuery);
                    } else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setRight(minSubQuery);
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(x.getSubQuery());
            case LessThanOrGreater:
            case NotEqual:
                this.hasChange = true;
                SQLInSubQueryExpr notInSubQueryExpr = new SQLInSubQueryExpr(x.getSubQuery());
                x.getSubQuery().setParent(notInSubQueryExpr);
                notInSubQueryExpr.setNot(true);
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    SQLBinaryOpExpr xp = (SQLBinaryOpExpr) x.getParent();
                    if (xp.getLeft().equals(x)) {
                        notInSubQueryExpr.setExpr(xp.getRight());
                    } else if (xp.getRight().equals(x)) {
                        notInSubQueryExpr.setExpr(xp.getLeft());
                    }
                    if (xp.getParent() instanceof MySqlSelectQueryBlock) {
                        ((MySqlSelectQueryBlock) xp.getParent()).setWhere(notInSubQueryExpr);
                    } else if (xp.getParent() instanceof SQLBinaryOpExpr) {
                        SQLBinaryOpExpr pp = ((SQLBinaryOpExpr) xp.getParent());
                        if (pp.getLeft().equals(xp)) {
                            pp.setLeft(notInSubQueryExpr);
                        } else if (pp.getRight().equals(xp)) {
                            pp.setRight(notInSubQueryExpr);
                        }
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(notInSubQueryExpr);
            default:
                break;
        }
    }
    addSubQuerys(x.getSubQuery());
    return super.visit(x);
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLInSubQueryExpr(com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 5 with SQLInSubQueryExpr

use of com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method visit.

/* 
     *  遇到 some 将子查询改写成  SELECT MIN(name) FROM subtest1
     *  例如:
     *        select * from subtest where id > some (select name from subtest1);
     *    >/>= some ----> >/>= min
     *    </<= some ----> </<= max
     *    <>   some ----> not in
     *    =    some ----> in
     *    other  不改写
     */
@Override
public boolean visit(SQLSomeExpr x) {
    setSubQueryRelationOrFlag(x);
    List<SQLSelectItem> itemlist = ((SQLSelectQueryBlock) (x.getSubQuery().getQuery())).getSelectList();
    SQLExpr sexpr = itemlist.get(0).getExpr();
    if (x.getParent() instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr parentExpr = (SQLBinaryOpExpr) x.getParent();
        SQLAggregateExpr saexpr = null;
        switch(parentExpr.getOperator()) {
            case GreaterThan:
            case GreaterThanOrEqual:
            case NotLessThan:
                this.hasChange = true;
                if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
                    saexpr = new SQLAggregateExpr("MIN");
                    saexpr.getArguments().add(sexpr);
                    saexpr.setParent(itemlist.get(0));
                    itemlist.get(0).setExpr(saexpr);
                }
                SQLQueryExpr maxSubQuery = new SQLQueryExpr(x.getSubQuery());
                x.getSubQuery().setParent(maxSubQuery);
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setLeft(maxSubQuery);
                    } else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setRight(maxSubQuery);
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(x.getSubQuery());
            case LessThan:
            case LessThanOrEqual:
            case NotGreaterThan:
                this.hasChange = true;
                if (sexpr instanceof SQLIdentifierExpr || (sexpr instanceof SQLPropertyExpr && ((SQLPropertyExpr) sexpr).getOwner() instanceof SQLIdentifierExpr)) {
                    saexpr = new SQLAggregateExpr("MAX");
                    saexpr.getArguments().add(sexpr);
                    saexpr.setParent(itemlist.get(0));
                    itemlist.get(0).setExpr(saexpr);
                }
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                SQLQueryExpr minSubQuery = new SQLQueryExpr(x.getSubQuery());
                x.getSubQuery().setParent(minSubQuery);
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    if (((SQLBinaryOpExpr) x.getParent()).getLeft().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setLeft(minSubQuery);
                    } else if (((SQLBinaryOpExpr) x.getParent()).getRight().equals(x)) {
                        ((SQLBinaryOpExpr) x.getParent()).setRight(minSubQuery);
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(x.getSubQuery());
            case LessThanOrGreater:
            case NotEqual:
                this.hasChange = true;
                SQLInSubQueryExpr notInSubQueryExpr = new SQLInSubQueryExpr(x.getSubQuery());
                x.getSubQuery().setParent(notInSubQueryExpr);
                notInSubQueryExpr.setNot(true);
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    SQLBinaryOpExpr xp = (SQLBinaryOpExpr) x.getParent();
                    if (xp.getLeft().equals(x)) {
                        notInSubQueryExpr.setExpr(xp.getRight());
                    } else if (xp.getRight().equals(x)) {
                        notInSubQueryExpr.setExpr(xp.getLeft());
                    }
                    if (xp.getParent() instanceof MySqlSelectQueryBlock) {
                        ((MySqlSelectQueryBlock) xp.getParent()).setWhere(notInSubQueryExpr);
                    } else if (xp.getParent() instanceof SQLBinaryOpExpr) {
                        SQLBinaryOpExpr pp = ((SQLBinaryOpExpr) xp.getParent());
                        if (pp.getLeft().equals(xp)) {
                            pp.setLeft(notInSubQueryExpr);
                        } else if (pp.getRight().equals(xp)) {
                            pp.setRight(notInSubQueryExpr);
                        }
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(notInSubQueryExpr);
            case Equality:
                this.hasChange = true;
                SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr(x.getSubQuery());
                x.getSubQuery().setParent(inSubQueryExpr);
                inSubQueryExpr.setNot(false);
                // 生成新的SQLQueryExpr 替换当前 SQLAllExpr 节点
                if (x.getParent() instanceof SQLBinaryOpExpr) {
                    SQLBinaryOpExpr xp = (SQLBinaryOpExpr) x.getParent();
                    if (xp.getLeft().equals(x)) {
                        inSubQueryExpr.setExpr(xp.getRight());
                    } else if (xp.getRight().equals(x)) {
                        inSubQueryExpr.setExpr(xp.getLeft());
                    }
                    if (xp.getParent() instanceof MySqlSelectQueryBlock) {
                        ((MySqlSelectQueryBlock) xp.getParent()).setWhere(inSubQueryExpr);
                    } else if (xp.getParent() instanceof SQLBinaryOpExpr) {
                        SQLBinaryOpExpr pp = ((SQLBinaryOpExpr) xp.getParent());
                        if (pp.getLeft().equals(xp)) {
                            pp.setLeft(inSubQueryExpr);
                        } else if (pp.getRight().equals(xp)) {
                            pp.setRight(inSubQueryExpr);
                        }
                    }
                }
                addSubQuerys(x.getSubQuery());
                return super.visit(inSubQueryExpr);
            default:
                break;
        }
    }
    addSubQuerys(x.getSubQuery());
    return super.visit(x);
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLInSubQueryExpr(com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)17 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)11 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)9 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)9 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)7 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)7 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)6 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)6 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)6 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)6 SQLObject (com.alibaba.druid.sql.ast.SQLObject)3 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)3 SQLExprImpl (com.alibaba.druid.sql.ast.SQLExprImpl)2 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)2 SQLOrderingSpecification (com.alibaba.druid.sql.ast.SQLOrderingSpecification)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLExistsExpr (com.alibaba.druid.sql.ast.expr.SQLExistsExpr)2 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)2 SQLListExpr (com.alibaba.druid.sql.ast.expr.SQLListExpr)2 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)2