Ejemplo n.º 1
0
        public RouteContext Decorate(RouteContext routeContext, ShardingConnectorMetaData metaData, ShardingRule shardingRule,
                                     ConfigurationProperties properties)
        {
            var sqlStatementContext = routeContext.GetSqlCommandContext();
            // var tableNames = sqlStatementContext.GetTablesContext().GetTableNames();
            // if (!shardingRule.TableRules.Any(o => tableNames.Any(t => o.LogicTable.EqualsIgnoreCase(t))))
            // {
            //     return routeContext;
            // }
            var parameters = routeContext.GetParameterContext();

            ShardingCommandValidatorFactory.NewInstance(
                sqlStatementContext.GetSqlCommand())
            .IfPresent(validator => validator.Validate(shardingRule, sqlStatementContext.GetSqlCommand(), parameters));
            ShardingConditions shardingConditions = GetShardingConditions(parameters, sqlStatementContext, metaData.Schema, shardingRule);
            var needMergeShardingValues           = IsNeedMergeShardingValues(sqlStatementContext, shardingRule);

            if (needMergeShardingValues && sqlStatementContext.GetSqlCommand() is DMLCommand)
            {
                CheckSubQueryShardingValues(sqlStatementContext, shardingRule, shardingConditions);
                MergeShardingConditions(shardingConditions);
            }
            var         shardingRouteEngine = ShardingRouteEngineFactory.NewInstance(shardingRule, metaData, sqlStatementContext, shardingConditions, properties);
            RouteResult routeResult         = shardingRouteEngine.Route(shardingRule);

            if (needMergeShardingValues)
            {
                ShardingAssert.Else(1 == routeResult.GetRouteUnits().Count, "Must have one sharding with sub query.");
            }
            return(new RouteContext(sqlStatementContext, parameters, routeResult));
        }