From 776b221b3d90a6a033c6a732b19334b94b6b6dad Mon Sep 17 00:00:00 2001 From: xuejmnet <326308290@qq.com> Date: Mon, 21 Feb 2022 22:51:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=83=A8=E5=88=86=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E7=A7=BB=E9=99=A4=E6=8E=A5=E5=8F=A3=E5=88=86=E7=89=87?= =?UTF-8?q?=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/Sample.BulkConsole/Entities/Order.cs | 2 +- .../Domain/Entities/SysUserLogByMonth.cs | 2 +- .../Domain/Entities/SysUserMod.cs | 2 +- .../Domain/Entities/SysUserMod.cs | 2 +- .../Domain/Entities/SysUserSalary.cs | 2 +- .../Domain/Entities/SysUserMod.cs | 2 +- .../Domain/Entities/TestLogByWeek.cs | 2 +- .../Bootstrapers/ShardingBootstrapper.cs | 8 +- src/ShardingCore/Core/IPrint.cs | 15 +++ src/ShardingCore/Core/IShardingDataSource.cs | 1 + src/ShardingCore/Core/IShardingTable.cs | 1 + .../RouteRuleEngine/DataSourceRouteResult.cs | 7 +- .../RoutingRuleEngine/TableRouteResult.cs | 7 +- .../Helpers/EntityMetadataHelper.cs | 98 +++++++++---------- .../ShardingExecutors/CompileParameter.cs | 7 +- .../DefaultShardingComplierExecutor.cs | 9 +- .../QueryCompilerContextFactory.cs | 17 +++- .../Domain/Entities/Order.cs | 2 +- .../Domain/Entities/Order.cs | 2 +- .../Domain/Entities/Order.cs | 2 +- .../Domain/Entities/Order.cs | 2 +- 21 files changed, 121 insertions(+), 71 deletions(-) create mode 100644 src/ShardingCore/Core/IPrint.cs diff --git a/samples/Sample.BulkConsole/Entities/Order.cs b/samples/Sample.BulkConsole/Entities/Order.cs index e4b3d772..0a811309 100644 --- a/samples/Sample.BulkConsole/Entities/Order.cs +++ b/samples/Sample.BulkConsole/Entities/Order.cs @@ -10,7 +10,7 @@ namespace Sample.BulkConsole.Entities * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class Order:IShardingTable + public class Order { public string Id { get; set; } public string OrderNo { get; set; } diff --git a/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs b/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs index 673d316a..17bffa70 100644 --- a/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs +++ b/samples/Sample.MySql/Domain/Entities/SysUserLogByMonth.cs @@ -6,7 +6,7 @@ using ShardingCore.Core; namespace Sample.MySql.Domain.Entities { - public class SysUserLogByMonth : IShardingTable + public class SysUserLogByMonth { public string Id { get; set; } [ShardingTableKey] diff --git a/samples/Sample.MySql/Domain/Entities/SysUserMod.cs b/samples/Sample.MySql/Domain/Entities/SysUserMod.cs index dec9cb46..e55a0609 100644 --- a/samples/Sample.MySql/Domain/Entities/SysUserMod.cs +++ b/samples/Sample.MySql/Domain/Entities/SysUserMod.cs @@ -8,7 +8,7 @@ namespace Sample.MySql.Domain.Entities * @Date: Tuesday, 26 January 2021 12:25:39 * @Email: 326308290@qq.com */ - public class SysUserMod:IShardingTable + public class SysUserMod { /// /// 用户Id用于分表 diff --git a/samples/Sample.SqlServer/Domain/Entities/SysUserMod.cs b/samples/Sample.SqlServer/Domain/Entities/SysUserMod.cs index 520762d7..b82a2c3f 100644 --- a/samples/Sample.SqlServer/Domain/Entities/SysUserMod.cs +++ b/samples/Sample.SqlServer/Domain/Entities/SysUserMod.cs @@ -12,7 +12,7 @@ namespace Sample.SqlServer.Domain.Entities /// /// 用户表 /// - public class SysUserMod : IShardingTable + public class SysUserMod { /// /// 用户Id用于分表 diff --git a/samples/Sample.SqlServer/Domain/Entities/SysUserSalary.cs b/samples/Sample.SqlServer/Domain/Entities/SysUserSalary.cs index ed48f076..ea33df96 100644 --- a/samples/Sample.SqlServer/Domain/Entities/SysUserSalary.cs +++ b/samples/Sample.SqlServer/Domain/Entities/SysUserSalary.cs @@ -8,7 +8,7 @@ namespace Sample.SqlServer.Domain.Entities * @Date: Monday, 01 February 2021 15:43:22 * @Email: 326308290@qq.com */ - public class SysUserSalary:IShardingTable + public class SysUserSalary { public string Id { get; set; } public string UserId { get; set; } diff --git a/samples/Sample.SqlServer3x/Domain/Entities/SysUserMod.cs b/samples/Sample.SqlServer3x/Domain/Entities/SysUserMod.cs index fbd0b423..580bffef 100644 --- a/samples/Sample.SqlServer3x/Domain/Entities/SysUserMod.cs +++ b/samples/Sample.SqlServer3x/Domain/Entities/SysUserMod.cs @@ -8,7 +8,7 @@ namespace Sample.SqlServer3x.Domain.Entities * @Date: Thursday, 14 January 2021 15:36:43 * @Email: 326308290@qq.com */ - public class SysUserMod:IShardingTable + public class SysUserMod { /// /// 用户Id用于分表 diff --git a/samples/Samples.AutoByDate.SqlServer/Domain/Entities/TestLogByWeek.cs b/samples/Samples.AutoByDate.SqlServer/Domain/Entities/TestLogByWeek.cs index d5b78c00..c1f5a6a8 100644 --- a/samples/Samples.AutoByDate.SqlServer/Domain/Entities/TestLogByWeek.cs +++ b/samples/Samples.AutoByDate.SqlServer/Domain/Entities/TestLogByWeek.cs @@ -6,7 +6,7 @@ using ShardingCore.Core; namespace Samples.AutoByDate.SqlServer.Domain.Entities { - public class TestLogByWeek:IShardingTable + public class TestLogByWeek { public string Id { get; set; } [ShardingTableKey] diff --git a/src/ShardingCore/Bootstrapers/ShardingBootstrapper.cs b/src/ShardingCore/Bootstrapers/ShardingBootstrapper.cs index 3b7f4d79..ce20d912 100644 --- a/src/ShardingCore/Bootstrapers/ShardingBootstrapper.cs +++ b/src/ShardingCore/Bootstrapers/ShardingBootstrapper.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using ShardingCore.Extensions; using ShardingCore.Jobs; using ShardingCore.Jobs.Abstaractions; @@ -17,11 +18,13 @@ namespace ShardingCore.Bootstrapers */ public class ShardingBootstrapper : IShardingBootstrapper { + private readonly ILogger _logger; private readonly IEnumerable _dbContextTypeCollectors; private readonly DoOnlyOnce _doOnlyOnce = new DoOnlyOnce(); - public ShardingBootstrapper(IServiceProvider serviceProvider,IEnumerable dbContextTypeCollectors) + public ShardingBootstrapper(IServiceProvider serviceProvider,ILogger logger,IEnumerable dbContextTypeCollectors) { + _logger = logger; _dbContextTypeCollectors = dbContextTypeCollectors; ShardingContainer.SetServices(serviceProvider); } @@ -32,10 +35,13 @@ namespace ShardingCore.Bootstrapers { if (!_doOnlyOnce.IsUnDo()) return; + _logger.LogDebug("sharding core starting......"); foreach (var dbContextTypeCollector in _dbContextTypeCollectors) { var instance = (IShardingDbContextBootstrapper)ShardingContainer.CreateInstance(typeof(ShardingDbContextBootstrapper<>).GetGenericType0(dbContextTypeCollector.ShardingDbContextType)); + _logger.LogDebug($"{dbContextTypeCollector.ShardingDbContextType} start init......"); instance.Init(); + _logger.LogDebug($"{dbContextTypeCollector.ShardingDbContextType} complete init"); } var jobManager = ShardingContainer.GetService(); diff --git a/src/ShardingCore/Core/IPrint.cs b/src/ShardingCore/Core/IPrint.cs new file mode 100644 index 00000000..4bbcef0f --- /dev/null +++ b/src/ShardingCore/Core/IPrint.cs @@ -0,0 +1,15 @@ +using System; + +namespace ShardingCore.Core +{ +/* +* @Author: xjm +* @Description: +* @Date: Monday, 21 February 2022 22:33:17 +* @Email: 326308290@qq.com +*/ + public interface IPrint + { + string GetPrintInfo(); + } +} \ No newline at end of file diff --git a/src/ShardingCore/Core/IShardingDataSource.cs b/src/ShardingCore/Core/IShardingDataSource.cs index 3b4c56f0..7fcc17a4 100644 --- a/src/ShardingCore/Core/IShardingDataSource.cs +++ b/src/ShardingCore/Core/IShardingDataSource.cs @@ -10,6 +10,7 @@ namespace ShardingCore.Core * @Ver: 1.0 * @Email: 326308290@qq.com */ + [Obsolete("will remove")] public interface IShardingDataSource { } diff --git a/src/ShardingCore/Core/IShardingTable.cs b/src/ShardingCore/Core/IShardingTable.cs index e043b540..c6c73e89 100644 --- a/src/ShardingCore/Core/IShardingTable.cs +++ b/src/ShardingCore/Core/IShardingTable.cs @@ -11,6 +11,7 @@ namespace ShardingCore.Core * @Ver: 1.0 * @Email: 326308290@qq.com */ + [Obsolete("will remove")] public interface IShardingTable { } diff --git a/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/RouteRuleEngine/DataSourceRouteResult.cs b/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/RouteRuleEngine/DataSourceRouteResult.cs index b55f0ab6..c4345203 100644 --- a/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/RouteRuleEngine/DataSourceRouteResult.cs +++ b/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/RouteRuleEngine/DataSourceRouteResult.cs @@ -12,7 +12,7 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class DataSourceRouteResult + public class DataSourceRouteResult:IPrint { public DataSourceRouteResult(ISet intersectDataSources) { @@ -22,5 +22,10 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine /// 交集 /// public ISet IntersectDataSources { get; } + + public string GetPrintInfo() + { + return $"data source route result:{string.Join(",", IntersectDataSources)}"; + } } } diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteResult.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteResult.cs index 7b7d23dc..67b762b4 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteResult.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RoutingRuleEngine/TableRouteResult.cs @@ -13,7 +13,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine * @Date: Thursday, 28 January 2021 10:18:09 * @Email: 326308290@qq.com */ - public class TableRouteResult + public class TableRouteResult:IPrint { public TableRouteResult(IEnumerable replaceTables, Type shardingDbContextType) { @@ -39,6 +39,10 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine if (obj.GetType() != this.GetType()) return false; return Equals((TableRouteResult)obj); } + public string GetPrintInfo() + { + return $"table route result:has different tail:{HasDifferentTail},current table:[{string.Join(",", ReplaceTables.Select(o => o.FullName))}]"; + } #if !EFCORE2 @@ -46,6 +50,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine { return HashCode.Combine(ReplaceTables, ShardingDbContextType); } + #endif #if EFCORE2 diff --git a/src/ShardingCore/Helpers/EntityMetadataHelper.cs b/src/ShardingCore/Helpers/EntityMetadataHelper.cs index b9e3ea1d..503568ed 100644 --- a/src/ShardingCore/Helpers/EntityMetadataHelper.cs +++ b/src/ShardingCore/Helpers/EntityMetadataHelper.cs @@ -10,79 +10,73 @@ namespace ShardingCore.Helpers { private EntityMetadataHelper() { - } public static void Configure(EntityMetadataTableBuilder builder) where TEntity : class { var entityType = typeof(TEntity); - if (typeof(IShardingTable).IsAssignableFrom(entityType)) + + PropertyInfo[] shardingProperties = entityType.GetProperties(); + var shardingTableCount = 0; + foreach (var shardingProperty in shardingProperties) { - - PropertyInfo[] shardingProperties = entityType.GetProperties(); - var shardingTableCount = 0; - foreach (var shardingProperty in shardingProperties) + var attributes = shardingProperty.GetCustomAttributes(true); + if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingTableKeyAttribute)) is ShardingTableKeyAttribute shardingKey) { - var attributes = shardingProperty.GetCustomAttributes(true); - if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingTableKeyAttribute)) is ShardingTableKeyAttribute shardingKey) - { - if (shardingTableCount > 1) - throw new NotSupportedException($"{entityType} impl {nameof(IShardingTable)} should use single attribute [{nameof(ShardingTableKeyAttribute)}]"); + if (shardingTableCount > 1) + throw new NotSupportedException($"{entityType} should use single attribute [{nameof(ShardingTableKeyAttribute)}]"); - builder.ShardingProperty(shardingProperty.Name); - var autoCreateTable = - shardingKey.AutoCreateTableOnStart == ShardingKeyAutoCreateTableEnum.UnKnown - ? (bool?) null - : (shardingKey.AutoCreateTableOnStart == ShardingKeyAutoCreateTableEnum.Create); - builder.AutoCreateTable(autoCreateTable); - builder.TableSeparator(shardingKey.TableSeparator); - shardingTableCount++; - } + builder.ShardingProperty(shardingProperty.Name); + var autoCreateTable = + shardingKey.AutoCreateTableOnStart == ShardingKeyAutoCreateTableEnum.UnKnown + ? (bool?)null + : (shardingKey.AutoCreateTableOnStart == ShardingKeyAutoCreateTableEnum.Create); + builder.AutoCreateTable(autoCreateTable); + builder.TableSeparator(shardingKey.TableSeparator); + shardingTableCount++; + } - if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraTableKeyAttribute)) is - ShardingExtraTableKeyAttribute shardingExtraKey) - { - builder.ShardingExtraProperty(shardingProperty.Name); - } + if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraTableKeyAttribute)) is + ShardingExtraTableKeyAttribute shardingExtraKey) + { + builder.ShardingExtraProperty(shardingProperty.Name); } } } + public static void Configure(EntityMetadataDataSourceBuilder builder) where TEntity : class { var entityType = typeof(TEntity); - if (typeof(IShardingDataSource).IsAssignableFrom(entityType)) + + PropertyInfo[] shardingProperties = entityType.GetProperties(); + + + var shardingDataSourceCount = 0; + foreach (var shardingProperty in shardingProperties) { - - PropertyInfo[] shardingProperties = entityType.GetProperties(); - - - var shardingDataSourceCount = 0; - foreach (var shardingProperty in shardingProperties) + var attributes = shardingProperty.GetCustomAttributes(true); + if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingDataSourceKeyAttribute)) is ShardingDataSourceKeyAttribute shardingDataSourceKey) { - var attributes = shardingProperty.GetCustomAttributes(true); - if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingDataSourceKeyAttribute)) is ShardingDataSourceKeyAttribute shardingDataSourceKey) - { - if (shardingDataSourceCount > 1) - throw new NotSupportedException($"{entityType} impl {nameof(IShardingDataSource)} should use single attribute [{nameof(ShardingDataSourceKeyAttribute)}]"); - builder.ShardingProperty(shardingProperty.Name); + if (shardingDataSourceCount > 1) + throw new NotSupportedException($"{entityType} should use single attribute [{nameof(ShardingDataSourceKeyAttribute)}]"); + builder.ShardingProperty(shardingProperty.Name); - var autoCreateDataSource = shardingDataSourceKey.AutoCreateDataSourceTableOnStart == - ShardingKeyAutoCreateTableEnum.UnKnown - ? (bool?)null - : (shardingDataSourceKey.AutoCreateDataSourceTableOnStart == - ShardingKeyAutoCreateTableEnum.Create); + var autoCreateDataSource = shardingDataSourceKey.AutoCreateDataSourceTableOnStart == + ShardingKeyAutoCreateTableEnum.UnKnown + ? (bool?)null + : (shardingDataSourceKey.AutoCreateDataSourceTableOnStart == + ShardingKeyAutoCreateTableEnum.Create); - builder.AutoCreateDataSource(autoCreateDataSource); - shardingDataSourceCount++; - } - if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraDataSourceKeyAttribute)) is - ShardingExtraDataSourceKeyAttribute shardingExtraKey) - { - builder.ShardingExtraProperty(shardingProperty.Name); - } + builder.AutoCreateDataSource(autoCreateDataSource); + shardingDataSourceCount++; + } + if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraDataSourceKeyAttribute)) is + ShardingExtraDataSourceKeyAttribute shardingExtraKey) + { + builder.ShardingExtraProperty(shardingProperty.Name); } } } } -} +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/ShardingExecutors/CompileParameter.cs b/src/ShardingCore/Sharding/ShardingExecutors/CompileParameter.cs index 16129cab..968669d3 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/CompileParameter.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/CompileParameter.cs @@ -16,7 +16,7 @@ using ShardingCore.Sharding.Visitors.ShardingExtractParameters; */ namespace ShardingCore.ShardingExecutors { - public class CompileParameter:ICompileParameter + public class CompileParameter:ICompileParameter,IPrint { private readonly IShardingDbContext _shardingDbContext; private readonly Expression _nativeQueryExpression; @@ -91,5 +91,10 @@ namespace ShardingCore.ShardingExecutors { return _sameWithShardingComparer; } + + public string GetPrintInfo() + { + return $"is not support :{_isNotSupport},max query connections limit:{_maxQueryConnectionsLimit},connection mode:{_connectionMode},readonly:{_readOnly},as route:{_shardingRouteConfigure!=null},is sequence:{_isSequence},same with sharding comparer:{_sameWithShardingComparer}"; + } } } \ No newline at end of file diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingComplierExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingComplierExecutor.cs index 2d055977..9917f328 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingComplierExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingComplierExecutor.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using ShardingCore.Extensions; using ShardingCore.Sharding.ShardingExecutors.Abstractions; using ShardingCore.Sharding.Visitors.ShardingExtractParameters; @@ -12,17 +13,20 @@ namespace ShardingCore.Sharding.ShardingExecutors { public class DefaultShardingComplierExecutor: IShardingComplierExecutor { + private readonly ILogger _logger; private readonly IShardingTrackQueryExecutor _shardingTrackQueryExecutor; private readonly IQueryCompilerContextFactory _queryCompilerContextFactory; - public DefaultShardingComplierExecutor(IShardingTrackQueryExecutor shardingTrackQueryExecutor, IQueryCompilerContextFactory queryCompilerContextFactory) + public DefaultShardingComplierExecutor(ILogger logger,IShardingTrackQueryExecutor shardingTrackQueryExecutor, IQueryCompilerContextFactory queryCompilerContextFactory) { + _logger = logger; _shardingTrackQueryExecutor = shardingTrackQueryExecutor; _queryCompilerContextFactory = queryCompilerContextFactory; } public TResult Execute(IShardingDbContext shardingDbContext, Expression query) { var compileParameter = new CompileParameter(shardingDbContext,query); + _logger.LogDebug(compileParameter.GetPrintInfo()); using (new CustomerQueryScope(compileParameter)) { var queryCompilerContext = _queryCompilerContextFactory.Create(compileParameter); @@ -38,6 +42,7 @@ namespace ShardingCore.Sharding.ShardingExecutors CancellationToken cancellationToken = new CancellationToken()) { var compileParameter = new CompileParameter(shardingDbContext,query); + _logger.LogDebug(compileParameter.GetPrintInfo()); using (new CustomerQueryScope(compileParameter)) { @@ -51,6 +56,7 @@ namespace ShardingCore.Sharding.ShardingExecutors public IAsyncEnumerable ExecuteAsync(IShardingDbContext shardingDbContext, Expression query) { var compileParameter = new CompileParameter(shardingDbContext,query); + _logger.LogDebug(compileParameter.GetPrintInfo()); using (new CustomerQueryScope(compileParameter)) { var queryCompilerContext = _queryCompilerContextFactory.Create(compileParameter); @@ -62,6 +68,7 @@ namespace ShardingCore.Sharding.ShardingExecutors CancellationToken cancellationToken) { var compileParameter = new CompileParameter(shardingDbContext,query); + _logger.LogDebug(compileParameter.GetPrintInfo()); using (new CustomerQueryScope(compileParameter)) { var queryCompilerContext = _queryCompilerContextFactory.Create(compileParameter); diff --git a/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs b/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs index d8650230..86e239d2 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs @@ -5,6 +5,7 @@ using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore.Query; +using Microsoft.Extensions.Logging; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine; using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; using ShardingCore.Exceptions; @@ -18,6 +19,7 @@ namespace ShardingCore.Sharding.ShardingExecutors { public class QueryCompilerContextFactory : IQueryCompilerContextFactory { + private readonly ILogger _logger; private static readonly IQueryableCombine _enumerableQueryableCombine; private static readonly IQueryableCombine _allQueryableCombine; private static readonly IQueryableCombine _constantQueryableCombine; @@ -33,23 +35,32 @@ namespace ShardingCore.Sharding.ShardingExecutors _whereQueryableCombine = new WhereQueryableCombine(); } + public QueryCompilerContextFactory(ILogger logger) + { + _logger = logger; + } + public IQueryCompilerContext Create(ICompileParameter compileParameter) { var queryCompilerContext = QueryCompilerContext.Create(compileParameter); if (queryCompilerContext.GetQueryCompilerExecutor() is not null) { + _logger.LogDebug($"{queryCompilerContext.GetQueryExpression().ShardingPrint()} is native query"); return queryCompilerContext; } var queryableCombine = GetQueryableCombine(queryCompilerContext); - + _logger.LogDebug($"queryable combine:{queryableCombine.GetType()}"); var dataSourceRouteRuleEngineFactory = (IDataSourceRouteRuleEngineFactory)ShardingContainer.GetService(typeof(IDataSourceRouteRuleEngineFactory<>).GetGenericType0(queryCompilerContext.GetShardingDbContextType())); var tableRouteRuleEngineFactory = (ITableRouteRuleEngineFactory)ShardingContainer.GetService(typeof(ITableRouteRuleEngineFactory<>).GetGenericType0(queryCompilerContext.GetShardingDbContextType())); + _logger.LogDebug($"queryable combine before:{queryCompilerContext.GetQueryExpression().ShardingPrint()}"); var queryCombineResult = queryableCombine.Combine(queryCompilerContext); + _logger.LogDebug($"queryable combine after:{queryCombineResult.GetCombineQueryable().ShardingPrint()}"); var dataSourceRouteResult = dataSourceRouteRuleEngineFactory.Route(queryCombineResult.GetCombineQueryable(), compileParameter.GetShardingDbContext()); - var tableRouteResults = tableRouteRuleEngineFactory.Route(queryCombineResult.GetCombineQueryable()); - var routeResults = tableRouteResults as TableRouteResult[] ?? tableRouteResults.ToArray(); + _logger.LogDebug(dataSourceRouteResult.GetPrintInfo()); + var routeResults = tableRouteRuleEngineFactory.Route(queryCombineResult.GetCombineQueryable()).ToArray(); + _logger.LogDebug($"table route results:{string.Join(",",routeResults.Select(o=>o.GetPrintInfo()))}"); var mergeCombineCompilerContext = MergeQueryCompilerContext.Create(queryCompilerContext, queryCombineResult, dataSourceRouteResult, routeResults); return mergeCombineCompilerContext; diff --git a/test/ShardingCore.Test/Domain/Entities/Order.cs b/test/ShardingCore.Test/Domain/Entities/Order.cs index b12a8701..220dd839 100644 --- a/test/ShardingCore.Test/Domain/Entities/Order.cs +++ b/test/ShardingCore.Test/Domain/Entities/Order.cs @@ -3,7 +3,7 @@ using ShardingCore.Core; namespace ShardingCore.Test.Domain.Entities { - public class Order:IShardingDataSource,IShardingTable + public class Order { public Guid Id { get; set; } [ShardingDataSourceKey] diff --git a/test/ShardingCore.Test2x/Domain/Entities/Order.cs b/test/ShardingCore.Test2x/Domain/Entities/Order.cs index c9b3042a..e39b8efd 100644 --- a/test/ShardingCore.Test2x/Domain/Entities/Order.cs +++ b/test/ShardingCore.Test2x/Domain/Entities/Order.cs @@ -3,7 +3,7 @@ using ShardingCore.Core; namespace ShardingCore.Test2x.Domain.Entities { - public class Order:IShardingDataSource,IShardingTable + public class Order { public Guid Id { get; set; } [ShardingDataSourceKey] diff --git a/test/ShardingCore.Test3x/Domain/Entities/Order.cs b/test/ShardingCore.Test3x/Domain/Entities/Order.cs index 421446be..f9967c82 100644 --- a/test/ShardingCore.Test3x/Domain/Entities/Order.cs +++ b/test/ShardingCore.Test3x/Domain/Entities/Order.cs @@ -3,7 +3,7 @@ using ShardingCore.Core; namespace ShardingCore.Test3x.Domain.Entities { - public class Order:IShardingDataSource,IShardingTable + public class Order { public Guid Id { get; set; } [ShardingDataSourceKey] diff --git a/test/ShardingCore.Test5x/Domain/Entities/Order.cs b/test/ShardingCore.Test5x/Domain/Entities/Order.cs index 6849544f..01faa08d 100644 --- a/test/ShardingCore.Test5x/Domain/Entities/Order.cs +++ b/test/ShardingCore.Test5x/Domain/Entities/Order.cs @@ -3,7 +3,7 @@ using ShardingCore.Core; namespace ShardingCore.Test5x.Domain.Entities { - public class Order:IShardingDataSource,IShardingTable + public class Order { public Guid Id { get; set; } [ShardingDataSourceKey]