From 9c60ad3924eec27e4c7b5d1fe8d37db53a6b709a Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Fri, 11 Nov 2022 14:11:41 +0800 Subject: [PATCH] =?UTF-8?q?[#179]=20=E5=B0=86ShardingQueryCompiler?= =?UTF-8?q?=E7=BB=A7=E6=89=BF=E7=88=B6=E7=B1=BBQueryCompiler,=E8=80=8C?= =?UTF-8?q?=E4=B8=8D=E6=98=AF=E7=9B=B4=E6=8E=A5=E5=AE=9E=E7=8E=B0IQueryCom?= =?UTF-8?q?piler,=E8=BF=99=E6=A0=B7=E5=8F=AF=E4=BB=A5=E4=BF=9D=E8=AF=81?= =?UTF-8?q?=E6=9C=AA=E5=88=86=E7=89=87=E7=9A=84=E8=A1=A8=E4=BD=BF=E7=94=A8?= =?UTF-8?q?zzz=E4=B8=8D=E9=9C=80=E8=A6=81=E5=A4=84=E7=90=86=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E8=BF=98=E6=98=AF=E6=94=AF=E6=8C=81=E7=9A=84,?= =?UTF-8?q?=E4=BB=85=E5=88=86=E7=89=87=E8=A1=A8=E5=A4=84=E7=90=86=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E7=89=B9=E6=AE=8A=E8=80=83=E8=99=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- benchmarks/ShardingCoreBenchmark/ZZZDemo.cs | 45 +++++++++++++++++++ nuget-publish.bat | 4 +- .../EFCores/ShardingQueryCompiler.cs | 22 ++++++++- 3 files changed, 67 insertions(+), 4 deletions(-) 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 @@ namespace ShardingCore6x 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.Linq.Expressions; 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) {