|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Linq.Expressions; |
| 6 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 7 | +using Microsoft.EntityFrameworkCore.Metadata; |
| 8 | +using Microsoft.EntityFrameworkCore.Utilities; |
| 9 | + |
| 10 | +namespace Microsoft.EntityFrameworkCore.Query |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// <para> |
| 14 | + /// An expression that represents creation of a collection in <see cref="ShapedQueryExpression.ShaperExpression" /> for relational providers. |
| 15 | + /// </para> |
| 16 | + /// <para> |
| 17 | + /// This type is typically used by database providers (and other extensions). It is generally |
| 18 | + /// not used in application code. |
| 19 | + /// </para> |
| 20 | + /// </summary> |
| 21 | + public class CollectionResultExpression : Expression, IPrintableExpression |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// Creates a new instance of the <see cref="CollectionResultExpression" /> class. |
| 25 | + /// </summary> |
| 26 | + /// <param name="projectionBindingExpression"> An expression reprensenting how to get the subquery from SelectExpression to get the elements. </param> |
| 27 | + /// <param name="navigation"> A navigation associated with this collection, if any. </param> |
| 28 | + /// <param name="elementType"> The clr type of individual elements in the collection. </param> |
| 29 | + public CollectionResultExpression( |
| 30 | + ProjectionBindingExpression projectionBindingExpression, |
| 31 | + INavigationBase? navigation, |
| 32 | + Type elementType) |
| 33 | + { |
| 34 | + ProjectionBindingExpression = projectionBindingExpression; |
| 35 | + Navigation = navigation; |
| 36 | + ElementType = elementType; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// The expression to get the subquery for this collection. |
| 41 | + /// </summary> |
| 42 | + public virtual ProjectionBindingExpression ProjectionBindingExpression { get; } |
| 43 | + /// <summary> |
| 44 | + /// The navigation if associated with the collection. |
| 45 | + /// </summary> |
| 46 | + public virtual INavigationBase? Navigation { get; } |
| 47 | + /// <summary> |
| 48 | + /// The clr type of elements of the collection. |
| 49 | + /// </summary> |
| 50 | + public virtual Type ElementType { get; } |
| 51 | + |
| 52 | + /// <inheritdoc /> |
| 53 | + public override Type Type => ProjectionBindingExpression.Type; |
| 54 | + /// <inheritdoc /> |
| 55 | + public override ExpressionType NodeType => ExpressionType.Extension; |
| 56 | + |
| 57 | + /// <inheritdoc /> |
| 58 | + protected override Expression VisitChildren(ExpressionVisitor visitor) |
| 59 | + { |
| 60 | + Check.NotNull(visitor, nameof(visitor)); |
| 61 | + |
| 62 | + var projectionBindingExpression = (ProjectionBindingExpression)visitor.Visit(ProjectionBindingExpression); |
| 63 | + |
| 64 | + return Update(projectionBindingExpression); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Creates a new expression that is like this one, but using the supplied children. If all of the children are the same, it will |
| 69 | + /// return this expression. |
| 70 | + /// </summary> |
| 71 | + /// <param name="projectionBindingExpression"> The <see cref="ProjectionBindingExpression" /> property of the result. </param> |
| 72 | + /// <returns> This expression if no children changed, or an expression with the updated children. </returns> |
| 73 | + public virtual CollectionResultExpression Update(ProjectionBindingExpression projectionBindingExpression) |
| 74 | + { |
| 75 | + Check.NotNull(projectionBindingExpression, nameof(projectionBindingExpression)); |
| 76 | + |
| 77 | + return projectionBindingExpression != ProjectionBindingExpression |
| 78 | + ? new CollectionResultExpression(projectionBindingExpression, Navigation, ElementType) |
| 79 | + : this; |
| 80 | + } |
| 81 | + |
| 82 | + /// <inheritdoc /> |
| 83 | + public virtual void Print(ExpressionPrinter expressionPrinter) |
| 84 | + { |
| 85 | + expressionPrinter.AppendLine("CollectionResultExpression:"); |
| 86 | + using (expressionPrinter.Indent()) |
| 87 | + { |
| 88 | + expressionPrinter.Append("ProjectionBindingExpression:"); |
| 89 | + expressionPrinter.Visit(ProjectionBindingExpression); |
| 90 | + expressionPrinter.AppendLine(); |
| 91 | + if (Navigation != null) |
| 92 | + { |
| 93 | + expressionPrinter.Append("Navigation:").AppendLine(Navigation.ToString()!); |
| 94 | + } |
| 95 | + expressionPrinter.Append("ElementType:").AppendLine(ElementType.ShortDisplayName()); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments