1 //---------------------------------------------------------------------
2 // <copyright file="WithStatement.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 using System.Data.Common.CommandTrees;
11 using System.Data.Common.CommandTrees.ExpressionBuilder;
12 using System.Data.Common.Utils;
14 using System.Collections.Generic;
15 using System.Data.Mapping.ViewGeneration.CqlGeneration;
16 using System.Data.Metadata.Edm;
17 using System.Diagnostics;
20 namespace System.Data.Mapping.ViewGeneration.Structures
23 /// A class to denote a part of the WITH RELATIONSHIP clause.
25 internal sealed class WithRelationship : InternalBase
28 internal WithRelationship(AssociationSet associationSet,
29 AssociationEndMember fromEnd,
30 EntityType fromEndEntityType,
31 AssociationEndMember toEnd,
32 EntityType toEndEntityType,
33 IEnumerable<MemberPath> toEndEntityKeyMemberPaths)
35 m_associationSet = associationSet;
37 m_fromEndEntityType = fromEndEntityType;
39 m_toEndEntityType = toEndEntityType;
40 m_toEndEntitySet = MetadataHelper.GetEntitySetAtEnd(associationSet, toEnd);
41 m_toEndEntityKeyMemberPaths = toEndEntityKeyMemberPaths;
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;
56 internal EntityType FromEndEntityType
58 get { return m_fromEndEntityType; }
63 internal StringBuilder AsEsql(StringBuilder builder, string blockAlias, int indentLevel)
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)
74 string fullFieldAlias = CqlWriter.GetQualifiedName(blockAlias, memberPath.CqlFieldAlias);
75 fields.Add(fullFieldAlias);
77 StringUtil.ToSeparatedString(builder, fields, ", ", null);
80 CqlWriter.AppendEscapedTypeName(builder, m_toEndEntityType);
84 CqlWriter.AppendEscapedTypeName(builder, m_associationSet.ElementType);
86 CqlWriter.AppendEscapedName(builder, m_fromEnd.Name);
88 CqlWriter.AppendEscapedName(builder, m_toEnd.Name);
94 internal DbRelatedEntityRef AsCqt(DbExpression row)
96 return DbExpressionBuilder.CreateRelatedEntityRef(
99 m_toEndEntitySet.CreateRef(m_toEndEntityType, m_toEndEntityKeyMemberPaths.Select(keyMember => row.Property(keyMember.CqlFieldAlias))));
103 /// Not supported in this class.
105 internal override void ToCompactString(StringBuilder builder)
107 Debug.Fail("Should not be called.");