public RouteResult Route(ShardingRule shardingRule)
        {
            ICollection <RouteResult> result            = new List <RouteResult>(logicTables.Count);
            ICollection <String>      bindingTableNames = new SortedSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var logicTable in logicTables)
            {
                var tableRule = shardingRule.FindTableRule(logicTable);
                if (tableRule != null)
                {
                    if (!bindingTableNames.Contains(logicTable))
                    {
                        result.Add(new ShardingStandardRoutingEngine(tableRule.LogicTable, sqlStatementContext, shardingConditions, properties).Route(shardingRule));
                    }
                    shardingRule.FindBindingTableRule(logicTable).IfPresent(bindingTableRule => bindingTableNames.AddAll(
                                                                                bindingTableRule.GetTableRules().Select(o => o.LogicTable).ToList()));
                }
            }
            if (result.IsEmpty())
            {
                throw new ShardingException($"Cannot find table rule and default data source with logic tables: '{logicTables}'");
            }
            if (1 == result.Count)
            {
                return(result.First());
            }
            return(new ShardingCartesianRoutingEngine(result).Route(shardingRule));
        }
Пример #2
0
        private ICollection <SqlToken> GenerateSqlTokens(ITableAvailable sqlStatementContext)
        {
            ICollection <SqlToken> result = new LinkedList <SqlToken>();

            foreach (var simpleTableSegment in sqlStatementContext.GetAllTables())
            {
                if (shardingRule.FindTableRule(simpleTableSegment.GetTableName().GetIdentifier().GetValue()) != null)
                {
                    result.Add(new TableToken(simpleTableSegment.GetStartIndex(), simpleTableSegment.GetStopIndex(), simpleTableSegment.GetTableName().GetIdentifier(), (ISqlCommandContext <ISqlCommand>)sqlStatementContext, shardingRule));
                }
            }
            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);
        }