1 //---------------------------------------------------------------------
2 // <copyright file="BooleanProjectedSlot.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 using System
.Data
.Mapping
.ViewGeneration
.Structures
;
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
20 /// This class represents slots for expressions over boolean variables, e.g., _from0, _from1, etc
22 internal sealed class BooleanProjectedSlot
: ProjectedSlot
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
29 internal BooleanProjectedSlot(BoolExpression expr
, CqlIdentifiers identifiers
, int originalCellNum
)
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");
41 /// The actual value of the slot - could be <see cref="CellIdBoolean"/>!
43 private readonly BoolExpression m_expr
;
45 /// A boolean corresponding to the original cell number (_from0)
47 private readonly CellIdBoolean m_originalCell
;
52 /// Returns "_from0", "_from1" etc. <paramref name="outputMember"/> is ignored.
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
);
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");
77 internal override DbExpression
AsCqt(DbExpression row
, MemberPath outputMember
)
79 if (m_expr
.IsTrue
|| m_expr
.IsFalse
)
81 return m_expr
.AsCqt(row
);
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
);