public SqlToken GenerateSqlToken(InsertCommandContext sqlCommandContext)
        {
            ICollection <InsertValuesSegment> insertValuesSegments = sqlCommandContext.GetSqlCommand().Values;
            InsertValuesToken result      = new ShardingInsertValuesToken(GetStartIndex(insertValuesSegments), GetStopIndex(insertValuesSegments));
            var originalDataNodesIterator = null == routeContext || routeContext.GetRouteResult().GetOriginalDataNodes().IsEmpty()
                ? null : routeContext.GetRouteResult().GetOriginalDataNodes().GetEnumerator();

            foreach (var insertValueContext in sqlCommandContext.GetInsertValueContexts())
            {
                List <IExpressionSegment> expressionSegments = insertValueContext.GetValueExpressions();
                ICollection <DataNode>    dataNodes          = null == originalDataNodesIterator ? new List <DataNode>(0) : originalDataNodesIterator.Next();
                result.InsertValues.Add(new ShardingInsertValue(expressionSegments, dataNodes));
            }
            return(result);
        }
Esempio n. 2
0
        private ICollection <ExecutionUnit> ExecuteRewrite(string sql, ParameterContext parameterContext, RouteContext routeContext)
        {
            RegisterRewriteDecorator();
            SqlRewriteContext sqlRewriteContext = _rewriter.CreateSqlRewriteContext(sql, parameterContext, routeContext.GetSqlCommandContext(), routeContext);

            return(!routeContext.GetRouteResult().GetRouteUnits().Any() ? Rewrite(sqlRewriteContext) : Rewrite(routeContext, sqlRewriteContext));
        }
Esempio n. 3
0
 public bool IsNeedRewrite(ISqlCommandContext <ISqlCommand> sqlCommandContext)
 {
     return(sqlCommandContext is SelectCommandContext selectCommandContext &&
            selectCommandContext.GetPaginationContext().HasPagination() && !routeContext.GetRouteResult().IsSingleRouting());
 }
Esempio n. 4
0
        private ICollection <ExecutionUnit> Rewrite(RouteContext routeContext, SqlRewriteContext sqlRewriteContext)
        {
            ICollection <ExecutionUnit> result = new LinkedList <ExecutionUnit>();
            var sqlRewriteResults = new SqlRouteRewriteEngine().Rewrite(sqlRewriteContext, routeContext.GetRouteResult());

            foreach (var sqlRewriteResult in sqlRewriteResults)
            {
                result.Add(new ExecutionUnit(sqlRewriteResult.Key.DataSourceMapper.ActualName, new SqlUnit(sqlRewriteResult.Value.Sql, sqlRewriteResult.Value.ParameterContext)));
            }
            return(result);
        }
Esempio n. 5
0
        private void AddSqlTokenGenerator(ICollection <ISqlTokenGenerator> sqlTokenGenerators, ISqlTokenGenerator toBeAddedSqlTokenGenerator)
        {
            if (toBeAddedSqlTokenGenerator is IIgnoreForSingleRoute ignoreForSingleRoute && _routeContext.GetRouteResult().IsSingleRouting())
            {
                return;
            }

            if (toBeAddedSqlTokenGenerator is IShardingRuleAware shardingRuleAware)
            {
                shardingRuleAware.SetShardingRule(_shardingRule);
            }
            if (toBeAddedSqlTokenGenerator is IRouteContextAware routeContextAware)
            {
                routeContextAware.SetRouteContext(_routeContext);
            }
            sqlTokenGenerators.Add(toBeAddedSqlTokenGenerator);
        }