From 86455805f0ed2befd060c7ba2d14f90e3fb86e7c Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Fri, 8 Jul 2022 08:45:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=91=BD=E5=90=8D=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E5=92=8C=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 3 +- .../EntityMetadataInitializer.cs | 2 +- .../Bootstrappers/ShardingBootstrapper.cs | 2 +- .../Bootstrappers/ShardingInitializer.cs | 6 +- .../DefaultEntityMetadataManager.cs | 18 +- .../IShardingRuntimeContext.cs | 1 - .../RuntimeContexts/ShardingRuntimeContext.cs | 8 +- .../ServiceProviders/IShardingProvider.cs | 1 - .../Core/ServiceProviders/ShardingProvider.cs | 5 - ...pleVirtualDataSourceConfigurationParams.cs | 14 +- .../DefaultDataSourceInitializer.cs | 2 +- .../EFCores/ShardingModelCustomizer.cs | 2 +- src/ShardingCore/Helpers/MigrationHelper.cs | 161 +++++++----------- .../IShardingDbContextOptionsBuilderConfig.cs | 38 ----- src/ShardingCore/Jobs/JobRunnerService.cs | 2 +- .../Logger/ShardingLoggerFactory.cs | 104 +++++------ .../DataSourceDbContext.cs | 2 +- .../DefaultShardingCompilerExecutor.cs | 2 +- .../DefaultShardingQueryExecutor.cs | 2 +- .../QueryCompilerContextFactory.cs | 2 +- src/ShardingCore/ShardingRuntimeBuilder.cs | 1 + .../TableCreator/ShardingTableCreator.cs | 1 - ...dingAutoCreateOperatorVirtualTableRoute.cs | 2 +- 23 files changed, 150 insertions(+), 231 deletions(-) delete mode 100644 src/ShardingCore/IShardingDbContextOptionsBuilderConfig.cs diff --git a/Directory.Build.props b/Directory.Build.props index ec250473..23fff7df 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,8 +4,9 @@ true xuejmnet - https://github.com/xuejmnet/sharding-core + https://github.com/dotnetcore/sharding-core git + https://github.com/dotnetcore/sharding-core xuejm 10.0 high performance lightweight solution for efcore sharding table and sharding database support read-write-separation diff --git a/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs b/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs index 0b53a60a..2ac257af 100644 --- a/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs +++ b/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs @@ -14,7 +14,7 @@ using ShardingCore.Extensions; using ShardingCore.Helpers; using ShardingCore.Jobs; using ShardingCore.Jobs.Abstaractions; -using ShardingCore.Logger; + using ShardingCore.Sharding.Abstractions; /* diff --git a/src/ShardingCore/Bootstrappers/ShardingBootstrapper.cs b/src/ShardingCore/Bootstrappers/ShardingBootstrapper.cs index 507a7dde..fd97c324 100644 --- a/src/ShardingCore/Bootstrappers/ShardingBootstrapper.cs +++ b/src/ShardingCore/Bootstrappers/ShardingBootstrapper.cs @@ -13,7 +13,7 @@ using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Jobs; using ShardingCore.Jobs.Abstaractions; -using ShardingCore.Logger; + using ShardingCore.Sharding.MergeEngines.ParallelControl; using ShardingCore.Sharding.ParallelTables; diff --git a/src/ShardingCore/Bootstrappers/ShardingInitializer.cs b/src/ShardingCore/Bootstrappers/ShardingInitializer.cs index 8bd95b6d..4b995ba0 100644 --- a/src/ShardingCore/Bootstrappers/ShardingInitializer.cs +++ b/src/ShardingCore/Bootstrappers/ShardingInitializer.cs @@ -9,7 +9,7 @@ using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Jobs; using ShardingCore.Jobs.Abstaractions; -using ShardingCore.Logger; + using ShardingCore.Sharding.MergeEngines.ParallelControl; using ShardingCore.Sharding.ParallelTables; @@ -25,9 +25,9 @@ namespace ShardingCore.Bootstrappers private readonly IParallelTableManager _parallelTableManager; private readonly DoOnlyOnce _doOnlyOnce = new DoOnlyOnce(); - public ShardingInitializer(IShardingProvider shardingProvider) + public ShardingInitializer(IShardingProvider shardingProvider,ILogger logger) { - _logger = ShardingLoggerFactory.DefaultFactory.CreateLogger(); + _logger = logger; _shardingProvider = shardingProvider; _routeConfigOptions = shardingProvider.GetRequiredService(); _entityMetadataManager = shardingProvider.GetRequiredService(); diff --git a/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs b/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs index 3f4b261e..f16f7044 100644 --- a/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs +++ b/src/ShardingCore/Core/EntityMetadatas/DefaultEntityMetadataManager.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; +using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Sharding.Abstractions; @@ -16,6 +17,7 @@ namespace ShardingCore.Core.EntityMetadatas public class DefaultEntityMetadataManager : IEntityMetadataManager { private readonly ConcurrentDictionary _caches = new(); + private readonly ConcurrentDictionary _logicTableCaches = new(); public bool AddEntityMetadata(EntityMetadata entityMetadata) { @@ -70,7 +72,11 @@ namespace ShardingCore.Core.EntityMetadatas public EntityMetadata TryGetByLogicTableName(string logicTableName) { - return _caches.Values.FirstOrDefault(o => o.LogicTableName == logicTableName); + if (_logicTableCaches.TryGetValue(logicTableName, out var metadata)) + { + return metadata; + } + return null; } /// @@ -99,6 +105,16 @@ namespace ShardingCore.Core.EntityMetadatas if (_caches.TryGetValue(efEntityType.ClrType, out var metadata)) { metadata.SetEntityModel(efEntityType); + if (string.IsNullOrWhiteSpace(metadata.LogicTableName)) + { + throw new ShardingCoreInvalidOperationException( + $"init model error, cant get logic table name:[{metadata.LogicTableName}] from entity:[{efEntityType.ClrType}]"); + } + if (!_logicTableCaches.TryAdd(metadata.LogicTableName, metadata)) + { + throw new ShardingCoreInvalidOperationException( + $"cant add logic table name caches for metadata:[{metadata.LogicTableName}-{efEntityType.ClrType}]"); + } return true; } diff --git a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs index 4cdd7132..3a8dbe20 100644 --- a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs @@ -54,7 +54,6 @@ namespace ShardingCore.Core.RuntimeContexts void CheckRequirement(); void GetOrCreateShardingRuntimeModel(DbContext dbContext); - void Initialize(); void AutoShardingCreate(); object GetService(Type serviceType); diff --git a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs index ee3201e2..592ce289 100644 --- a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs @@ -19,7 +19,7 @@ using ShardingCore.Core.VirtualRoutes.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.DynamicDataSources; using ShardingCore.Exceptions; -using ShardingCore.Logger; + using ShardingCore.Sharding.Abstractions; using ShardingCore.Sharding.ParallelTables; using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions; @@ -254,12 +254,6 @@ namespace ShardingCore.Core.RuntimeContexts } } } - // - // public void UseLogfactory(ILoggerFactory loggerFactory) - // { - // // ShardingLoggerFactory.DefaultFactory = loggerFactory; - // _applicationLoggerFactory = loggerFactory; - // } private void CheckIfBuild() diff --git a/src/ShardingCore/Core/ServiceProviders/IShardingProvider.cs b/src/ShardingCore/Core/ServiceProviders/IShardingProvider.cs index 7deaa5c2..54b6d35f 100644 --- a/src/ShardingCore/Core/ServiceProviders/IShardingProvider.cs +++ b/src/ShardingCore/Core/ServiceProviders/IShardingProvider.cs @@ -8,7 +8,6 @@ namespace ShardingCore.Core.ServiceProviders /// public interface IShardingProvider { - void UseApplicationServiceProvider(IServiceProvider applicationServiceProvider); /// /// 优先通过ShardingCore的IServiceProvider获取 /// 没有再通过ApplicationServiceProvider获取 diff --git a/src/ShardingCore/Core/ServiceProviders/ShardingProvider.cs b/src/ShardingCore/Core/ServiceProviders/ShardingProvider.cs index cba65178..c5d57d52 100644 --- a/src/ShardingCore/Core/ServiceProviders/ShardingProvider.cs +++ b/src/ShardingCore/Core/ServiceProviders/ShardingProvider.cs @@ -15,11 +15,6 @@ namespace ShardingCore.Core.ServiceProviders _applicationServiceProvider = applicationServiceProvider; } - public void UseApplicationServiceProvider(IServiceProvider applicationServiceProvider) - { - _applicationServiceProvider = applicationServiceProvider; - } - public object GetService(Type serviceType,bool tryApplicationServiceProvider=true) { var service = _internalServiceProvider?.GetService(serviceType); diff --git a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs index 82dac855..ebcfacea 100644 --- a/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs +++ b/src/ShardingCore/Core/VirtualDatabase/VirtualDataSources/SimpleVirtualDataSourceConfigurationParams.cs @@ -87,22 +87,12 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources public override void UseShellDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder) { - if (_options.ShellDbContextConfigure == null) - { - return; - } - - _options.ShellDbContextConfigure.Invoke(dbContextOptionsBuilder); + _options.ShellDbContextConfigure?.Invoke(dbContextOptionsBuilder); } public override void UseExecutorDbContextOptionBuilder(DbContextOptionsBuilder dbContextOptionsBuilder) { - if (_options.ExecutorDbContextConfigure == null ) - { - return; - } - - _options.ExecutorDbContextConfigure.Invoke(dbContextOptionsBuilder); + _options.ExecutorDbContextConfigure?.Invoke(dbContextOptionsBuilder); } } } diff --git a/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs b/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs index 59b8603c..21b01614 100644 --- a/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs +++ b/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs @@ -16,7 +16,7 @@ using ShardingCore.Core.VirtualRoutes.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Exceptions; using ShardingCore.Extensions; -using ShardingCore.Logger; + using ShardingCore.Sharding.Abstractions; using ShardingCore.TableExists.Abstractions; diff --git a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs index efe60b25..c09b75b5 100644 --- a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs +++ b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs @@ -12,7 +12,7 @@ using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.RuntimeContexts; using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Extensions.InternalExtensions; -using ShardingCore.Logger; + using ShardingCore.Sharding.Abstractions; using ShardingCore.Utils; diff --git a/src/ShardingCore/Helpers/MigrationHelper.cs b/src/ShardingCore/Helpers/MigrationHelper.cs index 5a764049..e8f98793 100644 --- a/src/ShardingCore/Helpers/MigrationHelper.cs +++ b/src/ShardingCore/Helpers/MigrationHelper.cs @@ -32,108 +32,59 @@ namespace ShardingCore.Helpers [ExcludeFromCodeCoverage] public class MigrationHelper { - private MigrationHelper() { } + private MigrationHelper() + { + } + public static void Generate( IShardingRuntimeContext shardingRuntimeContext, MigrationOperation operation, MigrationCommandListBuilder builder, ISqlGenerationHelper sqlGenerationHelper, List addCmds - ) + ) { - var migrationCommands = (List) builder.GetFieldValue("_commands"); + var migrationCommands = (List)builder.GetFieldValue("_commands"); var shardingMigrationManager = shardingRuntimeContext.GetRequiredService(); var virtualDataSource = shardingRuntimeContext.GetRequiredService(); - var currentCurrentDataSourceName = shardingMigrationManager.Current?.CurrentDataSourceName??virtualDataSource.DefaultDataSourceName; + var currentCurrentDataSourceName = shardingMigrationManager.Current?.CurrentDataSourceName ?? + virtualDataSource.DefaultDataSourceName; addCmds.ForEach(aAddCmd => + { + var (migrationResult, shardingCmds) = BuildDataSourceShardingCmds(shardingRuntimeContext, + virtualDataSource.DefaultDataSourceName, currentCurrentDataSourceName, operation, + aAddCmd.CommandText, sqlGenerationHelper); + if (!migrationResult.InDataSource) { - var (migrationResult,shardingCmds) = BuildDataSourceShardingCmds(shardingRuntimeContext,virtualDataSource.DefaultDataSourceName,currentCurrentDataSourceName,operation, aAddCmd.CommandText, sqlGenerationHelper); - if (!migrationResult.InDataSource) + if (migrationResult.CommandType == MigrationCommandTypeEnum.TableCommand) { - if (migrationResult.CommandType == MigrationCommandTypeEnum.TableCommand) + migrationCommands.Remove(aAddCmd); + } + } + else + { + if (migrationResult.CommandType == MigrationCommandTypeEnum.TableCommand) + { + //如果是分表 + if (shardingCmds.IsNotEmpty()) { migrationCommands.Remove(aAddCmd); - } - } - else - { - if (migrationResult.CommandType == MigrationCommandTypeEnum.TableCommand) - { - //如果是分表 - if (shardingCmds.IsNotEmpty()) + //针对builder的原始表进行移除 + shardingCmds.ForEach(aShardingCmd => { - migrationCommands.Remove(aAddCmd); - //针对builder的原始表进行移除 - shardingCmds.ForEach(aShardingCmd => - { - builder.Append(aShardingCmd) - .EndCommand(); - }); - } + builder.Append(aShardingCmd) + .EndCommand(); + }); } } - }); + } + }); } - private static List BuildShardingCmds(IShardingRuntimeContext shardingRuntimeContext,MigrationOperation operation, string sourceCmd, ISqlGenerationHelper sqlGenerationHelper) - { - //所有MigrationOperation定义 - //https://github.com/dotnet/efcore/tree/b970bf29a46521f40862a01db9e276e6448d3cb0/src/EFCore.Relational/Migrations/Operations - //ColumnOperation仅替换Table - //其余其余都是将Name和Table使用分表名替换 - var tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); - var tableRoutes = tableRouteManager.GetRoutes(); - var existsShardingTables = tableRoutes.ToDictionary(o => o.EntityMetadata.LogicTableName, o => o.GetTails().Select(p=>$"{o.EntityMetadata.LogicTableName}{o.EntityMetadata.TableSeparator}{p}").ToList()); - //Dictionary> _existsShardingTables - // = Cache.ServiceProvider.GetService().ExistsShardingTables; - List resList = new List(); - string absTableName = string.Empty; - - string name = operation.GetPropertyValue("Name") as string; - string tableName = operation.GetPropertyValue("Table") as string; - string pattern = string.Format("^({0})$|^({0}_.*?)$|^(.*?_{0}_.*?)$|^(.*?_{0})$", absTableName); - Func>, bool> where = x => - existsShardingTables.Any(y =>x.Key==y.Key&& Regex.IsMatch(name, BuildPattern(y.Key))); - - if (!string.IsNullOrWhiteSpace(tableName)) - { - absTableName = tableName; - } - else if (!string.IsNullOrWhiteSpace(name) && existsShardingTables.Any(x => where(x))) - { - absTableName = existsShardingTables.Where(x => where(x)).FirstOrDefault().Key; - } - - //分表 - if (!string.IsNullOrWhiteSpace(absTableName) && existsShardingTables.ContainsKey(absTableName)) - { - var shardings = existsShardingTables[absTableName]; - shardings.ForEach(aShardingTable => - { - string newCmd = sourceCmd; - GetReplaceGroups(operation, absTableName, aShardingTable).ForEach(aReplace => - { - newCmd = newCmd.Replace( - sqlGenerationHelper.DelimitIdentifier(aReplace.sourceName), - sqlGenerationHelper.DelimitIdentifier(aReplace.targetName)); - }); - if (newCmd.Contains("EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE'")) - { - newCmd=newCmd.Replace($"EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'{absTableName}'", $"EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'{aShardingTable}'"); - } - resList.Add(newCmd); - }); - } - - return resList; - - string BuildPattern(string absTableName) - { - return string.Format("^({0})$|^({0}_.*?)$|^(.*?_{0}_.*?)$|^(.*?_{0})$", absTableName); - } - } - private static (MigrationResult migrationResult,List) BuildDataSourceShardingCmds(IShardingRuntimeContext shardingRuntimeContext,string defaultDataSourceName,string dataSourceName,MigrationOperation operation, string sourceCmd, ISqlGenerationHelper sqlGenerationHelper) + private static (MigrationResult migrationResult, List) BuildDataSourceShardingCmds( + IShardingRuntimeContext shardingRuntimeContext, string defaultDataSourceName, string dataSourceName, + MigrationOperation operation, string sourceCmd, ISqlGenerationHelper sqlGenerationHelper) { //所有MigrationOperation定义 //https://github.com/dotnet/efcore/tree/b970bf29a46521f40862a01db9e276e6448d3cb0/src/EFCore.Relational/Migrations/Operations @@ -143,7 +94,9 @@ namespace ShardingCore.Helpers var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); var tableRouteManager = shardingRuntimeContext.GetTableRouteManager(); var tableRoutes = tableRouteManager.GetRoutes(); - var existsShardingTables = tableRoutes.ToDictionary(o => o.EntityMetadata.LogicTableName, o => o.GetTails().Select(p=>$"{o.EntityMetadata.LogicTableName}{o.EntityMetadata.TableSeparator}{p}").ToList()); + var existsShardingTables = tableRoutes.ToDictionary(o => o.EntityMetadata.LogicTableName, + o => o.GetTails().Select(p => $"{o.EntityMetadata.LogicTableName}{o.EntityMetadata.TableSeparator}{p}") + .ToList()); //Dictionary> _existsShardingTables // = Cache.ServiceProvider.GetService().ExistsShardingTables; List resList = new List(); @@ -153,7 +106,7 @@ namespace ShardingCore.Helpers string tableName = operation.GetPropertyValue("Table") as string; string pattern = string.Format("^({0})$|^({0}_.*?)$|^(.*?_{0}_.*?)$|^(.*?_{0})$", absTableName); Func>, bool> where = x => - existsShardingTables.Any(y =>x.Key==y.Key&& Regex.IsMatch(name, BuildPattern(y.Key))); + existsShardingTables.Any(y => x.Key == y.Key && Regex.IsMatch(name, BuildPattern(y.Key))); if (!string.IsNullOrWhiteSpace(tableName)) { @@ -163,7 +116,7 @@ namespace ShardingCore.Helpers { if (existsShardingTables.Any(x => where(x))) { - absTableName = existsShardingTables.Where(x => where(x)).FirstOrDefault().Key; + absTableName = existsShardingTables.FirstOrDefault(x => where(x)).Key; } else { @@ -176,33 +129,32 @@ namespace ShardingCore.Helpers if (entityMetadata != null) { migrationResult.CommandType = MigrationCommandTypeEnum.TableCommand; - - - bool isShardingDataSource =entityMetadata.IsShardingDataSource(); + + + bool isShardingDataSource = entityMetadata.IsShardingDataSource(); if (isShardingDataSource) { var virtualDataSourceRoute = dataSourceRouteManager.GetRoute(entityMetadata.EntityType); isShardingDataSource = virtualDataSourceRoute.GetAllDataSourceNames().Contains(dataSourceName); - + if (isShardingDataSource) { migrationResult.InDataSource = true; } else { - migrationResult.InDataSource = false; } } else { - migrationResult.InDataSource = defaultDataSourceName==dataSourceName; + migrationResult.InDataSource = defaultDataSourceName == dataSourceName; } //分表 - if (migrationResult.InDataSource&&!string.IsNullOrWhiteSpace(absTableName) && existsShardingTables.ContainsKey(absTableName)) + if (migrationResult.InDataSource && !string.IsNullOrWhiteSpace(absTableName) && + existsShardingTables.ContainsKey(absTableName)) { - var shardings = existsShardingTables[absTableName]; shardings.ForEach(aShardingTable => { @@ -213,22 +165,27 @@ namespace ShardingCore.Helpers sqlGenerationHelper.DelimitIdentifier(aReplace.sourceName), sqlGenerationHelper.DelimitIdentifier(aReplace.targetName)); }); - if (newCmd.Contains("EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE'")) + if (newCmd.Contains( + "EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE'")) { - newCmd=newCmd.Replace($"EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'{absTableName}'", $"EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'{aShardingTable}'"); + newCmd = newCmd.Replace( + $"EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'{absTableName}'", + $"EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'{aShardingTable}'"); } + resList.Add(newCmd); }); } } - return (migrationResult,resList); + return (migrationResult, resList); string BuildPattern(string absTableName) { return string.Format("^({0})$|^({0}_.*?)$|^(.*?_{0}_.*?)$|^(.*?_{0})$", absTableName); } } + private static List<(string sourceName, string targetName)> GetReplaceGroups( MigrationOperation operation, string sourceTableName, string targetTableName) { @@ -243,7 +200,11 @@ namespace ShardingCore.Helpers { if (!(operation is ColumnOperation columnOperation)) { - string[] patterns = new string[] { $"^()({sourceTableName})()$", $"^()({sourceTableName})(_.*?)$", $"^(.*?_)({sourceTableName})(_.*?)$", $"^(.*?_)({sourceTableName})()$" }; + string[] patterns = new string[] + { + $"^()({sourceTableName})()$", $"^()({sourceTableName})(_.*?)$", + $"^(.*?_)({sourceTableName})(_.*?)$", $"^(.*?_)({sourceTableName})()$" + }; foreach (var aPattern in patterns) { if (Regex.IsMatch(name, aPattern)) @@ -255,6 +216,7 @@ namespace ShardingCore.Helpers } } } + Func listPropertyWhere = x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericTypeDefinition() == typeof(List<>) @@ -280,7 +242,8 @@ namespace ShardingCore.Helpers { foreach (var aValue in (IEnumerable)propertyValue) { - resList.AddRange(GetReplaceGroups((MigrationOperation)aValue, sourceTableName, targetTableName)); + resList.AddRange(GetReplaceGroups((MigrationOperation)aValue, sourceTableName, + targetTableName)); } } }); @@ -288,4 +251,4 @@ namespace ShardingCore.Helpers return resList; } } -} +} \ No newline at end of file diff --git a/src/ShardingCore/IShardingDbContextOptionsBuilderConfig.cs b/src/ShardingCore/IShardingDbContextOptionsBuilderConfig.cs deleted file mode 100644 index 31f6e4de..00000000 --- a/src/ShardingCore/IShardingDbContextOptionsBuilderConfig.cs +++ /dev/null @@ -1,38 +0,0 @@ -//using System; -//using System.Collections.Generic; -//using System.Data.Common; -//using System.Text; -//using Microsoft.EntityFrameworkCore; -//using ShardingCore.Sharding.Abstractions; - -///* -//* @Author: xjm -//* @Description: -//* @Date: 2021/8/20 11:34:55 -//* @Ver: 1.0 -//* @Email: 326308290@qq.com -//*/ -//namespace ShardingCore -//{ -// /// -// /// 分片db context配置构造配置 -// /// -// /// -// public interface IShardingDbContextOptionsBuilderConfig where TShardingDbContext:DbContext,IShardingDbContext -// { -// /// -// /// 如何根据connectionString 配置 DbContextOptionsBuilder -// /// -// /// -// /// -// /// -// DbContextOptionsBuilder UseDbContextOptionsBuilder(string connectionString, DbContextOptionsBuilder dbContextOptionsBuilder); -// /// -// /// 如何根据dbConnection 配置DbContextOptionsBuilder -// /// -// /// -// /// -// /// -// DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection, DbContextOptionsBuilder dbContextOptionsBuilder); -// } -//} diff --git a/src/ShardingCore/Jobs/JobRunnerService.cs b/src/ShardingCore/Jobs/JobRunnerService.cs index 8b37fdae..cdf4a099 100644 --- a/src/ShardingCore/Jobs/JobRunnerService.cs +++ b/src/ShardingCore/Jobs/JobRunnerService.cs @@ -6,7 +6,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading; using System.Threading.Tasks; -using ShardingCore.Logger; + namespace ShardingCore.Jobs { diff --git a/src/ShardingCore/Logger/ShardingLoggerFactory.cs b/src/ShardingCore/Logger/ShardingLoggerFactory.cs index 86fc25fa..3d698505 100644 --- a/src/ShardingCore/Logger/ShardingLoggerFactory.cs +++ b/src/ShardingCore/Logger/ShardingLoggerFactory.cs @@ -1,52 +1,52 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.Contracts; -using System.Text; -using System.Threading; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; - -namespace ShardingCore.Logger -{ - /// - /// - /// - /// Author: xjm - /// Created: 2022/5/18 10:02:25 - /// Email: 326308290@qq.com - public sealed class ShardingLoggerFactory - { - static ILoggerFactory _defaultFactory; - - - static ILoggerFactory NewDefaultFactory() - { - var f = new NullLoggerFactory(); - return f; - } - - /// - /// Gets or sets the default factory. - /// - public static ILoggerFactory DefaultFactory - { - get - { - ILoggerFactory factory = Volatile.Read(ref _defaultFactory); - if (factory == null) - { - factory = NewDefaultFactory(); - ILoggerFactory current = Interlocked.CompareExchange(ref _defaultFactory, factory, null); - if (current != null) - { - return current; - } - } - return factory; - } - set => Volatile.Write(ref _defaultFactory, value); - } - public static ILogger CreateLogger() => DefaultFactory.CreateLogger(); - public static ILogger CreateLogger(string categoryName) => DefaultFactory.CreateLogger(categoryName); - } -} +// using System; +// using System.Collections.Generic; +// using System.Diagnostics.Contracts; +// using System.Text; +// using System.Threading; +// using Microsoft.Extensions.Logging; +// using Microsoft.Extensions.Logging.Abstractions; +// +// namespace ShardingCore.Logger +// { +// /// +// /// +// /// +// /// Author: xjm +// /// Created: 2022/5/18 10:02:25 +// /// Email: 326308290@qq.com +// public sealed class ShardingLoggerFactory +// { +// static ILoggerFactory _defaultFactory; +// +// +// static ILoggerFactory NewDefaultFactory() +// { +// var f = new NullLoggerFactory(); +// return f; +// } +// +// /// +// /// Gets or sets the default factory. +// /// +// public static ILoggerFactory DefaultFactory +// { +// get +// { +// ILoggerFactory factory = Volatile.Read(ref _defaultFactory); +// if (factory == null) +// { +// factory = NewDefaultFactory(); +// ILoggerFactory current = Interlocked.CompareExchange(ref _defaultFactory, factory, null); +// if (current != null) +// { +// return current; +// } +// } +// return factory; +// } +// set => Volatile.Write(ref _defaultFactory, value); +// } +// public static ILogger CreateLogger() => DefaultFactory.CreateLogger(); +// public static ILogger CreateLogger(string categoryName) => DefaultFactory.CreateLogger(categoryName); +// } +// } diff --git a/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs b/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs index dc52093e..3bfeec60 100644 --- a/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs +++ b/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs @@ -18,7 +18,7 @@ using ShardingCore.Core.RuntimeContexts; using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Infrastructures; -using ShardingCore.Logger; + using ShardingCore.Sharding.Abstractions; namespace ShardingCore.Sharding.ShardingDbContextExecutors diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs index c4d34be0..dac76c32 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Extensions; -using ShardingCore.Logger; + using ShardingCore.Sharding.Parsers.Abstractions; using ShardingCore.Sharding.ShardingExecutors.Abstractions; using ShardingCore.Sharding.Visitors.ShardingExtractParameters; diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs index f9ade623..2fe77f4f 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs @@ -14,7 +14,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using ShardingCore.Core; using ShardingCore.Extensions.InternalExtensions; -using ShardingCore.Logger; + using ShardingCore.Sharding.MergeEngines; #if EFCORE2 using Microsoft.EntityFrameworkCore.Internal; diff --git a/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs b/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs index 808e8586..d537573b 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs @@ -11,7 +11,7 @@ using System.Linq; using System.Linq.Expressions; using ShardingCore.Core; using ShardingCore.Extensions.InternalExtensions; -using ShardingCore.Logger; + using ShardingCore.Sharding.Parsers.Abstractions; namespace ShardingCore.Sharding.ShardingExecutors diff --git a/src/ShardingCore/ShardingRuntimeBuilder.cs b/src/ShardingCore/ShardingRuntimeBuilder.cs index ec8c7b7b..03d21f83 100644 --- a/src/ShardingCore/ShardingRuntimeBuilder.cs +++ b/src/ShardingCore/ShardingRuntimeBuilder.cs @@ -70,6 +70,7 @@ namespace ShardingCore { return Build(appServiceProvider, appServiceProvider?.GetService()); } + public IShardingRuntimeContext Build(IServiceProvider appServiceProvider, ILoggerFactory loggerFactory) { var shardingRuntimeContext = new ShardingRuntimeContext(); diff --git a/src/ShardingCore/TableCreator/ShardingTableCreator.cs b/src/ShardingCore/TableCreator/ShardingTableCreator.cs index 2ac23676..53192b7a 100644 --- a/src/ShardingCore/TableCreator/ShardingTableCreator.cs +++ b/src/ShardingCore/TableCreator/ShardingTableCreator.cs @@ -13,7 +13,6 @@ using System.Threading; using ShardingCore.Core.DbContextCreator; using ShardingCore.Core.ServiceProviders; using ShardingCore.Core.ShardingConfigurations; -using ShardingCore.Logger; using ShardingCore.Sharding; namespace ShardingCore.TableCreator diff --git a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs index 959c11e6..6911fc5c 100644 --- a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs @@ -17,7 +17,7 @@ using ShardingCore.Core.VirtualRoutes.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions; using ShardingCore.Extensions; using ShardingCore.Jobs.Abstaractions; -using ShardingCore.Logger; + using ShardingCore.TableCreator; namespace ShardingCore.VirtualRoutes.Abstractions