Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Constant.cs « Structures « ViewGeneration « Mapping « Data « System « System.Data.Entity « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a77b8a4ab8a66ebf1bc6857e24a4b70108ce307e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//---------------------------------------------------------------------
// <copyright file="Constant.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------

namespace System.Data.Mapping.ViewGeneration.Structures
{
    using System.Collections.Generic;
    using System.Data.Common.CommandTrees;
    using System.Data.Common.CommandTrees.ExpressionBuilder;
    using System.Data.Common.Utils;
    using System.Data.Mapping.ViewGeneration.CqlGeneration;
    using System.Data.Metadata.Edm;
    using System.Diagnostics;
    using System.Text;

    /// <summary>
    /// This class denotes a constant that can be stored in multiconstants or projected in fields.
    /// </summary>
    internal abstract class Constant : InternalBase
    {
        #region Fields
        internal static readonly IEqualityComparer<Constant> EqualityComparer = new CellConstantComparer();
        internal static readonly Constant Null = NullConstant.Instance;
        internal static readonly Constant NotNull = new NegatedConstant( new Constant[] { NullConstant.Instance });
        internal static readonly Constant Undefined = UndefinedConstant.Instance;
        /// <summary>
        /// Represents scalar constants within a finite set that are not specified explicitly in the domain.
        /// Currently only used as a Sentinel node to prevent expression optimization
        /// </summary>
        internal static readonly Constant AllOtherConstants = AllOtherConstantsConstant.Instance;
        #endregion

        #region Methods
        internal abstract bool IsNull();

        internal abstract bool IsNotNull();

        internal abstract bool IsUndefined();

        /// <summary>
        /// Returns true if this constant contains not null. 
        /// Implemented in <see cref="NegatedConstant"/> class, all other implementations return false.
        /// </summary>
        internal abstract bool HasNotNull();

        /// <summary>
        /// Generates eSQL for the constant expression.
        /// </summary>
        /// <param name="outputMember">The member to which this constant is directed</param>
        internal abstract StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias);

        /// <summary>
        /// Generates CQT for the constant expression.
        /// </summary>
        /// <param name="row">The input row.</param>
        /// <param name="outputMember">The member to which this constant is directed</param>
        internal abstract DbExpression AsCqt(DbExpression row, MemberPath outputMember);

        public override bool Equals(object obj)
        {
            Constant cellConst = obj as Constant;
            if (cellConst == null)
            {
                return false;
            }
            else
            {
                return IsEqualTo(cellConst);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        protected abstract bool IsEqualTo(Constant right);

        internal abstract string ToUserString();

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal static void ConstantsToUserString(StringBuilder builder, Set<Constant> constants)
        {
            bool isFirst = true;
            foreach (Constant constant in constants)
            {
                if (isFirst == false)
                {
                    builder.Append(System.Data.Entity.Strings.ViewGen_CommaBlank);
                }
                isFirst = false;
                string constrStr = constant.ToUserString();
                builder.Append(constrStr);
            }
        }
        #endregion

        #region Comparer class
        private class CellConstantComparer : IEqualityComparer<Constant>
        {
            public bool Equals(Constant left, Constant right)
            {
                // Quick check with references
                if (object.ReferenceEquals(left, right))
                {
                    // Gets the Null and Undefined case as well
                    return true;
                }
                // One of them is non-null at least. So if the other one is
                // null, we cannot be equal
                if (left == null || right == null)
                {
                    return false;
                }
                // Both are non-null at this point
                return left.IsEqualTo(right);
            }

            public int GetHashCode(Constant key)
            {
                EntityUtil.CheckArgumentNull(key, "key");
                return key.GetHashCode();
            }
        }
        #endregion

        #region Special constant classes (NullConstant, UndefinedConstant, AllOtherConstants)
        private sealed class NullConstant : Constant
        {
            internal static readonly Constant Instance = new NullConstant();

            private NullConstant() { }

            #region Methods
            internal override bool IsNull()
            {
                return true;
            }

            internal override bool IsNotNull()
            {
                return false;
            }

            internal override bool IsUndefined()
            {
                return false;
            }

            internal override bool HasNotNull()
            {
                return false;
            }

            internal override StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias)
            {
                Debug.Assert(outputMember.LeafEdmMember != null, "Constant can't correspond to an empty member path.");
                EdmType constType = Helper.GetModelTypeUsage(outputMember.LeafEdmMember).EdmType;

                builder.Append("CAST(NULL AS ");
                CqlWriter.AppendEscapedTypeName(builder, constType);
                builder.Append(')');
                return builder;
            }

            internal override DbExpression AsCqt(DbExpression row, MemberPath outputMember)
            {
                Debug.Assert(outputMember.LeafEdmMember != null, "Constant can't correspond to an empty path.");
                EdmType constType = Helper.GetModelTypeUsage(outputMember.LeafEdmMember).EdmType;

                return TypeUsage.Create(constType).Null();
            }

            public override int GetHashCode()
            {
                return 0;
            }

            protected override bool IsEqualTo(Constant right)
            {
                Debug.Assert(Object.ReferenceEquals(this, Instance), "this must be == Instance for NullConstant");
                return Object.ReferenceEquals(this, right);
            }

            internal override string ToUserString()
            {
                return System.Data.Entity.Strings.ViewGen_Null;
            }

            internal override void ToCompactString(StringBuilder builder)
            {
                builder.Append("NULL");
            }
            #endregion
        }
        private sealed class UndefinedConstant : Constant
        {
            internal static readonly Constant Instance = new UndefinedConstant();

            private UndefinedConstant() { }

            #region Methods
            internal override bool IsNull()
            {
                return false;
            }

            internal override bool IsNotNull()
            {
                return false;
            }

            internal override bool IsUndefined()
            {
                return true;
            }

            internal override bool HasNotNull()
            {
                return false;
            }

            /// <summary>
            /// Not supported in this class.
            /// </summary>
            internal override StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias)
            {
                Debug.Fail("Should not be called.");
                return null; // To keep the compiler happy
            }

            /// <summary>
            /// Not supported in this class.
            /// </summary>
            internal override DbExpression AsCqt(DbExpression row, MemberPath outputMember)
            {
                Debug.Fail("Should not be called.");
                return null; // To keep the compiler happy
            }

            public override int GetHashCode()
            {
                return 0;
            }

            protected override bool IsEqualTo(Constant right)
            {
                Debug.Assert(Object.ReferenceEquals(this, Instance), "this must be == Instance for NullConstant");
                return Object.ReferenceEquals(this, right);
            }

            /// <summary>
            /// Not supported in this class.
            /// </summary>
            internal override string ToUserString()
            {
                Debug.Fail("We should not emit a message about Undefined constants to the user.");
                return null;
            }

            internal override void ToCompactString(StringBuilder builder)
            {
                builder.Append("?");
            }
            #endregion
        }
        private sealed class AllOtherConstantsConstant : Constant
        {
            internal static readonly Constant Instance = new AllOtherConstantsConstant();

            private AllOtherConstantsConstant() { }

            #region Methods
            internal override bool IsNull()
            {
                return false;
            }

            internal override bool IsNotNull()
            {
                return false;
            }

            internal override bool IsUndefined()
            {
                return false;
            }

            internal override bool HasNotNull()
            {
                return false;
            }

            /// <summary>
            /// Not supported in this class.
            /// </summary>
            internal override StringBuilder AsEsql(StringBuilder builder, MemberPath outputMember, string blockAlias)
            {
                Debug.Fail("Should not be called.");
                return null; // To keep the compiler happy
            }

            /// <summary>
            /// Not supported in this class.
            /// </summary>
            internal override DbExpression AsCqt(DbExpression row, MemberPath outputMember)
            {
                Debug.Fail("Should not be called.");
                return null; // To keep the compiler happy
            }

            public override int GetHashCode()
            {
                return 0;
            }

            protected override bool IsEqualTo(Constant right)
            {
                Debug.Assert(Object.ReferenceEquals(this, Instance), "this must be == Instance for NullConstant");
                return Object.ReferenceEquals(this, right);
            }

            /// <summary>
            /// Not supported in this class.
            /// </summary>
            internal override string ToUserString()
            {
                Debug.Fail("We should not emit a message about Undefined constants to the user.");
                return null;
            }

            internal override void ToCompactString(StringBuilder builder)
            {
                builder.Append("AllOtherConstants");
            }
            #endregion
        }
        #endregion
    }
}