|
| 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 SqlServerAggregateFunctionExpression : SqlExpression |
| 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 SqlServerAggregateFunctionExpression( |
| 23 | + string name, |
| 24 | + IReadOnlyList<SqlExpression> arguments, |
| 25 | + IReadOnlyList<OrderingExpression> orderings, |
| 26 | + bool nullable, |
| 27 | + IEnumerable<bool> argumentsPropagateNullability, |
| 28 | + Type type, |
| 29 | + RelationalTypeMapping? typeMapping) |
| 30 | + : base(type, typeMapping) |
| 31 | + { |
| 32 | + Name = name; |
| 33 | + Arguments = arguments.ToList(); |
| 34 | + Orderings = orderings; |
| 35 | + IsNullable = nullable; |
| 36 | + ArgumentsPropagateNullability = argumentsPropagateNullability.ToList(); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 41 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 42 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 43 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 44 | + /// </summary> |
| 45 | + public virtual string Name { get; } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 49 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 50 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 51 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 52 | + /// </summary> |
| 53 | + public virtual IReadOnlyList<SqlExpression> Arguments { get; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 57 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 58 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 59 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 60 | + /// </summary> |
| 61 | + public virtual IReadOnlyList<OrderingExpression> Orderings { get; } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 65 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 66 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 67 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 68 | + /// </summary> |
| 69 | + public virtual bool IsNullable { get; } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 73 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 74 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 75 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 76 | + /// </summary> |
| 77 | + public virtual IReadOnlyList<bool> ArgumentsPropagateNullability { get; } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 81 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 82 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 83 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 84 | + /// </summary> |
| 85 | + protected override Expression VisitChildren(ExpressionVisitor visitor) |
| 86 | + { |
| 87 | + SqlExpression[]? arguments = null; |
| 88 | + for (var i = 0; i < Arguments.Count; i++) |
| 89 | + { |
| 90 | + var visitedArgument = (SqlExpression)visitor.Visit(Arguments[i]); |
| 91 | + if (visitedArgument != Arguments[i] && arguments is null) |
| 92 | + { |
| 93 | + arguments = new SqlExpression[Arguments.Count]; |
| 94 | + |
| 95 | + for (var j = 0; j < i; j++) |
| 96 | + { |
| 97 | + arguments[j] = Arguments[j]; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (arguments is not null) |
| 102 | + { |
| 103 | + arguments[i] = visitedArgument; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + OrderingExpression[]? orderings = null; |
| 108 | + for (var i = 0; i < Orderings.Count; i++) |
| 109 | + { |
| 110 | + var visitedOrdering = (OrderingExpression)visitor.Visit(Orderings[i]); |
| 111 | + if (visitedOrdering != Orderings[i] && orderings is null) |
| 112 | + { |
| 113 | + orderings = new OrderingExpression[Orderings.Count]; |
| 114 | + |
| 115 | + for (var j = 0; j < i; j++) |
| 116 | + { |
| 117 | + orderings[j] = Orderings[j]; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (orderings is not null) |
| 122 | + { |
| 123 | + orderings[i] = visitedOrdering; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return arguments is not null || orderings is not null |
| 128 | + ? new SqlServerAggregateFunctionExpression( |
| 129 | + Name, |
| 130 | + arguments ?? Arguments, |
| 131 | + orderings ?? Orderings, |
| 132 | + IsNullable, |
| 133 | + ArgumentsPropagateNullability, |
| 134 | + Type, |
| 135 | + TypeMapping) |
| 136 | + : this; |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 141 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 142 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 143 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 144 | + /// </summary> |
| 145 | + public virtual SqlServerAggregateFunctionExpression ApplyTypeMapping(RelationalTypeMapping? typeMapping) |
| 146 | + => new( |
| 147 | + Name, |
| 148 | + Arguments, |
| 149 | + Orderings, |
| 150 | + IsNullable, |
| 151 | + ArgumentsPropagateNullability, |
| 152 | + Type, |
| 153 | + typeMapping ?? TypeMapping); |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 157 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 158 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 159 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 160 | + /// </summary> |
| 161 | + public virtual SqlServerAggregateFunctionExpression Update( |
| 162 | + IReadOnlyList<SqlExpression> arguments, |
| 163 | + IReadOnlyList<OrderingExpression> orderings) |
| 164 | + => (ReferenceEquals(arguments, Arguments) || arguments.SequenceEqual(Arguments)) |
| 165 | + && (ReferenceEquals(orderings, Orderings) || orderings.SequenceEqual(Orderings)) |
| 166 | + ? this |
| 167 | + : new SqlServerAggregateFunctionExpression( |
| 168 | + Name, |
| 169 | + arguments, |
| 170 | + orderings, |
| 171 | + IsNullable, |
| 172 | + ArgumentsPropagateNullability, |
| 173 | + Type, |
| 174 | + TypeMapping); |
| 175 | + |
| 176 | + /// <inheritdoc /> |
| 177 | + protected override void Print(ExpressionPrinter expressionPrinter) |
| 178 | + { |
| 179 | + expressionPrinter.Append(Name); |
| 180 | + |
| 181 | + expressionPrinter.Append("("); |
| 182 | + expressionPrinter.VisitCollection(Arguments); |
| 183 | + expressionPrinter.Append(")"); |
| 184 | + |
| 185 | + if (Orderings.Count > 0) |
| 186 | + { |
| 187 | + expressionPrinter.Append(" WITHIN GROUP (ORDER BY "); |
| 188 | + expressionPrinter.VisitCollection(Orderings); |
| 189 | + expressionPrinter.Append(")"); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /// <inheritdoc /> |
| 194 | + public override bool Equals(object? obj) |
| 195 | + => obj is SqlServerAggregateFunctionExpression sqlServerFunctionExpression && Equals(sqlServerFunctionExpression); |
| 196 | + |
| 197 | + private bool Equals(SqlServerAggregateFunctionExpression? other) |
| 198 | + => ReferenceEquals(this, other) |
| 199 | + || other is not null |
| 200 | + && base.Equals(other) |
| 201 | + && Name == other.Name |
| 202 | + && Arguments.SequenceEqual(other.Arguments) |
| 203 | + && Orderings.SequenceEqual(other.Orderings); |
| 204 | + |
| 205 | + /// <inheritdoc /> |
| 206 | + public override int GetHashCode() |
| 207 | + { |
| 208 | + var hash = new HashCode(); |
| 209 | + hash.Add(base.GetHashCode()); |
| 210 | + hash.Add(Name); |
| 211 | + |
| 212 | + for (var i = 0; i < Arguments.Count; i++) |
| 213 | + { |
| 214 | + hash.Add(Arguments[i]); |
| 215 | + } |
| 216 | + |
| 217 | + for (var i = 0; i < Orderings.Count; i++) |
| 218 | + { |
| 219 | + hash.Add(Orderings[i]); |
| 220 | + } |
| 221 | + |
| 222 | + return hash.ToHashCode(); |
| 223 | + } |
| 224 | +} |
0 commit comments