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

Query: Update formatting of Update command #28797

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
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
18 changes: 9 additions & 9 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,17 +1271,17 @@ protected override Expression VisitUpdate(UpdateExpression updateExpression)
_relationalCommandBuilder.Append("UPDATE ");
Visit(updateExpression.Table);
_relationalCommandBuilder.AppendLine();
_relationalCommandBuilder.Append("SET ");
_relationalCommandBuilder.Append($"{_sqlGenerationHelper.DelimitIdentifier(updateExpression.ColumnValueSetters[0].Column.Name)} = ");
Visit(updateExpression.ColumnValueSetters[0].Value);
using (_relationalCommandBuilder.Indent())
{
_relationalCommandBuilder.Append("SET ");
GenerateList(updateExpression.ColumnValueSetters,
e =>
{
_relationalCommandBuilder.Append($"{_sqlGenerationHelper.DelimitIdentifier(e.Column.Name)} = ");
Visit(e.Value);

},
joinAction: e => e.AppendLine(","));
foreach (var columnValueSetter in updateExpression.ColumnValueSetters.Skip(1))
{
_relationalCommandBuilder.AppendLine(",");
_relationalCommandBuilder.Append($"{_sqlGenerationHelper.DelimitIdentifier(columnValueSetter.Column.Name)} = ");
Visit(columnValueSetter.Value);
}
}

var predicate = selectExpression.Predicate;
Expand Down
18 changes: 7 additions & 11 deletions src/EFCore.Relational/Query/SqlExpressions/UpdateExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,21 @@ public void Print(ExpressionPrinter expressionPrinter)
}
expressionPrinter.AppendLine();
expressionPrinter.AppendLine($"UPDATE {Table.Name} AS {Table.Alias}");
expressionPrinter.AppendLine("SET");
expressionPrinter.AppendLine("SET ");
expressionPrinter.Visit(ColumnValueSetters[0].Column);
expressionPrinter.Append(" = ");
expressionPrinter.Visit(ColumnValueSetters[0].Value);
using (expressionPrinter.Indent())
{
var first = true;
foreach (var columnValueSetter in ColumnValueSetters)
foreach (var columnValueSetter in ColumnValueSetters.Skip(1))
{
if (first)
{
first = false;
}
else
{
expressionPrinter.AppendLine(",");
}
expressionPrinter.AppendLine(",");
expressionPrinter.Visit(columnValueSetter.Column);
expressionPrinter.Append(" = ");
expressionPrinter.Visit(columnValueSetter.Value);
}
}
expressionPrinter.AppendLine();
expressionPrinter.Visit(SelectExpression);
}

Expand Down
26 changes: 14 additions & 12 deletions src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
Expand Down Expand Up @@ -91,22 +92,23 @@ protected override Expression VisitUpdate(UpdateExpression updateExpression)
GenerateTop(selectExpression);

Sql.AppendLine($"{Dependencies.SqlGenerationHelper.DelimitIdentifier(updateExpression.Table.Alias)}");
Sql.Append("SET ");
Visit(updateExpression.ColumnValueSetters[0].Column);
Sql.Append(" = ");
Visit(updateExpression.ColumnValueSetters[0].Value);

using (Sql.Indent())
{
Sql.Append("SET ");
GenerateList(updateExpression.ColumnValueSetters,
e =>
{
Visit(e.Column);
Sql.Append(" = ");
Visit(e.Value);

},
joinAction: e => e.AppendLine(","));
Sql.AppendLine();
foreach (var columnValueSetter in updateExpression.ColumnValueSetters.Skip(1))
{
Sql.AppendLine(",");
Visit(columnValueSetter.Column);
Sql.Append(" = ");
Visit(columnValueSetter.Value);
}
}

Sql.Append("FROM ");
Sql.AppendLine().Append("FROM ");
GenerateList(selectExpression.Tables, e => Visit(e), sql => sql.AppendLine());

if (selectExpression.Predicate != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public override async Task Update_where_hierarchy(bool async)

AssertExecuteUpdateSql(
@"UPDATE [a]
SET [a].[Name] = N'Animal'
SET [a].[Name] = N'Animal'
FROM [Animals] AS [a]
WHERE [a].[CountryId] = 1 AND [a].[Name] = N'Great spotted kiwi'");
}
Expand All @@ -146,7 +146,7 @@ public override async Task Update_where_hierarchy_derived(bool async)

AssertExecuteUpdateSql(
@"UPDATE [a]
SET [a].[Name] = N'Kiwi'
SET [a].[Name] = N'Kiwi'
FROM [Animals] AS [a]
WHERE [a].[Discriminator] = N'Kiwi' AND [a].[CountryId] = 1 AND [a].[Name] = N'Great spotted kiwi'");
}
Expand All @@ -157,7 +157,7 @@ public override async Task Update_where_using_hierarchy(bool async)

AssertExecuteUpdateSql(
@"UPDATE [c]
SET [c].[Name] = N'Monovia'
SET [c].[Name] = N'Monovia'
FROM [Countries] AS [c]
WHERE (
SELECT COUNT(*)
Expand All @@ -171,7 +171,7 @@ public override async Task Update_where_using_hierarchy_derived(bool async)

AssertExecuteUpdateSql(
@"UPDATE [c]
SET [c].[Name] = N'Monovia'
SET [c].[Name] = N'Monovia'
FROM [Countries] AS [c]
WHERE (
SELECT COUNT(*)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public override async Task Update_where_hierarchy(bool async)

AssertExecuteUpdateSql(
@"UPDATE [a]
SET [a].[Name] = N'Animal'
SET [a].[Name] = N'Animal'
FROM [Animals] AS [a]
WHERE [a].[Name] = N'Great spotted kiwi'");
}
Expand All @@ -145,7 +145,7 @@ public override async Task Update_where_hierarchy_derived(bool async)

AssertExecuteUpdateSql(
@"UPDATE [a]
SET [a].[Name] = N'Kiwi'
SET [a].[Name] = N'Kiwi'
FROM [Animals] AS [a]
WHERE [a].[Discriminator] = N'Kiwi' AND [a].[Name] = N'Great spotted kiwi'");
}
Expand All @@ -156,7 +156,7 @@ public override async Task Update_where_using_hierarchy(bool async)

AssertExecuteUpdateSql(
@"UPDATE [c]
SET [c].[Name] = N'Monovia'
SET [c].[Name] = N'Monovia'
FROM [Countries] AS [c]
WHERE (
SELECT COUNT(*)
Expand All @@ -170,7 +170,7 @@ public override async Task Update_where_using_hierarchy_derived(bool async)

AssertExecuteUpdateSql(
@"UPDATE [c]
SET [c].[Name] = N'Monovia'
SET [c].[Name] = N'Monovia'
FROM [Countries] AS [c]
WHERE (
SELECT COUNT(*)
Expand Down
Loading