Update .net core

This commit is contained in:
sunkaixuan 2022-08-10 15:09:42 +08:00
parent 2ea420bc7c
commit 020aeabaaa
10 changed files with 72 additions and 8 deletions

View File

@ -87,6 +87,18 @@ namespace SqlSugar
return false; return false;
} }
} }
public virtual bool IsValidConnectionNoClose()
{
try
{
this.Open();
return true;
}
catch (Exception)
{
return false;
}
}
public virtual void Open() public virtual void Open()
{ {
CheckConnection(); CheckConnection();

View File

@ -20,5 +20,6 @@ namespace SqlSugar
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted = value; } } public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted = value; } }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { set { this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql = value; } } public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { set { this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql = value; } }
public virtual Action<object, DataFilterModel> DataExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuting = value; } } public virtual Action<object, DataFilterModel> DataExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuting = value; } }
public virtual Action<object, DataAfterModel> DataExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuted = value; } }
} }
} }

View File

@ -16,6 +16,7 @@ namespace SqlSugar
string types = null; string types = null;
var fieldNames = GetDataReaderNames(dataReader,ref types); var fieldNames = GetDataReaderNames(dataReader,ref types);
string cacheKey = GetCacheKey(type,fieldNames) + types; string cacheKey = GetCacheKey(type,fieldNames) + types;
var dataAfterFunc = context.CurrentConnectionConfig?.AopEvents?.DataExecuted;
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
{ {
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader,fieldNames).CreateBuilder(type); var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader,fieldNames).CreateBuilder(type);
@ -29,6 +30,7 @@ namespace SqlSugar
{ {
result.Add(entytyList.Build(dataReader)); result.Add(entytyList.Build(dataReader));
} }
ExecuteDataAfterFun(context, dataAfterFunc, result);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -36,12 +38,14 @@ namespace SqlSugar
} }
return result; return result;
} }
protected async Task<List<T>> GetEntityListAsync<T>(SqlSugarProvider context, IDataReader dataReader) protected async Task<List<T>> GetEntityListAsync<T>(SqlSugarProvider context, IDataReader dataReader)
{ {
Type type = typeof(T); Type type = typeof(T);
string types = null; string types = null;
var fieldNames = GetDataReaderNames(dataReader,ref types); var fieldNames = GetDataReaderNames(dataReader,ref types);
string cacheKey = GetCacheKey(type, fieldNames)+types; string cacheKey = GetCacheKey(type, fieldNames)+types;
var dataAfterFunc = context.CurrentConnectionConfig?.AopEvents?.DataExecuted;
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
{ {
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader, fieldNames).CreateBuilder(type); var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader, fieldNames).CreateBuilder(type);
@ -55,6 +59,7 @@ namespace SqlSugar
{ {
result.Add(entytyList.Build(dataReader)); result.Add(entytyList.Build(dataReader));
} }
ExecuteDataAfterFun(context, dataAfterFunc, result);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -62,8 +67,24 @@ namespace SqlSugar
} }
return result; return result;
} }
private static void ExecuteDataAfterFun<T>(SqlSugarProvider context, Action<object, DataAfterModel> dataAfterFunc, List<T> result)
{
if (dataAfterFunc != null)
{
var entity = context.EntityMaintenance.GetEntityInfo<T>();
foreach (var item in result)
{
dataAfterFunc(item, new DataAfterModel()
{
EntityColumnInfos = entity.Columns,
Entity = entity,
EntityValue = item
});
}
}
}
private string GetCacheKey(Type type,List<string> keys) private string GetCacheKey(Type type,List<string> keys)
{ {
StringBuilder sb = new StringBuilder("DataReaderToList."); StringBuilder sb = new StringBuilder("DataReaderToList.");
sb.Append(type.FullName); sb.Append(type.FullName);

View File

@ -88,7 +88,7 @@ namespace SqlSugar
public Action<string, SugarParameter[]> OnLogExecuted { get; set; } public Action<string, SugarParameter[]> OnLogExecuted { get; set; }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { get; set; } public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { get; set; }
public Action<object, DataFilterModel> DataExecuting { get; set; } public Action<object, DataFilterModel> DataExecuting { get; set; }
public Action<object, DataAfterModel> DataExecuted { get; set; }
} }
public class ConfigureExternalServices public class ConfigureExternalServices
{ {

View File

@ -31,4 +31,23 @@ namespace SqlSugar
this.EntityColumnInfo.PropertyInfo.SetValue(EntityValue, value); this.EntityColumnInfo.PropertyInfo.SetValue(EntityValue, value);
} }
} }
public class DataAfterModel
{
public List<EntityColumnInfo> 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);
}
}
} }

View File

@ -46,6 +46,7 @@ namespace SqlSugar
var name =this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters[0].Name); var name =this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters[0].Name);
var parameter = (argExp as LambdaExpression).Parameters.Last(); var parameter = (argExp as LambdaExpression).Parameters.Last();
Context.InitMappingInfo(parameter.Type); Context.InitMappingInfo(parameter.Type);
this.Context.RefreshMapping();
var tableName= Context.GetTranslationTableName(parameter.Type.Name, true); var tableName= Context.GetTranslationTableName(parameter.Type.Name, true);
var joinString =string.Format(" {2} INNER JOIN {1} {0} ", var joinString =string.Format(" {2} INNER JOIN {1} {0} ",
this.Context.GetTranslationColumnName(parameter.Name), this.Context.GetTranslationColumnName(parameter.Name),

View File

@ -46,6 +46,7 @@ namespace SqlSugar
var name =this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters[0].Name); var name =this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters[0].Name);
var parameter = (argExp as LambdaExpression).Parameters.Last(); var parameter = (argExp as LambdaExpression).Parameters.Last();
Context.InitMappingInfo(parameter.Type); Context.InitMappingInfo(parameter.Type);
this.Context.RefreshMapping();
var tableName= Context.GetTranslationTableName(parameter.Type.Name, true); var tableName= Context.GetTranslationTableName(parameter.Type.Name, true);
var joinString =string.Format(" {2} LEFT JOIN {1} {0} ", var joinString =string.Format(" {2} LEFT JOIN {1} {0} ",
this.Context.GetTranslationColumnName(parameter.Name), this.Context.GetTranslationColumnName(parameter.Name),

View File

@ -43,12 +43,7 @@ namespace SqlSugar
public string GetValue(Expression expression = null) public string GetValue(Expression expression = null)
{ {
var exp = expression as MethodCallExpression; var exp = expression as MethodCallExpression;
var entityType = (exp.Arguments[0] as LambdaExpression).Parameters[0].Type; InitType(exp);
if (this.Context.InitMappingInfo != null)
{
this.Context.InitMappingInfo(entityType);
this.Context.RefreshMapping();
}
var result = ""; var result = "";
if (this.Context.JoinIndex == 0) if (this.Context.JoinIndex == 0)
result = SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.FieldSingle); result = SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.FieldSingle);
@ -60,6 +55,18 @@ namespace SqlSugar
return result; 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) public void SetShortName(MethodCallExpression exp, string result)
{ {
if (exp.Arguments[0] is LambdaExpression && result.IsContainsIn("+", "-","*","/")) if (exp.Arguments[0] is LambdaExpression && result.IsContainsIn("+", "-","*","/"))

View File

@ -165,6 +165,7 @@ namespace SqlSugar
void Open(); void Open();
SugarConnection OpenAlways(); SugarConnection OpenAlways();
bool IsValidConnection(); bool IsValidConnection();
bool IsValidConnectionNoClose();
void CheckConnection(); void CheckConnection();
void BeginTran(); void BeginTran();

View File

@ -87,6 +87,7 @@ namespace SqlSugar
OnExecutingChangeSql=it.AopEvents?.OnExecutingChangeSql, OnExecutingChangeSql=it.AopEvents?.OnExecutingChangeSql,
OnLogExecuted=it.AopEvents?.OnLogExecuted, OnLogExecuted=it.AopEvents?.OnLogExecuted,
OnLogExecuting= it.AopEvents?.OnLogExecuting, OnLogExecuting= it.AopEvents?.OnLogExecuting,
DataExecuted = it.AopEvents?.DataExecuted,
}, },
ConfigId = it.ConfigId, ConfigId = it.ConfigId,
ConfigureExternalServices =it.ConfigureExternalServices==null?null:new ConfigureExternalServices() { ConfigureExternalServices =it.ConfigureExternalServices==null?null:new ConfigureExternalServices() {