From 0c19e71d0d18ff32fe8d0da2f4a03fa216c4e57e Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 3 Dec 2022 21:00:33 +0800 Subject: [PATCH] Update Subquery.ToList --- .../DbBindProvider/DbBindAccessory.cs | 46 +++++++------ .../QueryableProvider/QueryableHelper.cs | 66 +++++++++++++++++-- .../SqlSugar/Infrastructure/ContextMethods.cs | 26 +++++++- .../SqlSugar/Interface/IContextMethods.cs | 1 + 4 files changed, 110 insertions(+), 29 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs index e026f103f..c56ef7a93 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs @@ -11,6 +11,7 @@ namespace SqlSugar public partial class DbBindAccessory { public QueryBuilder QueryBuilder { get; set; } + protected List GetEntityList(SqlSugarProvider context, IDataReader dataReader) { Type type = typeof(T); @@ -41,27 +42,6 @@ namespace SqlSugar return result; } - private void SetAppendColumns(IDataReader dataReader) - { - if (QueryBuilder!=null&&QueryBuilder.AppendColumns != null && QueryBuilder.AppendColumns.Any()) - { - if(QueryBuilder.AppendValues==null) - QueryBuilder.AppendValues = new List>(); - List addItems = new List(); - foreach (var item in QueryBuilder.AppendColumns) - { - var vi = dataReader.GetOrdinal(item.AsName); - var value = dataReader.GetValue(vi); - addItems.Add(new QueryableAppendColumn() { - Name=item.Name, - AsName=item.AsName, - Value=value - }); - } - QueryBuilder.AppendValues.Add(addItems); - } - } - protected async Task> GetEntityListAsync(SqlSugarProvider context, IDataReader dataReader) { Type type = typeof(T); @@ -81,6 +61,7 @@ namespace SqlSugar while (await((DbDataReader)dataReader).ReadAsync()) { result.Add(entytyList.Build(dataReader)); + SetAppendColumns(dataReader); } ExecuteDataAfterFun(context, dataAfterFunc, result); } @@ -90,6 +71,7 @@ namespace SqlSugar } return result; } + private static void ExecuteDataAfterFun(SqlSugarProvider context, Action dataAfterFunc, List result) { if (dataAfterFunc != null) @@ -119,6 +101,28 @@ namespace SqlSugar return sb.ToString(); } + private void SetAppendColumns(IDataReader dataReader) + { + if (QueryBuilder != null && QueryBuilder.AppendColumns != null && QueryBuilder.AppendColumns.Any()) + { + if (QueryBuilder.AppendValues == null) + QueryBuilder.AppendValues = new List>(); + List addItems = new List(); + foreach (var item in QueryBuilder.AppendColumns) + { + var vi = dataReader.GetOrdinal(item.AsName); + var value = dataReader.GetValue(vi); + addItems.Add(new QueryableAppendColumn() + { + Name = item.Name, + AsName = item.AsName, + Value = value + }); + } + QueryBuilder.AppendValues.Add(addItems); + } + } + private List GetDataReaderNames(IDataReader dataReader,ref string types) { List keys = new List(); diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs index 40d6a0c83..960ebc281 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs @@ -1228,6 +1228,7 @@ namespace SqlSugar { List result; this.Bind.QueryBuilder = this.QueryBuilder; + this.Context.Utilities.QueryBuilder = this.QueryBuilder; if (entityType == UtilConstants.DynamicType) { result = this.Context.Utilities.DataReaderToExpandoObjectList(dataReader) as List; @@ -1259,6 +1260,7 @@ namespace SqlSugar { List result; this.Bind.QueryBuilder = this.QueryBuilder; + this.Context.Utilities.QueryBuilder = this.QueryBuilder; if (entityType == UtilConstants.DynamicType) { result = await this.Context.Utilities.DataReaderToExpandoObjectListAsync(dataReader) as List; @@ -1386,6 +1388,8 @@ namespace SqlSugar asyncQueryableBuilder.IsCrossQueryWithAttr = this.QueryBuilder.IsCrossQueryWithAttr; asyncQueryableBuilder.CrossQueryItems = this.QueryBuilder.CrossQueryItems; asyncQueryableBuilder.SubToListParameters= this.QueryBuilder.SubToListParameters; + asyncQueryableBuilder.AppendColumns = this.Context.Utilities.TranslateCopy(this.QueryBuilder.AppendColumns); + asyncQueryableBuilder.AppendValues = this.Context.Utilities.TranslateCopy(this.QueryBuilder.AppendValues); } protected int SetCacheTime(int cacheDurationInSeconds) { @@ -1429,21 +1433,32 @@ namespace SqlSugar private void AppendSubToList(List result, List> appendValues, KeyValuePair subPara) { - var ps = this.QueryBuilder.PartitionByTemplate; + var ps = this.QueryBuilder.Parameters; var itemProperty = typeof(TResult).GetProperty(subPara.Key); var callType = itemProperty.PropertyType.GetGenericArguments()[0]; var methodParamters = new object[] { subPara.Value.ObjToString().Replace("@sugarIndex", "0"), ps }; var subList = ExpressionBuilderHelper.CallFunc(callType, methodParamters, this.Clone(), "SubQueryList"); - foreach (var item in result) + for(var i=0;i(); + } + else + { + itemProperty.SetValue(item, subList); + } } } private void AppendSubWhereToList(List result, List> appendValues, KeyValuePair subPara) { - var ps = this.QueryBuilder.PartitionByTemplate; + var ps = this.QueryBuilder.Parameters; var index = 0; List sqls = new List(); foreach (var item in result) @@ -1470,7 +1485,7 @@ namespace SqlSugar var callType = itemProperty.PropertyType.GetGenericArguments()[0]; var sqlstring = string.Join(" \r\n UNION ALL ", sqls); - var methodParamters = new object[] { sqlstring,ps}; + var methodParamters = new object[] { sqlstring, ps.ToArray() }; this.QueryBuilder.SubToListParameters = null; this.QueryBuilder.AppendColumns = new List() { new QueryableAppendColumn(){ Name="sugarIndex",AsName="sugarIndex" } @@ -1479,7 +1494,42 @@ namespace SqlSugar var subList = ExpressionBuilderHelper.CallFunc(callType, methodParamters, this.Clone(), "SubQueryList"); var appendValue = this.QueryBuilder.AppendValues; var list = (subList as IEnumerable).Cast().ToList(); - var resIndex = 0; + if (typeof(TResult).IsAnonymousType()) + { + SetSubListWithAnonymousType(result, itemProperty, appendValue, list, 0); + } + else + { + SetSubListWithClass(result, itemProperty, appendValue, list, 0); + } + } + private static int SetSubListWithAnonymousType(List result, PropertyInfo itemProperty, List> appendValue, List list, int resIndex) + { + for (int i = 0; i < result.Count; i++) + { + var item = result[i]; + var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList; + var appindex = 0; + foreach (var appValue in appendValue) + { + if (appValue[0].Value.ObjToInt() == i) + { + var addItem = list[appindex]; + setValue.Add(addItem); + } + appindex++; + } + var jobj = JObject.FromObject(item); + var prop = jobj.Property(itemProperty.Name); + prop.Value = JArray.FromObject(setValue); + result[i] = jobj.ToObject(); + //itemProperty.SetValue(item, setValue); + } + return resIndex; + } + + private static int SetSubListWithClass(List result, PropertyInfo itemProperty, List> appendValue, List list, int resIndex) + { foreach (var item in result) { var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList; @@ -1496,6 +1546,8 @@ namespace SqlSugar itemProperty.SetValue(item, setValue); resIndex++; } + + return resIndex; } private async Task _SubQueryAsync(List result) @@ -1509,7 +1561,7 @@ namespace SqlSugar } public List SubQueryList(string sql,object parameters) { - return this.Context.Ado.SqlQuery(sql); + return this.Context.Ado.SqlQuery(sql,parameters); } #endregion } diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs index 853901561..129171124 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs @@ -16,6 +16,8 @@ namespace SqlSugar public partial class ContextMethods : IContextMethods { public SqlSugarProvider Context { get; set; } + public QueryBuilder QueryBuilder { get; set; } + #region DataReader /// @@ -198,6 +200,7 @@ namespace SqlSugar Dictionary result = DataReaderToList(reader, tType, classProperties, reval); var stringValue = SerializeObject(result); reval.Add((T)DeserializeObject(stringValue)); + SetAppendColumns(reader); } } return reval; @@ -290,6 +293,7 @@ namespace SqlSugar Dictionary result = DataReaderToList(reader, tType, classProperties, reval); var stringValue = SerializeObject(result); reval.Add((T)DeserializeObject(stringValue)); + SetAppendColumns(reader); } } return reval; @@ -449,7 +453,27 @@ namespace SqlSugar return result; } - + private void SetAppendColumns(IDataReader dataReader) + { + if (QueryBuilder != null && QueryBuilder.AppendColumns != null && QueryBuilder.AppendColumns.Any()) + { + if (QueryBuilder.AppendValues == null) + QueryBuilder.AppendValues = new List>(); + List addItems = new List(); + foreach (var item in QueryBuilder.AppendColumns) + { + var vi = dataReader.GetOrdinal(item.AsName); + var value = dataReader.GetValue(vi); + addItems.Add(new QueryableAppendColumn() + { + Name = item.Name, + AsName = item.AsName, + Value = value + }); + } + QueryBuilder.AppendValues.Add(addItems); + } + } private static bool IsBytes(Dictionary readerValues, PropertyInfo item) { return item.PropertyType == UtilConstants.ByteArrayType && diff --git a/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs b/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs index 5e882eb04..098162782 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs @@ -12,6 +12,7 @@ namespace SqlSugar public partial interface IContextMethods { SqlSugarProvider Context { get; set; } + QueryBuilder QueryBuilder { get; set; } ExpandoObject DataReaderToExpandoObject(IDataReader reader); List DataReaderToExpandoObjectList(IDataReader reader); Task> DataReaderToExpandoObjectListAsync(IDataReader dataReader);