RouteService implements IRouteService { public ExecutePlan route(List<String> tables, List<CalculateUnit> units, String sql, SqlType sqlType){ if (logger.isDebugEnabled()){ logger.debug("计算单元:" + PrintUtil.printCalculates(units)); } if (tables == null || tables.size() == 0){ return buildExecutePlanTypeNo(sql, null, sqlType); } PartitionTable partitionTable = null; for (String tableName : tables){ if (BaymaxContext.isPartitionTable(tableName)){ if (partitionTable != null){ throw new BayMaxException("sql中包含了两个分区表"); }else { partitionTable = BaymaxContext.getPartitionTable(tableName); } } } if (partitionTable == null){ return buildExecutePlanTypeNo(sql, null, sqlType); } if (units == null || units.size() == 0){ return buildExecutePlanTypeAll(partitionTable, sqlType); } Set<TargetTableEntity> nodeSet = new LinkedHashSet<TargetTableEntity>(); for (CalculateUnit unit : units) { List<TargetTableEntity> temp = partitionTable.execute(unit); if (temp == null || temp.size() == 0){ return buildExecutePlanTypeAll(partitionTable, sqlType); }else { nodeSet.addAll(temp); } } if (nodeSet.size() == 0){ return buildExecutePlanTypeNo(sql, partitionTable.getLogicTableName(), sqlType); } return buildExecutePlanTypePartition(partitionTable, sqlType, nodeSet); }  ExecutePlan doRoute(String sql, Map<Integer, ParameterCommand> parameterCommand); ExecutePlan route(List<String> tables, List<CalculateUnit> units, String sql, SqlType sqlType); List<Object> buildParameters(Map<Integer, ParameterCommand> commonds);  }
@Test public void testRoute() throws Exception { List<String> tables = new ArrayList<String>(); List<CalculateUnit > units = new ArrayList<CalculateUnit>(); tables.add("table1"); CalculateUnit unit1 = new CalculateUnit(); unit1.addCondition(ConditionUnit.buildConditionUnit("table1", "a", new String[]{"2"}, ConditionUnitOperator.EQUAL)); units.add(unit1); ExecutePlan plan =routeService.route(tables, units, "select", SqlType.SELECT); System.out.println(plan); }
@Test public void testRoute_1() throws Exception { List<String> tables = new ArrayList<String>(); List<CalculateUnit > units = new ArrayList<CalculateUnit>(); tables.add("table1"); CalculateUnit unit1 = new CalculateUnit(); unit1.addCondition(ConditionUnit.buildConditionUnit("table1", "a", new String[]{"2"}, ConditionUnitOperator.EQUAL)); unit1.addCondition(ConditionUnit.buildConditionUnit("table1", "b", new String[]{"1"}, ConditionUnitOperator.EQUAL)); units.add(unit1); ExecutePlan plan =routeService.route(tables, units, "select", SqlType.SELECT); System.out.println(plan); }
@Test public void testRoute_2() throws Exception { List<String> tables = new ArrayList<String>(); List<CalculateUnit > units = new ArrayList<CalculateUnit>(); tables.add("table1"); CalculateUnit unit1 = new CalculateUnit(); unit1.addCondition(ConditionUnit.buildConditionUnit("table1", "a", new String[]{"2"}, ConditionUnitOperator.EQUAL)); CalculateUnit unit2 = new CalculateUnit(); units.add(unit1); units.add(unit2); ExecutePlan plan =routeService.route(tables, units, "select", SqlType.SELECT); System.out.println(plan); }