|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
| 5 | + |
| 6 | +namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 10 | +/// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 11 | +/// any release. You should only use it directly in your code with extreme caution and knowing that |
| 12 | +/// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 13 | +/// </summary> |
| 14 | +public class SqlServerSqlFunctionExpression : SqlFunctionExpression, IEquatable<SqlServerSqlFunctionExpression> |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 18 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 19 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 20 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 21 | + /// </summary> |
| 22 | + public SqlServerSqlFunctionExpression( |
| 23 | + string functionName, |
| 24 | + IEnumerable<SqlExpression> arguments, |
| 25 | + IReadOnlyList<OrderingExpression> aggregateOrderings, |
| 26 | + bool nullable, |
| 27 | + IEnumerable<bool> argumentsPropagateNullability, |
| 28 | + Type type, |
| 29 | + RelationalTypeMapping? typeMapping) |
| 30 | + : base(functionName, arguments, nullable, argumentsPropagateNullability, type, typeMapping) |
| 31 | + => AggregateOrderings = aggregateOrderings; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 35 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 36 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 37 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 38 | + /// </summary> |
| 39 | + public virtual IReadOnlyList<OrderingExpression> AggregateOrderings { get; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 43 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 44 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 45 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 46 | + /// </summary> |
| 47 | + protected override Expression VisitChildren(ExpressionVisitor visitor) |
| 48 | + { |
| 49 | + var visitedBase = (SqlFunctionExpression)base.VisitChildren(visitor); |
| 50 | + |
| 51 | + OrderingExpression[]? visitedAggregateOrderings = null; |
| 52 | + |
| 53 | + for (var i = 0; i < AggregateOrderings.Count; i++) |
| 54 | + { |
| 55 | + var visitedOrdering = (OrderingExpression)visitor.Visit(AggregateOrderings[i]); |
| 56 | + if (visitedOrdering != AggregateOrderings[i] && visitedAggregateOrderings is null) |
| 57 | + { |
| 58 | + visitedAggregateOrderings = new OrderingExpression[AggregateOrderings.Count]; |
| 59 | + |
| 60 | + for (var j = 0; j < visitedAggregateOrderings.Length; j++) |
| 61 | + { |
| 62 | + visitedAggregateOrderings[j] = AggregateOrderings[j]; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (visitedAggregateOrderings is not null) |
| 67 | + { |
| 68 | + visitedAggregateOrderings[i] = visitedOrdering; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return visitedBase != this || visitedAggregateOrderings is not null |
| 73 | + ? new SqlServerSqlFunctionExpression( |
| 74 | + Name, |
| 75 | + visitedBase.Arguments!, |
| 76 | + visitedAggregateOrderings ?? AggregateOrderings, |
| 77 | + IsNullable, |
| 78 | + ArgumentsPropagateNullability!, |
| 79 | + Type, |
| 80 | + TypeMapping) |
| 81 | + : this; |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 86 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 87 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 88 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 89 | + /// </summary> |
| 90 | + public override SqlServerSqlFunctionExpression ApplyTypeMapping(RelationalTypeMapping? typeMapping) |
| 91 | + => new( |
| 92 | + Name, |
| 93 | + Arguments!, |
| 94 | + AggregateOrderings, |
| 95 | + IsNullable, |
| 96 | + ArgumentsPropagateNullability!, |
| 97 | + Type, |
| 98 | + typeMapping ?? TypeMapping); |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 102 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 103 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 104 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 105 | + /// </summary> |
| 106 | + public override SqlFunctionExpression Update(SqlExpression? instance, IReadOnlyList<SqlExpression>? arguments) |
| 107 | + { |
| 108 | + Check.DebugAssert(arguments is not null, "arguments is not null"); |
| 109 | + Check.DebugAssert(instance is null, "instance not supported on SqlServerFunctionExpression"); |
| 110 | + |
| 111 | + return arguments.SequenceEqual(Arguments!) |
| 112 | + ? this |
| 113 | + : new SqlServerSqlFunctionExpression( |
| 114 | + Name, arguments, AggregateOrderings, IsNullable, ArgumentsPropagateNullability!, Type, TypeMapping); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 119 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 120 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 121 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 122 | + /// </summary> |
| 123 | + public virtual SqlFunctionExpression UpdateAggregateOrderings(IReadOnlyList<OrderingExpression> aggregateOrderings) |
| 124 | + => aggregateOrderings.SequenceEqual(AggregateOrderings) |
| 125 | + ? this |
| 126 | + : new SqlServerSqlFunctionExpression( |
| 127 | + Name, Arguments!, aggregateOrderings, IsNullable, ArgumentsPropagateNullability!, Type, TypeMapping); |
| 128 | + |
| 129 | + /// <inheritdoc /> |
| 130 | + protected override void Print(ExpressionPrinter expressionPrinter) |
| 131 | + { |
| 132 | + base.Print(expressionPrinter); |
| 133 | + |
| 134 | + if (AggregateOrderings.Count > 0) |
| 135 | + { |
| 136 | + expressionPrinter.Append(" WITHIN GROUP (ORDER BY "); |
| 137 | + expressionPrinter.VisitCollection(AggregateOrderings); |
| 138 | + expressionPrinter.Append(")"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /// <inheritdoc /> |
| 143 | + public override bool Equals(object? obj) |
| 144 | + => obj is SqlServerSqlFunctionExpression sqlServerFunctionExpression && Equals(sqlServerFunctionExpression); |
| 145 | + |
| 146 | + /// <inheritdoc /> |
| 147 | + public virtual bool Equals(SqlServerSqlFunctionExpression? other) |
| 148 | + => ReferenceEquals(this, other) |
| 149 | + || base.Equals(other) && AggregateOrderings.SequenceEqual(other.AggregateOrderings); |
| 150 | + |
| 151 | + /// <inheritdoc /> |
| 152 | + public override int GetHashCode() |
| 153 | + { |
| 154 | + var hash = new HashCode(); |
| 155 | + |
| 156 | + hash.Add(base.GetHashCode()); |
| 157 | + |
| 158 | + foreach (var orderingExpression in AggregateOrderings) |
| 159 | + { |
| 160 | + hash.Add(orderingExpression); |
| 161 | + } |
| 162 | + |
| 163 | + return hash.ToHashCode(); |
| 164 | + } |
| 165 | +} |
0 commit comments