public RouteResult Route(ShardingRule shardingRule) { if (IsDMLForModify(SqlCommandContext) && 1 != ((ITableAvailable)SqlCommandContext).GetAllTables().Count) { throw new ShardingException($"Cannot support Multiple-Table for '{SqlCommandContext.GetSqlCommand()}'."); } return(GenerateRouteResult(GetDataNodes(shardingRule, shardingRule.GetTableRule(LogicTableName)))); }
private ICollection <RouteUnit> GetAllRouteUnits(ShardingRule shardingRule, String logicTableName) { ICollection <RouteUnit> result = new LinkedList <RouteUnit>(); TableRule tableRule = shardingRule.GetTableRule(logicTableName); foreach (var dataNode in tableRule.ActualDataNodes) { RouteUnit routeUnit = new RouteUnit(new RouteMapper(dataNode.GetDataSourceName(), dataNode.GetDataSourceName()), new List <RouteMapper>() { new RouteMapper(logicTableName, dataNode.GetTableName()) }); result.Add(routeUnit); } return(result); }
public RouteResult Route(ShardingRule shardingRule) { RouteResult result = new RouteResult(); string dataSourceName = shardingRule.ShardingDataSourceNames.GetRandomDataSourceName(); RouteMapper dataSourceMapper = new RouteMapper(dataSourceName, dataSourceName); if (shardingRule.IsAllBroadcastTables(logicTables)) { List <RouteMapper> tableMappers = new List <RouteMapper>(logicTables.Count); foreach (var logicTable in logicTables) { tableMappers.Add(new RouteMapper(logicTable, logicTable)); } result.GetRouteUnits().Add(new RouteUnit(dataSourceMapper, tableMappers)); } else if (logicTables.IsEmpty()) { result.GetRouteUnits().Add(new RouteUnit(dataSourceMapper, new List <RouteMapper>(0))); } else if (1 == logicTables.Count) { string logicTableName = logicTables.First(); if (shardingRule.FindTableRule(logicTableName) == null) { result.GetRouteUnits().Add(new RouteUnit(dataSourceMapper, new List <RouteMapper>(0))); return(result); } DataNode dataNode = shardingRule.GetDataNode(logicTableName); result.GetRouteUnits().Add(new RouteUnit(dataSourceMapper, new List <RouteMapper>() { new RouteMapper(logicTableName, dataNode.GetTableName()) })); } else { List <RouteMapper> tableMappers = new List <RouteMapper>(logicTables.Count); ISet <string> availableDatasourceNames = null; bool first = true; foreach (var logicTable in logicTables) { TableRule tableRule = shardingRule.GetTableRule(logicTable); DataNode dataNode = tableRule.ActualDataNodes[0]; tableMappers.Add(new RouteMapper(logicTable, dataNode.GetTableName())); ISet <string> currentDataSourceNames = new HashSet <string>(); foreach (var actualDataNode in tableRule.ActualDataNodes) { currentDataSourceNames.Add(actualDataNode.GetDataSourceName()); } if (first) { availableDatasourceNames = currentDataSourceNames; first = false; } else { availableDatasourceNames = availableDatasourceNames.Intersect(currentDataSourceNames).ToHashSet(); } } if (availableDatasourceNames.IsEmpty()) { throw new ShardingException($"Cannot find actual dataSource intersection for logic tables: {logicTables}"); } dataSourceName = shardingRule.ShardingDataSourceNames.GetRandomDataSourceName(availableDatasourceNames); result.GetRouteUnits().Add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), tableMappers)); } return(result); }