Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Mapping / ViewGeneration / Structures / Constant.cs
blobc90fbb6688af54afa8cb4080ec976fcfd7b44964
1 //---------------------------------------------------------------------
2 // <copyright file="Constant.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 namespace System.Data.Mapping.ViewGeneration.Structures
12 using System.Collections.Generic;
13 using System.Data.Common.CommandTrees;
14 using System.Data.Common.CommandTrees.ExpressionBuilder;
15 using System.Data.Common.Utils;
16 using System.Data.Mapping.ViewGeneration.CqlGeneration;
17 using System.Data.Metadata.Edm;
18 using System.Diagnostics;
19 using System.Text;
21 /// <summary>
22 /// This class denotes a constant that can be stored in multiconstants or projected in fields.
23 /// </summary>
24 internal abstract class Constant : InternalBase
26 #region Fields
27 internal static readonly IEqualityComparer<Constant> EqualityComparer = new CellConstantComparer();
28 internal static readonly Constant Null = NullConstant.Instance;
29 internal static readonly Constant NotNull = new NegatedConstant( new Constant[] { NullConstant.Instance });
30 internal static readonly Constant Undefined = UndefinedConstant.Instance;
31 /// <summary>
32 /// Represents scalar constants within a finite set that are not specified explicitly in the domain.
33 /// Currently only used as a Sentinel node to prevent expression optimization
34 /// </summary>
35 internal static readonly Constant AllOtherConstants = AllOtherConstantsConstant.Instance;
36 #endregion
38 #region Methods
39 internal abstract bool IsNull();
41 internal abstract bool IsNotNull();
43 internal abstract bool IsUndefined();
45 /// <summary>
46 /// Returns true if this constant contains not null.
47 /// Implemented in <see cref="NegatedConstant"/> class, all other implementations return false.
48 /// </summary>
49 internal abstract bool HasNotNull();
51 /// <summary>
52 /// Generates eSQL for the constant expression.
53 /// </summary>
54 /// <param name="outputMember">The member to which this constant is directed</param>
55 internal abstract StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias);
57 /// <summary>
58 /// Generates CQT for the constant expression.
59 /// </summary>
60 /// <param name="row">The input row.</param>
61 /// <param name="outputMember">The member to which this constant is directed</param>
62 internal abstract DbExpression AsCqt(DbExpression row, MemberPath outputMember);
64 public override bool Equals(object obj)
66 Constant cellConst = obj as Constant;
67 if (cellConst == null)
69 return false;
71 else
73 return IsEqualTo(cellConst);
77 public override int GetHashCode()
79 return base.GetHashCode();
82 protected abstract bool IsEqualTo(Constant right);
84 internal abstract string ToUserString();
86 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
87 internal static void ConstantsToUserString(StringBuilder builder, Set<Constant> constants)
89 bool isFirst = true;
90 foreach (Constant constant in constants)
92 if (isFirst == false)
94 builder.Append(System.Data.Entity.Strings.ViewGen_CommaBlank);
96 isFirst = false;
97 string constrStr = constant.ToUserString();
98 builder.Append(constrStr);
101 #endregion
103 #region Comparer class
104 private class CellConstantComparer : IEqualityComparer<Constant>
106 public bool Equals(Constant left, Constant right)
108 // Quick check with references
109 if (object.ReferenceEquals(left, right))
111 // Gets the Null and Undefined case as well
112 return true;
114 // One of them is non-null at least. So if the other one is
115 // null, we cannot be equal
116 if (left == null || right == null)
118 return false;
120 // Both are non-null at this point
121 return left.IsEqualTo(right);
124 public int GetHashCode(Constant key)
126 EntityUtil.CheckArgumentNull(key, "key");
127 return key.GetHashCode();
130 #endregion
132 #region Special constant classes (NullConstant, UndefinedConstant, AllOtherConstants)
133 private sealed class NullConstant : Constant
135 internal static readonly Constant Instance = new NullConstant();
137 private NullConstant() { }
139 #region Methods
140 internal override bool IsNull()
142 return true;
145 internal override bool IsNotNull()
147 return false;
150 internal override bool IsUndefined()
152 return false;
155 internal override bool HasNotNull()
157 return false;
160 internal override StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias)
162 Debug.Assert(outputMember.LeafEdmMember != null, "Constant can't correspond to an empty member path.");
163 EdmType constType = Helper.GetModelTypeUsage(outputMember.LeafEdmMember).EdmType;
165 builder.Append("CAST(NULL AS ");
166 CqlWriter.AppendEscapedTypeName(builder, constType);
167 builder.Append(')');
168 return builder;
171 internal override DbExpression AsCqt(DbExpression row, MemberPath outputMember)
173 Debug.Assert(outputMember.LeafEdmMember != null, "Constant can't correspond to an empty path.");
174 EdmType constType = Helper.GetModelTypeUsage(outputMember.LeafEdmMember).EdmType;
176 return TypeUsage.Create(constType).Null();
179 public override int GetHashCode()
181 return 0;
184 protected override bool IsEqualTo(Constant right)
186 Debug.Assert(Object.ReferenceEquals(this, Instance), "this must be == Instance for NullConstant");
187 return Object.ReferenceEquals(this, right);
190 internal override string ToUserString()
192 return System.Data.Entity.Strings.ViewGen_Null;
195 internal override void ToCompactString(StringBuilder builder)
197 builder.Append("NULL");
199 #endregion
201 private sealed class UndefinedConstant : Constant
203 internal static readonly Constant Instance = new UndefinedConstant();
205 private UndefinedConstant() { }
207 #region Methods
208 internal override bool IsNull()
210 return false;
213 internal override bool IsNotNull()
215 return false;
218 internal override bool IsUndefined()
220 return true;
223 internal override bool HasNotNull()
225 return false;
228 /// <summary>
229 /// Not supported in this class.
230 /// </summary>
231 internal override StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias)
233 Debug.Fail("Should not be called.");
234 return null; // To keep the compiler happy
237 /// <summary>
238 /// Not supported in this class.
239 /// </summary>
240 internal override DbExpression AsCqt(DbExpression row, MemberPath outputMember)
242 Debug.Fail("Should not be called.");
243 return null; // To keep the compiler happy
246 public override int GetHashCode()
248 return 0;
251 protected override bool IsEqualTo(Constant right)
253 Debug.Assert(Object.ReferenceEquals(this, Instance), "this must be == Instance for NullConstant");
254 return Object.ReferenceEquals(this, right);
257 /// <summary>
258 /// Not supported in this class.
259 /// </summary>
260 internal override string ToUserString()
262 Debug.Fail("We should not emit a message about Undefined constants to the user.");
263 return null;
266 internal override void ToCompactString(StringBuilder builder)
268 builder.Append("?");
270 #endregion
272 private sealed class AllOtherConstantsConstant : Constant
274 internal static readonly Constant Instance = new AllOtherConstantsConstant();
276 private AllOtherConstantsConstant() { }
278 #region Methods
279 internal override bool IsNull()
281 return false;
284 internal override bool IsNotNull()
286 return false;
289 internal override bool IsUndefined()
291 return false;
294 internal override bool HasNotNull()
296 return false;
299 /// <summary>
300 /// Not supported in this class.
301 /// </summary>
302 internal override StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias)
304 Debug.Fail("Should not be called.");
305 return null; // To keep the compiler happy
308 /// <summary>
309 /// Not supported in this class.
310 /// </summary>
311 internal override DbExpression AsCqt(DbExpression row, MemberPath outputMember)
313 Debug.Fail("Should not be called.");
314 return null; // To keep the compiler happy
317 public override int GetHashCode()
319 return 0;
322 protected override bool IsEqualTo(Constant right)
324 Debug.Assert(Object.ReferenceEquals(this, Instance), "this must be == Instance for NullConstant");
325 return Object.ReferenceEquals(this, right);
328 /// <summary>
329 /// Not supported in this class.
330 /// </summary>
331 internal override string ToUserString()
333 Debug.Fail("We should not emit a message about Undefined constants to the user.");
334 return null;
337 internal override void ToCompactString(StringBuilder builder)
339 builder.Append("AllOtherConstants");
341 #endregion
343 #endregion