From 284a991a6cdf067c4f88334c034ddb0022cf1fa5 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Fri, 25 Aug 2023 22:17:54 +0800 Subject: [PATCH] =?UTF-8?q?[#235]=20=E6=94=AF=E6=8C=81linq=20where?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrderVirtualTableRoute.cs | 4 ++++ .../Controllers/WeatherForecastController.cs | 7 ++++++- .../Sample.MySql/Domain/Maps/SysTestMap.cs | 1 + samples/Sample.MySql/Startup.cs | 20 +++++++++++++++++++ .../Visitors/QueryableRouteDiscoverVisitor.cs | 20 +++++++++++++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/benchmarks/ShardingCoreBenchmark/ShardingDbContexts/OrderVirtualTableRoute.cs b/benchmarks/ShardingCoreBenchmark/ShardingDbContexts/OrderVirtualTableRoute.cs index 52df9fec..21bfeda1 100644 --- a/benchmarks/ShardingCoreBenchmark/ShardingDbContexts/OrderVirtualTableRoute.cs +++ b/benchmarks/ShardingCoreBenchmark/ShardingDbContexts/OrderVirtualTableRoute.cs @@ -21,5 +21,9 @@ namespace ShardingCore6x.ShardingDbContexts builder.ShardingProperty(o => o.Id); } + public override string CompareValueToKey(object shardingKey) + { + return shardingKey.ToString(); + } } } diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 84e9b7f4..f2583440 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -76,6 +76,11 @@ namespace Sample.MySql.Controllers public IQueryable GetAll() { + var dataSourceRouteManager = _shardingRuntimeContext.GetDataSourceRouteManager(); + // dataSourceRouteManager.GetRoute() + var routeManager = _shardingRuntimeContext.GetTableRouteManager(); + // routeManager.GetRoute() + // DynamicShardingHelper.DynamicAppendDataSource(_shardingRuntimeContext,"ds9","链接字符串",true,true); //如果你已经添加好了的情况下并且没有生成对应的库和表想要生成表和库 var dataSourceInitializer = _shardingRuntimeContext.GetDataSourceInitializer(); @@ -84,7 +89,7 @@ namespace Sample.MySql.Controllers // _defaultTableDbContext.ReadWriteSeparationReadOnly();//读库 // _defaultTableDbContext.ReadWriteSeparationWriteOnly();//写库 var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator(); - var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager(); + var tableRouteManager = routeManager; //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 var tableRoute = tableRouteManager.GetRoute(typeof(SysUserMod)); var tails = tableRoute.GetTails(); diff --git a/samples/Sample.MySql/Domain/Maps/SysTestMap.cs b/samples/Sample.MySql/Domain/Maps/SysTestMap.cs index 1e7f65e3..2ee57a61 100644 --- a/samples/Sample.MySql/Domain/Maps/SysTestMap.cs +++ b/samples/Sample.MySql/Domain/Maps/SysTestMap.cs @@ -13,6 +13,7 @@ namespace Sample.MySql.Domain.Maps builder.Property(o => o.Id).IsRequired().HasMaxLength(128); builder.Property(o => o.UserId).IsRequired().HasMaxLength(128); builder.Property(o => o.UserId).IsConcurrencyToken(); + builder.HasIndex(o => new { o.Id, o.UserId }); builder.ToTable(nameof(SysTest),"dbdbd0"); } } diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index ba21cd3f..2e8a9524 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -40,6 +40,25 @@ namespace Sample.MySql // return Task.CompletedTask; // } // } + + // public interface ITenant + // { + // string GetTenantId(); + // } + // + // public class DefaultTenant :ITenant + // { + // private readonly string _tenantId; + // + // public DefaultTenant(string tenantId) + // { + // _tenantId = tenantId; + // } + // public string GetTenantId() + // { + // return _tenantId; + // } + // } public class Startup { public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder => @@ -69,6 +88,7 @@ namespace Sample.MySql // optionsBuilder(sp, builder); // }); // + var type = typeof(MyUserRoute); services.AddDbContext(o => o.UseMySql( diff --git a/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs b/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs index 3f8b2faf..5aa1c128 100644 --- a/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs +++ b/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitor.cs @@ -287,6 +287,26 @@ namespace ShardingCore.Core.Internal.Visitors } } //todo + else if(lambdaExpression.Parameters[0].Type.IsGenericType&&lambdaExpression.Parameters[0].Type.IsAnonymousType()) + { + var typeGenericTypeArguments = lambdaExpression.Parameters[0].Type.GenericTypeArguments; + foreach (var typeGenericTypeArgument in typeGenericTypeArguments) + { + if (typeGenericTypeArgument == _entityMetadata.EntityType) + { + if (_entityLambdaExpression == null) + { + _entityLambdaExpression = lambdaExpression; + } + else + { + var body = Expression.AndAlso(_entityLambdaExpression.Body, lambdaExpression.Body); + var lambda = Expression.Lambda(body, _entityLambdaExpression.Parameters[0]); + _entityLambdaExpression = lambda; + } + } + } + } } } }