diff --git a/nuget-publish.bat b/nuget-publish.bat index 9ea4931b..3104163f 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,9 +1,9 @@ :start ::定义版本 -set EFCORE2=2.3.2.00-preview1 -set EFCORE3=3.3.2.00-preview1 -set EFCORE5=5.3.2.00-preview1 -set EFCORE6=6.3.2.00-preview1 +set EFCORE2=2.3.2.00-preview3 +set EFCORE3=3.3.2.00-preview3 +set EFCORE5=5.3.2.00-preview3 +set EFCORE6=6.3.2.00-preview3 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.SqlServerShardingAll/VirtualDataSourceRoutes/OrderVirtualDataSourceRoute.cs b/samples/Sample.SqlServerShardingAll/VirtualDataSourceRoutes/OrderVirtualDataSourceRoute.cs index dea68b0d..c062a331 100644 --- a/samples/Sample.SqlServerShardingAll/VirtualDataSourceRoutes/OrderVirtualDataSourceRoute.cs +++ b/samples/Sample.SqlServerShardingAll/VirtualDataSourceRoutes/OrderVirtualDataSourceRoute.cs @@ -34,7 +34,7 @@ namespace Sample.SqlServerShardingAll.VirtualDataSourceRoutes return true; } - public override Expression> GetMainRouteFilter(string shardingKey, ShardingOperatorEnum shardingOperator) + public override Expression> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) { var t = ShardingKeyToDataSourceName(shardingKey); diff --git a/samples/Sample.SqlServerShardingAll/VirtualDataSourceRoutes/SysUserVirtualDataSourceRoute.cs b/samples/Sample.SqlServerShardingAll/VirtualDataSourceRoutes/SysUserVirtualDataSourceRoute.cs index c643a56c..7103707d 100644 --- a/samples/Sample.SqlServerShardingAll/VirtualDataSourceRoutes/SysUserVirtualDataSourceRoute.cs +++ b/samples/Sample.SqlServerShardingAll/VirtualDataSourceRoutes/SysUserVirtualDataSourceRoute.cs @@ -34,7 +34,7 @@ namespace Sample.SqlServerShardingAll.VirtualDataSourceRoutes return true; } - public override Expression> GetMainRouteFilter(string shardingKey, ShardingOperatorEnum shardingOperator) + public override Expression> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) { var t = ShardingKeyToDataSourceName(shardingKey); diff --git a/samples/Sample.SqlServerShardingDataSource/VirtualRoutes/OrderVirtualDataSourceRoute.cs b/samples/Sample.SqlServerShardingDataSource/VirtualRoutes/OrderVirtualDataSourceRoute.cs index 45e35ae4..0f552543 100644 --- a/samples/Sample.SqlServerShardingDataSource/VirtualRoutes/OrderVirtualDataSourceRoute.cs +++ b/samples/Sample.SqlServerShardingDataSource/VirtualRoutes/OrderVirtualDataSourceRoute.cs @@ -34,7 +34,7 @@ namespace Sample.SqlServerShardingDataSource.VirtualRoutes return true; } - public override Expression> GetMainRouteFilter(string shardingKey, ShardingOperatorEnum shardingOperator) + public override Expression> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) { var t = ShardingKeyToDataSourceName(shardingKey); diff --git a/samples/Sample.SqlServerShardingDataSource/VirtualRoutes/SysUserVirtualDataSourceRoute.cs b/samples/Sample.SqlServerShardingDataSource/VirtualRoutes/SysUserVirtualDataSourceRoute.cs index bdc69c49..9197b878 100644 --- a/samples/Sample.SqlServerShardingDataSource/VirtualRoutes/SysUserVirtualDataSourceRoute.cs +++ b/samples/Sample.SqlServerShardingDataSource/VirtualRoutes/SysUserVirtualDataSourceRoute.cs @@ -34,7 +34,7 @@ namespace Sample.SqlServerShardingDataSource.VirtualRoutes return true; } - public override Expression> GetMainRouteFilter(string shardingKey, ShardingOperatorEnum shardingOperator) + public override Expression> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) { var t = ShardingKeyToDataSourceName(shardingKey); diff --git a/samples/Sample.SqlServerShardingTable/VirtualRoutes/MultiShardingOrderVirtualTableRoute.cs b/samples/Sample.SqlServerShardingTable/VirtualRoutes/MultiShardingOrderVirtualTableRoute.cs index 18a86df5..e79f9d2a 100644 --- a/samples/Sample.SqlServerShardingTable/VirtualRoutes/MultiShardingOrderVirtualTableRoute.cs +++ b/samples/Sample.SqlServerShardingTable/VirtualRoutes/MultiShardingOrderVirtualTableRoute.cs @@ -29,7 +29,7 @@ namespace Sample.SqlServerShardingTable.VirtualRoutes private Expression> GetIdRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator) { - //解析雪花id 需要考虑异常情况,传入的可能不是雪花id那么可以随机查询一张表 + //解析雪花id 需要考虑异常情况,传入的可能不是雪花id那么可以直接返回false因为是and链接所以直接就没有结果了//return tail => false; var analyzeIdToDateTime = SnowflakeId.AnalyzeIdToDateTime(Convert.ToInt64(shardingKey)); //当前时间的tail var t = TimeFormatToTail(analyzeIdToDateTime); diff --git a/src/ShardingCore/Core/ShardingExtraDataSourceKeyAttribute.cs b/src/ShardingCore/Core/ShardingExtraDataSourceKeyAttribute.cs new file mode 100644 index 00000000..2c1aa79b --- /dev/null +++ b/src/ShardingCore/Core/ShardingExtraDataSourceKeyAttribute.cs @@ -0,0 +1,18 @@ +using System; + +namespace ShardingCore.Core +{ +/* +* @Author: xjm +* @Description:用户数据库分数据源时进行判断 +* @Date: Friday, 05 February 2021 12:53:46 +* @Email: 326308290@qq.com +*/ + /// + /// 数据源分库额外规则字段 + /// + [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] + public class ShardingExtraDataSourceKeyAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/src/ShardingCore/Core/ShardingExtraTableKeyAttribute.cs b/src/ShardingCore/Core/ShardingExtraTableKeyAttribute.cs new file mode 100644 index 00000000..677454ad --- /dev/null +++ b/src/ShardingCore/Core/ShardingExtraTableKeyAttribute.cs @@ -0,0 +1,18 @@ +using System; + +namespace ShardingCore.Core +{ +/* +* @Author: xjm +* @Description: +* @Date: Wednesday, 16 December 2020 11:04:51 +* @Email: 326308290@qq.com +*/ + /// + /// 分片额外配置 + /// + [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] + public class ShardingExtraTableKeyAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingOperatorVirtualDataSourceRoute.cs b/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingOperatorVirtualDataSourceRoute.cs index a31505c2..167eecf2 100644 --- a/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingOperatorVirtualDataSourceRoute.cs +++ b/src/ShardingCore/Core/VirtualRoutes/DataSourceRoutes/Abstractions/AbstractShardingOperatorVirtualDataSourceRoute.cs @@ -27,7 +27,7 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions protected override List DoRouteWithPredicate(List allDataSourceNames, IQueryable queryable) { //获取路由后缀表达式 - var routeParseExpression = ShardingUtil.GetRouteParseExpression(queryable, EntityMetadata, GetRouteToFilter, false); + var routeParseExpression = ShardingUtil.GetRouteParseExpression(queryable, EntityMetadata, GetRouteFilter, false); //表达式缓存编译 var filter = CachingCompile(routeParseExpression); //通过编译结果进行过滤 @@ -43,12 +43,12 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions /// 操作 /// 操作 /// 如果返回true表示返回该表 第一个参数 tail 第二参数是否返回该物理表 - public Expression> GetRouteToFilter(object shardingKey, + public virtual Expression> GetRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName) { if (EntityMetadata.IsMainShardingDataSourceKey(shardingPropertyName)) { - return GetMainRouteFilter((TKey)shardingKey, shardingOperator); + return GetRouteToFilter((TKey)shardingKey, shardingOperator); } else { @@ -56,7 +56,7 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions } } - public abstract Expression> GetMainRouteFilter(TKey shardingKey, + public abstract Expression> GetRouteToFilter(TKey shardingKey, ShardingOperatorEnum shardingOperator); public virtual Expression> GetExtraRouteFilter(object shardingKey, diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs index d36fc3a8..b085da66 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/Abstractions/AbstractShardingOperatorVirtualTableRoute.cs @@ -37,7 +37,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions /// 操作 /// 分表字段 /// 如果返回true表示返回该表 第一个参数 tail 第二参数是否返回该物理表 - public Expression> GetRouteFilter(object shardingKey, + public virtual Expression> GetRouteFilter(object shardingKey, ShardingOperatorEnum shardingOperator, string shardingPropertyName) { if (EntityMetadata.IsMainShardingTableKey(shardingPropertyName)) diff --git a/src/ShardingCore/Helpers/EntityMetadataHelper.cs b/src/ShardingCore/Helpers/EntityMetadataHelper.cs index bbd39b1c..b9e3ea1d 100644 --- a/src/ShardingCore/Helpers/EntityMetadataHelper.cs +++ b/src/ShardingCore/Helpers/EntityMetadataHelper.cs @@ -38,6 +38,12 @@ namespace ShardingCore.Helpers builder.TableSeparator(shardingKey.TableSeparator); shardingTableCount++; } + + if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraTableKeyAttribute)) is + ShardingExtraTableKeyAttribute shardingExtraKey) + { + builder.ShardingExtraProperty(shardingProperty.Name); + } } } } @@ -69,6 +75,11 @@ namespace ShardingCore.Helpers builder.AutoCreateDataSource(autoCreateDataSource); shardingDataSourceCount++; } + if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraDataSourceKeyAttribute)) is + ShardingExtraDataSourceKeyAttribute shardingExtraKey) + { + builder.ShardingExtraProperty(shardingProperty.Name); + } } } diff --git a/test/ShardingCore.Test/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs b/test/ShardingCore.Test/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs index 54e17b7e..7a5a2cd2 100644 --- a/test/ShardingCore.Test/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs +++ b/test/ShardingCore.Test/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs @@ -42,7 +42,7 @@ namespace ShardingCore.Test.Shardings } - public override Expression> GetMainRouteFilter(string shardingKey, ShardingOperatorEnum shardingOperator) + public override Expression> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) { var t = ShardingKeyToDataSourceName(shardingKey); diff --git a/test/ShardingCore.Test2x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs b/test/ShardingCore.Test2x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs index e3637b45..da84d940 100644 --- a/test/ShardingCore.Test2x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs +++ b/test/ShardingCore.Test2x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs @@ -42,7 +42,7 @@ namespace ShardingCore.Test2x.Shardings } - public override Expression> GetMainRouteFilter(string shardingKey, ShardingOperatorEnum shardingOperator) + public override Expression> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) { var t = ShardingKeyToDataSourceName(shardingKey); diff --git a/test/ShardingCore.Test3x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs b/test/ShardingCore.Test3x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs index 398ecda2..3431855b 100644 --- a/test/ShardingCore.Test3x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs +++ b/test/ShardingCore.Test3x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs @@ -42,7 +42,7 @@ namespace ShardingCore.Test3x.Shardings } - public override Expression> GetMainRouteFilter(string shardingKey, ShardingOperatorEnum shardingOperator) + public override Expression> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) { var t = ShardingKeyToDataSourceName(shardingKey); diff --git a/test/ShardingCore.Test5x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs b/test/ShardingCore.Test5x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs index 10c64b0e..0b35ab4f 100644 --- a/test/ShardingCore.Test5x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs +++ b/test/ShardingCore.Test5x/Shardings/OrderAreaShardingVirtualDataSourceRoute.cs @@ -42,7 +42,7 @@ namespace ShardingCore.Test5x.Shardings } - public override Expression> GetMainRouteFilter(string shardingKey, ShardingOperatorEnum shardingOperator) + public override Expression> GetRouteToFilter(string shardingKey, ShardingOperatorEnum shardingOperator) { var t = ShardingKeyToDataSourceName(shardingKey);