添加部分备注信息

This commit is contained in:
xuejiaming 2024-05-28 08:37:08 +08:00
parent 1e715ffa2e
commit 64dc7d90e9
6 changed files with 59 additions and 10 deletions

View File

@ -21,6 +21,8 @@ namespace Sample.AutoCreateIfPresent
private readonly ConcurrentDictionary<string, object?> _tails = private readonly ConcurrentDictionary<string, object?> _tails =
new ConcurrentDictionary<string, object?>(StringComparer.OrdinalIgnoreCase); new ConcurrentDictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
// private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, object?>> _tails =
// new ConcurrentDictionary<string, ConcurrentDictionary<string, object?>>(StringComparer.OrdinalIgnoreCase);
private readonly object _lock = new object(); private readonly object _lock = new object();
private readonly object _initLock = new object(); private readonly object _initLock = new object();

View File

@ -1,12 +1,22 @@
using Sample.MySql.DbContexts;
using Sample.MySql.Domain.Entities; using Sample.MySql.Domain.Entities;
using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Core.ServiceProviders;
using ShardingCore.Core.VirtualRoutes; using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions; using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.Abstractions;
using ShardingCore.Helpers;
namespace Sample.MySql.Shardings namespace Sample.MySql.Shardings
{ {
public class SysUserLogByMonthDSRoute:AbstractShardingOperatorVirtualDataSourceRoute<SysUserLogByMonth,DateTime> public class SysUserLogByMonthDSRoute:AbstractShardingOperatorVirtualDataSourceRoute<SysUserLogByMonth,DateTime>
{ {
private readonly IShardingProvider _shardingProvider;
public SysUserLogByMonthDSRoute(IShardingProvider shardingProvider)
{
_shardingProvider = shardingProvider;
}
public override string ShardingKeyToDataSourceName(object shardingKey) public override string ShardingKeyToDataSourceName(object shardingKey)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@ -37,5 +47,13 @@ namespace Sample.MySql.Shardings
//判断过滤查询历史还是现在 //判断过滤查询历史还是现在
throw new NotImplementedException(); throw new NotImplementedException();
} }
// public override string RouteWithValue(object shardingKey)
// {
// //计算出数据源名
// var dataSourceName = ShardingKeyToDataSourceName(shardingKey);
// var shardingRuntimeContext = _shardingProvider.ApplicationServiceProvider.GetRequiredService<IShardingRuntimeContext<DefaultShardingDbContext>>();
// DynamicShardingHelper.DynamicAppendDataSource(shardingRuntimeContext,dataSourceName,$"server=127.0.0.1;port=3306;database=db_{dataSourceName};userid=root;password=root;",true,true);
// }
} }
} }

View File

@ -139,7 +139,8 @@ namespace Sample.MySql
{ {
// var logger = sp.ApplicationServiceProvider.GetService<ILogger<Startup>>(); // var logger = sp.ApplicationServiceProvider.GetService<ILogger<Startup>>();
// logger.LogInformation(conStr); // logger.LogInformation(conStr);
builder.UseMySql(conStr, new MySqlServerVersion(new Version())); builder.UseMySql(conStr, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger);
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
// .UseLoggerFactory(loggerFactory1) // .UseLoggerFactory(loggerFactory1)
// .EnableSensitiveDataLogging(); // .EnableSensitiveDataLogging();
@ -148,7 +149,8 @@ namespace Sample.MySql
o.UseShardingTransaction((connection, builder) => o.UseShardingTransaction((connection, builder) =>
{ {
builder builder
.UseMySql(connection, new MySqlServerVersion(new Version())); .UseMySql(connection, new MySqlServerVersion(new Version()))
.UseLoggerFactory(efLogger);
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) // .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
// .UseLoggerFactory(loggerFactory1) // .UseLoggerFactory(loggerFactory1)
// .EnableSensitiveDataLogging(); // .EnableSensitiveDataLogging();
@ -222,6 +224,7 @@ namespace Sample.MySql
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
// DynamicShardingHelper.DynamicAppendDataSource();
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
@ -236,6 +239,14 @@ namespace Sample.MySql
// virtualTableRoute.Append("2021"); // virtualTableRoute.Append("2021");
} }
app.ApplicationServices.UseAutoTryCompensateTable(); app.ApplicationServices.UseAutoTryCompensateTable();
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext<DefaultShardingDbContext>>();
// 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<IShardingRuntimeContext>(); // var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager(); // var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
// var entityMetadata = entityMetadataManager.TryGet<SysUserMod>(); // var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();

View File

@ -1,12 +1,21 @@
namespace ShardingCore namespace ShardingCore
{ {
/// <summary>
/// 初始化配置执行单元
/// </summary>
public class InitConfigureUnit public class InitConfigureUnit
{ {
/// <summary>
/// 初始化配置执行单元
/// </summary>
public InitConfigureUnit(string dataSourceName) public InitConfigureUnit(string dataSourceName)
{ {
DataSourceName = dataSourceName; DataSourceName = dataSourceName;
} }
/// <summary>
/// 数据源
/// </summary>
public string DataSourceName { get; } public string DataSourceName { get; }
} }
} }

View File

@ -4,13 +4,9 @@ using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.Sharding namespace ShardingCore.Sharding
{ {
/* /// <summary>
* @Author: xjm /// 默认的链接字符串管理器
* @Description: /// </summary>
* @Date: 2021/9/7 10:32:26
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public class DefaultConnectionStringManager : IConnectionStringManager public class DefaultConnectionStringManager : IConnectionStringManager
{ {
private readonly IVirtualDataSource _virtualDataSource; private readonly IVirtualDataSource _virtualDataSource;
@ -19,6 +15,11 @@ namespace ShardingCore.Sharding
{ {
_virtualDataSource = virtualDataSource; _virtualDataSource = virtualDataSource;
} }
/// <summary>
/// 获取链接字符串根据数据源名称
/// </summary>
/// <param name="dataSourceName"></param>
/// <returns></returns>
public string GetConnectionString(string dataSourceName) public string GetConnectionString(string dataSourceName)
{ {
if (_virtualDataSource.IsDefault(dataSourceName)) if (_virtualDataSource.IsDefault(dataSourceName))

View File

@ -35,12 +35,20 @@ namespace ShardingCore.Sharding
_queryableRewriteEngine = queryableRewriteEngine; _queryableRewriteEngine = queryableRewriteEngine;
_queryableOptimizeEngine = queryableOptimizeEngine; _queryableOptimizeEngine = queryableOptimizeEngine;
} }
/// <summary>
/// 创建流式合并上下文
/// </summary>
/// <param name="mergeQueryCompilerContext"></param>
/// <returns></returns>
public StreamMergeContext Create(IMergeQueryCompilerContext mergeQueryCompilerContext) public StreamMergeContext Create(IMergeQueryCompilerContext mergeQueryCompilerContext)
{ {
//表达式解析结果
var parseResult = _queryableParseEngine.Parse(mergeQueryCompilerContext); var parseResult = _queryableParseEngine.Parse(mergeQueryCompilerContext);
//表达式重写结果
var rewriteResult = _queryableRewriteEngine.GetRewriteQueryable(mergeQueryCompilerContext, parseResult); var rewriteResult = _queryableRewriteEngine.GetRewriteQueryable(mergeQueryCompilerContext, parseResult);
//表达式优化结果
var optimizeResult = _queryableOptimizeEngine.Optimize(mergeQueryCompilerContext, parseResult, rewriteResult); var optimizeResult = _queryableOptimizeEngine.Optimize(mergeQueryCompilerContext, parseResult, rewriteResult);
//合并上下文
CheckMergeContext(mergeQueryCompilerContext, parseResult, rewriteResult, optimizeResult); CheckMergeContext(mergeQueryCompilerContext, parseResult, rewriteResult, optimizeResult);
return new StreamMergeContext(mergeQueryCompilerContext, parseResult, rewriteResult,optimizeResult); return new StreamMergeContext(mergeQueryCompilerContext, parseResult, rewriteResult,optimizeResult);
} }