v3.4.6
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
using JNPF;
|
||||
using JNPF.DataEncryption;
|
||||
using JNPF.Extras.DatabaseAccessor.SqlSugar.Models;
|
||||
using JNPF.FriendlyException;
|
||||
using JNPF.JsonSerialization;
|
||||
using JNPF.Logging;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar;
|
||||
|
||||
@@ -16,95 +16,98 @@ namespace SqlSugar;
|
||||
public partial class SqlSugarRepository<TEntity> : SimpleClient<TEntity>, ISqlSugarRepository<TEntity>
|
||||
where TEntity : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public SqlSugarRepository(ISqlSugarClient context = null) : base(context)
|
||||
{
|
||||
// 获取数据库连接选项
|
||||
ConnectionStringsOptions connectionStrings = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
|
||||
|
||||
// 获取多租户选项
|
||||
TenantOptions tenant = App.GetConfig<TenantOptions>("Tenant", true);
|
||||
var httpContext = App.HttpContext;
|
||||
|
||||
base.Context = (SqlSugarScope)context;
|
||||
|
||||
string tenantId = connectionStrings.ConfigId;
|
||||
|
||||
if (httpContext?.GetEndpoint()?.Metadata?.GetMetadata<AllowAnonymousAttribute>() == null || !string.IsNullOrEmpty(httpContext?.Request.Query["token"]))
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public SqlSugarRepository(ISqlSugarClient context = null) : base(context)
|
||||
{
|
||||
if (tenant.MultiTenancy && httpContext != null)
|
||||
{
|
||||
var connectionConfig = new ConnectionConfigOptions();
|
||||
if (httpContext.Request.Headers.ContainsKey("Authorization"))
|
||||
// 获取数据库连接选项
|
||||
ConnectionStringsOptions connectionStrings = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
|
||||
|
||||
// 获取多租户选项
|
||||
TenantOptions tenant = App.GetConfig<TenantOptions>("Tenant", true);
|
||||
var httpContext = App.HttpContext;
|
||||
|
||||
base.Context = (SqlSugarScope)context;
|
||||
|
||||
string tenantId = connectionStrings.ConfigId;
|
||||
string tenantDbName = string.Empty;
|
||||
|
||||
if (httpContext?.GetEndpoint()?.Metadata?.GetMetadata<AllowAnonymousAttribute>() == null)
|
||||
{
|
||||
connectionConfig = JSON.Deserialize<ConnectionConfigOptions>(httpContext?.User.FindFirst("ConnectionConfig")?.Value);
|
||||
tenantId = connectionConfig.ConfigId;
|
||||
if (tenant.MultiTenancy && httpContext != null)
|
||||
{
|
||||
var connectionConfig = JSON.Deserialize<ConnectionConfigOptions>(httpContext?.User.FindFirst("ConnectionConfig")?.Value);
|
||||
tenantId = connectionConfig.ConfigId;
|
||||
tenantDbName = connectionConfig.ConfigList.FirstOrDefault().ServiceName;
|
||||
if (!base.Context.AsTenant().IsAnyConnection(connectionConfig.ConfigId))
|
||||
{
|
||||
base.Context.AsTenant().AddConnection(JNPFTenantExtensions.GetConfig(connectionConfig));
|
||||
}
|
||||
base.Context = base.Context.AsTenant().GetConnectionScope(connectionConfig.ConfigId);
|
||||
|
||||
if (!base.Context.Ado.IsValidConnection())
|
||||
{
|
||||
throw Oops.Oh("数据库连接错误");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context = base.Context.AsTenant().GetConnectionScope(tenantId);
|
||||
}
|
||||
}
|
||||
else if (!httpContext.Request.Headers.ContainsKey("Authorization"))
|
||||
// 字段数据隔离
|
||||
if (!"default".Equals(tenantId) && tenant.MultiTenancyType.Equals("COLUMN"))
|
||||
{
|
||||
var token = Regex.Match(httpContext.Request.QueryString.Value, @"[?&]token=Bearer%20([\w\.-]+)($|&)").Groups[1].Value;
|
||||
var claims = JWTEncryption.ReadJwtToken(token.Replace("Bearer ", "").Replace("bearer ", ""))?.Claims;
|
||||
connectionConfig = JSON.Deserialize<ConnectionConfigOptions>(claims.FirstOrDefault(e => e.Type == "ConnectionConfig").Value);
|
||||
tenantId = connectionConfig.ConfigId;
|
||||
base.Context.QueryFilter.AddTableFilter<ITenantFilter>(it => it.TenantId == tenantDbName);
|
||||
base.Context.Aop.DataExecuting = (oldValue, entityInfo) =>
|
||||
{
|
||||
if (entityInfo.PropertyName == "TenantId" && entityInfo.OperationType == DataFilterType.InsertByObject)
|
||||
{
|
||||
entityInfo.SetValue(tenantDbName);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (!base.Context.AsTenant().IsAnyConnection(connectionConfig.ConfigId))
|
||||
// 设置超时时间
|
||||
base.Context.Ado.CommandTimeOut = 30;
|
||||
base.Context.Aop.OnLogExecuted = (sql, pars) =>
|
||||
{
|
||||
base.Context.AsTenant().AddConnection(JNPFTenantExtensions.GetConfig(connectionConfig));
|
||||
}
|
||||
base.Context = base.Context.AsTenant().GetConnectionScope(connectionConfig.ConfigId);
|
||||
|
||||
if (!base.Context.Ado.IsValidConnection())
|
||||
{
|
||||
throw Oops.Oh("数据库连接错误");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context = base.Context.AsTenant().GetConnectionScope(tenantId);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置超时时间
|
||||
base.Context.Ado.CommandTimeOut = 30;
|
||||
base.Context.Aop.OnLogExecuted = (sql, pars) =>
|
||||
{
|
||||
var oldColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
var finalSql = UtilMethods.GetSqlString(Context.CurrentConnectionConfig.DbType, sql, pars);
|
||||
Console.WriteLine($"【{DateTime.Now.ToString("HH:mm:ss.fff")}——SQL执行完成】{Context.Ado.SqlExecutionTime.TotalMilliseconds} ms");
|
||||
Console.WriteLine(finalSql);
|
||||
Console.ForegroundColor = oldColor;
|
||||
if (Context.Ado.SqlExecutionTime.TotalMilliseconds > 3000)
|
||||
{
|
||||
Log.Warning($"慢查询: {Context.Ado.SqlExecutionTime.TotalMilliseconds}ms, SQL: " + finalSql);
|
||||
}
|
||||
Console.WriteLine();
|
||||
//App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + base.Context.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
||||
};
|
||||
base.Context.Aop.OnError = (ex) =>
|
||||
{
|
||||
Log.Error("SQL执行错误: " + UtilMethods.GetSqlString(base.Context.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
|
||||
//App.PrintToMiniProfiler("SqlSugar", "Error", $"{ex.Message}{Environment.NewLine}{ex.Sql}{pars}{Environment.NewLine}");
|
||||
};
|
||||
|
||||
if (base.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
{
|
||||
base.Context.Aop.OnExecutingChangeSql = (sql, pars) =>
|
||||
{
|
||||
if (pars != null)
|
||||
{
|
||||
foreach (var item in pars)
|
||||
{
|
||||
//如果是DbTppe=string设置成OracleDbType.Nvarchar2
|
||||
item.IsNvarchar2 = true;
|
||||
}
|
||||
var oldColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
var finalSql = UtilMethods.GetSqlString(Context.CurrentConnectionConfig.DbType, sql, pars);
|
||||
Console.WriteLine($"【{DateTime.Now.ToString("HH:mm:ss.fff")}——SQL执行完成】{Context.Ado.SqlExecutionTime.TotalMilliseconds} ms");
|
||||
Console.WriteLine(finalSql);
|
||||
Console.ForegroundColor = oldColor;
|
||||
if (Context.Ado.SqlExecutionTime.TotalMilliseconds > 3000)
|
||||
{
|
||||
Log.Warning($"慢查询: {Context.Ado.SqlExecutionTime.TotalMilliseconds}ms, SQL: " + finalSql);
|
||||
}
|
||||
Console.WriteLine();
|
||||
//App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + base.Context.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
||||
};
|
||||
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
|
||||
};
|
||||
base.Context.Aop.OnError = (ex) =>
|
||||
{
|
||||
Log.Error("SQL执行错误: " + UtilMethods.GetSqlString(base.Context.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
|
||||
//App.PrintToMiniProfiler("SqlSugar", "Error", $"{ex.Message}{Environment.NewLine}{ex.Sql}{pars}{Environment.NewLine}");
|
||||
};
|
||||
|
||||
|
||||
if (base.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
|
||||
{
|
||||
base.Context.Aop.OnExecutingChangeSql = (sql, pars) =>
|
||||
{
|
||||
if (pars != null)
|
||||
{
|
||||
foreach (var item in pars)
|
||||
{
|
||||
//如果是DbTppe=string设置成OracleDbType.Nvarchar2
|
||||
item.IsNvarchar2 = true;
|
||||
}
|
||||
};
|
||||
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user