Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Mapping / ViewGeneration / CqlGeneration / AliasedSlot.cs
1 //---------------------------------------------------------------------
2 // <copyright file="AliasedSlot.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.Linq;
11 using System.Data.Mapping.ViewGeneration.Structures;
12 using System.Text;
13 using System.Diagnostics;
14 using System.Data.Common.CommandTrees;
15 using System.Data.Common.CommandTrees.ExpressionBuilder;
16 using System.Data.Common.Utils;
17 using System.Collections.Generic;
18
19 namespace System.Data.Mapping.ViewGeneration.CqlGeneration
20 {
21     /// <summary>
22     /// Encapsulates a slot in a particular cql block.
23     /// </summary>
24     internal sealed class QualifiedSlot : ProjectedSlot
25     {
26         #region Constructor
27         /// <summary>
28         /// Creates a qualified slot "block_alias.slot_alias"
29         /// </summary>
30         internal QualifiedSlot(CqlBlock block, ProjectedSlot slot)
31         {
32             Debug.Assert(block != null && slot != null, "Null input to QualifiedSlot constructor");
33             m_block = block;
34             m_slot = slot; // Note: slot can be another qualified slot.
35         }
36         #endregion
37
38         #region Fields
39         private readonly CqlBlock m_block;
40         private readonly ProjectedSlot m_slot;
41         #endregion
42
43         #region Methods
44         /// <summary>
45         /// Creates new <see cref="ProjectedSlot"/> that is qualified with <paramref name="block"/>.CqlAlias.
46         /// If current slot is composite (such as <see cref="CaseStatementProjectedSlot"/>, then this method recursively qualifies all parts
47         /// and returns a new deeply qualified slot (as opposed to <see cref="CqlBlock.QualifySlotWithBlockAlias"/>).
48         /// </summary>
49         internal override ProjectedSlot DeepQualify(CqlBlock block)
50         {
51             // We take the slot inside this and change the block
52             QualifiedSlot result = new QualifiedSlot(block, m_slot);
53             return result;
54         }
55
56         /// <summary>
57         /// Delegates alias generation to the leaf slot in the qualified chain.
58         /// </summary>
59         internal override string GetCqlFieldAlias(MemberPath outputMember)
60         {
61             // Keep looking inside the chain of qualified slots till we find a non-qualified slot and then get the alias name for it.
62             string result = GetOriginalSlot().GetCqlFieldAlias(outputMember);
63             return result;
64         }
65
66         /// <summary>
67         /// Walks the chain of <see cref="QualifiedSlot"/>s starting from the current one and returns the original slot.
68         /// </summary>
69         internal ProjectedSlot GetOriginalSlot()
70         {
71             ProjectedSlot slot = m_slot;
72             while (true)
73             {
74                 QualifiedSlot qualifiedSlot = slot as QualifiedSlot;
75                 if (qualifiedSlot == null)
76                 {
77                     break;
78                 }
79                 slot = qualifiedSlot.m_slot;
80             }
81             return slot;
82         }
83
84         internal string GetQualifiedCqlName(MemberPath outputMember)
85         {
86             return CqlWriter.GetQualifiedName(m_block.CqlAlias, GetCqlFieldAlias(outputMember));
87         }
88
89         internal override StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias, int indentLevel)
90         {
91             Debug.Assert(blockAlias == null || m_block.CqlAlias == blockAlias, "QualifiedSlot: blockAlias mismatch");
92             builder.Append(GetQualifiedCqlName(outputMember));
93             return builder;
94         }
95
96         internal override DbExpression AsCqt(DbExpression row, MemberPath outputMember)
97         {
98             return m_block.GetInput(row).Property(GetCqlFieldAlias(outputMember));
99         }
100
101         internal override void ToCompactString(StringBuilder builder)
102         {
103             StringUtil.FormatStringBuilder(builder, "{0} ", m_block.CqlAlias);
104             m_slot.ToCompactString(builder);
105         }
106         #endregion
107     }
108 }