[#179] 将ShardingQueryCompiler继承父类QueryCompiler,而不是直接实现IQueryCompiler,这样可以保证未分片的表使用zzz不需要处理修改还是支持的,仅分片表处理需要特殊考虑

This commit is contained in:
xuejiaming 2022-11-11 14:11:41 +08:00
parent 1d15a8dc6a
commit 9c60ad3924
3 changed files with 67 additions and 4 deletions

View File

@ -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<int> ShardingBulkUpdateAsync2<T>(this IQueryable<T> queryable,
// Expression<Func<T, T>> 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<T>().Where(where)
// .UpdateAsync(updateFactory, cancellationToken: cancellationToken);
// }
//
// return effectRows;
// }
// private static IShardingDbContext GetShardingDbContext<T>(IQueryable<T> 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();
// }
}
}

View File

@ -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

View File

@ -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
/// <summary>
/// 当前查询编译拦截
/// </summary>
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<DbLoggerCategory.Query> 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<DbLoggerCategory.Query> 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<TResult>(Expression query)
{