diff --git a/benchmarks/ShardingCoreBenchmark/ZZZDemo.cs b/benchmarks/ShardingCoreBenchmark/ZZZDemo.cs index c5fd470f..5c02ebee 100644 --- a/benchmarks/ShardingCoreBenchmark/ZZZDemo.cs +++ b/benchmarks/ShardingCoreBenchmark/ZZZDemo.cs @@ -1,6 +1,11 @@ using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query.Internal; +using ShardingCore.Core.VirtualRoutes.TableRoutes; +using ShardingCore.EFCores; +using ShardingCore.Exceptions; using ShardingCore.Extensions; +using ShardingCore.Sharding; using ShardingCore.Sharding.Abstractions; using ShardingCore6x.ShardingDbContexts; using Z.EntityFramework.Plus; @@ -70,5 +75,45 @@ public static async Task ShardingBulkUpdateAsync(this DefaultShardingDbC return effectRows; } + // public static async Task ShardingBulkUpdateAsync2(this IQueryable queryable, + // Expression> updateFactory, + // CancellationToken cancellationToken = default(CancellationToken)) where T : class + // { + // var shardingDbContext = GetShardingDbContext(queryable); + // var shardingRuntimeContext = ((DbContext)shardingDbContext).GetShardingRuntimeContext(); + // var tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); + // var routeTailFactory = shardingRuntimeContext.GetRouteTailFactory(); + // var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); + // var tableRouteUnits = tableRouteManager.RouteTo(typeof(T),virtualDataSource.DefaultDataSourceName,new ShardingTableRouteConfig(queryable:queryable)); + // tableRouteUnits.Select(tableRouteUnit => + // { + // var dbContext = shardingDbContext.GetDbContext(tableRouteUnit.DataSourceName, + // CreateDbContextStrategyEnum.ShareConnection, routeTailFactory.Create(tableRouteUnit.Tail)); + // queryable.Expression.re + // }) + // var dbContexts = shardingDbContext.BulkShardingTableExpression(where); + // var effectRows = 0; + // foreach (var dbContext in dbContexts) + // { + // effectRows += await dbContext.Set().Where(where) + // .UpdateAsync(updateFactory, cancellationToken: cancellationToken); + // } + // + // return effectRows; + // } + // private static IShardingDbContext GetShardingDbContext(IQueryable source) + // { + // + // var entityQueryProvider = source.Provider as EntityQueryProvider??throw new ShardingCoreInvalidOperationException($"cant use sharding page that {nameof(IQueryable)} provider not {nameof(EntityQueryProvider)}"); + // + // var shardingQueryCompiler = ObjectExtension.GetFieldValue(entityQueryProvider,"_queryCompiler") as ShardingQueryCompiler??throw new ShardingCoreInvalidOperationException($"cant use sharding page that {nameof(EntityQueryProvider)} not contains {nameof(ShardingQueryCompiler)} filed named _queryCompiler"); + // var dbContextAvailable = shardingQueryCompiler as IShardingDbContextAvailable; + // if (dbContextAvailable == null) + // { + // throw new ShardingCoreInvalidOperationException($"cant use sharding page that {nameof(ShardingQueryCompiler)} not impl {nameof(IShardingDbContextAvailable)}"); + // } + // + // return dbContextAvailable.GetShardingDbContext(); + // } } } \ No newline at end of file diff --git a/nuget-publish.bat b/nuget-publish.bat index 4ff532be..275be2fb 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,7 +1,7 @@ :start ::定义版本 -set SHARDINGCORE7=7.0.0.1 -set SHARDINGCORE2_6=6.8.0.6 +set SHARDINGCORE7=7.0.0.2 +set SHARDINGCORE2_6=6.8.0.7 ::删除所有bin与obj下的文件 @echo off diff --git a/src/ShardingCore/EFCores/ShardingQueryCompiler.cs b/src/ShardingCore/EFCores/ShardingQueryCompiler.cs index 2a4b480d..a21aca8e 100644 --- a/src/ShardingCore/EFCores/ShardingQueryCompiler.cs +++ b/src/ShardingCore/EFCores/ShardingQueryCompiler.cs @@ -11,6 +11,9 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage; using ShardingCore.Core; using ShardingCore.Core.RuntimeContexts; @@ -20,17 +23,32 @@ namespace ShardingCore.EFCores /// /// 当前查询编译拦截 /// - public class ShardingQueryCompiler : IQueryCompiler,IShardingDbContextAvailable + public class ShardingQueryCompiler : QueryCompiler,IShardingDbContextAvailable { private readonly IShardingDbContext _shardingDbContext; private readonly IShardingCompilerExecutor _shardingCompilerExecutor; - public ShardingQueryCompiler(ICurrentDbContext currentContext,IShardingRuntimeContext shardingRuntimeContext) +#if !NETCOREAPP2_0 + public ShardingQueryCompiler(IShardingRuntimeContext shardingRuntimeContext,IQueryContextFactory queryContextFactory, ICompiledQueryCache compiledQueryCache, ICompiledQueryCacheKeyGenerator compiledQueryCacheKeyGenerator, IDatabase database, IDiagnosticsLogger logger, ICurrentDbContext currentContext, IEvaluatableExpressionFilter evaluatableExpressionFilter, IModel model) + : base(queryContextFactory, compiledQueryCache, compiledQueryCacheKeyGenerator, database, logger, currentContext, evaluatableExpressionFilter, model) + { + _shardingDbContext = currentContext.Context as IShardingDbContext ?? + throw new ShardingCoreException("db context operator is not IShardingDbContext"); + _shardingCompilerExecutor = shardingRuntimeContext.GetShardingCompilerExecutor(); + } +#endif +#if NETCOREAPP2_0 + + + public ShardingQueryCompiler(IShardingRuntimeContext shardingRuntimeContext,IQueryContextFactory queryContextFactory, ICompiledQueryCache compiledQueryCache, ICompiledQueryCacheKeyGenerator compiledQueryCacheKeyGenerator, IDatabase database, IDiagnosticsLogger logger, ICurrentDbContext currentContext, IQueryModelGenerator queryModelGenerator) + : base(queryContextFactory, compiledQueryCache, compiledQueryCacheKeyGenerator, database, logger, currentContext, queryModelGenerator) { _shardingDbContext = currentContext.Context as IShardingDbContext ?? throw new ShardingCoreException("db context operator is not IShardingDbContext"); _shardingCompilerExecutor = shardingRuntimeContext.GetShardingCompilerExecutor(); } +#endif + public TResult Execute(Expression query) {