Skip to content

Commit

Permalink
[#179] 将ShardingQueryCompiler继承父类QueryCompiler,而不是直接实现IQueryCompiler,…
Browse files Browse the repository at this point in the history
…这样可以保证未分片的表使用zzz不需要处理修改还是支持的,仅分片表处理需要特殊考虑
  • Loading branch information
xuejmnet committed Nov 11, 2022
1 parent 1d15a8d commit 9c60ad3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
45 changes: 45 additions & 0 deletions benchmarks/ShardingCoreBenchmark/ZZZDemo.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -70,5 +75,45 @@ public static async Task<int> ShardingBulkUpdateAsync<T>(this DefaultShardingDbC

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();
// }
}
}
4 changes: 2 additions & 2 deletions nuget-publish.bat
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 20 additions & 2 deletions src/ShardingCore/EFCores/ShardingQueryCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand Down

0 comments on commit 9c60ad3

Please sign in to comment.