diff --git a/samples/Sample.AutoCreateIfPresent/AreaDeviceRoute.cs b/samples/Sample.AutoCreateIfPresent/AreaDeviceRoute.cs index b807b952..3a82d015 100644 --- a/samples/Sample.AutoCreateIfPresent/AreaDeviceRoute.cs +++ b/samples/Sample.AutoCreateIfPresent/AreaDeviceRoute.cs @@ -21,6 +21,8 @@ namespace Sample.AutoCreateIfPresent private readonly ConcurrentDictionary _tails = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + // private readonly ConcurrentDictionary> _tails = + // new ConcurrentDictionary>(StringComparer.OrdinalIgnoreCase); private readonly object _lock = new object(); private readonly object _initLock = new object(); diff --git a/samples/Sample.MySql/Shardings/SysUserLogByMonthDSRoute.cs b/samples/Sample.MySql/Shardings/SysUserLogByMonthDSRoute.cs index 45b1cee2..e08b5e78 100644 --- a/samples/Sample.MySql/Shardings/SysUserLogByMonthDSRoute.cs +++ b/samples/Sample.MySql/Shardings/SysUserLogByMonthDSRoute.cs @@ -1,12 +1,22 @@ +using Sample.MySql.DbContexts; using Sample.MySql.Domain.Entities; using ShardingCore.Core.EntityMetadatas; +using ShardingCore.Core.RuntimeContexts; +using ShardingCore.Core.ServiceProviders; using ShardingCore.Core.VirtualRoutes; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions; +using ShardingCore.Helpers; namespace Sample.MySql.Shardings { public class SysUserLogByMonthDSRoute:AbstractShardingOperatorVirtualDataSourceRoute { + private readonly IShardingProvider _shardingProvider; + + public SysUserLogByMonthDSRoute(IShardingProvider shardingProvider) + { + _shardingProvider = shardingProvider; + } public override string ShardingKeyToDataSourceName(object shardingKey) { throw new NotImplementedException(); @@ -37,5 +47,13 @@ namespace Sample.MySql.Shardings //判断过滤查询历史还是现在 throw new NotImplementedException(); } + + // public override string RouteWithValue(object shardingKey) + // { + // //计算出数据源名 + // var dataSourceName = ShardingKeyToDataSourceName(shardingKey); + // var shardingRuntimeContext = _shardingProvider.ApplicationServiceProvider.GetRequiredService>(); + // DynamicShardingHelper.DynamicAppendDataSource(shardingRuntimeContext,dataSourceName,$"server=127.0.0.1;port=3306;database=db_{dataSourceName};userid=root;password=root;",true,true); + // } } } diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 7091de91..89f16597 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -139,7 +139,8 @@ namespace Sample.MySql { // var logger = sp.ApplicationServiceProvider.GetService>(); // logger.LogInformation(conStr); - builder.UseMySql(conStr, new MySqlServerVersion(new Version())); + builder.UseMySql(conStr, new MySqlServerVersion(new Version())) + .UseLoggerFactory(efLogger); // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) // .UseLoggerFactory(loggerFactory1) // .EnableSensitiveDataLogging(); @@ -148,7 +149,8 @@ namespace Sample.MySql o.UseShardingTransaction((connection, builder) => { builder - .UseMySql(connection, new MySqlServerVersion(new Version())); + .UseMySql(connection, new MySqlServerVersion(new Version())) + .UseLoggerFactory(efLogger); // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) // .UseLoggerFactory(loggerFactory1) // .EnableSensitiveDataLogging(); @@ -222,6 +224,7 @@ namespace Sample.MySql // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + // DynamicShardingHelper.DynamicAppendDataSource(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); @@ -236,6 +239,14 @@ namespace Sample.MySql // virtualTableRoute.Append("2021"); } app.ApplicationServices.UseAutoTryCompensateTable(); + + + // var shardingRuntimeContext = app.ApplicationServices.GetRequiredService>(); + // var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); + // var connectionString = virtualDataSource.GetConnectionString("dataSourceName"); + // var defaultConnectionString1 = virtualDataSource.GetDefaultDataSource().ConnectionString; + // var defaultConnectionString2 = virtualDataSource.GetPhysicDataSource("dataSourceName").ConnectionString; + // shardingRuntimeContext.UseAutoTryCompensateTable(); // var shardingRuntimeContext = app.ApplicationServices.GetRequiredService(); // var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); // var entityMetadata = entityMetadataManager.TryGet(); diff --git a/src/ShardingCore/InitConfigureUnit.cs b/src/ShardingCore/InitConfigureUnit.cs index fe5a41dc..894a4bc3 100644 --- a/src/ShardingCore/InitConfigureUnit.cs +++ b/src/ShardingCore/InitConfigureUnit.cs @@ -1,12 +1,21 @@ namespace ShardingCore { + /// + /// 初始化配置执行单元 + /// public class InitConfigureUnit { + /// + /// 初始化配置执行单元 + /// public InitConfigureUnit(string dataSourceName) { DataSourceName = dataSourceName; } + /// + /// 数据源 + /// public string DataSourceName { get; } } } diff --git a/src/ShardingCore/Sharding/DefaultConnectionStringManager.cs b/src/ShardingCore/Sharding/DefaultConnectionStringManager.cs index 15148c5b..439b7774 100644 --- a/src/ShardingCore/Sharding/DefaultConnectionStringManager.cs +++ b/src/ShardingCore/Sharding/DefaultConnectionStringManager.cs @@ -4,13 +4,9 @@ using ShardingCore.Sharding.Abstractions; namespace ShardingCore.Sharding { - /* - * @Author: xjm - * @Description: - * @Date: 2021/9/7 10:32:26 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ + /// + /// 默认的链接字符串管理器 + /// public class DefaultConnectionStringManager : IConnectionStringManager { private readonly IVirtualDataSource _virtualDataSource; @@ -19,6 +15,11 @@ namespace ShardingCore.Sharding { _virtualDataSource = virtualDataSource; } + /// + /// 获取链接字符串根据数据源名称 + /// + /// + /// public string GetConnectionString(string dataSourceName) { if (_virtualDataSource.IsDefault(dataSourceName)) diff --git a/src/ShardingCore/Sharding/StreamMergeContextFactory.cs b/src/ShardingCore/Sharding/StreamMergeContextFactory.cs index d6ee2874..d5f87610 100644 --- a/src/ShardingCore/Sharding/StreamMergeContextFactory.cs +++ b/src/ShardingCore/Sharding/StreamMergeContextFactory.cs @@ -35,12 +35,20 @@ namespace ShardingCore.Sharding _queryableRewriteEngine = queryableRewriteEngine; _queryableOptimizeEngine = queryableOptimizeEngine; } + /// + /// 创建流式合并上下文 + /// + /// + /// public StreamMergeContext Create(IMergeQueryCompilerContext mergeQueryCompilerContext) { + //表达式解析结果 var parseResult = _queryableParseEngine.Parse(mergeQueryCompilerContext); - + //表达式重写结果 var rewriteResult = _queryableRewriteEngine.GetRewriteQueryable(mergeQueryCompilerContext, parseResult); + //表达式优化结果 var optimizeResult = _queryableOptimizeEngine.Optimize(mergeQueryCompilerContext, parseResult, rewriteResult); + //合并上下文 CheckMergeContext(mergeQueryCompilerContext, parseResult, rewriteResult, optimizeResult); return new StreamMergeContext(mergeQueryCompilerContext, parseResult, rewriteResult,optimizeResult); }