From dbe9092b3a3bd07520d36693a3cd429a1d2a77d1 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 10 Apr 2022 02:26:23 +0800 Subject: [PATCH] Update Core --- .../QueryableProvider/QueryableContext.cs | 17 +++++ .../QueryableProvider/QueryableProvider.cs | 73 ++++++++++++++++++- .../SugarProvider/SqlSugarProvider.cs | 37 ++++++++++ .../SqlSugar/Interface/IQueryable.cs | 3 +- .../SqlSugar/Interface/ISqlSugarClient.cs | 5 ++ Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs | 13 ++++ Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs | 10 +++ 7 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableContext.cs diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableContext.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableContext.cs new file mode 100644 index 000000000..a13332237 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableContext.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class MapperContext + { + public ISqlSugarClient context { get; set; } + + public List list { get; set; } + public Dictionary TempChildLists { get; set; } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index d7794a4a4..0684bcee8 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -637,7 +637,14 @@ namespace SqlSugar { if (item != null) { - values.Add(item.ToString().ToSqlValue()); + if (UtilMethods.IsNumber(item.GetType().Name)) + { + values.Add(item.ToString()); + } + else + { + values.Add(item.ToString().ToSqlValue()); + } } } this.Where(string.Format(QueryBuilder.InTemplate, filed, string.Join(",", values))); @@ -1300,6 +1307,67 @@ namespace SqlSugar InitMapping(); return _ToList(); } + public List SetContext(Expression> thisFiled, Expression> mappingFiled, ParameterT parameter) + { + List result = new List(); + var entity = this.Context.EntityMaintenance.GetEntityInfo(); + var queryableContext = this.Context.TempItems["Queryable_To_Context"] as MapperContext; + var list = queryableContext.list; + var pkName = (((mappingFiled as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name; + var key = thisFiled.ToString() +typeof(ParameterT).FullName + typeof(T).FullName; + var ids = list.Select(it => it.GetType().GetProperty(pkName).GetValue(it)).ToArray(); + if (queryableContext.TempChildLists == null) + queryableContext.TempChildLists = new Dictionary(); + if (list != null && queryableContext.TempChildLists.ContainsKey(key)) + { + result = (List)queryableContext.TempChildLists[key]; + } + else + { + if (queryableContext.TempChildLists == null) + queryableContext.TempChildLists = new Dictionary(); + this.Context.Utilities.PageEach(ids, 200, pageIds => + { + result.AddRange(this.Clone().In(thisFiled, pageIds).ToList()); + }); + queryableContext.TempChildLists[key]= result; + } + var name = (((thisFiled as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name; + var pkValue = parameter.GetType().GetProperty(pkName).GetValue(parameter); + result = result.Where(it => it.GetType().GetProperty(name).GetValue(it).ObjToString() == pkValue.ObjToString()).ToList(); + return result; + } + + public async Task> SetContextAsync(Expression> thisFiled, Expression> mappingFiled, ParameterT parameter) + { + List result = new List(); + var entity = this.Context.EntityMaintenance.GetEntityInfo(); + var queryableContext = this.Context.TempItems["Queryable_To_Context"] as MapperContext; + var list = queryableContext.list; + var pkName = (((mappingFiled as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name; + var key = thisFiled.ToString() + typeof(ParameterT).FullName + typeof(T).FullName; + var ids = list.Select(it => it.GetType().GetProperty(pkName).GetValue(it)).ToArray(); + if (queryableContext.TempChildLists == null) + queryableContext.TempChildLists = new Dictionary(); + if (list != null && queryableContext.TempChildLists.ContainsKey(key)) + { + result = (List)queryableContext.TempChildLists[key]; + } + else + { + if (queryableContext.TempChildLists == null) + queryableContext.TempChildLists = new Dictionary(); + await this.Context.Utilities.PageEachAsync(ids, 200, async pageIds => + { + result.AddRange(await this.Clone().In(thisFiled, pageIds).ToListAsync()); + }); + queryableContext.TempChildLists[key] = result; + } + var name = (((thisFiled as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name; + var pkValue = parameter.GetType().GetProperty(pkName).GetValue(parameter); + result = result.Where(it => it.GetType().GetProperty(name).GetValue(it).ObjToString() == pkValue.ObjToString()).ToList(); + return result; + } public virtual void ForEach(Action action, int singleMaxReads = 300,System.Threading.CancellationTokenSource cancellationTokenSource = null) { Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0, ErrorMessage.GetThrowMessage("no support Skip take, use PageForEach", "不支持Skip Take,请使用 Queryale.PageForEach")); @@ -2299,7 +2367,8 @@ namespace SqlSugar { FieldName=this.SqlBuilder.GetTranslationColumnName(whereCol.DbColumnName), ConditionalType= ConditionalType.In, - FieldValue=string.Join(",",inValues.Distinct()) + FieldValue=string.Join(",",inValues.Distinct()), + CSharpTypeName=whereCol.PropertyInfo.PropertyType.Name } }; var list = this.Context.Queryable().Where(wheres).ToList(); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index 5247434a5..a4d076646 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -1271,5 +1271,42 @@ namespace SqlSugar return new FastestProvider(this); } #endregion + + #region + public void ThenMapper(IEnumerable list, Action action) + { + MapperContext result = new MapperContext(); + result.context = this.Context; + if (result.context.TempItems == null) + { + result.context.TempItems = new Dictionary(); + } + var key = "Queryable_To_Context"; + result.context.TempItems.Add(key, result); + result.list = list.ToList(); + foreach (var item in list) + { + action.Invoke(item); + } + result.context.TempItems.Remove(key); + } + public async Task ThenMapperAsync(IEnumerable list, Func action) + { + MapperContext result = new MapperContext(); + result.context = this.Context; + if (result.context.TempItems == null) + { + result.context.TempItems = new Dictionary(); + } + var key = "Queryable_To_Context"; + result.context.TempItems.Add(key, result); + result.list = list.ToList(); + foreach (var item in list) + { + await action.Invoke(item); + } + result.context.TempItems.Remove(key); + } + #endregion } } diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs index 42495d22b..fb43996e5 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs @@ -132,7 +132,8 @@ namespace SqlSugar Task AvgAsync(Expression> expression); List ToList(); - + List SetContext(Expression> thisFiled, Expression> mappingFiled, ParameterT parameter); + Task > SetContextAsync(Expression> thisFiled, Expression> mappingFiled, ParameterT parameter); Dictionary ToDictionary(Expression> key, Expression> value); Task> ToDictionaryAsync(Expression> key, Expression> value); List> ToDictionaryList(); diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs index 3cd372dc0..faf50eaf9 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs @@ -193,5 +193,10 @@ namespace SqlSugar IFastest Fastest() where T : class, new(); #endregion + #region ThenMapper + void ThenMapper(IEnumerable list, Action action); + Task ThenMapperAsync(IEnumerable list, Func action); + #endregion + } } \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs index 74761e7db..c4cca3c45 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs @@ -25,6 +25,8 @@ namespace SqlSugar private MappingColumnList _MappingColumns; private IgnoreColumnList _IgnoreColumns; private IgnoreColumnList _IgnoreInsertColumns; + + internal Guid? AsyncId { get; set; } internal bool? IsSingleInstance { get; set; } @@ -591,6 +593,17 @@ namespace SqlSugar } #endregion + #region ThenMapper + public void ThenMapper(IEnumerable list, Action action) + { + this.Context.ThenMapper(list, action); + } + public Task ThenMapperAsync(IEnumerable list, Func action) + { + return this.Context.ThenMapperAsync(list,action); + } + #endregion + #region More api public IContextMethods Utilities { get { return this.Context.Utilities; } set { this.Context.Utilities = value; } } public AopProvider Aop => this.Context.Aop; diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs index cfdba2629..0da46c31d 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs @@ -662,5 +662,15 @@ namespace SqlSugar { return ScopedContext.Fastest(); } + + public void ThenMapper(IEnumerable list, Action action) + { + ScopedContext.ThenMapper(list, action); + } + + public Task ThenMapperAsync(IEnumerable list, Func action) + { + return ScopedContext.ThenMapperAsync(list, action); + } } }