feat: 完善sass多租户,支持不同数据库类型

This commit is contained in:
橙子
2024-07-07 00:59:34 +08:00
parent b49ae9c034
commit 38bfc87fc1
6 changed files with 91 additions and 23 deletions

View File

@@ -1,4 +1,5 @@
using System.Reflection;
using System;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
@@ -37,7 +38,7 @@ namespace Yi.Framework.SqlSugarCore
public IEntityChangeEventHelper EntityChangeEventHelper => LazyServiceProvider.LazyGetService<IEntityChangeEventHelper>(NullEntityChangeEventHelper.Instance);
public DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService<IOptions<DbConnOptions>>().Value;
public AbpDbConnectionOptions ConnectionOptions=> LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDbConnectionOptions>>().Value;
public AbpDbConnectionOptions ConnectionOptions => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDbConnectionOptions>>().Value;
private ISqlSugarDbConnectionCreator _dbConnectionCreator;
public void SetSqlSugarClient(ISqlSugarClient sqlSugarClient)
@@ -56,9 +57,11 @@ namespace Yi.Framework.SqlSugarCore
connectionCreator.OnLogExecuting = OnLogExecuting;
connectionCreator.OnLogExecuted = OnLogExecuted;
var currentConnection = GetCurrentConnectionString();
var currentDbType = GetCurrentDbType();
SqlSugarClient = new SqlSugarClient(connectionCreator.Build(action: options =>
{
options.ConnectionString = currentConnection;
}));
connectionCreator.SetDbAop(SqlSugarClient);
}
@@ -78,7 +81,7 @@ namespace Yi.Framework.SqlSugarCore
//开启了多租户
var connectionStringResolver = LazyServiceProvider.LazyGetRequiredService<IConnectionStringResolver>();
var connectionString = connectionStringResolver.ResolveAsync().Result;
var connectionString = connectionStringResolver.ResolveAsync().GetAwaiter().GetResult();
//没有检测到使用多租户功能,默认使用默认库即可
@@ -90,6 +93,50 @@ namespace Yi.Framework.SqlSugarCore
return connectionString!;
}
protected virtual DbType GetCurrentDbType()
{
if (CurrentTenant.Name is not null)
{
var dbTypeFromTenantName = GetDbTypeFromTenantName(CurrentTenant.Name);
if (dbTypeFromTenantName is not null)
{
return dbTypeFromTenantName.Value;
}
}
Volo.Abp.Check.NotNull(Options.DbType, "默认DbType未配置");
return Options.DbType!.Value;
}
//根据租户name进行匹配db类型: Test_Sqlite[来自AI]
private DbType? GetDbTypeFromTenantName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return null;
}
// 查找下划线的位置
int underscoreIndex = name.LastIndexOf('_');
if (underscoreIndex == -1 || underscoreIndex == name.Length - 1)
{
return null;
}
// 提取 枚举 部分
string enumString = name.Substring(underscoreIndex + 1);
// 尝试将 尾缀 转换为枚举
if (Enum.TryParse<DbType>(enumString, out DbType result))
{
return result;
}
// 条件不满足时返回 null
return null;
}
/// <summary>
/// 上下文对象扩展

View File

@@ -14,7 +14,7 @@ namespace Yi.Framework.SqlSugarCore.Uow
public class UnitOfWorkSqlsugarDbContextProvider<TDbContext> : ISugarDbContextProvider<TDbContext> where TDbContext : ISqlSugarDbContext
{
private readonly ISqlSugarDbConnectionCreator _dbConnectionCreator;
public ILogger<UnitOfWorkSqlsugarDbContextProvider<TDbContext>> Logger { get; set; }
public IServiceProvider ServiceProvider { get; set; }
@@ -64,8 +64,7 @@ namespace Yi.Framework.SqlSugarCore.Uow
//lock (_databaseApiLock)
//{
//尝试当前工作单元获取db
var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey);
@@ -74,7 +73,7 @@ namespace Yi.Framework.SqlSugarCore.Uow
{
//db根据连接字符串来创建
databaseApi = new SqlSugarDatabaseApi(
CreateDbContextAsync(unitOfWork, connectionStringName, connectionString).Result
await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString)
);
//await Console.Out.WriteLineAsync(">>>----------------实例化了db"+ ((SqlSugarDatabaseApi)databaseApi).DbContext.SqlSugarClient.ContextID.ToString());
@@ -83,8 +82,6 @@ namespace Yi.Framework.SqlSugarCore.Uow
}
return (TDbContext)((SqlSugarDatabaseApi)databaseApi).DbContext;
//}
}