Skip to content

Commit

Permalink
Query: Update formatting of Update command
Browse files Browse the repository at this point in the history
  • Loading branch information
smitpatel committed Aug 19, 2022
1 parent 7107cf0 commit a99fc9c
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 163 deletions.
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.SetColumnValues[0].Column.Name)} = ");
Visit(updateExpression.SetColumnValues[0].Value);
using (_relationalCommandBuilder.Indent())
{
_relationalCommandBuilder.Append("SET ");
GenerateList(updateExpression.SetColumnValues,
e =>
{
_relationalCommandBuilder.Append($"{_sqlGenerationHelper.DelimitIdentifier(e.Column.Name)} = ");
Visit(e.Value);
},
joinAction: e => e.AppendLine(","));
foreach (var setColumnValue in updateExpression.SetColumnValues.Skip(1))
{
_relationalCommandBuilder.AppendLine(",");
_relationalCommandBuilder.Append($"{_sqlGenerationHelper.DelimitIdentifier(setColumnValue.Column.Name)} = ");
Visit(setColumnValue.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(SetColumnValues[0].Column);
expressionPrinter.Append(" = ");
expressionPrinter.Visit(SetColumnValues[0].Value);
using (expressionPrinter.Indent())
{
var first = true;
foreach (var setColumnValue in SetColumnValues)
foreach (var setColumnValue in SetColumnValues.Skip(1))
{
if (first)
{
first = false;
}
else
{
expressionPrinter.AppendLine(",");
}
expressionPrinter.AppendLine(",");
expressionPrinter.Visit(setColumnValue.Column);
expressionPrinter.Append(" = ");
expressionPrinter.Visit(setColumnValue.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.SetColumnValues[0].Column);
Sql.Append(" = ");
Visit(updateExpression.SetColumnValues[0].Value);

using (Sql.Indent())
{
Sql.Append("SET ");
GenerateList(updateExpression.SetColumnValues,
e =>
{
Visit(e.Column);
Sql.Append(" = ");
Visit(e.Value);
},
joinAction: e => e.AppendLine(","));
Sql.AppendLine();
foreach (var setColumnValue in updateExpression.SetColumnValues.Skip(1))
{
Sql.AppendLine(",");
Visit(setColumnValue.Column);
Sql.Append(" = ");
Visit(setColumnValue.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

0 comments on commit a99fc9c

Please sign in to comment.