-
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.
To maintain clean separation between translation and SQL generation
- Loading branch information
Showing
4 changed files
with
209 additions
and
77 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
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,73 @@ | ||
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<TableExpression> FromItems { get; } | ||
|
||
/// <summary> | ||
/// The WHERE predicate for the DELETE. | ||
/// </summary> | ||
public SqlExpression? Predicate { get; } | ||
|
||
public PostgresDeleteExpression(TableExpression table, IReadOnlyList<TableExpression> 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) | ||
{ | ||
expressionPrinter | ||
.Append("USING ") | ||
.Append(string.Join(", ", FromItems.Select(fi => $"{fi.Name} AS {fi.Alias}"))); | ||
} | ||
|
||
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(); | ||
} |
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
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.BulkOperationWithUnsupportedOperatorInSqlGeneration); | ||
} | ||
|
||
var fromItems = new List<TableExpression>(); | ||
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 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.BulkOperationWithUnsupportedOperatorInSqlGeneration); | ||
} | ||
} | ||
|
||
// 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