Search in sources :

Example 1 with SQLTableSource

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

the class JoinParser method parser.

public void parser() {
    masterTable = "";
    SQLTableSource table = mysqlQuery.getFrom();
    parserTable(table, tableFilter, false);
    parserFields(mysqlQuery.getSelectList());
    parserMasterTable();
    parserWhere(mysqlQuery.getWhere(), "");
    // getJoinField();
    parserOrderBy(mysqlQuery.getOrderBy());
    parserLimit();
    // LOGGER.info("field "+fieldAliasMap);	  	   
    // LOGGER.info("master "+masterTable);
    //  LOGGER.info("join Lkey "+getJoinLkey()); 
    //  LOGGER.info("join Rkey "+getJoinRkey()); 	   
    LOGGER.info("SQL: " + this.stmt);
}
Also used : SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource)

Example 2 with SQLTableSource

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

the class DruidMycatRouteStrategy method routeDisTable.

private RouteResultset routeDisTable(SQLStatement statement, RouteResultset rrs) throws SQLSyntaxErrorException {
    SQLTableSource tableSource = null;
    if (statement instanceof SQLInsertStatement) {
        SQLInsertStatement insertStatement = (SQLInsertStatement) statement;
        tableSource = insertStatement.getTableSource();
        for (RouteResultsetNode node : rrs.getNodes()) {
            SQLExprTableSource from2 = getDisTable(tableSource, node);
            insertStatement.setTableSource(from2);
            node.setStatement(insertStatement.toString());
        }
    }
    if (statement instanceof SQLDeleteStatement) {
        SQLDeleteStatement deleteStatement = (SQLDeleteStatement) statement;
        tableSource = deleteStatement.getTableSource();
        for (RouteResultsetNode node : rrs.getNodes()) {
            SQLExprTableSource from2 = getDisTable(tableSource, node);
            deleteStatement.setTableSource(from2);
            node.setStatement(deleteStatement.toString());
        }
    }
    if (statement instanceof SQLUpdateStatement) {
        SQLUpdateStatement updateStatement = (SQLUpdateStatement) statement;
        tableSource = updateStatement.getTableSource();
        for (RouteResultsetNode node : rrs.getNodes()) {
            SQLExprTableSource from2 = getDisTable(tableSource, node);
            updateStatement.setTableSource(from2);
            node.setStatement(updateStatement.toString());
        }
    }
    return rrs;
}
Also used : SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) RouteResultsetNode(io.mycat.route.RouteResultsetNode) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource)

Example 3 with SQLTableSource

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

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 SQLTableSource

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

the class MySqlCreateTableParser method parseCrateTable.

public MySqlCreateTableStatement parseCrateTable(boolean acceptCreate) {
    if (acceptCreate) {
        accept(Token.CREATE);
    }
    MySqlCreateTableStatement stmt = new MySqlCreateTableStatement();
    if (identifierEquals("TEMPORARY")) {
        lexer.nextToken();
        stmt.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
    }
    accept(Token.TABLE);
    if (lexer.token() == Token.IF || identifierEquals("IF")) {
        lexer.nextToken();
        accept(Token.NOT);
        accept(Token.EXISTS);
        stmt.setIfNotExiists(true);
    }
    stmt.setName(this.exprParser.name());
    if (lexer.token() == Token.LIKE) {
        lexer.nextToken();
        SQLName name = this.exprParser.name();
        stmt.setLike(name);
    }
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        if (lexer.token() == Token.LIKE) {
            lexer.nextToken();
            SQLName name = this.exprParser.name();
            stmt.setLike(name);
        } else {
            for (; ; ) {
                if (//
                lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_CHARS) {
                    SQLColumnDefinition column = this.exprParser.parseColumn();
                    stmt.getTableElementList().add(column);
                } else if (//
                lexer.token() == Token.CONSTRAINT || //
                lexer.token() == Token.PRIMARY || lexer.token() == Token.UNIQUE) {
                    SQLTableConstraint constraint = this.parseConstraint();
                    stmt.getTableElementList().add(constraint);
                } else if (lexer.token() == (Token.INDEX)) {
                    lexer.nextToken();
                    MySqlTableIndex idx = new MySqlTableIndex();
                    if (lexer.token() == Token.IDENTIFIER) {
                        if (!"USING".equalsIgnoreCase(lexer.stringVal())) {
                            idx.setName(this.exprParser.name());
                        }
                    }
                    if (identifierEquals("USING")) {
                        lexer.nextToken();
                        idx.setIndexType(lexer.stringVal());
                        lexer.nextToken();
                    }
                    accept(Token.LPAREN);
                    for (; ; ) {
                        idx.addColumn(this.exprParser.expr());
                        if (!(lexer.token() == (Token.COMMA))) {
                            break;
                        } else {
                            lexer.nextToken();
                        }
                    }
                    accept(Token.RPAREN);
                    if (identifierEquals("USING")) {
                        lexer.nextToken();
                        idx.setIndexType(lexer.stringVal());
                        lexer.nextToken();
                    }
                    stmt.getTableElementList().add(idx);
                } else if (lexer.token() == (Token.KEY)) {
                    stmt.getTableElementList().add(parseConstraint());
                } else if (lexer.token() == (Token.PRIMARY)) {
                    SQLTableConstraint pk = parseConstraint();
                    pk.setParent(stmt);
                    stmt.getTableElementList().add(pk);
                } else if (lexer.token() == (Token.FOREIGN)) {
                    SQLForeignKeyConstraint fk = this.getExprParser().parseForeignKey();
                    fk.setParent(stmt);
                    stmt.getTableElementList().add(fk);
                } else if (lexer.token() == Token.CHECK) {
                    SQLCheck check = this.exprParser.parseCheck();
                    stmt.getTableElementList().add(check);
                } else {
                    SQLColumnDefinition column = this.exprParser.parseColumn();
                    stmt.getTableElementList().add(column);
                }
                if (!(lexer.token() == (Token.COMMA))) {
                    break;
                } else {
                    lexer.nextToken();
                }
            }
        }
        accept(Token.RPAREN);
    }
    for (; ; ) {
        if (identifierEquals("ENGINE")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("ENGINE", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("AUTO_INCREMENT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("AUTO_INCREMENT", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("AVG_ROW_LENGTH")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("AVG_ROW_LENGTH", this.exprParser.expr());
            continue;
        }
        if (lexer.token() == Token.DEFAULT) {
            lexer.nextToken();
            parseTableOptionCharsetOrCollate(stmt);
            continue;
        }
        if (parseTableOptionCharsetOrCollate(stmt)) {
            continue;
        }
        if (identifierEquals("CHECKSUM")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("CHECKSUM", this.exprParser.expr());
            continue;
        }
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("COMMENT", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("CONNECTION")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("CONNECTION", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("DATA")) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("DATA DIRECTORY", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("DELAY_KEY_WRITE")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("DELAY_KEY_WRITE", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("INDEX")) {
            lexer.nextToken();
            acceptIdentifier("DIRECTORY");
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("INDEX DIRECTORY", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("INSERT_METHOD")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("INSERT_METHOD", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("KEY_BLOCK_SIZE")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("KEY_BLOCK_SIZE", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("MAX_ROWS")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("MAX_ROWS", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("MIN_ROWS")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("MIN_ROWS", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("PACK_KEYS")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("PACK_KEYS", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("PASSWORD")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("PASSWORD", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("ROW_FORMAT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("ROW_FORMAT", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("STATS_AUTO_RECALC")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("STATS_AUTO_RECALC", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("STATS_PERSISTENT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("STATS_PERSISTENT", this.exprParser.expr());
            continue;
        }
        if (identifierEquals("STATS_SAMPLE_PAGES")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            stmt.getTableOptions().put("STATS_SAMPLE_PAGES", this.exprParser.expr());
            continue;
        }
        if (lexer.token() == Token.UNION) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            accept(Token.LPAREN);
            SQLTableSource tableSrc = this.createSQLSelectParser().parseTableSource();
            stmt.getTableOptions().put("UNION", tableSrc);
            accept(Token.RPAREN);
            continue;
        }
        if (lexer.token() == Token.TABLESPACE) {
            lexer.nextToken();
            TableSpaceOption option = new TableSpaceOption();
            option.setName(this.exprParser.name());
            if (identifierEquals("STORAGE")) {
                lexer.nextToken();
                option.setStorage(this.exprParser.name());
            }
            stmt.getTableOptions().put("TABLESPACE", option);
            continue;
        }
        if (identifierEquals("TABLEGROUP")) {
            lexer.nextToken();
            SQLName tableGroup = this.exprParser.name();
            stmt.setTableGroup(tableGroup);
            continue;
        }
        if (identifierEquals("TYPE")) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.getTableOptions().put("TYPE", this.exprParser.expr());
            lexer.nextToken();
            continue;
        }
        if (lexer.token() == Token.PARTITION) {
            lexer.nextToken();
            accept(Token.BY);
            SQLPartitionBy partitionClause;
            boolean linera = false;
            if (identifierEquals("LINEAR")) {
                lexer.nextToken();
                linera = true;
            }
            if (lexer.token() == Token.KEY) {
                MySqlPartitionByKey clause = new MySqlPartitionByKey();
                lexer.nextToken();
                if (linera) {
                    clause.setLinear(true);
                }
                accept(Token.LPAREN);
                if (lexer.token() != Token.RPAREN) {
                    for (; ; ) {
                        clause.addColumn(this.exprParser.name());
                        if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                        break;
                    }
                }
                accept(Token.RPAREN);
                partitionClause = clause;
                partitionClauseRest(clause);
            } else if (identifierEquals("HASH")) {
                lexer.nextToken();
                SQLPartitionByHash clause = new SQLPartitionByHash();
                if (linera) {
                    clause.setLinear(true);
                }
                if (lexer.token() == Token.KEY) {
                    lexer.nextToken();
                    clause.setKey(true);
                }
                accept(Token.LPAREN);
                clause.setExpr(this.exprParser.expr());
                accept(Token.RPAREN);
                partitionClause = clause;
                partitionClauseRest(clause);
            } else if (identifierEquals("RANGE")) {
                SQLPartitionByRange clause = partitionByRange();
                partitionClause = clause;
                partitionClauseRest(clause);
            } else if (identifierEquals("LIST")) {
                lexer.nextToken();
                SQLPartitionByList clause = new SQLPartitionByList();
                if (lexer.token() == Token.LPAREN) {
                    lexer.nextToken();
                    clause.setExpr(this.exprParser.expr());
                    accept(Token.RPAREN);
                } else {
                    acceptIdentifier("COLUMNS");
                    accept(Token.LPAREN);
                    for (; ; ) {
                        clause.addColumn(this.exprParser.name());
                        if (lexer.token() == Token.COMMA) {
                            lexer.nextToken();
                            continue;
                        }
                        break;
                    }
                    accept(Token.RPAREN);
                }
                partitionClause = clause;
                partitionClauseRest(clause);
            } else {
                throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
            }
            if (lexer.token() == Token.LPAREN) {
                lexer.nextToken();
                for (; ; ) {
                    SQLPartition partitionDef = this.getExprParser().parsePartition();
                    partitionClause.addPartition(partitionDef);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    } else {
                        break;
                    }
                }
                accept(Token.RPAREN);
            }
            stmt.setPartitioning(partitionClause);
            continue;
        }
        break;
    }
    if (lexer.token() == (Token.ON)) {
        throw new ParserException("TODO");
    }
    if (lexer.token() == (Token.AS)) {
        lexer.nextToken();
    }
    if (lexer.token() == (Token.SELECT)) {
        SQLSelect query = new MySqlSelectParser(this.exprParser).select();
        stmt.setSelect(query);
    }
    while (lexer.token() == (Token.HINT)) {
        this.exprParser.parseHints(stmt.getOptionHints());
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlTableIndex(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLForeignKeyConstraint(com.alibaba.druid.sql.ast.statement.SQLForeignKeyConstraint) MySqlPartitionByKey(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey) SQLPartitionByRange(com.alibaba.druid.sql.ast.SQLPartitionByRange) SQLPartitionByHash(com.alibaba.druid.sql.ast.SQLPartitionByHash) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLCheck(com.alibaba.druid.sql.ast.statement.SQLCheck) SQLPartition(com.alibaba.druid.sql.ast.SQLPartition) SQLTableConstraint(com.alibaba.druid.sql.ast.statement.SQLTableConstraint) SQLPartitionBy(com.alibaba.druid.sql.ast.SQLPartitionBy) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) TableSpaceOption(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption) SQLPartitionByList(com.alibaba.druid.sql.ast.SQLPartitionByList)

Example 5 with SQLTableSource

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

the class DruidSelectDb2Parser method parseNativePageSql.

protected void parseNativePageSql(SQLStatement stmt, RouteResultset rrs, OracleSelectQueryBlock mysqlSelectQuery, SchemaConfig schema) {
    //第一层子查询
    SQLExpr where = mysqlSelectQuery.getWhere();
    SQLTableSource from = mysqlSelectQuery.getFrom();
    if (where instanceof SQLBinaryOpExpr && from instanceof SQLSubqueryTableSource) {
        SQLBinaryOpExpr one = (SQLBinaryOpExpr) where;
        SQLExpr left = one.getLeft();
        SQLBinaryOperator operator = one.getOperator();
        SQLSelectQuery subSelect = ((SQLSubqueryTableSource) from).getSelect().getQuery();
        SQLOrderBy orderBy = null;
        if (subSelect instanceof OracleSelectQueryBlock) {
            boolean hasRowNumber = false;
            OracleSelectQueryBlock subSelectOracle = (OracleSelectQueryBlock) subSelect;
            List<SQLSelectItem> sqlSelectItems = subSelectOracle.getSelectList();
            for (SQLSelectItem sqlSelectItem : sqlSelectItems) {
                SQLExpr sqlExpr = sqlSelectItem.getExpr();
                if (sqlExpr instanceof SQLAggregateExpr) {
                    SQLAggregateExpr agg = (SQLAggregateExpr) sqlExpr;
                    if ("row_number".equalsIgnoreCase(agg.getMethodName()) && agg.getOver() != null) {
                        hasRowNumber = true;
                        orderBy = agg.getOver().getOrderBy();
                    }
                }
            }
            if (hasRowNumber) {
                if ((operator == SQLBinaryOperator.LessThan || operator == SQLBinaryOperator.LessThanOrEqual) && one.getRight() instanceof SQLIntegerExpr) {
                    SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
                    int firstrownum = right.getNumber().intValue();
                    if (operator == SQLBinaryOperator.LessThan && firstrownum != 0) {
                        firstrownum = firstrownum - 1;
                    }
                    if (subSelect instanceof OracleSelectQueryBlock) {
                        rrs.setLimitStart(0);
                        rrs.setLimitSize(firstrownum);
                        //为了继续解出order by 等
                        mysqlSelectQuery = (OracleSelectQueryBlock) subSelect;
                        if (orderBy != null) {
                            OracleSelect oracleSelect = (OracleSelect) subSelect.getParent();
                            oracleSelect.setOrderBy(orderBy);
                        }
                        parseOrderAggGroupOracle(stmt, rrs, mysqlSelectQuery, schema);
                        isNeedParseOrderAgg = false;
                    }
                } else if (operator == SQLBinaryOperator.BooleanAnd && left instanceof SQLBinaryOpExpr && one.getRight() instanceof SQLBinaryOpExpr) {
                    SQLBinaryOpExpr leftE = (SQLBinaryOpExpr) left;
                    SQLBinaryOpExpr rightE = (SQLBinaryOpExpr) one.getRight();
                    SQLBinaryOpExpr small = null;
                    SQLBinaryOpExpr larger = null;
                    int firstrownum = 0;
                    int lastrownum = 0;
                    if (leftE.getRight() instanceof SQLIntegerExpr && (leftE.getOperator() == SQLBinaryOperator.GreaterThan || leftE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual)) {
                        small = leftE;
                        firstrownum = ((SQLIntegerExpr) leftE.getRight()).getNumber().intValue();
                        if (leftE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
                            firstrownum = firstrownum - 1;
                        }
                    } else if (leftE.getRight() instanceof SQLIntegerExpr && (leftE.getOperator() == SQLBinaryOperator.LessThan || leftE.getOperator() == SQLBinaryOperator.LessThanOrEqual)) {
                        larger = leftE;
                        lastrownum = ((SQLIntegerExpr) leftE.getRight()).getNumber().intValue();
                        if (leftE.getOperator() == SQLBinaryOperator.LessThan && lastrownum != 0) {
                            lastrownum = lastrownum - 1;
                        }
                    }
                    if (rightE.getRight() instanceof SQLIntegerExpr && (rightE.getOperator() == SQLBinaryOperator.GreaterThan || rightE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual)) {
                        small = rightE;
                        firstrownum = ((SQLIntegerExpr) rightE.getRight()).getNumber().intValue();
                        if (rightE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
                            firstrownum = firstrownum - 1;
                        }
                    } else if (rightE.getRight() instanceof SQLIntegerExpr && (rightE.getOperator() == SQLBinaryOperator.LessThan || rightE.getOperator() == SQLBinaryOperator.LessThanOrEqual)) {
                        larger = rightE;
                        lastrownum = ((SQLIntegerExpr) rightE.getRight()).getNumber().intValue();
                        if (rightE.getOperator() == SQLBinaryOperator.LessThan && lastrownum != 0) {
                            lastrownum = lastrownum - 1;
                        }
                    }
                    if (small != null && larger != null) {
                        setLimitIFChange(stmt, rrs, schema, small, firstrownum, lastrownum);
                        if (orderBy != null) {
                            OracleSelect oracleSelect = (OracleSelect) subSelect.getParent();
                            oracleSelect.setOrderBy(orderBy);
                        }
                        parseOrderAggGroupOracle(stmt, rrs, (OracleSelectQueryBlock) subSelect, schema);
                        isNeedParseOrderAgg = false;
                    }
                }
            } else {
                parseNativeSql(stmt, rrs, mysqlSelectQuery, schema);
            }
        }
    } else {
        parseNativeSql(stmt, rrs, mysqlSelectQuery, schema);
    }
    if (isNeedParseOrderAgg) {
        parseOrderAggGroupOracle(stmt, rrs, mysqlSelectQuery, schema);
    }
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLBinaryOperator(com.alibaba.druid.sql.ast.expr.SQLBinaryOperator) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) OracleSelect(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Aggregations

SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)14 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)6 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)5 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)4 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)4 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 Limit (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit)2 OracleSelect (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect)2 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)2 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)2 SQLServerTop (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop)2 RouteResultsetNode (io.mycat.route.RouteResultsetNode)2