Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1851 修复 FieldAliasOptions.AsProperty 生成Sql 错误 #1852

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 27 additions & 60 deletions FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,30 @@ internal bool EndsWithDbNestedField(string dbField, string dbNestedField)
public bool ReadAnonymousField(List<SelectTableInfo> _tables, Func<Type, string, string> _tableRule, StringBuilder field, ReadAnonymousTypeInfo parent, ref int index, Expression exp, Select0Provider select,
BaseDiyMemberExpression diymemexp, List<GlobalFilter.Item> whereGlobalFilter, List<string> findIncludeMany, List<Expression> findSubSelectMany, bool isAllDtoMap)
{
void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
void LocalSetFieldAlias(ReadAnonymousTypeInfo typeInfo, ref int localIndex, bool isdiymemexp)
{
if (localIndex >= 0)
{
parent.DbNestedField = $"as{++localIndex}";
field.Append(_common.FieldAsAlias(parent.DbNestedField));
typeInfo.DbNestedField = $"as{++localIndex}";
field.Append(_common.FieldAsAlias(typeInfo.DbNestedField));
}
else if (isdiymemexp && diymemexp?.ParseExpMapResult != null)
{
parent.DbNestedField = diymemexp.ParseExpMapResult.DbNestedField;
if (EndsWithDbNestedField(parent.DbField, $" {parent.DbNestedField}") == false && //#1510 group by 产生的 DbField 自带 alias,因此需要此行判断
string.IsNullOrEmpty(parent.CsName) == false && localIndex == ReadAnonymousFieldAsCsName)
typeInfo.DbNestedField = diymemexp.ParseExpMapResult.DbNestedField;
if (EndsWithDbNestedField(typeInfo.DbField, $" {typeInfo.DbNestedField}") == false && //#1510 group by 产生的 DbField 自带 alias,因此需要此行判断
string.IsNullOrEmpty(typeInfo.CsName) == false && localIndex == ReadAnonymousFieldAsCsName)
{
parent.DbNestedField = GetFieldAsCsName(parent.CsName);
if (EndsWithDbNestedField(parent.DbField, parent.DbNestedField) == false) //DbField 和 CsName 相同的时候,不处理
field.Append(_common.FieldAsAlias(parent.DbNestedField));
typeInfo.DbNestedField = GetFieldAsCsName(typeInfo.CsName);
if (EndsWithDbNestedField(typeInfo.DbField, typeInfo.DbNestedField) == false) //DbField 和 CsName 相同的时候,不处理
field.Append(_common.FieldAsAlias(typeInfo.DbNestedField));
}
}
else if (string.IsNullOrEmpty(parent.CsName) == false)
else if (string.IsNullOrEmpty(typeInfo.CsName) == false)
{
parent.DbNestedField = GetFieldAsCsName(parent.CsName);
if (localIndex == ReadAnonymousFieldAsCsName && EndsWithDbNestedField(parent.DbField, parent.DbNestedField) == false) //DbField 和 CsName 相同的时候,不处理
field.Append(_common.FieldAsAlias(parent.DbNestedField));
typeInfo.DbNestedField = GetFieldAsCsName(typeInfo.CsName);
var dbField = typeInfo.DbField ?? diymemexp?._field;
if (localIndex == ReadAnonymousFieldAsCsName && EndsWithDbNestedField(dbField, typeInfo.DbNestedField) == false) //DbField 和 CsName 相同的时候,不处理
field.Append(_common.FieldAsAlias(typeInfo.DbNestedField));
}
}
var isGroupAddField = true;
Expand All @@ -112,7 +113,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
case ExpressionType.NegateChecked:
parent.DbField = $"-({ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, getTSC())})";
field.Append(", ").Append(parent.DbField);
LocalSetFieldAlias(ref index, false);
LocalSetFieldAlias(parent, ref index, false);
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
case ExpressionType.Convert: return ReadAnonymousField(_tables, _tableRule, field, parent, ref index, (exp as UnaryExpression)?.Operand, select, diymemexp, whereGlobalFilter, findIncludeMany, findSubSelectMany, isAllDtoMap);
Expand All @@ -131,7 +132,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
else
parent.DbField = _common.FormatSql("{0}", constExp?.Value);
field.Append(", ").Append(parent.DbField);
LocalSetFieldAlias(ref index, false);
LocalSetFieldAlias(parent, ref index, false);
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
case ExpressionType.Conditional:
Expand Down Expand Up @@ -160,7 +161,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
else
parent.DbField = ExpressionLambdaToSql(exp, getTSC());
field.Append(", ").Append(parent.DbField);
LocalSetFieldAlias(ref index, false);
LocalSetFieldAlias(parent, ref index, false);
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
case ExpressionType.Parameter:
Expand All @@ -187,11 +188,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
if (withTempQueryParser != null)
child.DbField = $"{withTempQueryParser.ParseExpMatchedTable.Alias}.{child.DbNestedField}";
field.Append(", ").Append(child.DbField);
if (index >= 0)
{
child.DbNestedField = $"as{++index}";
field.Append(_common.FieldAsAlias(child.DbNestedField));
}
LocalSetFieldAlias(child, ref index, false);
}
return false;
}
Expand All @@ -213,11 +210,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
MapType = map[idx].Column.Attribute.MapType
};
field.Append(", ").Append(_common.RereadColumn(map[idx].Column, child.DbField));
if (index >= 0)
{
child.DbNestedField = $"as{++index}";
field.Append(_common.FieldAsAlias(child.DbNestedField));
}
LocalSetFieldAlias(child, ref index, false);
parent.Childs.Add(child);
}
if (_tables?.Count > 1)
Expand Down Expand Up @@ -277,22 +270,12 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
if (diymemexp != null && exp is MemberExpression expMem2 && expMem2.Member.Name == "Key" && expMem2.Expression.Type.FullName.StartsWith("FreeSql.ISelectGroupingAggregate`"))
{
field.Append(diymemexp._field);
string dbNestedField = null;
if (diymemexp._map.Childs.Any() == false) //处理 GroupBy(a => a.Title) ToSql(g => new { tit = a.Key }, FieldAliasOptions.AsProperty) 问题
{
if (index >= 0)
{
dbNestedField = $"as{++index}";
field.Append(_common.FieldAsAlias(dbNestedField));
}
else if (string.IsNullOrEmpty(parent.CsName) == false)
{
dbNestedField = GetFieldAsCsName(parent.CsName);
if (index == ReadAnonymousFieldAsCsName && EndsWithDbNestedField(diymemexp._field, dbNestedField) == false) //DbField 和 CsName 相同的时候,不处理
field.Append(_common.FieldAsAlias(dbNestedField));
}
LocalSetFieldAlias(parent, ref index, false);
}
var parentProp = parent.Property;
var dbNestedField = parent.DbNestedField;
diymemexp._map.CopyTo(parent); //可能会清空 parent.DbNestedField、CsName 值
parent.Property = parentProp; //若不加此行,会引用 GroupBy(..).ToList(a => new Dto { key = a.Key }) null 错误,CopyTo 之后 Property 变为 null
if (string.IsNullOrWhiteSpace(dbNestedField) == false)
Expand All @@ -310,7 +293,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
if (findcol != null) pdbfield = _common.RereadColumn(findcol, pdbfield);
}
field.Append(", ").Append(pdbfield);
LocalSetFieldAlias(ref index, _tables != null ||
LocalSetFieldAlias(parent, ref index, _tables != null ||
SelectGroupingProvider._ParseExpOnlyDbField.Value != pdbfield);
}
finally
Expand Down Expand Up @@ -373,11 +356,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
diychild.DbField = $"{dtTb.Alias}.{diymemexp.ParseExpMapResult.DbNestedField}";
diychild.DbNestedField = diymemexp.ParseExpMapResult.DbNestedField;
field.Append(", ").Append(diychild.DbField);
if (index >= 0)
{
diychild.DbNestedField = $"as{++index}";
field.Append(_common.FieldAsAlias(diychild.DbNestedField));
}
LocalSetFieldAlias(diychild, ref index, false);
isBreaked = true;
break;
}
Expand Down Expand Up @@ -407,11 +386,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
child.DbField = $"{dtTb.Alias}.{_common.QuoteSqlName(trydtocol.Attribute.Name)}";
child.DbNestedField = _common.QuoteSqlName(trydtocol.Attribute.Name);
field.Append(", ").Append(_common.RereadColumn(trydtocol, child.DbField));
if (index >= 0)
{
child.DbNestedField = $"as{++index}";
field.Append(_common.FieldAsAlias(child.DbNestedField));
}
LocalSetFieldAlias(child, ref index, false);
}
break;
}
Expand Down Expand Up @@ -505,11 +480,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
diychild.DbField = $"{dtTb.Alias}.{diymemexp.ParseExpMapResult.DbNestedField}";
diychild.DbNestedField = diymemexp.ParseExpMapResult.DbNestedField;
field.Append(", ").Append(diychild.DbField);
if (index >= 0)
{
diychild.DbNestedField = $"as{++index}";
field.Append(_common.FieldAsAlias(diychild.DbNestedField));
}
LocalSetFieldAlias(diychild, ref index, false);
isBreaked = true;
break;
}
Expand Down Expand Up @@ -538,11 +509,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
child.DbField = _common.RereadColumn(trydtocol, $"{dtTb.Alias}.{_common.QuoteSqlName(trydtocol.Attribute.Name)}");
child.DbNestedField = _common.QuoteSqlName(trydtocol.Attribute.Name);
field.Append(", ").Append(child.DbField);
if (index >= 0)
{
child.DbNestedField = $"as{++index}";
field.Append(_common.FieldAsAlias(child.DbNestedField));
}
LocalSetFieldAlias(child, ref index, false);
}
break;
}
Expand All @@ -553,7 +520,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
}
parent.DbField = ExpressionLambdaToSql(exp, getTSC()); //解决 new { a = id + 1 } 翻译后 ((id+1)) 问题
field.Append(", ").Append(parent.DbField);
LocalSetFieldAlias(ref index, false);
LocalSetFieldAlias(parent, ref index, false);
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
if (isGroupAddField == false && isGroupAddFieldProvider != null) isGroupAddFieldProvider._addFieldAlias = true;
return false;
Expand Down