添加快捷比较路由针对分表和分库的处理

This commit is contained in:
xuejiaming 2022-09-26 21:20:08 +08:00
parent 1342d13eb3
commit e89cbc806d
7 changed files with 86 additions and 13 deletions

View File

@ -38,13 +38,13 @@ namespace Sample.MySql.Controllers
public IQueryable<SysTest> GetAll()
{
var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator();
var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager();
//系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可
var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod));
//一定要先在路由里面添加尾巴
virtualTableRoute.Append("20220921");
shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921");
// var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator();
// var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager();
// //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可
// var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod));
// //一定要先在路由里面添加尾巴
// virtualTableRoute.Append("20220921");
// shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921");
return _defaultTableDbContext.Set<SysTest>();
}
@ -83,7 +83,7 @@ namespace Sample.MySql.Controllers
}).ToListAsync();
var firstOrDefault = _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw($"select * from {nameof(SysUserMod)}").FirstOrDefault();
// var firstOrDefault = _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw($"select * from {nameof(SysUserMod)}").FirstOrDefault();
var sysUserMods1 = _defaultTableDbContext.Set<SysTest>()
.Select(o => new ssss(){ Id = o.Id, C = GetAll().Count(x => x.Id == o.Id) }).ToList();

View File

@ -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();
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using ShardingCore.Core.EntityMetadatas;
namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions
{
public abstract class AbstractShardingComparerVirtualDataSourceRouteclass<TEntity, TKey> : AbstractShardingOperatorVirtualDataSourceRoute<TEntity, TKey> where TEntity : class
{
protected abstract IComparer<string> GetComparer();
public override Func<string, bool> 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;
}
}
}
}
}

View File

@ -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<TEntity, TKey> : AbstractShardingOperatorVirtualTableRoute<TEntity, TKey> where TEntity : class
{
protected abstract IComparer<string> GetComparer();
public override Func<string, bool> 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;
}
}
}
}
}

View File

@ -22,7 +22,6 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
///
/// </summary>
/// <param name="dataSourceRouteResult"></param>
/// <param name="allPhysicTables"></param>
/// <param name="queryable"></param>
/// <returns></returns>
protected override List<TableRouteUnit> DoRouteWithPredicate(DataSourceRouteResult dataSourceRouteResult, IQueryable queryable)

View File

@ -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);

View File

@ -132,7 +132,6 @@ namespace ShardingCore.Helpers
dbContextOptionBuilder.UseShardingOptions(shardingRuntimeContext);
return dbContextOptionBuilder.Options;
}
/// <summary>
/// 动态添加读写分离链接字符串
/// </summary>
@ -140,16 +139,16 @@ namespace ShardingCore.Helpers
/// <param name="dataSourceName"></param>
/// <param name="connectionString"></param>
/// <param name="readNodeName"></param>
/// <returns></returns>
/// <exception cref="ShardingCoreInvalidOperationException"></exception>
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(