Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Mapping / ViewGeneration / CqlGeneration / BooleanProjectedSlot.cs
blob113e28387d8f460ef7e25d818844ce77e9c75fdb
1 //---------------------------------------------------------------------
2 // <copyright file="BooleanProjectedSlot.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 using System.Data.Mapping.ViewGeneration.Structures;
11 using System.Text;
12 using System.Diagnostics;
13 using System.Data.Common.CommandTrees;
14 using System.Data.Common.CommandTrees.ExpressionBuilder;
15 using System.Data.Common.Utils;
17 namespace System.Data.Mapping.ViewGeneration.CqlGeneration
19 /// <summary>
20 /// This class represents slots for expressions over boolean variables, e.g., _from0, _from1, etc
21 /// </summary>
22 internal sealed class BooleanProjectedSlot : ProjectedSlot
24 #region Constructor
25 /// <summary>
26 /// Creates a boolean slot for expression that comes from originalCellNum, i.e.,
27 /// the value of the slot is <paramref name="expr"/> and the name is "_from{<paramref name="originalCellNum"/>}", e.g., _from2
28 /// </summary>
29 internal BooleanProjectedSlot(BoolExpression expr, CqlIdentifiers identifiers, int originalCellNum)
31 m_expr = expr;
32 m_originalCell = new CellIdBoolean(identifiers, originalCellNum);
34 Debug.Assert(!(expr.AsLiteral is CellIdBoolean) ||
35 BoolLiteral.EqualityComparer.Equals((CellIdBoolean)expr.AsLiteral, m_originalCell), "Cellid boolean for the slot and cell number disagree");
37 #endregion
39 #region Fields
40 /// <summary>
41 /// The actual value of the slot - could be <see cref="CellIdBoolean"/>!
42 /// </summary>
43 private readonly BoolExpression m_expr;
44 /// <summary>
45 /// A boolean corresponding to the original cell number (_from0)
46 /// </summary>
47 private readonly CellIdBoolean m_originalCell;
48 #endregion
50 #region Methods
51 /// <summary>
52 /// Returns "_from0", "_from1" etc. <paramref name="outputMember"/> is ignored.
53 /// </summary>
54 internal override string GetCqlFieldAlias(MemberPath outputMember)
56 return m_originalCell.SlotName;
59 internal override StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias, int indentLevel)
61 if (m_expr.IsTrue || m_expr.IsFalse)
63 // No Case statement for TRUE and FALSE
64 m_expr.AsEsql(builder, blockAlias);
66 else
68 // Produce "CASE WHEN boolExpr THEN True ELSE False END" in order to enforce the two-state boolean logic:
69 // if boolExpr returns the boolean Unknown, it gets converted to boolean False.
70 builder.Append("CASE WHEN ");
71 m_expr.AsEsql(builder, blockAlias);
72 builder.Append(" THEN True ELSE False END");
74 return builder;
77 internal override DbExpression AsCqt(DbExpression row, MemberPath outputMember)
79 if (m_expr.IsTrue || m_expr.IsFalse)
81 return m_expr.AsCqt(row);
83 else
85 // Produce "CASE WHEN boolExpr THEN True ELSE False END" in order to enforce the two-state boolean logic:
86 // if boolExpr returns the boolean Unknown, it gets converted to boolean False.
87 return DbExpressionBuilder.Case(new DbExpression[] { m_expr.AsCqt(row) }, new DbExpression[] { DbExpressionBuilder.True }, DbExpressionBuilder.False);
91 internal override void ToCompactString(StringBuilder builder)
93 StringUtil.FormatStringBuilder(builder, "<{0}, ", m_originalCell.SlotName);
94 m_expr.ToCompactString(builder);
95 builder.Append('>');
97 #endregion