diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 0572a531..76575c25 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -38,13 +38,13 @@ namespace Sample.MySql.Controllers public IQueryable GetAll() { - var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator(); - var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager(); - //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 - var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod)); - //一定要先在路由里面添加尾巴 - virtualTableRoute.Append("20220921"); - shardingTableCreator.CreateTable("ds0","20220921"); + // var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator(); + // var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager(); + // //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可 + // var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod)); + // //一定要先在路由里面添加尾巴 + // virtualTableRoute.Append("20220921"); + // shardingTableCreator.CreateTable("ds0","20220921"); return _defaultTableDbContext.Set(); } @@ -83,7 +83,7 @@ namespace Sample.MySql.Controllers }).ToListAsync(); - var firstOrDefault = _defaultTableDbContext.Set().FromSqlRaw($"select * from {nameof(SysUserMod)}").FirstOrDefault(); + // var firstOrDefault = _defaultTableDbContext.Set().FromSqlRaw($"select * from {nameof(SysUserMod)}").FirstOrDefault(); var sysUserMods1 = _defaultTableDbContext.Set() .Select(o => new ssss(){ Id = o.Id, C = GetAll().Count(x => x.Id == o.Id) }).ToList(); diff --git a/samples/Sample.MySql/Program.cs b/samples/Sample.MySql/Program.cs index b7b33031..49d989cf 100644 --- a/samples/Sample.MySql/Program.cs +++ b/samples/Sample.MySql/Program.cs @@ -13,6 +13,7 @@ namespace Sample.MySql { public static void Main(string[] args) { + var length = "B-SF-C30-190(S4)-D100P6KS60RCM-Ⅳ(2)Q-Ⅴ(3)T-Ⅳ(4)-GB/T 14902".Length; CreateHostBuilder(args).Build().Run(); } diff --git a/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingComparerVirtualDataSourceRoute.cs b/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingComparerVirtualDataSourceRoute.cs new file mode 100644 index 00000000..8c18d021 --- /dev/null +++ b/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingComparerVirtualDataSourceRoute.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using ShardingCore.Core.EntityMetadatas; + +namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions +{ + public abstract class AbstractShardingComparerVirtualDataSourceRouteclass : AbstractShardingOperatorVirtualDataSourceRoute where TEntity : class + { + + protected abstract IComparer GetComparer(); + public override Func GetRouteToFilter(TKey shardingKey, ShardingOperatorEnum shardingOperator) + { + var dataSourceName = ShardingKeyToDataSourceName(shardingKey); + var comparer = GetComparer(); + switch (shardingOperator) + { + case ShardingOperatorEnum.GreaterThan: + case ShardingOperatorEnum.GreaterThanOrEqual: + return dataSource => comparer.Compare(dataSource, dataSourceName) >= 0; + case ShardingOperatorEnum.LessThan: + case ShardingOperatorEnum.LessThanOrEqual: + return dataSource => comparer.Compare(dataSource, dataSourceName) <= 0; + case ShardingOperatorEnum.Equal: return dataSource => comparer.Compare(dataSource, dataSourceName)==0; + default: + { +#if DEBUG + Console.WriteLine($"shardingOperator is not equal scan all table tail"); +#endif + return dataSource => true; + } + } + } + } +} diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingComparerVirtualTableRoute.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingComparerVirtualTableRoute.cs new file mode 100644 index 00000000..20fb6ae8 --- /dev/null +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingComparerVirtualTableRoute.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using ShardingCore.Core.EntityMetadatas; + +namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions +{ + + public abstract class AbstractShardingComparerVirtualTableRoute : AbstractShardingOperatorVirtualTableRoute where TEntity : class + { + protected abstract IComparer GetComparer(); + + public override Func GetRouteToFilter(TKey shardingKey, ShardingOperatorEnum shardingOperator) + { + var t = ShardingKeyToTail(shardingKey); + var comparer = GetComparer(); + switch (shardingOperator) + { + case ShardingOperatorEnum.GreaterThan: + case ShardingOperatorEnum.GreaterThanOrEqual: + return tail => comparer.Compare(tail, t) >= 0; + case ShardingOperatorEnum.LessThan: + case ShardingOperatorEnum.LessThanOrEqual: + return tail => comparer.Compare(tail, t) <= 0; + case ShardingOperatorEnum.Equal: return tail => comparer.Compare(tail, t)==0; + default: + { +#if DEBUG + Console.WriteLine($"shardingOperator is not equal scan all table tail"); +#endif + return tail => true; + } + } + } + } +} \ No newline at end of file diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs index abfc4f7f..f0d46de5 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs @@ -22,7 +22,6 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions /// /// /// - /// /// /// protected override List DoRouteWithPredicate(DataSourceRouteResult dataSourceRouteResult, IQueryable queryable) diff --git a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs index b4245a93..080f66cd 100644 --- a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs +++ b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs @@ -13,6 +13,7 @@ using ShardingCore.Core; using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; +using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Extensions.InternalExtensions; @@ -77,6 +78,9 @@ namespace ShardingCore.EFCores { var clrType = mutableEntityType.ClrType; var entityMetadata = entityMetadataManager.TryGet(clrType); + if (entityMetadata == null) + throw new ShardingCoreInvalidOperationException($"not found entity type:[{clrType}]'s entity metadata"); + var shardingEntity = entityMetadata.EntityType; var tableSeparator = entityMetadata.TableSeparator; var entity = modelBuilder.Entity(shardingEntity); diff --git a/src/ShardingCore/Helpers/DynamicShardingHelper.cs b/src/ShardingCore/Helpers/DynamicShardingHelper.cs index ccb9ecb5..6aae59e4 100644 --- a/src/ShardingCore/Helpers/DynamicShardingHelper.cs +++ b/src/ShardingCore/Helpers/DynamicShardingHelper.cs @@ -132,7 +132,6 @@ namespace ShardingCore.Helpers dbContextOptionBuilder.UseShardingOptions(shardingRuntimeContext); return dbContextOptionBuilder.Options; } - /// /// 动态添加读写分离链接字符串 /// @@ -140,16 +139,16 @@ namespace ShardingCore.Helpers /// /// /// + /// /// - public static void DynamicAppendReadWriteConnectionString(IShardingRuntimeContext shardingRuntimeContext, string dataSourceName, + public static bool DynamicAppendReadWriteConnectionString(IShardingRuntimeContext shardingRuntimeContext, string dataSourceName, string connectionString, string readNodeName=null) { var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); if (virtualDataSource.ConnectionStringManager is IReadWriteConnectionStringManager readWriteAppendConnectionString) { - readWriteAppendConnectionString.AddReadConnectionString(dataSourceName, connectionString, readNodeName); - return; + return readWriteAppendConnectionString.AddReadConnectionString(dataSourceName, connectionString, readNodeName); } throw new ShardingCoreInvalidOperationException(