From 020aeabaaa40df3a970d10aff3a5d4fdc77ac962 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 10 Aug 2022 15:09:42 +0800 Subject: [PATCH] Update .net core --- .../Abstract/AdoProvider/AdoProvider.cs | 12 ++++++++++ .../Abstract/AopProvider/AopProvider.cs | 1 + .../DbBindProvider/DbBindAccessory.cs | 23 ++++++++++++++++++- .../SqlSugar/Entities/ConnectionConfig.cs | 2 +- .../SqlSugar/Enum/DataFilterType.cs | 19 +++++++++++++++ .../Subquery/Items/SubInnerJoin.cs | 1 + .../Subquery/Items/SubLeftJoin.cs | 1 + .../Subquery/Items/SubSelect.cs | 19 ++++++++++----- Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs | 1 + .../SqlSugar/Utilities/UtilMethods.cs | 1 + 10 files changed, 72 insertions(+), 8 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index b244d4120..832c7f701 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -87,6 +87,18 @@ namespace SqlSugar return false; } } + public virtual bool IsValidConnectionNoClose() + { + try + { + this.Open(); + return true; + } + catch (Exception) + { + return false; + } + } public virtual void Open() { CheckConnection(); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/AopProvider/AopProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/AopProvider/AopProvider.cs index a405ac1e9..b22f89a4b 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/AopProvider/AopProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/AopProvider/AopProvider.cs @@ -20,5 +20,6 @@ namespace SqlSugar public Action OnLogExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted = value; } } public Func> OnExecutingChangeSql { set { this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql = value; } } public virtual Action DataExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuting = value; } } + public virtual Action DataExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuted = value; } } } } diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs index 428d2ccca..c19373484 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs @@ -16,6 +16,7 @@ namespace SqlSugar string types = null; var fieldNames = GetDataReaderNames(dataReader,ref types); string cacheKey = GetCacheKey(type,fieldNames) + types; + var dataAfterFunc = context.CurrentConnectionConfig?.AopEvents?.DataExecuted; IDataReaderEntityBuilder entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => { var cacheResult = new IDataReaderEntityBuilder(context, dataReader,fieldNames).CreateBuilder(type); @@ -29,6 +30,7 @@ namespace SqlSugar { result.Add(entytyList.Build(dataReader)); } + ExecuteDataAfterFun(context, dataAfterFunc, result); } catch (Exception ex) { @@ -36,12 +38,14 @@ namespace SqlSugar } return result; } + protected async Task> GetEntityListAsync(SqlSugarProvider context, IDataReader dataReader) { Type type = typeof(T); string types = null; var fieldNames = GetDataReaderNames(dataReader,ref types); string cacheKey = GetCacheKey(type, fieldNames)+types; + var dataAfterFunc = context.CurrentConnectionConfig?.AopEvents?.DataExecuted; IDataReaderEntityBuilder entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => { var cacheResult = new IDataReaderEntityBuilder(context, dataReader, fieldNames).CreateBuilder(type); @@ -55,6 +59,7 @@ namespace SqlSugar { result.Add(entytyList.Build(dataReader)); } + ExecuteDataAfterFun(context, dataAfterFunc, result); } catch (Exception ex) { @@ -62,8 +67,24 @@ namespace SqlSugar } return result; } + private static void ExecuteDataAfterFun(SqlSugarProvider context, Action dataAfterFunc, List result) + { + if (dataAfterFunc != null) + { + var entity = context.EntityMaintenance.GetEntityInfo(); + foreach (var item in result) + { + dataAfterFunc(item, new DataAfterModel() + { + EntityColumnInfos = entity.Columns, + Entity = entity, + EntityValue = item + }); + } + } + } - private string GetCacheKey(Type type,List keys) + private string GetCacheKey(Type type,List keys) { StringBuilder sb = new StringBuilder("DataReaderToList."); sb.Append(type.FullName); diff --git a/Src/Asp.NetCore2/SqlSugar/Entities/ConnectionConfig.cs b/Src/Asp.NetCore2/SqlSugar/Entities/ConnectionConfig.cs index fdbad7915..b1bbf3141 100644 --- a/Src/Asp.NetCore2/SqlSugar/Entities/ConnectionConfig.cs +++ b/Src/Asp.NetCore2/SqlSugar/Entities/ConnectionConfig.cs @@ -88,7 +88,7 @@ namespace SqlSugar public Action OnLogExecuted { get; set; } public Func> OnExecutingChangeSql { get; set; } public Action DataExecuting { get; set; } - + public Action DataExecuted { get; set; } } public class ConfigureExternalServices { diff --git a/Src/Asp.NetCore2/SqlSugar/Enum/DataFilterType.cs b/Src/Asp.NetCore2/SqlSugar/Enum/DataFilterType.cs index c35c486d6..039ab15f9 100644 --- a/Src/Asp.NetCore2/SqlSugar/Enum/DataFilterType.cs +++ b/Src/Asp.NetCore2/SqlSugar/Enum/DataFilterType.cs @@ -31,4 +31,23 @@ namespace SqlSugar this.EntityColumnInfo.PropertyInfo.SetValue(EntityValue, value); } } + public class DataAfterModel + { + + public List EntityColumnInfos { get; set; } + public object EntityValue { get; set; } + public EntityInfo Entity { get; set; } + public object GetValue(string propertyName) + { + var propety=EntityColumnInfos.FirstOrDefault(it => it.PropertyName == propertyName); + Check.ExceptionEasy(propety==null,$"Aop.DataExecuted error . { Entity.EntityName} no property {propertyName}.", $"Aop.DataExecuted 出错 {Entity.EntityName}不存在属性{propertyName}"); + return propety.PropertyInfo.GetValue(EntityValue); + } + public void SetValue(string propertyName,object value) + { + var propety = EntityColumnInfos.FirstOrDefault(it => it.PropertyName == propertyName); + Check.ExceptionEasy(propety == null, $"Aop.DataExecuted error . { Entity.EntityName} no property {propertyName}.", $"Aop.DataExecuted 出错 {Entity.EntityName}不存在属性{propertyName}"); + propety.PropertyInfo.SetValue(EntityValue,value); + } + } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubInnerJoin.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubInnerJoin.cs index afe847314..ae7675d7a 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubInnerJoin.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubInnerJoin.cs @@ -46,6 +46,7 @@ namespace SqlSugar var name =this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters[0].Name); var parameter = (argExp as LambdaExpression).Parameters.Last(); Context.InitMappingInfo(parameter.Type); + this.Context.RefreshMapping(); var tableName= Context.GetTranslationTableName(parameter.Type.Name, true); var joinString =string.Format(" {2} INNER JOIN {1} {0} ", this.Context.GetTranslationColumnName(parameter.Name), diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubLeftJoin.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubLeftJoin.cs index dcbe48638..79cb43780 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubLeftJoin.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubLeftJoin.cs @@ -46,6 +46,7 @@ namespace SqlSugar var name =this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters[0].Name); var parameter = (argExp as LambdaExpression).Parameters.Last(); Context.InitMappingInfo(parameter.Type); + this.Context.RefreshMapping(); var tableName= Context.GetTranslationTableName(parameter.Type.Name, true); var joinString =string.Format(" {2} LEFT JOIN {1} {0} ", this.Context.GetTranslationColumnName(parameter.Name), diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubSelect.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubSelect.cs index 561cd79dc..37dcb8b47 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubSelect.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/Subquery/Items/SubSelect.cs @@ -43,12 +43,7 @@ namespace SqlSugar public string GetValue(Expression expression = null) { var exp = expression as MethodCallExpression; - var entityType = (exp.Arguments[0] as LambdaExpression).Parameters[0].Type; - if (this.Context.InitMappingInfo != null) - { - this.Context.InitMappingInfo(entityType); - this.Context.RefreshMapping(); - } + InitType(exp); var result = ""; if (this.Context.JoinIndex == 0) result = SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.FieldSingle); @@ -60,6 +55,18 @@ namespace SqlSugar return result; } + private void InitType(MethodCallExpression exp) + { + foreach (var arg in (exp.Arguments[0] as LambdaExpression).Parameters) + { + if (this.Context.InitMappingInfo != null) + { + this.Context.InitMappingInfo(arg.Type); + this.Context.RefreshMapping(); + } + } + } + public void SetShortName(MethodCallExpression exp, string result) { if (exp.Arguments[0] is LambdaExpression && result.IsContainsIn("+", "-","*","/")) diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs index eecc87dba..cce2dae03 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IAdo.cs @@ -165,6 +165,7 @@ namespace SqlSugar void Open(); SugarConnection OpenAlways(); bool IsValidConnection(); + bool IsValidConnectionNoClose(); void CheckConnection(); void BeginTran(); diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs index 646b3a9d2..a01cf35fd 100644 --- a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs @@ -87,6 +87,7 @@ namespace SqlSugar OnExecutingChangeSql=it.AopEvents?.OnExecutingChangeSql, OnLogExecuted=it.AopEvents?.OnLogExecuted, OnLogExecuting= it.AopEvents?.OnLogExecuting, + DataExecuted = it.AopEvents?.DataExecuted, }, ConfigId = it.ConfigId, ConfigureExternalServices =it.ConfigureExternalServices==null?null:new ConfigureExternalServices() {