修复逻辑表对应的对象如果是分片对象只能有一个普通对象可以有多个 发布x.6.0.10
This commit is contained in:
parent
cee125106f
commit
65dbc3bf99
|
@ -60,22 +60,24 @@ namespace Sample.SqlServer.UnionAllMerge
|
|||
{
|
||||
if (_unionAllMergeManager?.Current != null)
|
||||
{
|
||||
var entityMetadata = _entityMetadataManager.TryGetByLogicTableName(tableExpression.Name);
|
||||
var entityMetadatas = _entityMetadataManager.TryGetByLogicTableName(tableExpression.Name);
|
||||
var tableRouteResults = _unionAllMergeManager?.Current.TableRoutesResults.ToArray();
|
||||
if (tableRouteResults.IsNotEmpty() &&entityMetadata!=null&&
|
||||
tableRouteResults[0].ReplaceTables.Any(o =>o.EntityType==entityMetadata.EntityType));
|
||||
if (tableRouteResults.IsNotEmpty() &&
|
||||
entityMetadatas.IsNotEmpty()&&
|
||||
entityMetadatas.Count==1&&
|
||||
tableRouteResults[0].ReplaceTables.Any(o =>o.EntityType==entityMetadatas[0].EntityType));
|
||||
{
|
||||
var tails = tableRouteResults.Select(o => o.ReplaceTables.FirstOrDefault(r => r.EntityType==entityMetadata.EntityType)?.Tail).ToHashSet();
|
||||
var tails = tableRouteResults.Select(o => o.ReplaceTables.FirstOrDefault(r => r.EntityType==entityMetadatas[0].EntityType)?.Tail).ToHashSet();
|
||||
|
||||
var sqlGenerationHelper = typeof(QuerySqlGenerator).GetTypeFieldValue(this, "_sqlGenerationHelper") as ISqlGenerationHelper;
|
||||
string newTableName = null;
|
||||
if (tails.Count == 1)
|
||||
{
|
||||
newTableName = sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{entityMetadata.TableSeparator}{tails.First()}", tableExpression.Schema);
|
||||
newTableName = sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{entityMetadatas[0].TableSeparator}{tails.First()}", tableExpression.Schema);
|
||||
}
|
||||
else
|
||||
{
|
||||
newTableName = "(" + string.Join(" union all ", tails.Select(tail => $"select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{entityMetadata.TableSeparator}{tail}", tableExpression.Schema)}")) + ")";
|
||||
newTableName = "(" + string.Join(" union all ", tails.Select(tail => $"select * from {sqlGenerationHelper.DelimitIdentifier($"{tableExpression.Name}{entityMetadatas[0].TableSeparator}{tail}", tableExpression.Schema)}")) + ")";
|
||||
}
|
||||
|
||||
var relationalCommandBuilder = typeof(QuerySqlGenerator).GetTypeFieldValue(this, "_relationalCommandBuilder") as IRelationalCommandBuilder;
|
||||
|
|
|
@ -17,7 +17,7 @@ namespace ShardingCore.Core.EntityMetadatas
|
|||
public class DefaultEntityMetadataManager : IEntityMetadataManager
|
||||
{
|
||||
private readonly ConcurrentDictionary<Type, EntityMetadata> _caches = new();
|
||||
private readonly ConcurrentDictionary<string/*logic table name*/, EntityMetadata> _logicTableCaches = new();
|
||||
private readonly ConcurrentDictionary<string/*logic table name*/, List<EntityMetadata>> _logicTableCaches = new();
|
||||
|
||||
public bool AddEntityMetadata(EntityMetadata entityMetadata)
|
||||
{
|
||||
|
@ -70,7 +70,7 @@ namespace ShardingCore.Core.EntityMetadatas
|
|||
return entityMetadata;
|
||||
}
|
||||
|
||||
public EntityMetadata TryGetByLogicTableName(string logicTableName)
|
||||
public List<EntityMetadata> TryGetByLogicTableName(string logicTableName)
|
||||
{
|
||||
if (_logicTableCaches.TryGetValue(logicTableName, out var metadata))
|
||||
{
|
||||
|
@ -110,12 +110,23 @@ namespace ShardingCore.Core.EntityMetadatas
|
|||
throw new ShardingCoreInvalidOperationException(
|
||||
$"init model error, cant get logic table name:[{metadata.LogicTableName}] from entity:[{efEntityType.ClrType}]");
|
||||
}
|
||||
if (!_logicTableCaches.TryAdd(metadata.LogicTableName, metadata))
|
||||
if (!_logicTableCaches.TryGetValue(metadata.LogicTableName, out var metadatas))
|
||||
{
|
||||
metadatas = new List<EntityMetadata>();
|
||||
_logicTableCaches.TryAdd(metadata.LogicTableName, metadatas);
|
||||
}
|
||||
|
||||
if (metadatas.Any(o => o.EntityType != efEntityType.ClrType))
|
||||
{
|
||||
metadatas.Add(metadata);
|
||||
return true;
|
||||
}
|
||||
//添加完成后检查逻辑表对应的对象不可以存在两个以上的分片
|
||||
if (metadatas.Count > 1 && metadatas.Any(o => o.IsShardingTable() || o.IsShardingDataSource()))
|
||||
{
|
||||
throw new ShardingCoreInvalidOperationException(
|
||||
$"cant add logic table name caches for metadata:[{metadata.LogicTableName}-{efEntityType.ClrType}]");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
|
@ -37,7 +37,7 @@ namespace ShardingCore.Core.EntityMetadatas
|
|||
/// <param name="entityType"></param>
|
||||
/// <returns></returns>
|
||||
EntityMetadata? TryGet(Type entityType);
|
||||
EntityMetadata? TryGetByLogicTableName(string logicTableName);
|
||||
List<EntityMetadata> TryGetByLogicTableName(string logicTableName);
|
||||
/// <summary>
|
||||
/// 是否是分片对象(包括分表或者分库)
|
||||
/// </summary>
|
||||
|
|
|
@ -125,16 +125,15 @@ namespace ShardingCore.Helpers
|
|||
}
|
||||
|
||||
MigrationResult migrationResult = new MigrationResult();
|
||||
var entityMetadata = entityMetadataManager.TryGetByLogicTableName(absTableName);
|
||||
if (entityMetadata != null)
|
||||
var entityMetadatas = entityMetadataManager.TryGetByLogicTableName(absTableName);
|
||||
if (entityMetadatas.IsNotEmpty())
|
||||
{
|
||||
migrationResult.CommandType = MigrationCommandTypeEnum.TableCommand;
|
||||
|
||||
|
||||
bool isShardingDataSource = entityMetadata.IsShardingDataSource();
|
||||
bool isShardingDataSource =entityMetadatas.Count==1&& entityMetadatas[0].IsShardingDataSource();
|
||||
if (isShardingDataSource)
|
||||
{
|
||||
var virtualDataSourceRoute = dataSourceRouteManager.GetRoute(entityMetadata.EntityType);
|
||||
var virtualDataSourceRoute = dataSourceRouteManager.GetRoute(entityMetadatas[0].EntityType);
|
||||
isShardingDataSource = virtualDataSourceRoute.GetAllDataSourceNames().Contains(dataSourceName);
|
||||
|
||||
if (isShardingDataSource)
|
||||
|
|
Loading…
Reference in New Issue