Java Code Examples for com.alibaba.druid.sql.ast.SQLExpr#accept()

The following examples show how to use com.alibaba.druid.sql.ast.SQLExpr#accept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestMySQLItemVisitor.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGroupbyOrder() {
    MySqlSelectQueryBlock query = getQuery("select col1,col2 from table1 group by col1 desc,col2 asc ");
    SQLSelectGroupByClause groupBy = query.getGroupBy();
    int i = 0;
    for (SQLExpr p : groupBy.getItems()) {
        i++;
        String groupCol = "col" + i;
        MySqlOrderingExpr groupitem = (MySqlOrderingExpr) p;
        SQLExpr q = groupitem.getExpr();
        MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null, null);
        q.accept(v);
        Item item = v.getItem();
        Assert.assertEquals(true, groupCol.equals(item.getItemName()));
    }
}
 
Example 2
Source File: Item.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
public final String getItemName() {
    if (itemName == null || itemName.length() == 0) {
        SQLExpr expr = toExpression();
        StringBuilder sb = new StringBuilder();
        MySqlOutputVisitor ov = new MySqlOutputVisitor(sb);
        ov.setShardingSupport(false);
        expr.accept(ov);
        itemName = sb.toString();
    }
    return itemName;
}
 
Example 3
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean visit(SQLSelectItem x) {
    //need to protect parser SQLSelectItem, or SQLBinaryOpExpr may add to whereUnit
    // eg:id =1 will add to whereUnit
    inSelect = true;
    SQLExpr sqlExpr = x.getExpr();
    if (sqlExpr instanceof SQLMethodInvokeExpr && ItemCreate.getInstance().isInnerFunc(sqlExpr.toString().replace("(", "").replace(")", ""))) {
        containsInnerFunction = true;
    } else if (sqlExpr instanceof SQLIdentifierExpr && ItemCreate.getInstance().isInnerFunc(sqlExpr.toString())) {
        containsInnerFunction = true;
    }
    sqlExpr.accept(this);
    inSelect = false;

    //alias for select item is useless
    //        String alias = x.getAlias();
    //
    //        Map<String, String> aliasMap = this.getAliasMap();
    //        if (alias != null && (!alias.isEmpty()) && aliasMap != null) {
    //            if (x.getExpr() instanceof SQLName) {
    //                boolean isSelf = false;
    //                String itemName = x.getExpr().toString();
    //                if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
    //                    isSelf = StringUtil.equalsIgnoreCase(alias, itemName);
    //                } else {
    //                    isSelf = StringUtil.equals(alias, itemName);
    //                }
    //                if (!isSelf) {
    //                    putAliasToMap(aliasMap, alias, x.getExpr().toString());
    //                }
    //            } else {
    //                putAliasToMap(aliasMap, alias, null);
    //            }
    //        }
    return false;
}
 
Example 4
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
/**
 * turn all the condition in or into conditionList
 * exp (conditionA OR conditionB) into conditionList{conditionA,conditionB}
 * so the conditionA,conditionB can be group with outer conditions
 *
 */
private void resetConditionsFromWhereUnit(WhereUnit whereUnit) {
    if (!whereUnit.isFinishedExtend()) {
        List<List<Condition>> retList = new ArrayList<>();
        List<Condition> outSideCondition = new ArrayList<>();
        outSideCondition.addAll(conditions);
        List<Relationship> outSideRelationship = new ArrayList<>();
        outSideRelationship.addAll(relationships);
        this.conditions.clear();
        this.relationships.clear();
        for (SQLExpr sqlExpr : whereUnit.getSplitedExprList()) {
            sqlExpr.accept(this);
            List<Condition> conds = new ArrayList<>();
            conds.addAll(getConditions());
            conds.addAll(outSideCondition);
            Set<Relationship> relations = new HashSet<>();
            relations.addAll(getRelationships());
            relations.addAll(outSideRelationship);
            ConditionUtil.extendConditionsFromRelations(conds, relations);
            retList.add(conds);
            this.conditions.clear();
            this.relationships.clear();
        }
        whereUnit.setOrConditionList(retList);

        for (WhereUnit subWhere : whereUnit.getSubWhereUnit()) {
            resetConditionsFromWhereUnit(subWhere);
        }
        whereUnit.setFinishedExtend(true);
    }
}
 
Example 5
Source File: TestMySQLItemVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGroupby() {
    MySqlSelectQueryBlock query = getQuery("select col1,col2 from table1 group by col1,col2");
    SQLSelectGroupByClause groupBy = query.getGroupBy();
    int i = 0;
    for (SQLExpr p : groupBy.getItems()) {
        i++;
        String groupCol = "col" + i;
        MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null, null);
        p.accept(v);
        Item item = v.getItem();
        Assert.assertEquals(true, groupCol.equals(item.getItemName()));
    }
}
 
Example 6
Source File: TestMySQLItemVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGroupbyHaving() {
    MySqlSelectQueryBlock query = getQuery("select col1  from table1 group by col1 having count(*)>1  ");
    SQLSelectGroupByClause groupBy = query.getGroupBy();
    SQLExpr q = groupBy.getHaving();
    MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null, null);
    q.accept(v);
    Item item = v.getItem();
    Assert.assertEquals(true, "COUNT(*) > 1".equals(item.getItemName()));
}
 
Example 7
Source File: TestMySQLItemVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testOrderby() {
    MySqlSelectQueryBlock query = getQuery("select col1,col2  from table1 order by col1 asc, col2 desc ");
    SQLOrderBy orderBy = query.getOrderBy();
    int i = 0;
    for (SQLSelectOrderByItem p : orderBy.getItems()) {
        i++;
        String orderCol = "col" + i;
        SQLExpr expr = p.getExpr();
        MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null, null);
        expr.accept(v);
        Item item = v.getItem();
        Assert.assertEquals(true, orderCol.equals(item.getItemName()));
    }
}
 
Example 8
Source File: TestMySQLItemVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testWhere() {
    MySqlSelectQueryBlock query = getQuery("select col1,col2  from table1 where a =1 ");
    SQLExpr expr = query.getWhere();

    MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset,null, null);
    expr.accept(v);
    Item item = v.getItem();
    Assert.assertEquals(true, "a = 1".equals(item.getItemName()));
}
 
Example 9
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
private boolean isExprHasOr(SQLExpr expr) {
	expr.accept(this);
	return hasOrCondition;
}
 
Example 10
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean visit(SQLJoinTableSource x) {
    switch (x.getJoinType()) {
        case LEFT_OUTER_JOIN:
        case RIGHT_OUTER_JOIN:
        case FULL_OUTER_JOIN:
            inOuterJoin = true;
            break;
        default:
            inOuterJoin = false;
            break;
    }

    SQLTableSource left = x.getLeft(), right = x.getRight();

    left.accept(this);
    right.accept(this);

    SQLExpr condition = x.getCondition();
    if (condition != null) {
        condition.accept(this);
    }

    if (x.getUsing().size() > 0 &&
            left instanceof SQLExprTableSource && right instanceof SQLExprTableSource) {
        SQLExpr leftExpr = ((SQLExprTableSource) left).getExpr();
        SQLExpr rightExpr = ((SQLExprTableSource) right).getExpr();

        for (SQLExpr expr : x.getUsing()) {
            if (expr instanceof SQLIdentifierExpr) {
                String name = ((SQLIdentifierExpr) expr).getName();
                /*
                when the shard1 a join shard2 b using(id)
                the intermediate condition should be a.id = b.id instead of shard1.id = shard2.id
                 */
                SQLPropertyExpr leftPropExpr = new SQLPropertyExpr(leftExpr, name);
                if (left.getAlias() != null) {
                    leftPropExpr.setOwner(left.getAlias());
                }
                SQLPropertyExpr rightPropExpr = new SQLPropertyExpr(rightExpr, name);
                if (right.getAlias() != null) {
                    rightPropExpr.setOwner(right.getAlias());
                }

                leftPropExpr.setResolvedTableSource(left);
                rightPropExpr.setResolvedTableSource(right);

                SQLBinaryOpExpr usingCondition = new SQLBinaryOpExpr(leftPropExpr, SQLBinaryOperator.Equality, rightPropExpr);
                usingCondition.accept(this);
            }
        }
    }

    inOuterJoin = false;
    return false;
}
 
Example 11
Source File: ServerSchemaStatVisitor.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
private boolean isExprHasOr(SQLExpr expr) {
    expr.accept(this);
    reBuildWhereUnits();
    return hasOrCondition;
}