Search in sources :

Example 11 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method getMergedConditionList.

/**
 * 条件合并:多个WhereUnit中的条件组合
 * @return
 */
private List<List<Condition>> getMergedConditionList(List<WhereUnit> whereUnitList) {
    List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
    if (whereUnitList.size() == 0) {
        return mergedConditionList;
    }
    mergedConditionList.addAll(whereUnitList.get(0).getConditionList());
    for (int i = 1; i < whereUnitList.size(); i++) {
        mergedConditionList = merge(mergedConditionList, whereUnitList.get(i).getConditionList());
    }
    return mergedConditionList;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint)

Example 12 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project Mycat-Server by MyCATApache.

the class DefaultDruidParser method buildRouteCalculateUnits.

private List<RouteCalculateUnit> buildRouteCalculateUnits(SchemaStatVisitor visitor, List<List<Condition>> conditionList) {
    List<RouteCalculateUnit> retList = new ArrayList<RouteCalculateUnit>();
    // 遍历condition ,找分片字段
    for (int i = 0; i < conditionList.size(); i++) {
        RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
        for (Condition condition : conditionList.get(i)) {
            List<Object> values = condition.getValues();
            if (values.size() == 0) {
                continue;
            }
            if (checkConditionValues(values)) {
                String columnName = StringUtil.removeBackquote(condition.getColumn().getName().toUpperCase());
                String tableName = StringUtil.removeBackquote(condition.getColumn().getTable().toUpperCase());
                if (visitor.getAliasMap() != null && visitor.getAliasMap().get(tableName) != null && !visitor.getAliasMap().get(tableName).equals(tableName)) {
                    tableName = visitor.getAliasMap().get(tableName);
                }
                if (visitor.getAliasMap() != null && visitor.getAliasMap().get(StringUtil.removeBackquote(condition.getColumn().getTable().toUpperCase())) == null) {
                    // 子查询的别名条件忽略掉,不参数路由计算,否则后面找不到表
                    continue;
                }
                String operator = condition.getOperator();
                // 只处理between ,in和=3中操作符
                if (operator.equals("between")) {
                    RangeValue rv = new RangeValue(values.get(0), values.get(1), RangeValue.EE);
                    routeCalculateUnit.addShardingExpr(tableName.toUpperCase(), columnName, rv);
                } else if (operator.equals("=") || operator.toLowerCase().equals("in")) {
                    // 只处理=号和in操作符,其他忽略
                    routeCalculateUnit.addShardingExpr(tableName.toUpperCase(), columnName, values.toArray());
                }
            }
        }
        retList.add(routeCalculateUnit);
    }
    return retList;
}
Also used : RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) Condition(com.alibaba.druid.stat.TableStat.Condition) ArrayList(java.util.ArrayList) RangeValue(io.mycat.sqlengine.mpp.RangeValue)

Example 13 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project Mycat-Server by MyCATApache.

the class DQLRouteTest method visitorParse.

@SuppressWarnings("unchecked")
private List<RouteCalculateUnit> visitorParse(RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor) throws Exception {
    stmt.accept(visitor);
    List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
    if (visitor.hasOrCondition()) {
        // 包含or语句
        // TODO
        // 根据or拆分
        mergedConditionList = visitor.splitConditions();
    } else {
        // 不包含OR语句
        mergedConditionList.add(visitor.getConditions());
    }
    if (visitor.getAliasMap() != null) {
        for (Map.Entry<String, String> entry : visitor.getAliasMap().entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (key != null && key.indexOf("`") >= 0) {
                key = key.replaceAll("`", "");
            }
            if (value != null && value.indexOf("`") >= 0) {
                value = value.replaceAll("`", "");
            }
            // 表名前面带database的,去掉
            if (key != null) {
                int pos = key.indexOf(".");
                if (pos > 0) {
                    key = key.substring(pos + 1);
                }
            }
            if (key.equals(value)) {
                ctx.addTable(key.toUpperCase());
            }
            // else {
            // tableAliasMap.put(key, value);
            // }
            tableAliasMap.put(key.toUpperCase(), value);
        }
        visitor.getAliasMap().putAll(tableAliasMap);
        ctx.setTableAliasMap(tableAliasMap);
    }
    // 利用反射机制单元测试DefaultDruidParser类的私有方法buildRouteCalculateUnits
    Class<?> clazz = Class.forName("io.mycat.route.parser.druid.impl.DefaultDruidParser");
    Method buildRouteCalculateUnits = clazz.getDeclaredMethod("buildRouteCalculateUnits", new Class[] { SchemaStatVisitor.class, List.class });
    // System.out.println("buildRouteCalculateUnits:\t" + buildRouteCalculateUnits);
    Object newInstance = clazz.newInstance();
    buildRouteCalculateUnits.setAccessible(true);
    Object returnValue = buildRouteCalculateUnits.invoke(newInstance, new Object[] { visitor, mergedConditionList });
    List<RouteCalculateUnit> retList = new ArrayList<RouteCalculateUnit>();
    if (returnValue instanceof ArrayList<?>) {
        retList.add(((ArrayList<RouteCalculateUnit>) returnValue).get(0));
    // retList = (ArrayList<RouteCalculateUnit>)returnValue;
    // System.out.println(taskList.get(0).getTablesAndConditions().values());
    }
    return retList;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project druid by alibaba.

the class OracleSchemaStatVisitorTest method test_0.

public void test_0() throws Exception {
    String sql = // 
    "SELECT id, name name from department d" + "   WHERE d.id = ? order by name desc";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    List<Object> parameters = new ArrayList<Object>();
    parameters.add(23456);
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    visitor.setParameters(parameters);
    statemen.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("coditions : " + visitor.getConditions());
    System.out.println("relationships : " + visitor.getRelationships());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("department")));
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("department", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("department", "name")));
    Assert.assertEquals(1, visitor.getConditions().size());
    Condition condition = visitor.getConditions().get(0);
    Assert.assertSame(parameters.get(0), condition.getValues().get(0));
    Column orderByColumn = visitor.getOrderByColumns().iterator().next();
    Assert.assertEquals(SQLOrderingSpecification.DESC, orderByColumn.getAttributes().get("orderBy.type"));
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) Column(com.alibaba.druid.stat.TableStat.Column) OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) ArrayList(java.util.ArrayList) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 15 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project druid by alibaba.

the class OracleSchemaStatVisitorTest5 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT B.OBJ_ID AS BDZID,B.BDZMC,count(A.*) " + "FROM (" + "     SELECT BDZ " + "     FROM (" + "         SELECT JLZB.OBJ_ID,JLZB.BDZ, JLB.BHSBID, JLB.CLZ " + "         FROM SCYW.T_YJ_DWYJ_YXRZ_CDBHCLJL JLB " + "         INNER JOIN SCYW.T_YJ_DWYJ_YXRZ_YXJLZB JLZB ON JLB.YXJLID = JLZB.OBJ_ID) T " + "         WHERE T.OBJ_ID IN('SYS_B_02','SYS_B_03','SYS_B_04') " + "             AND 'SYS_B_05'='SYS_B_06'" + ") A, SCYW.T_SB_ZNYC_DZ B " + "WHERE A.BDZ=B.OBJ_ID " + "     AND B.OBJ_ID IN (" + "         SELECT DZ.OBJ_ID " + "         FROM T_SB_ZNYC_DZ DZ " + "         WHERE DZ.WHBZ = 'SYS_B_07' " + "             OR DZ.OBJ_ID IN (" + "                 SELECT ZFBZ.ZFID " + "                 FROM T_SB_ZNYC_ZFBZ ZFBZ " + "                 WHERE ZFBZ.BZID = 'SYS_B_08' AND ZFBZ.BZLB = 'SYS_B_09'" + "             ) " + "             AND DZ.FBZT = 'SYS_B_10'" + "         ) " + "GROUP BY B.OBJ_ID,B.BDZMC";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    statemen.accept(visitor);
    System.out.println(sql);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("relationShip : " + visitor.getRelationships());
    System.out.println("where : " + visitor.getConditions());
    System.out.println("groupBy : " + visitor.getGroupByColumns());
    for (Condition condition : visitor.getConditions()) {
        String table = condition.getColumn().getTable();
        Assert.assertTrue("table not exists : " + table, visitor.containsTable(table));
    }
    Assert.assertEquals(5, visitor.getTables().size());
    Assert.assertEquals(true, visitor.containsTable("T_SB_ZNYC_ZFBZ"));
    Assert.assertEquals(13, visitor.getColumns().size());
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Aggregations

Condition (com.alibaba.druid.stat.TableStat.Condition)28 ArrayList (java.util.ArrayList)22 List (java.util.List)16 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 Column (com.alibaba.druid.stat.TableStat.Column)7 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)4 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 Method (java.lang.reflect.Method)3 RouteCalculateUnit (com.actiontech.dble.route.parser.druid.RouteCalculateUnit)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)2 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2