1 //---------------------------------------------------------------------
2 // <copyright file="AliasedSlot.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 using System.Data.Mapping.ViewGeneration.Structures;
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;
19 namespace System.Data.Mapping.ViewGeneration.CqlGeneration
22 /// Encapsulates a slot in a particular cql block.
24 internal sealed class QualifiedSlot : ProjectedSlot
28 /// Creates a qualified slot "block_alias.slot_alias"
30 internal QualifiedSlot(CqlBlock block, ProjectedSlot slot)
32 Debug.Assert(block != null && slot != null, "Null input to QualifiedSlot constructor");
34 m_slot = slot; // Note: slot can be another qualified slot.
39 private readonly CqlBlock m_block;
40 private readonly ProjectedSlot m_slot;
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"/>).
49 internal override ProjectedSlot DeepQualify(CqlBlock block)
51 // We take the slot inside this and change the block
52 QualifiedSlot result = new QualifiedSlot(block, m_slot);
57 /// Delegates alias generation to the leaf slot in the qualified chain.
59 internal override string GetCqlFieldAlias(MemberPath outputMember)
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);
67 /// Walks the chain of <see cref="QualifiedSlot"/>s starting from the current one and returns the original slot.
69 internal ProjectedSlot GetOriginalSlot()
71 ProjectedSlot slot = m_slot;
74 QualifiedSlot qualifiedSlot = slot as QualifiedSlot;
75 if (qualifiedSlot == null)
79 slot = qualifiedSlot.m_slot;
84 internal string GetQualifiedCqlName(MemberPath outputMember)
86 return CqlWriter.GetQualifiedName(m_block.CqlAlias, GetCqlFieldAlias(outputMember));
89 internal override StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias, int indentLevel)
91 Debug.Assert(blockAlias == null || m_block.CqlAlias == blockAlias, "QualifiedSlot: blockAlias mismatch");
92 builder.Append(GetQualifiedCqlName(outputMember));
96 internal override DbExpression AsCqt(DbExpression row, MemberPath outputMember)
98 return m_block.GetInput(row).Property(GetCqlFieldAlias(outputMember));
101 internal override void ToCompactString(StringBuilder builder)
103 StringUtil.FormatStringBuilder(builder, "{0} ", m_block.CqlAlias);
104 m_slot.ToCompactString(builder);