😎代码优化
This commit is contained in:
parent
e33777d783
commit
249fabf32c
@ -131,15 +131,15 @@ public static class SqlSugarExtension
|
|||||||
|
|
||||||
Expression binaryExpresioFilter;
|
Expression binaryExpresioFilter;
|
||||||
|
|
||||||
if (filter.Logic.HasValue)
|
if (Enum.IsDefined(typeof(FilterLogicEnum), filter.Logic))
|
||||||
{
|
{
|
||||||
if (filter.Filters is null) throw new ArgumentException("The Filters attribute is required when declaring a logic");
|
if (filter.Filters is null) throw new ArgumentException("The Filters attribute is required when declaring a logic");
|
||||||
binaryExpresioFilter = CreateFilterExpression(filter.Logic.Value, filter.Filters, parameter);
|
binaryExpresioFilter = CreateFilterExpression(filter.Logic, filter.Filters, parameter);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var filterValid = GetValidFilter(filter);
|
var filterValid = GetValidFilter(filter);
|
||||||
binaryExpresioFilter = CreateFilterExpression(filterValid.Field!, filterValid.Operator.Value, filterValid.Value, parameter);
|
binaryExpresioFilter = CreateFilterExpression(filterValid.Field!, filterValid.Operator, filterValid.Value, parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
var lambda = Expression.Lambda<Func<T, bool>>(binaryExpresioFilter, parameter);
|
var lambda = Expression.Lambda<Func<T, bool>>(binaryExpresioFilter, parameter);
|
||||||
@ -175,15 +175,15 @@ public static class SqlSugarExtension
|
|||||||
{
|
{
|
||||||
Expression bExpresionFilter;
|
Expression bExpresionFilter;
|
||||||
|
|
||||||
if (filter.Logic.HasValue)
|
if (Enum.IsDefined(typeof(FilterLogicEnum), filter.Logic))
|
||||||
{
|
{
|
||||||
if (filter.Filters is null) throw new ArgumentException("The Filters attribute is required when declaring a logic");
|
if (filter.Filters is null) throw new ArgumentException("The Filters attribute is required when declaring a logic");
|
||||||
bExpresionFilter = CreateFilterExpression(filter.Logic.Value, filter.Filters, parameter);
|
bExpresionFilter = CreateFilterExpression(filter.Logic, filter.Filters, parameter);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var filterValid = GetValidFilter(filter);
|
var filterValid = GetValidFilter(filter);
|
||||||
bExpresionFilter = CreateFilterExpression(filterValid.Field!, filterValid.Operator.Value, filterValid.Value, parameter);
|
bExpresionFilter = CreateFilterExpression(filterValid.Field!, filterValid.Operator, filterValid.Value, parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
filterExpression = filterExpression is null ? bExpresionFilter : CombineFilter(filterLogic, filterExpression, bExpresionFilter);
|
filterExpression = filterExpression is null ? bExpresionFilter : CombineFilter(filterLogic, filterExpression, bExpresionFilter);
|
||||||
|
|||||||
@ -153,7 +153,6 @@ public static class SqlSugarSetup
|
|||||||
if (ex.Parametres == null) return;
|
if (ex.Parametres == null) return;
|
||||||
var log = $"【{DateTime.Now}——错误SQL】\r\n{UtilMethods.GetNativeSql(ex.Sql, (SugarParameter[])ex.Parametres)}\r\n";
|
var log = $"【{DateTime.Now}——错误SQL】\r\n{UtilMethods.GetNativeSql(ex.Sql, (SugarParameter[])ex.Parametres)}\r\n";
|
||||||
Log.Error(log, ex);
|
Log.Error(log, ex);
|
||||||
App.PrintToMiniProfiler("SqlSugar", "Error", log);
|
|
||||||
};
|
};
|
||||||
if (enableConsoleSql)
|
if (enableConsoleSql)
|
||||||
{
|
{
|
||||||
@ -177,7 +176,6 @@ public static class SqlSugarSetup
|
|||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
Console.WriteLine(log);
|
Console.WriteLine(log);
|
||||||
Console.ForegroundColor = originColor;
|
Console.ForegroundColor = originColor;
|
||||||
App.PrintToMiniProfiler("SqlSugar", "Info", log);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
dbProvider.Aop.OnLogExecuted = (sql, pars) =>
|
dbProvider.Aop.OnLogExecuted = (sql, pars) =>
|
||||||
@ -198,7 +196,6 @@ public static class SqlSugarSetup
|
|||||||
var firstMethodName = dbProvider.Ado.SqlStackTrace.FirstMethodName; // 方法名
|
var firstMethodName = dbProvider.Ado.SqlStackTrace.FirstMethodName; // 方法名
|
||||||
var log = $"【{DateTime.Now}——超时SQL】\r\n【所在文件名】:{fileName}\r\n【代码行数】:{fileLine}\r\n【方法名】:{firstMethodName}\r\n" + $"【SQL语句】:{UtilMethods.GetNativeSql(sql, pars)}";
|
var log = $"【{DateTime.Now}——超时SQL】\r\n【所在文件名】:{fileName}\r\n【代码行数】:{fileLine}\r\n【方法名】:{firstMethodName}\r\n" + $"【SQL语句】:{UtilMethods.GetNativeSql(sql, pars)}";
|
||||||
Log.Warning(log);
|
Log.Warning(log);
|
||||||
App.PrintToMiniProfiler("SqlSugar", "Slow", log);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 数据审计
|
// 数据审计
|
||||||
@ -380,6 +377,7 @@ public static class SqlSugarSetup
|
|||||||
// 初始化表结构
|
// 初始化表结构
|
||||||
if (config.TableSettings.EnableInitTable || isFirstRun)
|
if (config.TableSettings.EnableInitTable || isFirstRun)
|
||||||
{
|
{
|
||||||
|
var totalWatch = Stopwatch.StartNew(); // 开始总计时
|
||||||
Log.Information($"初始化表结构 {config.DbType} - {config.ConfigId}");
|
Log.Information($"初始化表结构 {config.DbType} - {config.ConfigId}");
|
||||||
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))
|
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))
|
||||||
.Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
|
.Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
|
||||||
@ -392,6 +390,15 @@ public static class SqlSugarSetup
|
|||||||
else
|
else
|
||||||
entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
|
entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
|
||||||
|
|
||||||
|
// 删除视图再初始化表结构,防止因为视图导致无法同步表结构
|
||||||
|
var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
|
||||||
|
foreach (var viewType in viewTypeList)
|
||||||
|
{
|
||||||
|
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
|
||||||
|
if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
|
||||||
|
dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
|
||||||
|
}
|
||||||
|
|
||||||
int taskIndex = 0, size = entityTypes.Count;
|
int taskIndex = 0, size = entityTypes.Count;
|
||||||
var taskList = entityTypes.Select(entityType => Task.Run(() =>
|
var taskList = entityTypes.Select(entityType => Task.Run(() =>
|
||||||
{
|
{
|
||||||
@ -405,6 +412,10 @@ public static class SqlSugarSetup
|
|||||||
Console.WriteLine($"初始化表 {entityType,-64} ({config.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003}) 耗时:{stopWatch.ElapsedMilliseconds} ms");
|
Console.WriteLine($"初始化表 {entityType,-64} ({config.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003}) 耗时:{stopWatch.ElapsedMilliseconds} ms");
|
||||||
}));
|
}));
|
||||||
Task.WaitAll(taskList.ToArray());
|
Task.WaitAll(taskList.ToArray());
|
||||||
|
|
||||||
|
totalWatch.Stop(); // 停止总计时
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
Console.WriteLine($"初始化表结构 {config.DbType} - {config.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds} ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化视图
|
// 初始化视图
|
||||||
@ -512,6 +523,7 @@ public static class SqlSugarSetup
|
|||||||
/// <param name="enableIncreSeed"></param>
|
/// <param name="enableIncreSeed"></param>
|
||||||
private static void InitSeedData(SqlSugarScopeProvider dbProvider, bool enableIncreSeed)
|
private static void InitSeedData(SqlSugarScopeProvider dbProvider, bool enableIncreSeed)
|
||||||
{
|
{
|
||||||
|
var totalWatch = Stopwatch.StartNew(); // 开始总计时
|
||||||
Log.Information($"初始化种子数据 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId}");
|
Log.Information($"初始化种子数据 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId}");
|
||||||
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
|
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
|
||||||
.Where(u => !u.IsDefined(typeof(TenantSeedAttribute), false))
|
.Where(u => !u.IsDefined(typeof(TenantSeedAttribute), false))
|
||||||
@ -533,6 +545,10 @@ public static class SqlSugarSetup
|
|||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.WriteLine($"初始化种子数据 {seedType.FullName,-58} ({dbProvider.CurrentConnectionConfig.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},数据量:{tuple.Value.Item1:D003},新增 {tuple.Value.Item2:D003} 条记录,更新 {tuple.Value.Item3:D003} 条记录,耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
|
Console.WriteLine($"初始化种子数据 {seedType.FullName,-58} ({dbProvider.CurrentConnectionConfig.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},数据量:{tuple.Value.Item1:D003},新增 {tuple.Value.Item2:D003} 条记录,更新 {tuple.Value.Item3:D003} 条记录,耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
totalWatch.Stop(); // 停止总计时
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
Console.WriteLine($"初始化种子数据 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds:N0} ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -541,6 +557,7 @@ public static class SqlSugarSetup
|
|||||||
/// <param name="dbProvider"></param>
|
/// <param name="dbProvider"></param>
|
||||||
private static void InitView(SqlSugarScopeProvider dbProvider)
|
private static void InitView(SqlSugarScopeProvider dbProvider)
|
||||||
{
|
{
|
||||||
|
var totalWatch = Stopwatch.StartNew(); // 开始总计时
|
||||||
Log.Information($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId}");
|
Log.Information($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId}");
|
||||||
var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
|
var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
|
||||||
|
|
||||||
@ -554,7 +571,7 @@ public static class SqlSugarSetup
|
|||||||
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
|
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
|
||||||
|
|
||||||
// 如果视图存在,则删除视图
|
// 如果视图存在,则删除视图
|
||||||
if (dbProvider.DbMaintenance.GetViewInfoList().Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
|
if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
|
||||||
dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
|
dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
|
||||||
|
|
||||||
// 获取初始化视图查询SQL
|
// 获取初始化视图查询SQL
|
||||||
@ -570,6 +587,10 @@ public static class SqlSugarSetup
|
|||||||
Console.WriteLine($"初始化视图 {viewType.FullName,-58} ({dbProvider.CurrentConnectionConfig.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
|
Console.WriteLine($"初始化视图 {viewType.FullName,-58} ({dbProvider.CurrentConnectionConfig.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
|
||||||
}));
|
}));
|
||||||
Task.WaitAll(taskList.ToArray());
|
Task.WaitAll(taskList.ToArray());
|
||||||
|
|
||||||
|
totalWatch.Stop(); // 停止总计时
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
Console.WriteLine($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds:N0} ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user