Search in sources :

Example 21 with SQLSelectOrderByItem

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

the class SQLASTParameterizedVisitor method visit.

@Override
public boolean visit(SQLIntegerExpr x) {
    SQLObject parent = x.getParent();
    if (parent instanceof SQLSelectGroupByClause || parent instanceof SQLSelectOrderByItem) {
        return false;
    }
    parameterizeAndExportPara(x);
    return false;
}
Also used : SQLSelectGroupByClause(com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)

Example 22 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

public boolean visit(final SQLOrderBy x) {
    for (SQLSelectOrderByItem each : x.getItems()) {
        SQLExpr expr = each.getExpr();
        OrderByType orderByType = null == each.getType() ? OrderByType.ASC : OrderByType.valueOf(each.getType());
        if (expr instanceof SQLIntegerExpr) {
            getParseContext().addOrderByColumn(((SQLIntegerExpr) expr).getNumber().intValue(), orderByType);
        } else if (expr instanceof SQLIdentifierExpr) {
            getParseContext().addOrderByColumn(Optional.<String>absent(), ((SQLIdentifierExpr) expr).getName(), orderByType);
        } else if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr sqlPropertyExpr = (SQLPropertyExpr) expr;
            getParseContext().addOrderByColumn(Optional.of(sqlPropertyExpr.getOwner().toString()), sqlPropertyExpr.getName(), orderByType);
        }
    }
    return super.visit(x);
}
Also used : SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) OrderByType(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn.OrderByType)

Example 23 with SQLSelectOrderByItem

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

the class JoinParser method parserOrderBy.

private void parserOrderBy(SQLOrderBy orderby) {
    if (orderby != null) {
        for (int i = 0; i < orderby.getItems().size(); i++) {
            SQLSelectOrderByItem orderitem = orderby.getItems().get(i);
            tableFilter.addOrders(orderitem.getExpr().toString(), getSQLExprToAsc(orderitem.getType()));
        }
    }
}
Also used : SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)

Example 24 with SQLSelectOrderByItem

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

the class DruidSelectParser method parseOrderAggGroupMysql.

protected void parseOrderAggGroupMysql(SchemaConfig schema, SQLStatement stmt, RouteResultset rrs, MySqlSelectQueryBlock mysqlSelectQuery) {
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    // rrs.setGroupByCols((String[])visitor.getGroupByColumns().toArray());
    if (!isNeedParseOrderAgg) {
        return;
    }
    Map<String, String> aliaColumns = parseAggGroupCommon(schema, stmt, rrs, mysqlSelectQuery);
    // setOrderByCols
    if (mysqlSelectQuery.getOrderBy() != null) {
        List<SQLSelectOrderByItem> orderByItems = mysqlSelectQuery.getOrderBy().getItems();
        rrs.setOrderByCols(buildOrderByCols(orderByItems, aliaColumns));
    }
    isNeedParseOrderAgg = false;
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)

Example 25 with SQLSelectOrderByItem

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

the class SQLQueryResultHandler method dohandler.

@Override
public String dohandler(SQLStatement statement, SQLSelect sqlselect, SQLObject parent, List param) {
    if (parent.getParent() instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr pp = (SQLBinaryOpExpr) parent.getParent();
        SQLExprImpl listExpr = null;
        if (null == param || param.isEmpty()) {
            listExpr = new SQLNullExpr();
        } else {
            listExpr = new SQLListExpr();
            ((SQLListExpr) listExpr).getItems().addAll(param);
        }
        if (pp.getLeft().equals(parent)) {
            pp.setLeft(listExpr);
        } else if (pp.getRight().equals(parent)) {
            pp.setRight(listExpr);
        }
    } else if (parent.getParent() instanceof SQLSelectItem) {
        SQLSelectItem pp = (SQLSelectItem) parent.getParent();
        SQLExprImpl listExpr = null;
        if (null == param || param.isEmpty()) {
            listExpr = new SQLNullExpr();
        } else {
            listExpr = new SQLListExpr();
            ((SQLListExpr) listExpr).getItems().addAll(param);
        }
        pp.setExpr(listExpr);
    } else if (parent.getParent() instanceof SQLSelectGroupByClause) {
        SQLSelectGroupByClause pp = (SQLSelectGroupByClause) parent.getParent();
        List<SQLExpr> items = pp.getItems();
        for (int i = 0; i < items.size(); i++) {
            SQLExpr expr = items.get(i);
            if (expr instanceof SQLQueryExpr && ((SQLQueryExpr) expr).getSubQuery().equals(sqlselect)) {
                SQLExprImpl listExpr = null;
                if (null == param || param.isEmpty()) {
                    listExpr = new SQLNullExpr();
                } else {
                    listExpr = new SQLListExpr();
                    ((SQLListExpr) listExpr).getItems().addAll(param);
                }
                items.set(i, listExpr);
            }
        }
    } else if (parent.getParent() instanceof SQLSelectOrderByItem) {
        SQLSelectOrderByItem orderItem = (SQLSelectOrderByItem) parent.getParent();
        SQLExprImpl listExpr = null;
        if (null == param || param.isEmpty()) {
            listExpr = new SQLNullExpr();
        } else {
            listExpr = new SQLListExpr();
            ((SQLListExpr) listExpr).getItems().addAll(param);
        }
        listExpr.setParent(orderItem);
        orderItem.setExpr(listExpr);
    } else if (parent.getParent() instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) parent.getParent();
        // select * from subtest1 a where (select 1 from subtest3); 这种情况会进入到当前分支.
        // 改写为   select * from subtest1 a where (1); 或  select * from subtest1 a where (null);
        SQLExprImpl listExpr = null;
        if (null == param || param.isEmpty()) {
            listExpr = new SQLNullExpr();
        } else {
            listExpr = new SQLListExpr();
            ((SQLListExpr) listExpr).getItems().addAll(param);
        }
        listExpr.setParent(query);
        query.setWhere(listExpr);
    }
    return statement.toString();
}
Also used : SQLSelectGroupByClause(com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExprImpl(com.alibaba.druid.sql.ast.SQLExprImpl) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLListExpr(com.alibaba.druid.sql.ast.expr.SQLListExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)34 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)11 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)5 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLListExpr (com.alibaba.druid.sql.ast.expr.SQLListExpr)3 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)3 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)3 SQLConstraint (com.alibaba.druid.sql.ast.statement.SQLConstraint)3 SQLSelectGroupByClause (com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause)3 Order (com.actiontech.dble.plan.Order)2 SQLExprImpl (com.alibaba.druid.sql.ast.SQLExprImpl)2 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLOrderingSpecification (com.alibaba.druid.sql.ast.SQLOrderingSpecification)2 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)2 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)2 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)2 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)2 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)2