- 修复 v3.2.688 WithTempQuery 场景的 DTO 映射查询;

This commit is contained in:
2881099 2023-02-28 18:50:08 +08:00
parent 74567733a7
commit 62408b2ba7

View File

@ -330,31 +330,37 @@ namespace FreeSql.Internal
{ {
if (diymemexp != null && dtTb.Parameter != null) if (diymemexp != null && dtTb.Parameter != null)
{ {
var dbTbExp = MatchDtoProperty(dtTb, dtoProp); var isBreaked = false;
if (dbTbExp?.Any() == true) var dbTbExps = MatchDtoPropertys(dtTb, dtoProp); //嵌套查询临时类,可能匹配到多个 DTO
foreach (var dbTbExp in dbTbExps)
{ {
var dbfield = diymemexp.ParseExp(dbTbExp); if (dbTbExp?.Any() == true)
if (diymemexp.ParseExpMapResult != null)
{ {
var diychild = new ReadAnonymousTypeInfo var dbfield = diymemexp.ParseExp(dbTbExp);
if (diymemexp.ParseExpMapResult != null)
{ {
Property = dtoProp, var diychild = new ReadAnonymousTypeInfo
CsName = dtoProp.Name, {
CsType = dbTbExp.LastOrDefault().Type, Property = dtoProp,
MapType = dbTbExp.LastOrDefault().Type CsName = dtoProp.Name,
}; CsType = dbTbExp.LastOrDefault().Type,
parent.Childs.Add(diychild); MapType = dbTbExp.LastOrDefault().Type
diychild.DbField = $"{dtTb.Alias}.{diymemexp.ParseExpMapResult.DbNestedField}"; };
diychild.DbNestedField = diymemexp.ParseExpMapResult.DbNestedField; parent.Childs.Add(diychild);
field.Append(", ").Append(diychild.DbField); diychild.DbField = $"{dtTb.Alias}.{diymemexp.ParseExpMapResult.DbNestedField}";
if (index >= 0) diychild.DbNestedField = diymemexp.ParseExpMapResult.DbNestedField;
{ field.Append(", ").Append(diychild.DbField);
diychild.DbNestedField = $"as{++index}"; if (index >= 0)
field.Append(_common.FieldAsAlias(diychild.DbNestedField)); {
diychild.DbNestedField = $"as{++index}";
field.Append(_common.FieldAsAlias(diychild.DbNestedField));
}
isBreaked = true;
break;
} }
break;
} }
} }
if (isBreaked) break;
} }
continue; continue;
} }
@ -456,31 +462,37 @@ namespace FreeSql.Internal
{ {
if (diymemexp != null && dtTb.Parameter != null) if (diymemexp != null && dtTb.Parameter != null)
{ {
var dbTbExp = MatchDtoProperty(dtTb, dtoProp); var isBreaked = false;
if (dbTbExp?.Any() == true) var dbTbExps = MatchDtoPropertys(dtTb, dtoProp); //嵌套查询临时类,可能匹配到多个 DTO
foreach (var dbTbExp in dbTbExps)
{ {
var dbfield = diymemexp.ParseExp(dbTbExp); if (dbTbExp?.Any() == true)
if (diymemexp.ParseExpMapResult != null)
{ {
var diychild = new ReadAnonymousTypeInfo var dbfield = diymemexp.ParseExp(dbTbExp);
if (diymemexp.ParseExpMapResult != null)
{ {
Property = dtoProp, var diychild = new ReadAnonymousTypeInfo
CsName = dtoProp.Name, {
CsType = dbTbExp.LastOrDefault().Type, Property = dtoProp,
MapType = dbTbExp.LastOrDefault().Type CsName = dtoProp.Name,
}; CsType = dbTbExp.LastOrDefault().Type,
parent.Childs.Add(diychild); MapType = dbTbExp.LastOrDefault().Type
diychild.DbField = $"{dtTb.Alias}.{diymemexp.ParseExpMapResult.DbNestedField}"; };
diychild.DbNestedField = diymemexp.ParseExpMapResult.DbNestedField; parent.Childs.Add(diychild);
field.Append(", ").Append(diychild.DbField); diychild.DbField = $"{dtTb.Alias}.{diymemexp.ParseExpMapResult.DbNestedField}";
if (index >= 0) diychild.DbNestedField = diymemexp.ParseExpMapResult.DbNestedField;
{ field.Append(", ").Append(diychild.DbField);
diychild.DbNestedField = $"as{++index}"; if (index >= 0)
field.Append(_common.FieldAsAlias(diychild.DbNestedField)); {
diychild.DbNestedField = $"as{++index}";
field.Append(_common.FieldAsAlias(diychild.DbNestedField));
}
isBreaked = true;
break;
} }
break;
} }
} }
if (isBreaked) break;
} }
continue; continue;
} }
@ -2498,20 +2510,12 @@ namespace FreeSql.Internal
//return string.Concat(_ado.AddslashesProcessParam(obj, mapType, mapColumn)); //return string.Concat(_ado.AddslashesProcessParam(obj, mapType, mapColumn));
} }
public static Expression[] MatchDtoProperty(SelectTableInfo tb, PropertyInfo dtoProp) public List<Expression[]> MatchDtoPropertys(SelectTableInfo tb, PropertyInfo dtoProp)
{ {
if (tb == null || dtoProp == null || tb.Parameter == null) return null; if (tb == null || dtoProp == null || tb.Parameter == null) return null;
var exp = LocalMatch(tb.Parameter.Type, tb.Parameter); var retList = new List<Expression[]>();
if (exp == null) return null; LocalMatch(tb.Parameter.Type, tb.Parameter);
var exps = new Stack<Expression>(); return retList;
while (true)
{
exps.Push(exp);
exp = (exp as MemberExpression).Expression;
if (exp.NodeType == ExpressionType.Parameter) break;
}
if (exps.Any()) return exps.ToArray();
return null;
Expression LocalMatch(Type type, Expression memExp) Expression LocalMatch(Type type, Expression memExp)
{ {
@ -2522,14 +2526,34 @@ namespace FreeSql.Internal
{ {
foreach (var typeProp in typeProps.Values) foreach (var typeProp in typeProps.Values)
{ {
if (Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(type)) return null; if (Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(typeProp.PropertyType)) continue;
var nextExp = Expression.MakeMemberAccess(memExp, typeProp); if (typeProp.PropertyType.IsAnonymousType() || _common.GetTableByEntity(typeProp.PropertyType)?.Columns.Any() == true)
var ret = LocalMatch(typeProp.PropertyType, nextExp); {
if (ret != null) return ret; var nextExp = Expression.MakeMemberAccess(memExp, typeProp);
var ret = LocalMatch(typeProp.PropertyType, nextExp);
if (ret != null)
{
var expPath = LocalGetExpressionPath(ret);
if (expPath != null) retList.Add(expPath);
}
}
} }
} }
return null; return null;
} }
Expression[] LocalGetExpressionPath(Expression exp)
{
if (exp == null) return null;
var exps = new Stack<Expression>();
while (true)
{
exps.Push(exp);
exp = (exp as MemberExpression).Expression;
if (exp.NodeType == ExpressionType.Parameter) break;
}
if (exps.Any()) return exps.ToArray();
return null;
}
} }
public static void ParseExpressionNoAsSelect(object sender, Aop.ParseExpressionEventArgs e, Func<Type, string, string> tableRule, List<GlobalFilter.Item> whereGlobalFilter) public static void ParseExpressionNoAsSelect(object sender, Aop.ParseExpressionEventArgs e, Func<Type, string, string> tableRule, List<GlobalFilter.Item> whereGlobalFilter)