Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Mapping / ViewGeneration / Structures / WithStatement.cs
1 //---------------------------------------------------------------------
2 // <copyright file="WithStatement.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 using System.Data.Common.CommandTrees;
11 using System.Data.Common.CommandTrees.ExpressionBuilder;
12 using System.Data.Common.Utils;
13 using System.Text;
14 using System.Collections.Generic;
15 using System.Data.Mapping.ViewGeneration.CqlGeneration;
16 using System.Data.Metadata.Edm;
17 using System.Diagnostics;
18 using System.Linq;
19
20 namespace System.Data.Mapping.ViewGeneration.Structures
21 {
22     /// <summary>
23     /// A class to denote a part of the WITH RELATIONSHIP clause.
24     /// </summary>
25     internal sealed class WithRelationship : InternalBase
26     {
27         #region Constructors
28         internal WithRelationship(AssociationSet associationSet,
29                                   AssociationEndMember fromEnd,
30                                   EntityType fromEndEntityType,
31                                   AssociationEndMember toEnd,
32                                   EntityType toEndEntityType,
33                                   IEnumerable<MemberPath> toEndEntityKeyMemberPaths)
34         {
35             m_associationSet = associationSet;
36             m_fromEnd = fromEnd;
37             m_fromEndEntityType = fromEndEntityType;
38             m_toEnd = toEnd;
39             m_toEndEntityType = toEndEntityType;
40             m_toEndEntitySet = MetadataHelper.GetEntitySetAtEnd(associationSet, toEnd);
41             m_toEndEntityKeyMemberPaths = toEndEntityKeyMemberPaths;
42         }
43         #endregion
44
45         #region Fields
46         private readonly AssociationSet m_associationSet;
47         private readonly RelationshipEndMember m_fromEnd;
48         private readonly EntityType m_fromEndEntityType;
49         private readonly RelationshipEndMember m_toEnd;
50         private readonly EntityType m_toEndEntityType;
51         private readonly EntitySet m_toEndEntitySet;
52         private readonly IEnumerable<MemberPath> m_toEndEntityKeyMemberPaths;
53         #endregion
54
55         #region Properties
56         internal EntityType FromEndEntityType
57         {
58             get { return m_fromEndEntityType; }
59         }
60         #endregion
61
62         #region Methods
63         internal StringBuilder AsEsql(StringBuilder builder, string blockAlias, int indentLevel)
64         {
65             StringUtil.IndentNewLine(builder, indentLevel + 1);
66             builder.Append("RELATIONSHIP(");
67             List<string> fields = new List<string>();
68             // If the variable is a relation end, we will gets it scope Extent, e.g., CPerson1 for the CPerson end of CPersonAddress1.
69             builder.Append("CREATEREF(");
70             CqlWriter.AppendEscapedQualifiedName(builder, m_toEndEntitySet.EntityContainer.Name, m_toEndEntitySet.Name);
71             builder.Append(", ROW(");
72             foreach (MemberPath memberPath in m_toEndEntityKeyMemberPaths)
73             {
74                 string fullFieldAlias = CqlWriter.GetQualifiedName(blockAlias, memberPath.CqlFieldAlias);
75                 fields.Add(fullFieldAlias);
76             }
77             StringUtil.ToSeparatedString(builder, fields, ", ", null);
78             builder.Append(')');
79             builder.Append(",");
80             CqlWriter.AppendEscapedTypeName(builder, m_toEndEntityType);
81             builder.Append(')');
82
83             builder.Append(',');
84             CqlWriter.AppendEscapedTypeName(builder, m_associationSet.ElementType);
85             builder.Append(',');
86             CqlWriter.AppendEscapedName(builder, m_fromEnd.Name);
87             builder.Append(',');
88             CqlWriter.AppendEscapedName(builder, m_toEnd.Name);
89             builder.Append(')');
90             builder.Append(' ');
91             return builder;
92         }
93
94         internal DbRelatedEntityRef AsCqt(DbExpression row)
95         {
96             return DbExpressionBuilder.CreateRelatedEntityRef(
97                 m_fromEnd, 
98                 m_toEnd,
99                 m_toEndEntitySet.CreateRef(m_toEndEntityType, m_toEndEntityKeyMemberPaths.Select(keyMember => row.Property(keyMember.CqlFieldAlias))));
100         }
101
102         /// <summary>
103         /// Not supported in this class.
104         /// </summary>
105         internal override void ToCompactString(StringBuilder builder)
106         {
107             Debug.Fail("Should not be called.");
108         }
109         #endregion
110     }
111 }