|
| 1 | +namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// An expression that represents a PostgreSQL <c>unnest</c> function call in a SQL tree. |
| 5 | +/// </summary> |
| 6 | +/// <remarks> |
| 7 | +/// <para> |
| 8 | +/// This expression is just a <see cref="TableValuedFunctionExpression" />, adding the ability to provide an explicit column name |
| 9 | +/// for its output (<c>SELECT * FROM unnest(array) AS f(foo)</c>). This is necessary since when the column name isn't explicitly |
| 10 | +/// specified, it is automatically identical to the table alias (<c>f</c> above); since the table alias may get uniquified by |
| 11 | +/// EF, this would break queries. |
| 12 | +/// </para> |
| 13 | +/// <para> |
| 14 | +/// See <see href="https://www.postgresql.org/docs/current/functions-array.html#ARRAY-FUNCTIONS-TABLE">unnest</see> for more |
| 15 | +/// information and examples. |
| 16 | +/// </para> |
| 17 | +/// <para> |
| 18 | +/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 19 | +/// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 20 | +/// any release. You should only use it directly in your code with extreme caution and knowing that |
| 21 | +/// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 22 | +/// </para> |
| 23 | +/// </remarks> |
| 24 | +public class PostgresUnnestExpression : TableValuedFunctionExpression |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// The array to be un-nested into a table. |
| 28 | + /// </summary> |
| 29 | + /// <remarks> |
| 30 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 31 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 32 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 33 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 34 | + /// </remarks> |
| 35 | + public virtual SqlExpression Array |
| 36 | + => Arguments[0]; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// The name of the column to be projected out from the <c>unnest</c> call. |
| 40 | + /// </summary> |
| 41 | + /// <remarks> |
| 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 | + /// </remarks> |
| 47 | + public virtual string ColumnName { get; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 51 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 52 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 53 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 54 | + /// </summary> |
| 55 | + public PostgresUnnestExpression(SqlExpression array, string columnName) |
| 56 | + : this("u", array, columnName) |
| 57 | + { |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 62 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 63 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 64 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 65 | + /// </summary> |
| 66 | + public PostgresUnnestExpression(string alias, SqlExpression array, string columnName) |
| 67 | + : base(alias, "unnest", schema: null, builtIn: true, new[] { array }) |
| 68 | + { |
| 69 | + ColumnName = columnName; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 74 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 75 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 76 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 77 | + /// </summary> |
| 78 | + public override TableValuedFunctionExpression Update(IReadOnlyList<SqlExpression> arguments) |
| 79 | + => arguments is [var singleArgument] |
| 80 | + ? Update(singleArgument) |
| 81 | + : throw new ArgumentException(); |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Creates a new expression that is like this one, but using the supplied children. If all of the children are the same, it will |
| 85 | + /// return this expression. |
| 86 | + /// </summary> |
| 87 | + /// <param name="array">The <see cref="Array" /> property of the result.</param> |
| 88 | + /// <returns>This expression if no children changed, or an expression with the updated children.</returns> |
| 89 | + public virtual PostgresUnnestExpression Update(SqlExpression array) |
| 90 | + => array != Array |
| 91 | + ? new PostgresUnnestExpression(Alias, array, ColumnName) |
| 92 | + : this; |
| 93 | + |
| 94 | + /// <inheritdoc /> |
| 95 | + protected override void Print(ExpressionPrinter expressionPrinter) |
| 96 | + { |
| 97 | + expressionPrinter.Append(Name); |
| 98 | + expressionPrinter.Append("("); |
| 99 | + expressionPrinter.VisitCollection(Arguments); |
| 100 | + expressionPrinter.Append(")"); |
| 101 | + |
| 102 | + PrintAnnotations(expressionPrinter); |
| 103 | + expressionPrinter |
| 104 | + .Append(" AS ") |
| 105 | + .Append(Alias) |
| 106 | + .Append("(") |
| 107 | + .Append(ColumnName) |
| 108 | + .Append(")"); |
| 109 | + } |
| 110 | + |
| 111 | + /// <inheritdoc /> |
| 112 | + public override bool Equals(object? obj) |
| 113 | + => obj != null |
| 114 | + && (ReferenceEquals(this, obj) |
| 115 | + || obj is PostgresUnnestExpression unnestExpression |
| 116 | + && Equals(unnestExpression)); |
| 117 | + |
| 118 | + private bool Equals(PostgresUnnestExpression unnestExpression) |
| 119 | + => base.Equals(unnestExpression) && ColumnName == unnestExpression.ColumnName; |
| 120 | + |
| 121 | + /// <inheritdoc /> |
| 122 | + public override int GetHashCode() |
| 123 | + => base.GetHashCode(); |
| 124 | +} |
0 commit comments