|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Internal; |
| 3 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions; |
| 4 | +using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal; |
| 5 | + |
| 6 | +namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A postprocessor that rewrites join predicates for PERIOD foreign keys. For PERIOD FKs, the range column join |
| 10 | +/// condition must use PostgreSQL range containment (<c>@></c>) rather than equality (<c>=</c>), since the principal's |
| 11 | +/// range must contain the dependent's range. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 15 | +/// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 16 | +/// any release. You should only use it directly in your code with extreme caution and knowing that |
| 17 | +/// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 18 | +/// </remarks> |
| 19 | +public class NpgsqlPeriodForeignKeyPostprocessor(QueryTrackingBehavior queryTrackingBehavior) : ExpressionVisitor |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 23 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 24 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 25 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 26 | + /// </summary> |
| 27 | + protected override Expression VisitExtension(Expression extensionExpression) |
| 28 | + => extensionExpression switch |
| 29 | + { |
| 30 | + ShapedQueryExpression shapedQueryExpression |
| 31 | + => shapedQueryExpression.Update( |
| 32 | + Visit(shapedQueryExpression.QueryExpression), |
| 33 | + Visit(shapedQueryExpression.ShaperExpression)), |
| 34 | + |
| 35 | + // For equality predicates between columns, check if they correspond to a PERIOD FK range column |
| 36 | + // and replace with range containment (@>). |
| 37 | + // Note that EF's change tracker assumes equality, but the temporal foreign key (PERIOD) uses containment; |
| 38 | + // the change tracker is therefore currently incompatible with it. We check and throw, directing the user to use |
| 39 | + // a no-tracking query instead. |
| 40 | + SqlBinaryExpression { OperatorType: ExpressionType.Equal } eqExpression |
| 41 | + when eqExpression.Left is ColumnExpression leftCol |
| 42 | + && eqExpression.Right is ColumnExpression rightCol |
| 43 | + && TryGetPeriodFkInfo(leftCol, rightCol, out var principalColumn, out var dependentColumn) |
| 44 | + => queryTrackingBehavior is QueryTrackingBehavior.TrackAll |
| 45 | + ? throw new InvalidOperationException(NpgsqlStrings.PeriodForeignKeyTrackingNotSupported) |
| 46 | + : new PgBinaryExpression( |
| 47 | + PgExpressionType.Contains, |
| 48 | + principalColumn, |
| 49 | + dependentColumn, |
| 50 | + typeof(bool), |
| 51 | + eqExpression.TypeMapping), |
| 52 | + |
| 53 | + _ => base.VisitExtension(extensionExpression) |
| 54 | + }; |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Determines whether two columns in an equality predicate correspond to the range column of a PERIOD FK, |
| 58 | + /// and if so, identifies which is the principal column and which is the dependent column. |
| 59 | + /// </summary> |
| 60 | + private static bool TryGetPeriodFkInfo( |
| 61 | + ColumnExpression leftCol, |
| 62 | + ColumnExpression rightCol, |
| 63 | + [NotNullWhen(true)] out ColumnExpression? principalColumn, |
| 64 | + [NotNullWhen(true)] out ColumnExpression? dependentColumn) |
| 65 | + { |
| 66 | + principalColumn = null; |
| 67 | + dependentColumn = null; |
| 68 | + |
| 69 | + // We need column metadata to identify the FK |
| 70 | + if (leftCol.Column is not { } leftColumnBase || rightCol.Column is not { } rightColumnBase) |
| 71 | + { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // Check all properties mapped to the left column for PERIOD FK participation. |
| 76 | + // We check both GetContainingForeignKeys() (property is on the dependent/FK side) and |
| 77 | + // GetContainingKeys() -> GetReferencingForeignKeys() (property is on the principal/PK side). |
| 78 | + foreach (var leftMapping in leftColumnBase.PropertyMappings) |
| 79 | + { |
| 80 | + var leftProperty = leftMapping.Property; |
| 81 | + |
| 82 | + foreach (var fk in GetPeriodForeignKeys(leftProperty)) |
| 83 | + { |
| 84 | + // The range property is the last one in the FK |
| 85 | + var fkRangeProperty = fk.Properties[^1]; |
| 86 | + var principalRangeProperty = fk.PrincipalKey.Properties[^1]; |
| 87 | + |
| 88 | + // Determine if the left column is the dependent or principal range property, |
| 89 | + // and look for the counterpart on the right column. |
| 90 | + IProperty expectedRight; |
| 91 | + ColumnExpression candidatePrincipal, candidateDependent; |
| 92 | + |
| 93 | + if (leftProperty == fkRangeProperty) |
| 94 | + { |
| 95 | + expectedRight = principalRangeProperty; |
| 96 | + candidatePrincipal = rightCol; |
| 97 | + candidateDependent = leftCol; |
| 98 | + } |
| 99 | + else if (leftProperty == principalRangeProperty) |
| 100 | + { |
| 101 | + expectedRight = fkRangeProperty; |
| 102 | + candidatePrincipal = leftCol; |
| 103 | + candidateDependent = rightCol; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + foreach (var rightMapping in rightColumnBase.PropertyMappings) |
| 111 | + { |
| 112 | + if (rightMapping.Property == expectedRight) |
| 113 | + { |
| 114 | + principalColumn = candidatePrincipal; |
| 115 | + dependentColumn = candidateDependent; |
| 116 | + return true; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return false; |
| 123 | + |
| 124 | + static IEnumerable<IForeignKey> GetPeriodForeignKeys(IProperty property) |
| 125 | + { |
| 126 | + foreach (var fk in property.GetContainingForeignKeys()) |
| 127 | + { |
| 128 | + if (fk.GetPeriod() == true) |
| 129 | + { |
| 130 | + yield return fk; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + foreach (var key in property.GetContainingKeys()) |
| 135 | + { |
| 136 | + foreach (var fk in key.GetReferencingForeignKeys()) |
| 137 | + { |
| 138 | + if (fk.GetPeriod() == true) |
| 139 | + { |
| 140 | + yield return fk; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments