Search in sources :

Example 16 with SQLIntegerExpr

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

the class HintSQLHandler method parseProcedure.

private Procedure parseProcedure(String sql, Map hintMap) {
    boolean fields = hintMap.containsKey("list_fields");
    boolean isResultList = hintMap != null && ("list".equals(hintMap.get("result_type")) || fields);
    Procedure procedure = new Procedure();
    procedure.setOriginSql(sql);
    procedure.setResultList(isResultList);
    List<String> sqls = Splitter.on(";").trimResults().splitToList(sql);
    Set<String> outSet = new HashSet<>();
    for (int i = sqls.size() - 1; i >= 0; i--) {
        String s = sqls.get(i);
        if (Strings.isNullOrEmpty(s)) {
            continue;
        }
        SQLStatementParser parser = new MySqlStatementParser(s);
        SQLStatement statement = parser.parseStatement();
        if (statement instanceof SQLSelectStatement) {
            MySqlSelectQueryBlock selectQuery = (MySqlSelectQueryBlock) ((SQLSelectStatement) statement).getSelect().getQuery();
            if (selectQuery != null) {
                List<SQLSelectItem> selectItems = selectQuery.getSelectList();
                for (SQLSelectItem selectItem : selectItems) {
                    String select = selectItem.toString();
                    outSet.add(select);
                    procedure.getSelectColumns().add(select);
                }
            }
            procedure.setSelectSql(s);
        } else if (statement instanceof SQLCallStatement) {
            SQLCallStatement sqlCallStatement = (SQLCallStatement) statement;
            procedure.setName(sqlCallStatement.getProcedureName().getSimpleName());
            List<SQLExpr> paramterList = sqlCallStatement.getParameters();
            for (int i1 = 0; i1 < paramterList.size(); i1++) {
                SQLExpr sqlExpr = paramterList.get(i1);
                String pName = sqlExpr.toString();
                String pType = outSet.contains(pName) ? ProcedureParameter.OUT : ProcedureParameter.IN;
                ProcedureParameter parameter = new ProcedureParameter();
                parameter.setIndex(i1 + 1);
                parameter.setName(pName);
                parameter.setParameterType(pType);
                if (pName.startsWith("@")) {
                    procedure.getParamterMap().put(pName, parameter);
                } else {
                    procedure.getParamterMap().put(String.valueOf(i1 + 1), parameter);
                }
            }
            procedure.setCallSql(s);
        } else if (statement instanceof SQLSetStatement) {
            procedure.setSetSql(s);
            SQLSetStatement setStatement = (SQLSetStatement) statement;
            List<SQLAssignItem> sets = setStatement.getItems();
            for (SQLAssignItem set : sets) {
                String name = set.getTarget().toString();
                SQLExpr value = set.getValue();
                ProcedureParameter parameter = procedure.getParamterMap().get(name);
                if (parameter != null) {
                    if (value instanceof SQLIntegerExpr) {
                        parameter.setValue(((SQLIntegerExpr) value).getNumber());
                        parameter.setJdbcType(Types.INTEGER);
                    } else if (value instanceof SQLNumberExpr) {
                        parameter.setValue(((SQLNumberExpr) value).getNumber());
                        parameter.setJdbcType(Types.NUMERIC);
                    } else if (value instanceof SQLTextLiteralExpr) {
                        parameter.setValue(((SQLTextLiteralExpr) value).getText());
                        parameter.setJdbcType(Types.VARCHAR);
                    } else if (value instanceof SQLValuableExpr) {
                        parameter.setValue(((SQLValuableExpr) value).getValue());
                        parameter.setJdbcType(Types.VARCHAR);
                    }
                }
            }
        }
    }
    if (fields) {
        String list_fields = (String) hintMap.get("list_fields");
        List<String> listFields = Splitter.on(",").trimResults().splitToList(list_fields);
        for (String field : listFields) {
            if (!procedure.getParamterMap().containsKey(field)) {
                ProcedureParameter parameter = new ProcedureParameter();
                parameter.setParameterType(ProcedureParameter.OUT);
                parameter.setName(field);
                parameter.setJdbcType(-10);
                parameter.setIndex(procedure.getParamterMap().size() + 1);
                procedure.getParamterMap().put(field, parameter);
            }
        }
        procedure.getListFields().addAll(listFields);
    }
    return procedure;
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLValuableExpr(com.alibaba.druid.sql.ast.expr.SQLValuableExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) List(java.util.List) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) HashSet(java.util.HashSet) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr)

Example 17 with SQLIntegerExpr

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

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

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

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

the class ParseUtil method changeInsertAddSlot.

public static String changeInsertAddSlot(String sql, int slotValue) {
    SQLStatementParser parser = new MycatStatementParser(sql);
    MySqlInsertStatement insert = (MySqlInsertStatement) parser.parseStatement();
    insert.getColumns().add(new SQLIdentifierExpr("_slot"));
    insert.getValues().getValues().add(new SQLIntegerExpr(slotValue));
    return insert.toString();
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MycatStatementParser(io.mycat.route.parser.druid.MycatStatementParser) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)

Aggregations

SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)24 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)16 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)9 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)9 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)5 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)4 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 List (java.util.List)4 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)3 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)3 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)3 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)3 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)2