Skip to content

Commit

Permalink
- 优化 ToSql 别名大小写问题;#467
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Dec 3, 2020
1 parent 390be18 commit b193d86
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
9 changes: 0 additions & 9 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1126,12 +1126,12 @@ public void AsTable()
WF_ProcessInstance = p
});

Assert.Equal(@"SELECT max(a.`Id`) TaskId, max(a.`Type`) TaskType, a.`ProcessId`, a.`NodeId`, a.`NodeName`
Assert.Equal(@"SELECT max(a.`Id`) `TaskId`, max(a.`Type`) `TaskType`, a.`ProcessId`, a.`NodeId`, a.`NodeName`
FROM `WF_Task` a
WHERE (a.`IsFinished` = 1 AND (a.`AuditorId` = '1' OR a.`AuditorId` = '1cb71584-a6dd-4b26-8c88-ed9fb8cf87a3'))
GROUP BY a.`ProcessId`, a.`NodeId`, a.`NodeName`", sqltmp12);
Assert.Equal(@"SELECT a.`TaskId` as1, a.`TaskType` as2, a.`ProcessId` as3, a.`NodeId` as4, a.`NodeName` as5, b.`Id` as6, b.`TaskType` as7, b.`ProcessId` as8, b.`NodeId` as9, b.`CreateTime` as10, b.`IsFinished` as11, b.`EnabledMark` as12
FROM ( SELECT max(a.`Id`) TaskId, max(a.`Type`) TaskType, a.`ProcessId`, a.`NodeId`, a.`NodeName`
FROM ( SELECT max(a.`Id`) `TaskId`, max(a.`Type`) `TaskType`, a.`ProcessId`, a.`NodeId`, a.`NodeName`
FROM `WF_Task` a
WHERE (a.`IsFinished` = 1 AND (a.`AuditorId` = '1' OR a.`AuditorId` = '1cb71584-a6dd-4b26-8c88-ed9fb8cf87a3'))
GROUP BY a.`ProcessId`, a.`NodeId`, a.`NodeName` ) a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,12 +1137,12 @@ public void AsTable()
WF_ProcessInstance = p
});

Assert.Equal(@"SELECT max(a.`Id`) TaskId, max(a.`Type`) TaskType, a.`ProcessId`, a.`NodeId`, a.`NodeName`
Assert.Equal(@"SELECT max(a.`Id`) `TaskId`, max(a.`Type`) `TaskType`, a.`ProcessId`, a.`NodeId`, a.`NodeName`
FROM `WF_Task` a
WHERE (a.`IsFinished` = 1 AND (a.`AuditorId` = '1' OR a.`AuditorId` = '1cb71584-a6dd-4b26-8c88-ed9fb8cf87a3'))
GROUP BY a.`ProcessId`, a.`NodeId`, a.`NodeName`", sqltmp12);
Assert.Equal(@"SELECT a.`TaskId` as1, a.`TaskType` as2, a.`ProcessId` as3, a.`NodeId` as4, a.`NodeName` as5, b.`Id` as6, b.`TaskType` as7, b.`ProcessId` as8, b.`NodeId` as9, b.`CreateTime` as10, b.`IsFinished` as11, b.`EnabledMark` as12
FROM ( SELECT max(a.`Id`) TaskId, max(a.`Type`) TaskType, a.`ProcessId`, a.`NodeId`, a.`NodeName`
FROM ( SELECT max(a.`Id`) `TaskId`, max(a.`Type`) `TaskType`, a.`ProcessId`, a.`NodeId`, a.`NodeName`
FROM `WF_Task` a
WHERE (a.`IsFinished` = 1 AND (a.`AuditorId` = '1' OR a.`AuditorId` = '1cb71584-a6dd-4b26-8c88-ed9fb8cf87a3'))
GROUP BY a.`ProcessId`, a.`NodeId`, a.`NodeName` ) a
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.Tests/FreeSql.Tests/Issues/494.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void SelectTest()
Name = w.Address,
No = w.Number.ToString()
}, FieldAliasOptions.AsProperty);
Assert.Equal(@"SELECT cast(a.""Number"" as character) CardNumber, ((a.""CommType"" + 1)) CardType, a.""Address"" Name, cast(a.""Number"" as character) No
Assert.Equal(@"SELECT cast(a.""Number"" as character) ""CardNumber"", ((a.""CommType"" + 1)) ""CardType"", a.""Address"" ""Name"", cast(a.""Number"" as character) ""No""
FROM ""WorkSite"" a", sql);
}

Expand Down
4 changes: 2 additions & 2 deletions FreeSql.Tests/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,12 +1206,12 @@ UNION ALL
WF_ProcessInstance = p
});

Assert.Equal(@"SELECT max(a.`Id`) TaskId, max(a.`Type`) TaskType, a.`ProcessId`, a.`NodeId`, a.`NodeName`
Assert.Equal(@"SELECT max(a.`Id`) `TaskId`, max(a.`Type`) `TaskType`, a.`ProcessId`, a.`NodeId`, a.`NodeName`
FROM `WF_Task` a
WHERE (a.`IsFinished` = 1 AND (a.`AuditorId` = '1' OR a.`AuditorId` = '1cb71584-a6dd-4b26-8c88-ed9fb8cf87a3'))
GROUP BY a.`ProcessId`, a.`NodeId`, a.`NodeName`", sqltmp12);
Assert.Equal(@"SELECT a.`TaskId` as1, a.`TaskType` as2, a.`ProcessId` as3, a.`NodeId` as4, a.`NodeName` as5, b.`Id` as6, b.`TaskType` as7, b.`ProcessId` as8, b.`NodeId` as9, b.`CreateTime` as10, b.`IsFinished` as11, b.`EnabledMark` as12
FROM ( SELECT max(a.`Id`) TaskId, max(a.`Type`) TaskType, a.`ProcessId`, a.`NodeId`, a.`NodeName`
FROM ( SELECT max(a.`Id`) `TaskId`, max(a.`Type`) `TaskType`, a.`ProcessId`, a.`NodeId`, a.`NodeName`
FROM `WF_Task` a
WHERE (a.`IsFinished` = 1 AND (a.`AuditorId` = '1' OR a.`AuditorId` = '1cb71584-a6dd-4b26-8c88-ed9fb8cf87a3'))
GROUP BY a.`ProcessId`, a.`NodeId`, a.`NodeName` ) a
Expand Down
24 changes: 17 additions & 7 deletions FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public CommonExpression(CommonUtils common)
}

internal const int ReadAnonymousFieldAsCsName = -53129;
internal string GetFieldAsCsName(string csname)
{
csname = _common.QuoteSqlName(csname);
if (_common.CodeFirst.IsSyncStructureToLower) csname = csname.ToLower();
if (_common.CodeFirst.IsSyncStructureToUpper) csname = csname.ToUpper();
return csname;
}
public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder field, ReadAnonymousTypeInfo parent, ref int index, Expression exp, Select0Provider select, BaseDiyMemberExpression diymemexp, List<GlobalFilter.Item> whereGlobalFilter, List<string> findIncludeMany, bool isAllDtoMap)
{
Func<ExpTSC> getTSC = () => new ExpTSC { _tables = _tables, diymemexp = diymemexp, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = false, style = ExpressionStyle.Where, whereGlobalFilter = whereGlobalFilter, dbParams = select?._params }; //#462 添加 DbParams 解决
Expand All @@ -47,7 +54,7 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
parent.DbField = $"-({ExpressionLambdaToSql(exp, getTSC())})";
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(parent.CsName));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(GetFieldAsCsName(parent.CsName)));
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
case ExpressionType.Convert: return ReadAnonymousField(_tables, field, parent, ref index, (exp as UnaryExpression)?.Operand, select, diymemexp, whereGlobalFilter, findIncludeMany, isAllDtoMap);
Expand All @@ -67,7 +74,7 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
parent.DbField = _common.FormatSql("{0}", constExp?.Value);
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(parent.CsName));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(GetFieldAsCsName(parent.CsName)));
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
case ExpressionType.Conditional:
Expand All @@ -90,7 +97,7 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
parent.DbField = ExpressionLambdaToSql(exp, getTSC());
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(parent.CsName));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(GetFieldAsCsName(parent.CsName)));
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
case ExpressionType.Parameter:
Expand Down Expand Up @@ -187,9 +194,12 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
parent.DbField = ExpressionLambdaToSql(exp, getTSC());
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false &&
parent.DbField.EndsWith(_common.QuoteSqlName(parent.CsName), StringComparison.CurrentCultureIgnoreCase) == false //DbField 和 CsName 相同的时候,不处理
) field.Append(_common.FieldAsAlias(parent.CsName));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false)
{
var csname = GetFieldAsCsName(parent.CsName);
if (parent.DbField.EndsWith(csname, StringComparison.CurrentCultureIgnoreCase) == false) //DbField 和 CsName 相同的时候,不处理
field.Append(_common.FieldAsAlias(csname));
}
if (parent.MapType == null) parent.MapType = SearchColumnByField(_tables, null, parent.DbField)?.Attribute.MapType ?? exp.Type;
return false;
}
Expand Down Expand Up @@ -336,7 +346,7 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
parent.DbField = $"({ExpressionLambdaToSql(exp, getTSC())})";
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(parent.CsName));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(GetFieldAsCsName(parent.CsName)));
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
}
Expand Down

0 comments on commit b193d86

Please sign in to comment.