1 //---------------------------------------------------------------------
2 // <copyright file="OptionalColumn.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 //---------------------------------------------------------------------
10 using System
.Collections
.Generic
;
11 using System
.Diagnostics
;
13 namespace System
.Data
.SqlClient
.SqlGen
16 /// Represents a column in a select list that should be printed only if it is later used.
17 /// Such columns get added by <see cref="SqlGenerator.AddDefaultColumns"/>.
18 /// The SymbolUsageManager associated with the OptionalColumn has the information whether the column
19 /// has been used based on its symbol.
21 internal sealed class OptionalColumn
24 private readonly SymbolUsageManager m_usageManager
;
26 // The SqlBuilder that contains the column building blocks (e.g: "c.X as X1")
27 private readonly SqlBuilder m_builder
= new SqlBuilder();
29 // The symbol representing the optional column
30 private readonly Symbol m_symbol
;
33 #region Internal Methods
35 /// Append to the "fragment" representing this column
37 internal void Append(object s
)
42 internal void MarkAsUsed()
44 this.m_usageManager
.MarkAsUsed(this.m_symbol
);
49 internal OptionalColumn(SymbolUsageManager usageManager
, Symbol symbol
)
51 this.m_usageManager
= usageManager
;
52 this.m_symbol
= symbol
;
56 #region Internal members
59 /// Writes that fragment that represents the optional column
60 /// if the usage manager says it is used.
62 /// <param name="writer"></param>
63 /// <param name="sqlGenerator"></param>
64 public bool WriteSqlIfUsed(SqlWriter writer
, SqlGenerator sqlGenerator
, string separator
)
66 if (m_usageManager
.IsUsed(m_symbol
))
68 writer
.Write(separator
);
69 m_builder
.WriteSql(writer
, sqlGenerator
);