private IDictionary <Column, ICollection <IRouteValue> > CreateRouteValueMap(
            ISqlCommandContext <ISqlCommand> sqlCommandContext, AndPredicateSegment andPredicate,
            ParameterContext parameterContext)
        {
            IDictionary <Column, ICollection <IRouteValue> > result = new Dictionary <Column, ICollection <IRouteValue> >();

            foreach (var predicate in andPredicate.GetPredicates())
            {
                var tableName = sqlCommandContext.GetTablesContext()
                                .FindTableName(predicate.GetColumn(), schemaMetaData);
                if (tableName == null ||
                    !shardingRule.IsShardingColumn(predicate.GetColumn().GetIdentifier().GetValue(), tableName))
                {
                    continue;
                }

                Column column     = new Column(predicate.GetColumn().GetIdentifier().GetValue(), tableName);
                var    routeValue =
                    ConditionValueGeneratorFactory.Generate(predicate.GetPredicateRightValue(), column,
                                                            parameterContext);
                if (routeValue == null)
                {
                    continue;
                }

                if (!result.ContainsKey(column))
                {
                    result.Add(column, new LinkedList <IRouteValue>());
                }

                result[column].Add(routeValue);
            }

            return(result);
        }
Exemplo n.º 2
0
        private ICollection <string> GetLogicTableNames()
        {
            if (sqlCommandContext.GetSqlCommand() is DropIndexCommand dropIndexCommand && dropIndexCommand.Indexes.Any())
            {
                return(GetTableNamesFromMetaData(dropIndexCommand));
            }

            return(sqlCommandContext.GetTablesContext().GetTableNames());
        }
Exemplo n.º 3
0
        private IDictionary <string, string> GetLogicAndActualTables(RouteUnit routeUnit)
        {
            ICollection <string>         tableNames = sqlCommandContext.GetTablesContext().GetTableNames();
            IDictionary <string, string> result     = new Dictionary <string, string>(tableNames.Count);

            foreach (var tableMapper in routeUnit.TableMappers)
            {
                result.Add(tableMapper.LogicName.ToLower(), tableMapper.ActualName);
                result.AddAll(shardingRule.GetLogicAndActualTablesFromBindingTable(routeUnit.DataSourceMapper.LogicName, tableMapper.LogicName, tableMapper.ActualName, tableNames));
            }
            return(result);
        }
Exemplo n.º 4
0
 private void CheckSubQueryShardingValues(ISqlCommandContext <ISqlCommand> sqlStatementContext, ShardingRule shardingRule, ShardingConditions shardingConditions)
 {
     foreach (var tableName in sqlStatementContext.GetTablesContext().GetTableNames())
     {
         var tableRule = shardingRule.FindTableRule(tableName);
         if (tableRule != null && IsRoutingByHint(shardingRule, tableRule) &&
             HintManager.GetDatabaseShardingValues(tableName).Any() && HintManager.GetTableShardingValues(tableName).Any())
         {
             return;
         }
     }
     ShardingAssert.If(shardingConditions.Conditions.IsEmpty(), "Must have sharding column with subquery.");
     if (shardingConditions.Conditions.Count > 1)
     {
         ShardingAssert.Else(IsSameShardingCondition(shardingRule, shardingConditions), "Sharding value must same with subquery.");
     }
 }
        public static IShardingRouteEngine NewInstance(ShardingRule shardingRule,
                                                       ShardingConnectorMetaData metaData, ISqlCommandContext <ISqlCommand> sqlCommandContext,
                                                       ShardingConditions shardingConditions, ConfigurationProperties properties)
        {
            var sqlStatement = sqlCommandContext.GetSqlCommand();
            ICollection <String> tableNames = sqlCommandContext.GetTablesContext().GetTableNames();

            if (sqlStatement is TCLCommand)
            {
                return(new ShardingDatabaseBroadcastRoutingEngine());
            }
            if (sqlStatement is DDLCommand)
            {
                return(new ShardingTableBroadcastRoutingEngine(metaData.Schema, sqlCommandContext));
            }
            if (sqlStatement is DALCommand)
            {
                return(GetDALRoutingEngine(shardingRule, sqlStatement, tableNames));
            }
            if (sqlStatement is DCLCommand)
            {
                return(GetDCLRoutingEngine(sqlCommandContext, metaData));
            }
            if (shardingRule.IsAllInDefaultDataSource(tableNames))
            {
                return(new ShardingDefaultDatabaseRoutingEngine(tableNames));
            }
            if (shardingRule.IsAllBroadcastTables(tableNames))
            {
                if (sqlStatement is SelectCommand)
                {
                    return(new ShardingUnicastRoutingEngine(tableNames));
                }
                return(new ShardingDatabaseBroadcastRoutingEngine());
            }
            if (sqlCommandContext.GetSqlCommand() is DMLCommand && tableNames.IsEmpty() && shardingRule.HasDefaultDataSourceName())
            {
                return(new ShardingDefaultDatabaseRoutingEngine(tableNames));
            }
            if (sqlCommandContext.GetSqlCommand() is DMLCommand && shardingConditions.IsAlwaysFalse() || tableNames.IsEmpty() || !shardingRule.TableRuleExists(tableNames))
            {
                return(new ShardingUnicastRoutingEngine(tableNames));
            }
            return(GetShardingRoutingEngine(shardingRule, sqlCommandContext, shardingConditions, tableNames, properties));
        }
Exemplo n.º 6
0
 private bool IsNeedMergeShardingValues(ISqlCommandContext <ISqlCommand> sqlStatementContext, ShardingRule shardingRule)
 {
     return(sqlStatementContext is SelectCommandContext selectCommandContext && selectCommandContext.IsContainsSubQuery() &&
            shardingRule.GetShardingLogicTableNames(sqlStatementContext.GetTablesContext().GetTableNames()).Any());
 }