-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Following dotnet/efcore#28492 Part of #2450
- Loading branch information
Showing
21 changed files
with
1,400 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/EFCore.PG/Query/Expressions/Internal/PostgresDeleteExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal; | ||
|
||
public sealed class PostgresDeleteExpression : Expression, IPrintableExpression | ||
{ | ||
/// <summary> | ||
/// The tables that rows are to be deleted from. | ||
/// </summary> | ||
public TableExpression Table { get; } | ||
|
||
/// <summary> | ||
/// Additional tables which can be referenced in the predicate. | ||
/// </summary> | ||
public IReadOnlyList<TableExpressionBase> FromItems { get; } | ||
|
||
/// <summary> | ||
/// The WHERE predicate for the DELETE. | ||
/// </summary> | ||
public SqlExpression? Predicate { get; } | ||
|
||
public PostgresDeleteExpression(TableExpression table, IReadOnlyList<TableExpressionBase> fromItems, SqlExpression? predicate) | ||
=> (Table, FromItems, Predicate) = (table, fromItems, predicate); | ||
|
||
/// <inheritdoc /> | ||
public override Type Type | ||
=> typeof(object); | ||
|
||
/// <inheritdoc /> | ||
public override ExpressionType NodeType | ||
=> ExpressionType.Extension; | ||
|
||
protected override Expression VisitChildren(ExpressionVisitor visitor) | ||
=> Predicate is null | ||
? this | ||
: Update((SqlExpression?)visitor.Visit(Predicate)); | ||
|
||
public PostgresDeleteExpression Update(SqlExpression? predicate) | ||
=> predicate == Predicate | ||
? this | ||
: new PostgresDeleteExpression(Table, FromItems, predicate); | ||
|
||
public void Print(ExpressionPrinter expressionPrinter) | ||
{ | ||
expressionPrinter.AppendLine($"DELETE FROM {Table.Name} AS {Table.Alias}"); | ||
|
||
if (FromItems.Count > 0) | ||
{ | ||
var first = true; | ||
foreach (var fromItem in FromItems) | ||
{ | ||
if (first) | ||
{ | ||
expressionPrinter.Append("USING "); | ||
first = false; | ||
} | ||
else | ||
{ | ||
expressionPrinter.Append(", "); | ||
} | ||
|
||
expressionPrinter.Visit(fromItem); | ||
} | ||
} | ||
|
||
if (Predicate is not null) | ||
{ | ||
expressionPrinter.Append("WHERE "); | ||
expressionPrinter.Visit(Predicate); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object? obj) | ||
=> obj != null | ||
&& (ReferenceEquals(this, obj) | ||
|| obj is PostgresDeleteExpression pgDeleteExpression | ||
&& Equals(pgDeleteExpression)); | ||
|
||
private bool Equals(PostgresDeleteExpression pgDeleteExpression) | ||
=> Table == pgDeleteExpression.Table | ||
&& FromItems.SequenceEqual(pgDeleteExpression.FromItems) | ||
&& (Predicate is null ? pgDeleteExpression.Predicate is null : Predicate.Equals(pgDeleteExpression.Predicate)); | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode() => Table.GetHashCode(); | ||
} |
86 changes: 86 additions & 0 deletions
86
src/EFCore.PG/Query/Internal/NonQueryConvertingExpressionVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal; | ||
|
||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal; | ||
|
||
/// <summary> | ||
/// Converts the relational <see cref="NonQueryExpression" /> into a PG-specific <see cref="PostgresDeleteExpression" />, which | ||
/// precisely models a DELETE statement in PostgreSQL. This is done to handle the PG-specific USING syntax for table joining. | ||
/// </summary> | ||
public class NonQueryConvertingExpressionVisitor : ExpressionVisitor | ||
{ | ||
public virtual Expression Process(Expression node) | ||
=> node switch | ||
{ | ||
DeleteExpression deleteExpression => VisitDelete(deleteExpression), | ||
|
||
_ => node | ||
}; | ||
|
||
protected virtual Expression VisitDelete(DeleteExpression deleteExpression) | ||
{ | ||
var selectExpression = deleteExpression.SelectExpression; | ||
|
||
if (selectExpression.Offset != null | ||
|| selectExpression.Limit != null | ||
|| selectExpression.Having != null | ||
|| selectExpression.Orderings.Count > 0 | ||
|| selectExpression.GroupBy.Count > 0 | ||
|| selectExpression.Projection.Count > 0) | ||
{ | ||
throw new InvalidOperationException( | ||
RelationalStrings.ExecuteOperationWithUnsupportedOperatorInSqlGeneration( | ||
nameof(RelationalQueryableExtensions.ExecuteDelete))); | ||
} | ||
|
||
var fromItems = new List<TableExpressionBase>(); | ||
SqlExpression? joinPredicates = null; | ||
|
||
// The SelectExpression also contains the target table being modified (same as deleteExpression.Table). | ||
// If it has additional inner joins, use the PostgreSQL-specific USING syntax to express the join. | ||
// Note that the non-join TableExpression isn't necessary the target table - through projection the last table being | ||
// joined may be the one being modified. | ||
foreach (var tableBase in selectExpression.Tables) | ||
{ | ||
switch (tableBase) | ||
{ | ||
case TableExpression tableExpression: | ||
if (tableExpression != deleteExpression.Table) | ||
{ | ||
fromItems.Add(tableExpression); | ||
} | ||
|
||
break; | ||
|
||
case InnerJoinExpression { Table: { } tableExpression } innerJoinExpression: | ||
if (tableExpression != deleteExpression.Table) | ||
{ | ||
fromItems.Add(tableExpression); | ||
} | ||
|
||
joinPredicates = joinPredicates is null | ||
? innerJoinExpression.JoinPredicate | ||
: new SqlBinaryExpression( | ||
ExpressionType.AndAlso, joinPredicates, innerJoinExpression.JoinPredicate, typeof(bool), | ||
innerJoinExpression.JoinPredicate.TypeMapping); | ||
break; | ||
|
||
default: | ||
throw new InvalidOperationException( | ||
RelationalStrings.ExecuteOperationWithUnsupportedOperatorInSqlGeneration( | ||
nameof(RelationalQueryableExtensions.ExecuteDelete))); | ||
} | ||
} | ||
|
||
// Combine the join predicates (if any) before the user-provided predicate | ||
var predicate = (joinPredicates, selectExpression.Predicate) switch | ||
{ | ||
(null, not null) => selectExpression.Predicate, | ||
(not null, null) => joinPredicates, | ||
(null, null) => null, | ||
(not null, not null) => new SqlBinaryExpression( | ||
ExpressionType.AndAlso, joinPredicates, selectExpression.Predicate, typeof(bool), joinPredicates.TypeMapping) | ||
}; | ||
|
||
return new PostgresDeleteExpression(deleteExpression.Table, fromItems, predicate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/EFCore.PG/Query/Internal/NpgsqlQueryableMethodTranslatingExpressionVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal; | ||
|
||
public class NpgsqlQueryableMethodTranslatingExpressionVisitor : RelationalQueryableMethodTranslatingExpressionVisitor | ||
{ | ||
public NpgsqlQueryableMethodTranslatingExpressionVisitor( | ||
QueryableMethodTranslatingExpressionVisitorDependencies dependencies, | ||
RelationalQueryableMethodTranslatingExpressionVisitorDependencies relationalDependencies, | ||
QueryCompilationContext queryCompilationContext) | ||
: base(dependencies, relationalDependencies, queryCompilationContext) | ||
{ | ||
} | ||
|
||
protected override bool IsValidSelectExpressionForExecuteDelete( | ||
SelectExpression selectExpression, | ||
EntityShaperExpression entityShaperExpression, | ||
[NotNullWhen(true)] out TableExpression? tableExpression) | ||
{ | ||
// The default relational behavior is to allow only single-table expressions, and the only permitted feature is a predicate. | ||
// Here we extend this to also inner joins to tables, which we generate via the PostgreSQL-specific USING construct. | ||
if (selectExpression.Offset == null | ||
&& selectExpression.Limit == null | ||
// If entity type has primary key then Distinct is no-op | ||
&& (!selectExpression.IsDistinct || entityShaperExpression.EntityType.FindPrimaryKey() != null) | ||
&& selectExpression.GroupBy.Count == 0 | ||
&& selectExpression.Having == null | ||
&& selectExpression.Orderings.Count == 0) | ||
{ | ||
TableExpressionBase? table = null; | ||
if (selectExpression.Tables.Count == 1) | ||
{ | ||
table = selectExpression.Tables[0]; | ||
} | ||
else if (selectExpression.Tables.All(t => t is TableExpression or InnerJoinExpression)) | ||
{ | ||
var projectionBindingExpression = (ProjectionBindingExpression)entityShaperExpression.ValueBufferExpression; | ||
var entityProjectionExpression = (EntityProjectionExpression)selectExpression.GetProjection(projectionBindingExpression); | ||
var column = entityProjectionExpression.BindProperty(entityShaperExpression.EntityType.GetProperties().First()); | ||
table = column.Table; | ||
if (table is JoinExpressionBase joinExpressionBase) | ||
{ | ||
table = joinExpressionBase.Table; | ||
} | ||
} | ||
|
||
if (table is TableExpression te) | ||
{ | ||
tableExpression = te; | ||
return true; | ||
} | ||
} | ||
|
||
tableExpression = null; | ||
return false; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/EFCore.PG/Query/Internal/NpgsqlQueryableMethodTranslatingExpressionVisitorFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal; | ||
|
||
public class NpgsqlQueryableMethodTranslatingExpressionVisitorFactory : IQueryableMethodTranslatingExpressionVisitorFactory | ||
{ | ||
public NpgsqlQueryableMethodTranslatingExpressionVisitorFactory( | ||
QueryableMethodTranslatingExpressionVisitorDependencies dependencies, | ||
RelationalQueryableMethodTranslatingExpressionVisitorDependencies relationalDependencies) | ||
{ | ||
Dependencies = dependencies; | ||
RelationalDependencies = relationalDependencies; | ||
} | ||
|
||
protected virtual QueryableMethodTranslatingExpressionVisitorDependencies Dependencies { get; } | ||
|
||
protected virtual RelationalQueryableMethodTranslatingExpressionVisitorDependencies RelationalDependencies { get; } | ||
|
||
public virtual QueryableMethodTranslatingExpressionVisitor Create(QueryCompilationContext queryCompilationContext) | ||
=> new NpgsqlQueryableMethodTranslatingExpressionVisitor(Dependencies, RelationalDependencies, queryCompilationContext); | ||
} |
10 changes: 10 additions & 0 deletions
10
test/EFCore.PG.FunctionalTests/BulkUpdates/FiltersInheritanceBulkUpdatesNpgsqlFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.BulkUpdates; | ||
|
||
public class FiltersInheritanceBulkUpdatesNpgsqlFixture : InheritanceBulkUpdatesNpgsqlFixture | ||
{ | ||
protected override bool EnableFilters | ||
=> true; | ||
} |
Oops, something went wrong.