This commit is contained in:
xuejiaming 2022-12-13 17:36:51 +08:00
parent b4eb90ccc1
commit 5e7d659a8e
9 changed files with 107 additions and 6 deletions

View File

@ -6,6 +6,7 @@ using Sample.MySql.Domain.Entities;
using Sample.MySql.multi;
using Sample.MySql.Shardings;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
@ -73,6 +74,13 @@ namespace Sample.MySql.Controllers
[HttpGet]
public async Task<IActionResult> Get()
{
var virtualDataSource = _shardingRuntimeContext.GetVirtualDataSource();
virtualDataSource.AddPhysicDataSource(new DefaultPhysicDataSource("2023", "xxxxxxxx", false));
var dataSourceRouteManager = _shardingRuntimeContext.GetDataSourceRouteManager();
var virtualDataSourceRoute = dataSourceRouteManager.GetRoute(typeof(SysUserMod));
virtualDataSourceRoute.AddDataSourceName("2023");
var dataSourceInitializer = _shardingRuntimeContext.GetDataSourceInitializer();
dataSourceInitializer.InitConfigure("2023",true,true);
using (var dbContextTransaction = _defaultTableDbContext.Database.BeginTransaction())
{
@ -224,5 +232,17 @@ namespace Sample.MySql.Controllers
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
[HttpGet]
public async Task<IActionResult> Get4()
{
var sql=from a in _defaultTableDbContext.Set<TestMod>()
join b in _defaultTableDbContext.Set<TestModItem>()
on a.Id equals b.MainId
select a;
var xx = await sql.ToListAsync();
// var sysUserMods1 = await _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync();
// var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
}
}

View File

@ -42,6 +42,8 @@ namespace Sample.MySql.DbContexts
modelBuilder.Entity<SysUserLogByMonth>().HasData(new SysUserLogByMonth() { Id = "1", Time = DateTime.Now });
modelBuilder.Entity<SysTest>().HasData(new SysTest() { Id = "1", UserId = "123" });
modelBuilder.Entity<TestMod>().ToTable(nameof(TestMod));
modelBuilder.Entity<TestModItem>().ToTable(nameof(TestModItem));
}

View File

@ -0,0 +1,6 @@
namespace Sample.MySql.Domain.Entities;
public class TestMod
{
public string Id { get; set; }
}

View File

@ -0,0 +1,7 @@
namespace Sample.MySql.Domain.Entities;
public class TestModItem
{
public string Id { get; set; }
public string MainId { get; set; }
}

View File

@ -19,6 +19,13 @@ namespace Sample.MySql.Shardings
{
_logger = logger;
}
protected override List<string> CalcTailsOnStart()
{
var tails = base.CalcTailsOnStart();
return tails;
}
public override DateTime GetBeginTime()
{
return new DateTime(2021, 1, 01);

View File

@ -0,0 +1,29 @@
using Sample.MySql.Domain.Entities;
using ShardingCore.Core.EntityMetadatas;
using ShardingCore.VirtualRoutes.Mods;
namespace Sample.MySql.Shardings;
public class TestModRoute:AbstractSimpleShardingModKeyStringVirtualTableRoute<TestMod>
{
public TestModRoute() : base(2, 3)
{
}
public override void Configure(EntityMetadataTableBuilder<TestMod> builder)
{
builder.ShardingProperty(o => o.Id);
}
}
public class TestModItemRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<TestModItem>
{
public TestModItemRoute() : base(2, 3)
{
}
public override void Configure(EntityMetadataTableBuilder<TestModItem> builder)
{
builder.ShardingProperty(o => o.MainId);
}
}

View File

@ -15,6 +15,7 @@ using ShardingCore.Core.RuntimeContexts;
using ShardingCore.EFCores;
using ShardingCore.Extensions;
using ShardingCore.Helpers;
using ShardingCore.Sharding.ParallelTables;
using ShardingCore.Sharding.ReadWriteConfigurations;
using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
@ -76,6 +77,13 @@ namespace Sample.MySql
o.AddShardingTableRoute<SysUserLogByMonthRoute>();
// o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
o.AddShardingTableRoute<TestModRoute>();
o.AddShardingTableRoute<TestModItemRoute>();
o.AddParallelTableGroupNode(new ParallelTableGroupNode(new List<ParallelTableComparerType>()
{
new ParallelTableComparerType(typeof(TestMod)),
new ParallelTableComparerType(typeof(TestModItem)),
}));
}).UseConfig((sp,o) =>
{
var memoryCache = sp.ApplicationServiceProvider.GetRequiredService<IMemoryCache>();

View File

@ -46,7 +46,9 @@ namespace ShardingCore.Core.EntityMetadatas
public bool IsOnlyShardingTable(Type entityType)
{
return IsShardingTable(entityType) && !IsShardingDataSource(entityType);
if (!_caches.TryGetValue(entityType, out var entityMetadata))
return false;
return entityMetadata.IsMultiTableMapping&&!entityMetadata.IsMultiDataSourceMapping;
}
/// <summary>
@ -63,7 +65,9 @@ namespace ShardingCore.Core.EntityMetadatas
public bool IsOnlyShardingDataSource(Type entityType)
{
return IsShardingDataSource(entityType) && !IsShardingTable(entityType);
if (!_caches.TryGetValue(entityType, out var entityMetadata))
return false;
return entityMetadata.IsMultiDataSourceMapping&&!entityMetadata.IsMultiTableMapping;
}
/// <summary>

View File

@ -7,6 +7,7 @@ using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Extensions;
using ShardingCore.Sharding.MergeEngines.Common;
using ShardingCore.Sharding.MergeEngines.Common.Abstractions;
using ShardingCore.Sharding.ParallelTables;
namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
@ -21,12 +22,14 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
{
private readonly ITableRouteManager _tableRouteManager;
private readonly IEntityMetadataManager _entityMetadataManager;
private readonly IParallelTableManager _parallelTableManager;
public TableRouteRuleEngine(ITableRouteManager tableRouteManager,
IEntityMetadataManager entityMetadataManager)
IEntityMetadataManager entityMetadataManager,IParallelTableManager parallelTableManager)
{
_tableRouteManager = tableRouteManager;
_entityMetadataManager = entityMetadataManager;
_parallelTableManager = parallelTableManager;
}
private List<TableRouteUnit> GetEntityRouteUnit(DataSourceRouteResult dataSourceRouteResult,
@ -108,12 +111,14 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
if (routeMaps.ContainsKey(dataSourceName))
{
var routeMap = routeMaps[dataSourceName];
var tableRouteResults = routeMap.Select(o => o.Value).Cartesian()
.Select(o => new TableRouteResult(o.ToList())).Where(o => !o.IsEmpty).ToList();
var routeResults = routeMap.Select(o => o.Value).Cartesian()
.Select(o => new TableRouteResult(o.ToList())).Where(o => !o.IsEmpty).ToArray();
var tableRouteResults = GetTableRouteResults(tableRouteRuleContext, routeResults);
if (tableRouteResults.IsNotEmpty())
{
dataSourceCount++;
if (tableRouteResults.Count > 1)
if (tableRouteResults.Length > 1)
{
isCrossTable = true;
}
@ -153,5 +158,18 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
// return sqlRouteUnits;
// return routeMaps.Select(o => o.Value).Cartesian().Where(o=>o).Select(o => new TableRouteResult(o,_shardingDatabaseProvider.GetShardingDbContextType()));
}
private TableRouteResult[] GetTableRouteResults(TableRouteRuleContext tableRouteRuleContext,TableRouteResult[] routeResults)
{
if (tableRouteRuleContext.QueryEntities.Count > 1&& routeResults.Length>0)
{
var queryShardingTables = tableRouteRuleContext.QueryEntities.Keys.Where(o => _entityMetadataManager.IsShardingTable(o)).ToArray();
if (queryShardingTables.Length > 1 && _parallelTableManager.IsParallelTableQuery(queryShardingTables))
{
return routeResults.Where(o => !o.HasDifferentTail).ToArray();
}
}
return routeResults;
}
}
}