Browse Source

Update Subquery.ToList

pull/29/head
sunkaixuan 2 years ago
parent
commit
0c19e71d0d
  1. 46
      Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs
  2. 66
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs
  3. 26
      Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs
  4. 1
      Src/Asp.Net/SqlSugar/Interface/IContextMethods.cs

46
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<T> GetEntityList<T>(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<QueryableAppendColumn>>();
List<QueryableAppendColumn> addItems = new List<QueryableAppendColumn>();
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<List<T>> GetEntityListAsync<T>(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<T>(SqlSugarProvider context, Action<object, DataAfterModel> dataAfterFunc, List<T> 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<QueryableAppendColumn>>();
List<QueryableAppendColumn> addItems = new List<QueryableAppendColumn>();
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<string> GetDataReaderNames(IDataReader dataReader,ref string types)
{
List<string> keys = new List<string>();

66
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs

@ -1228,6 +1228,7 @@ namespace SqlSugar
{
List<TResult> result;
this.Bind.QueryBuilder = this.QueryBuilder;
this.Context.Utilities.QueryBuilder = this.QueryBuilder;
if (entityType == UtilConstants.DynamicType)
{
result = this.Context.Utilities.DataReaderToExpandoObjectList(dataReader) as List<TResult>;
@ -1259,6 +1260,7 @@ namespace SqlSugar
{
List<TResult> 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<TResult>;
@ -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<TResult>(List<TResult> result, List<List<QueryableAppendColumn>> appendValues, KeyValuePair<string, object> 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<result.Count; i++)
{
var item = result[i];
var setValue = Activator.CreateInstance(itemProperty.PropertyType, true) as IList;
itemProperty.SetValue(item, subList);
if (typeof(TResult).IsAnonymousType())
{
var jobj = JObject.FromObject(item);
var prop = jobj.Property(itemProperty.Name);
prop.Value = JArray.FromObject(subList);
result[i] = jobj.ToObject<TResult>();
}
else
{
itemProperty.SetValue(item, subList);
}
}
}
private void AppendSubWhereToList<TResult>(List<TResult> result, List<List<QueryableAppendColumn>> appendValues, KeyValuePair<string, object> subPara)
{
var ps = this.QueryBuilder.PartitionByTemplate;
var ps = this.QueryBuilder.Parameters;
var index = 0;
List<string> sqls = new List<string>();
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<QueryableAppendColumn>() {
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<object>().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<TResult>(List<TResult> result, PropertyInfo itemProperty, List<List<QueryableAppendColumn>> appendValue, List<object> 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<TResult>();
//itemProperty.SetValue(item, setValue);
}
return resIndex;
}
private static int SetSubListWithClass<TResult>(List<TResult> result, PropertyInfo itemProperty, List<List<QueryableAppendColumn>> appendValue, List<object> 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<TResult>(List<TResult> result)
@ -1509,7 +1561,7 @@ namespace SqlSugar
}
public List<Type> SubQueryList<Type>(string sql,object parameters)
{
return this.Context.Ado.SqlQuery<Type>(sql);
return this.Context.Ado.SqlQuery<Type>(sql,parameters);
}
#endregion
}

26
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
/// <summary>
@ -198,6 +200,7 @@ namespace SqlSugar
Dictionary<string, object> result = DataReaderToList(reader, tType, classProperties, reval);
var stringValue = SerializeObject(result);
reval.Add((T)DeserializeObject<T>(stringValue));
SetAppendColumns(reader);
}
}
return reval;
@ -290,6 +293,7 @@ namespace SqlSugar
Dictionary<string, object> result = DataReaderToList(reader, tType, classProperties, reval);
var stringValue = SerializeObject(result);
reval.Add((T)DeserializeObject<T>(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<QueryableAppendColumn>>();
List<QueryableAppendColumn> addItems = new List<QueryableAppendColumn>();
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<string, object> readerValues, PropertyInfo item)
{
return item.PropertyType == UtilConstants.ByteArrayType &&

1
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<ExpandoObject> DataReaderToExpandoObjectList(IDataReader reader);
Task<List<ExpandoObject>> DataReaderToExpandoObjectListAsync(IDataReader dataReader);

Loading…
Cancel
Save