Search in sources :

Example 36 with SQLBinaryOpExpr

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

the class DruidInsertParser method parserSingleInsert.

/**
	 * 单条insert(非批量)
	 * @param schema
	 * @param rrs
	 * @param partitionColumn
	 * @param tableName
	 * @param insertStmt
	 * @throws SQLNonTransientException
	 */
private void parserSingleInsert(SchemaConfig schema, RouteResultset rrs, String partitionColumn, String tableName, MySqlInsertStatement insertStmt) throws SQLNonTransientException {
    boolean isFound = false;
    for (int i = 0; i < insertStmt.getColumns().size(); i++) {
        if (partitionColumn.equalsIgnoreCase(StringUtil.removeBackquote(insertStmt.getColumns().get(i).toString()))) {
            //找到分片字段
            isFound = true;
            String column = StringUtil.removeBackquote(insertStmt.getColumns().get(i).toString());
            String value = StringUtil.removeBackquote(insertStmt.getValues().getValues().get(i).toString());
            RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
            routeCalculateUnit.addShardingExpr(tableName, column, value);
            ctx.addRouteCalculateUnit(routeCalculateUnit);
            //mycat是单分片键,找到了就返回
            break;
        }
    }
    if (!isFound) {
        //分片表的
        String msg = "bad insert sql (sharding column:" + partitionColumn + " not provided," + insertStmt;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    //INSERT INTO TABLEName (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;
    if (insertStmt.getDuplicateKeyUpdate() != null) {
        List<SQLExpr> updateList = insertStmt.getDuplicateKeyUpdate();
        for (SQLExpr expr : updateList) {
            SQLBinaryOpExpr opExpr = (SQLBinaryOpExpr) expr;
            String column = StringUtil.removeBackquote(opExpr.getLeft().toString().toUpperCase());
            if (column.equals(partitionColumn)) {
                String msg = "Sharding column can't be updated: " + tableName + " -> " + partitionColumn;
                LOGGER.warn(msg);
                throw new SQLNonTransientException(msg);
            }
        }
    }
}
Also used : RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) SQLNonTransientException(java.sql.SQLNonTransientException) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 37 with SQLBinaryOpExpr

use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr 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)

Example 38 with SQLBinaryOpExpr

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

the class DruidSelectOracleParser 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();
        //解析只有一层rownum限制大小
        if (one.getRight() instanceof SQLIntegerExpr && "rownum".equalsIgnoreCase(left.toString()) && (operator == SQLBinaryOperator.LessThanOrEqual || operator == SQLBinaryOperator.LessThan)) {
            SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
            int firstrownum = right.getNumber().intValue();
            if (operator == SQLBinaryOperator.LessThan && firstrownum != 0) {
                firstrownum = firstrownum - 1;
            }
            SQLSelectQuery subSelect = ((SQLSubqueryTableSource) from).getSelect().getQuery();
            if (subSelect instanceof OracleSelectQueryBlock) {
                rrs.setLimitStart(0);
                rrs.setLimitSize(firstrownum);
                //为了继续解出order by 等
                mysqlSelectQuery = (OracleSelectQueryBlock) subSelect;
                parseOrderAggGroupOracle(stmt, rrs, mysqlSelectQuery, schema);
                isNeedParseOrderAgg = false;
            }
        } else //解析oracle三层嵌套分页
        if (one.getRight() instanceof SQLIntegerExpr && !"rownum".equalsIgnoreCase(left.toString()) && (operator == SQLBinaryOperator.GreaterThan || operator == SQLBinaryOperator.GreaterThanOrEqual)) {
            parseThreeLevelPageSql(stmt, rrs, schema, (SQLSubqueryTableSource) from, one, operator);
        } else //解析oracle rownumber over分页
        {
            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);
                            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) List(java.util.List) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 39 with SQLBinaryOpExpr

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

the class DruidSelectSqlServerParser method parseSqlServerPageSql.

private void parseSqlServerPageSql(SQLStatement stmt, RouteResultset rrs, SQLServerSelectQueryBlock sqlserverSelectQuery, SchemaConfig schema) {
    //第一层子查询
    SQLExpr where = sqlserverSelectQuery.getWhere();
    SQLTableSource from = sqlserverSelectQuery.getFrom();
    if (sqlserverSelectQuery.getTop() != null) {
        SQLServerTop top = sqlserverSelectQuery.getTop();
        SQLExpr sqlExpr = top.getExpr();
        if (sqlExpr instanceof SQLIntegerExpr) {
            int topValue = ((SQLIntegerExpr) sqlExpr).getNumber().intValue();
            rrs.setLimitStart(0);
            rrs.setLimitSize(topValue);
        }
    } else 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 SQLServerSelectQueryBlock) {
            boolean hasRowNumber = false;
            boolean hasSubTop = false;
            int subTop = 0;
            SQLServerSelectQueryBlock subSelectOracle = (SQLServerSelectQueryBlock) 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 (subSelectOracle.getFrom() instanceof SQLSubqueryTableSource) {
                SQLSubqueryTableSource subFrom = (SQLSubqueryTableSource) subSelectOracle.getFrom();
                if (subFrom.getSelect().getQuery() instanceof SQLServerSelectQueryBlock) {
                    SQLServerSelectQueryBlock sqlSelectQuery = (SQLServerSelectQueryBlock) subFrom.getSelect().getQuery();
                    if (sqlSelectQuery.getTop() != null) {
                        SQLExpr sqlExpr = sqlSelectQuery.getTop().getExpr();
                        if (sqlExpr instanceof SQLIntegerExpr) {
                            hasSubTop = true;
                            subTop = ((SQLIntegerExpr) sqlExpr).getNumber().intValue();
                            orderBy = subFrom.getSelect().getOrderBy();
                        }
                    }
                }
            }
            if (hasRowNumber) {
                if (hasSubTop && (operator == SQLBinaryOperator.GreaterThan || operator == SQLBinaryOperator.GreaterThanOrEqual) && one.getRight() instanceof SQLIntegerExpr) {
                    SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
                    int firstrownum = right.getNumber().intValue();
                    if (operator == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
                        firstrownum = firstrownum - 1;
                    }
                    int lastrownum = subTop;
                    setLimitIFChange(stmt, rrs, schema, one, firstrownum, lastrownum);
                    if (orderBy != null) {
                        SQLServerSelect oracleSelect = (SQLServerSelect) subSelect.getParent();
                        oracleSelect.setOrderBy(orderBy);
                    }
                    parseOrderAggGroupSqlServer(schema, stmt, rrs, (SQLServerSelectQueryBlock) subSelect);
                    isNeedParseOrderAgg = false;
                } else 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 SQLServerSelectQueryBlock) {
                        rrs.setLimitStart(0);
                        rrs.setLimitSize(firstrownum);
                        //为了继续解出order by 等
                        sqlserverSelectQuery = (SQLServerSelectQueryBlock) subSelect;
                        if (orderBy != null) {
                            SQLServerSelect oracleSelect = (SQLServerSelect) subSelect.getParent();
                            oracleSelect.setOrderBy(orderBy);
                        }
                        parseOrderAggGroupSqlServer(schema, stmt, rrs, sqlserverSelectQuery);
                        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) {
                            SQLServerSelect oracleSelect = (SQLServerSelect) subSelect.getParent();
                            oracleSelect.setOrderBy(orderBy);
                        }
                        parseOrderAggGroupSqlServer(schema, stmt, rrs, (SQLServerSelectQueryBlock) subSelect);
                        isNeedParseOrderAgg = false;
                    }
                }
            }
        }
    }
}
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) SQLServerSelect(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelect) 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) SQLServerTop(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop) SQLServerSelectQueryBlock(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) List(java.util.List) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Example 40 with SQLBinaryOpExpr

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

the class LargeOrTest method test_largeAnd.

public void test_largeAnd() throws Exception {
    StringBuffer buf = new StringBuffer();
    buf.append("SELECT 1 FROM T WHERE ID = ?");
    for (int i = 0; i < 10000; ++i) {
        buf.append(" AND ID = ?");
    }
    String sql = buf.toString();
    OracleStatementParser parser = new OracleStatementParser(sql);
    SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatementList().get(0);
    SQLSelectQueryBlock select = (SQLSelectQueryBlock) stmt.getSelect().getQuery();
    SQLBinaryOpExpr where = (SQLBinaryOpExpr) select.getWhere();
    SQLBinaryOpExpr last = (SQLBinaryOpExpr) where.getRight();
    Assert.assertEquals(SQLBinaryOperator.Equality, last.getOperator());
}
Also used : SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Aggregations

SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)46 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)30 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)9 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)8 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)8 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)7 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)7 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)6 SQLObject (com.alibaba.druid.sql.ast.SQLObject)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)5 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)4 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)4 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 SQLOver (com.alibaba.druid.sql.ast.SQLOver)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3