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

InitializerFacet.cs « ELinq « Objects « 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: 207b739739f18f779d1a14ea8cc60a01cea2ec6e (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//---------------------------------------------------------------------
// <copyright file="InitializerFacet.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner  Microsoft
//---------------------------------------------------------------------

namespace System.Data.Objects.ELinq
{
    using System.Collections.Generic;
    using System.Data.Common;
    using System.Data.Common.Internal.Materialization;
    using System.Data.Metadata.Edm;
    using System.Data.Objects.DataClasses;
    using System.Data.Objects.Internal;
    using System.Diagnostics;
    using System.Globalization;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Security;
    using System.Security.Permissions;
    using System.Threading;

    /// <summary>
    /// Facet encapsulating information necessary to initialize a LINQ projection
    /// result.
    /// </summary>
    internal abstract class InitializerMetadata : IEquatable<InitializerMetadata>
    {
        internal readonly Type ClrType;
        internal static readonly MethodInfo UserExpressionMarker = typeof(InitializerMetadata).GetMethod("MarkAsUserExpression", BindingFlags.NonPublic | BindingFlags.Static);
        private static long s_identifier;
        internal readonly string Identity;
        private static readonly string s_identifierPrefix = typeof(InitializerMetadata).Name;

        private InitializerMetadata(Type clrType)
        {
            Debug.Assert(null != clrType);
            ClrType = clrType;
            Identity = s_identifierPrefix + Interlocked.Increment(ref s_identifier).ToString(CultureInfo.InvariantCulture);
        }

        // Gets the kind of this initializer (grouping, row, etc.)
        internal abstract InitializerMetadataKind Kind { get; }

        // Attempts to retrieve the initializer facet from a type usage
        internal static bool TryGetInitializerMetadata(TypeUsage typeUsage, out InitializerMetadata initializerMetadata)
        {
            initializerMetadata = null;
            if (BuiltInTypeKind.RowType == typeUsage.EdmType.BuiltInTypeKind)
            {
                initializerMetadata = ((RowType)typeUsage.EdmType).InitializerMetadata;
            }
            return null != initializerMetadata;
        }

        // Initializes an initializer for an IGrouping return type
        // Requires: resultType is IGrouping<T, K> instance.
        internal static InitializerMetadata CreateGroupingInitializer(EdmItemCollection itemCollection, Type resultType)
        {
            return itemCollection.GetCanonicalInitializerMetadata(new GroupingInitializerMetadata(resultType));
        }

        // Initializes an initializer for a MemberInit expression
        internal static InitializerMetadata CreateProjectionInitializer(EdmItemCollection itemCollection, MemberInitExpression initExpression,
            MemberInfo[] members)
        {
            return itemCollection.GetCanonicalInitializerMetadata(new ProjectionInitializerMetadata(initExpression, members));
        }

        // Initializes an initializer for a New expression
        internal static InitializerMetadata CreateProjectionInitializer(EdmItemCollection itemCollection, NewExpression newExpression)
        {
            return itemCollection.GetCanonicalInitializerMetadata(new ProjectionNewMetadata(newExpression));
        }

        // Initializes an initializer for a New expression with no properties
        internal static InitializerMetadata CreateEmptyProjectionInitializer(EdmItemCollection itemCollection, NewExpression newExpression)
        {
            return itemCollection.GetCanonicalInitializerMetadata(new EmptyProjectionNewMetadata(newExpression));
        }

        // Creates metadata for entity collection materialization
        internal static InitializerMetadata CreateEntityCollectionInitializer(EdmItemCollection itemCollection, Type type, NavigationProperty navigationProperty)
        {
            return itemCollection.GetCanonicalInitializerMetadata(new EntityCollectionInitializerMetadata(type, navigationProperty));
        }

        private static T MarkAsUserExpression<T>(T value)
        {
            // No op. This is used as a marker inside of an expression tree to indicate
            // that the input expression is not trusted.
            return value;
        }

        internal virtual void AppendColumnMapKey(ColumnMapKeyBuilder builder)
        {
            // by default, the type is sufficient (more information is needed for EntityCollection and initializers)
            builder.Append("CLR-", this.ClrType);
        }

        public override bool Equals(object obj)
        {
            Debug.Fail("use typed Equals method only");
            return Equals(obj as InitializerMetadata);
        }

        public bool Equals(InitializerMetadata other)
        {
            Debug.Assert(null != other, "must not use a null key");
            if (object.ReferenceEquals(this, other)) { return true; }
            if (this.Kind != other.Kind) { return false; }
            if (!this.ClrType.Equals(other.ClrType)) { return false; }
            return IsStructurallyEquivalent(other);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2303", Justification="ClrType is not expected to be an Embedded Interop Type.")]
        public override int GetHashCode()
        {
            return ClrType.GetHashCode();
        }

        /// <summary>
        /// Requires: other has the same type as this and refers to the same CLR type
        /// Determine whether this Metadata is compatible with the other based on record layout.
        /// </summary>
        protected virtual bool IsStructurallyEquivalent(InitializerMetadata other)
        {
            return true;
        }
        
        /// <summary>
        /// Produces an expression initializing an instance of ClrType (given emitters for input
        /// columns)
        /// </summary>
        internal abstract Expression Emit(Translator translator, List<TranslatorResult> propertyTranslatorResults);

        /// <summary>
        /// Yields expected types for input columns. Null values are returned for children
        /// whose type is irrelevant to the initializer.
        /// </summary>
        internal abstract IEnumerable<Type> GetChildTypes();

        /// <summary>
        /// return a list of propertyReader expressions from an array of translator results.
        /// </summary>
        /// <param name="propertyTranslatorResults"></param>
        /// <returns></returns>
        protected static List<Expression> GetPropertyReaders(List<TranslatorResult> propertyTranslatorResults)
        {
            List<Expression> propertyReaders = propertyTranslatorResults.Select(s => s.UnwrappedExpression).ToList();
            return propertyReaders;
        }

        /// <summary>
        /// Implementation of IGrouping that can be initialized using the standard
        /// initializer pattern supported by ELinq
        /// </summary>
        /// <typeparam name="K">Type of key</typeparam>
        /// <typeparam name="T">Type of record</typeparam>
        private class Grouping<K, T> : IGrouping<K, T>
        {
            public Grouping(K key, IEnumerable<T> group)
            {
                _key = key;
                _group = group;
            }

            private readonly K _key;
            private readonly IEnumerable<T> _group;

            public K Key
            {
                get { return _key; }
            }

            public IEnumerable<T> Group
            {
                get { return _group; }
            }

            IEnumerator<T> IEnumerable<T>.GetEnumerator()
            {
                if (null == _group)
                {
                    yield break;
                }
                foreach (T member in _group)
                {
                    yield return member;
                }
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return ((IEnumerable<T>)this).GetEnumerator();
            }
        }
        
        /// <summary>
        /// Metadata for grouping initializer.
        /// </summary>
        private class GroupingInitializerMetadata : InitializerMetadata
        {
            internal GroupingInitializerMetadata(Type type)
                : base(type)
            {
            }

            internal override InitializerMetadataKind Kind { get { return InitializerMetadataKind.Grouping; } }

            internal override Expression Emit(Translator translator, List<TranslatorResult> propertyTranslatorResults)
            {
                // Create expression of the form:
                // new Grouping<K, T>(children[0], children[1])

                // Collect information...
                Debug.Assert(ClrType.IsGenericType &&
                    typeof(IGrouping<,>).Equals(ClrType.GetGenericTypeDefinition()));
                Debug.Assert(propertyTranslatorResults.Count == 2);
                Type keyType = this.ClrType.GetGenericArguments()[0];
                Type groupElementType = this.ClrType.GetGenericArguments()[1];
                Type groupType = typeof(Grouping<,>).MakeGenericType(keyType, groupElementType);
                ConstructorInfo constructor = groupType.GetConstructors().Single();

                // new Grouping<K, T>(children[0], children[1])
                Expression newGrouping = Expression.Convert(Expression.New(constructor, GetPropertyReaders(propertyTranslatorResults)), this.ClrType);

                return newGrouping;
            }

            internal override IEnumerable<Type> GetChildTypes()
            {
                // Collect information...
                Debug.Assert(ClrType.IsGenericType &&
                    typeof(IGrouping<,>).Equals(ClrType.GetGenericTypeDefinition()));
                Type keyType = this.ClrType.GetGenericArguments()[0];
                Type groupElementType = this.ClrType.GetGenericArguments()[1];

                // key
                yield return keyType;
                // group
                yield return typeof(IEnumerable<>).MakeGenericType(groupElementType);
            }
        }

        /// <summary>
        /// Metadata for anonymous type materialization.
        /// </summary>
        private class ProjectionNewMetadata : InitializerMetadata
        {
            internal ProjectionNewMetadata(NewExpression newExpression)
                : base(newExpression.Type)
            {
                Debug.Assert(null != newExpression);
                _newExpression = newExpression;
            }

            private readonly NewExpression _newExpression;

            internal override InitializerMetadataKind Kind { get { return InitializerMetadataKind.ProjectionNew; } }

            protected override bool IsStructurallyEquivalent(InitializerMetadata other)
            {                
                // caller must ensure the type matches                
                ProjectionNewMetadata otherProjection = (ProjectionNewMetadata)other;
                if (this._newExpression.Members == null && otherProjection._newExpression.Members == null)
                {
                    return true;
                }

                if (this._newExpression.Members == null || otherProjection._newExpression.Members == null)
                {
                    return false;
                }

                if (this._newExpression.Members.Count != otherProjection._newExpression.Members.Count)
                {
                    return false;
                }

                for (int i = 0; i < this._newExpression.Members.Count; i++)
                {
                    MemberInfo thisMember = this._newExpression.Members[i];
                    MemberInfo otherMember = otherProjection._newExpression.Members[i];
                    if (!thisMember.Equals(otherMember))
                    {
                        return false;
                    }
                }

                return true;
            }

            internal override Expression Emit(Translator translator, List<TranslatorResult> propertyTranslatorResults)
            {
                // Create expression of the form:
                // _newExpression(children)

                // (ClrType)null
                Expression nullProjection = Expression.Constant(null, this.ClrType);

                // _newExpression with members rebound
                Expression newProjection = Expression.New(_newExpression.Constructor, GetPropertyReaders(propertyTranslatorResults));

                // Indicate that this expression is provided by the user and should not be trusted.
                return Expression.Call(UserExpressionMarker.MakeGenericMethod(newProjection.Type), newProjection);
            }

            internal override IEnumerable<Type> GetChildTypes()
            {
                // return all argument types
                return _newExpression.Arguments.Select(arg => arg.Type);
            }

            internal override void AppendColumnMapKey(ColumnMapKeyBuilder builder)
            {
                base.AppendColumnMapKey(builder);
                builder.Append(_newExpression.Constructor.ToString());
                foreach (var member in _newExpression.Members ?? Enumerable.Empty<MemberInfo>())
                {
                    builder.Append("DT", member.DeclaringType);
                    builder.Append("." + member.Name);
                }
            }
        }

        private class EmptyProjectionNewMetadata : ProjectionNewMetadata
        {
            internal EmptyProjectionNewMetadata(NewExpression newExpression)
                : base(newExpression)
            {
            }
            internal override Expression Emit(Translator translator, List<TranslatorResult> propertyReaders)
            {
                // ignore sentinel column
                return base.Emit(translator, new List<TranslatorResult>());
            }
            internal override IEnumerable<Type> GetChildTypes()
            {
                // ignore sentinel column
                yield return null;
            }
        }

        /// <summary>
        /// Metadata for standard projection initializers.
        /// </summary>
        private class ProjectionInitializerMetadata : InitializerMetadata
        {
            internal ProjectionInitializerMetadata(MemberInitExpression initExpression, MemberInfo[] members)
                : base(initExpression.Type)
            {
                Debug.Assert(null != initExpression);
                Debug.Assert(null != members);
                _initExpression = initExpression;
                _members = members;
            }

            private readonly MemberInitExpression _initExpression;
            private readonly MemberInfo[] _members;

            internal override InitializerMetadataKind Kind { get { return InitializerMetadataKind.ProjectionInitializer; } }

            protected override bool IsStructurallyEquivalent(InitializerMetadata other)
            {
                // caller must ensure the type matches
                ProjectionInitializerMetadata otherProjection = (ProjectionInitializerMetadata)other;
                if (this._initExpression.Bindings.Count != otherProjection._initExpression.Bindings.Count)
                {
                    return false;
                }

                for (int i = 0; i < this._initExpression.Bindings.Count; i++)
                {
                    MemberBinding thisBinding = this._initExpression.Bindings[i];
                    MemberBinding otherBinding = otherProjection._initExpression.Bindings[i];
                    if (!thisBinding.Member.Equals(otherBinding.Member))
                    {
                        return false;
                    }
                }

                return true;
            }

            internal override Expression Emit(Translator translator, List<TranslatorResult> propertyReaders)
            {
                // Create expression of the form:
                // _initExpression(children)

                // create member bindings (where values are taken from children)
                MemberBinding[] memberBindings = new MemberBinding[_initExpression.Bindings.Count];
                MemberBinding[] constantMemberBindings = new MemberBinding[memberBindings.Length];
                for (int i = 0; i < memberBindings.Length; i++)
                {
                    MemberBinding originalBinding = _initExpression.Bindings[i];
                    Expression value = propertyReaders[i].UnwrappedExpression;
                    MemberBinding newBinding = Expression.Bind(originalBinding.Member, value);
                    MemberBinding constantBinding = Expression.Bind(originalBinding.Member, Expression.Constant(
                        TypeSystem.GetDefaultValue(value.Type), value.Type));
                    memberBindings[i] = newBinding;
                    constantMemberBindings[i] = constantBinding;
                }

                Expression newProjection = Expression.MemberInit(_initExpression.NewExpression, memberBindings);

                // Indicate that this expression is provided by the user and should not be trusted.
                return Expression.Call(UserExpressionMarker.MakeGenericMethod(newProjection.Type), newProjection);
            }

            internal override IEnumerable<Type> GetChildTypes()
            {
                // return all argument types
                foreach (var binding in _initExpression.Bindings)
                {
                    // determine member type
                    Type memberType;
                    string name;
                    TypeSystem.PropertyOrField(binding.Member, out name, out memberType);
                    yield return memberType;
                }
            }

            internal override void AppendColumnMapKey(ColumnMapKeyBuilder builder)
            {
                base.AppendColumnMapKey(builder);
                foreach (var binding in _initExpression.Bindings)
                {
                    builder.Append(",", binding.Member.DeclaringType);
                    builder.Append("." + binding.Member.Name);
                }
            }
        }

        /// <summary>
        /// Metadata for entity collection initializer.
        /// </summary>
        private class EntityCollectionInitializerMetadata : InitializerMetadata
        {
            internal EntityCollectionInitializerMetadata(Type type, NavigationProperty navigationProperty)
                : base(type)
            {
                Debug.Assert(null != navigationProperty);
                _navigationProperty = navigationProperty;
            }

            private readonly NavigationProperty _navigationProperty;

            internal override InitializerMetadataKind Kind { get { return InitializerMetadataKind.EntityCollection; } }

            /// <summary>
            /// Make sure the other metadata instance generates the same property
            /// (otherwise, we get incorrect behavior where multiple nav props return
            /// the same type)
            /// </summary>
            protected override bool IsStructurallyEquivalent(InitializerMetadata other)
            {
                // caller must ensure the type matches
                EntityCollectionInitializerMetadata otherInitializer = (EntityCollectionInitializerMetadata)other;
                return this._navigationProperty.Equals(otherInitializer._navigationProperty);
            }

            private static readonly MethodInfo s_createEntityCollectionMethod = typeof(EntityCollectionInitializerMetadata).GetMethod("CreateEntityCollection",
                BindingFlags.Static | BindingFlags.Public);

            internal override Expression Emit(Translator translator, List<TranslatorResult> propertyTranslatorResults)
            {
                Debug.Assert(propertyTranslatorResults.Count > 1, "no properties?");
                Debug.Assert(propertyTranslatorResults[1] is CollectionTranslatorResult, "not a collection?");

                Type elementType = GetElementType();
                MethodInfo createEntityCollectionMethod = s_createEntityCollectionMethod.MakeGenericMethod(elementType);

                Expression shaper = Translator.Shaper_Parameter;
                Expression owner = propertyTranslatorResults[0].Expression;

                CollectionTranslatorResult collectionResult = propertyTranslatorResults[1] as CollectionTranslatorResult;

                Expression coordinator = collectionResult.ExpressionToGetCoordinator;

                // CreateEntityCollection(shaper, owner, elements, relationshipName, targetRoleName)
                Expression result = Expression.Call(createEntityCollectionMethod,
                    shaper, owner, coordinator, Expression.Constant(_navigationProperty.RelationshipType.FullName), Expression.Constant(_navigationProperty.ToEndMember.Name));

                return result;
            }

            public static EntityCollection<T> CreateEntityCollection<T>(Shaper state, IEntityWrapper wrappedOwner, Coordinator<T> coordinator, string relationshipName, string targetRoleName)
                where T : class
            {
                if (null == wrappedOwner.Entity)
                {
                    return null;
                }
                else
                {
                    EntityCollection<T> result = wrappedOwner.RelationshipManager.GetRelatedCollection<T>(relationshipName, targetRoleName);
                    // register a handler for deferred loading (when the nested result has been consumed)
                    coordinator.RegisterCloseHandler((readerState, elements) => result.Load(elements, readerState.MergeOption));
                    return result;
                }
            }

            internal override IEnumerable<Type> GetChildTypes()
            {
                Type elementType = GetElementType();
                yield return null; // defer in determining entity type...
                yield return typeof(IEnumerable<>).MakeGenericType(elementType);
            }


            internal override void AppendColumnMapKey(ColumnMapKeyBuilder builder)
            {
                base.AppendColumnMapKey(builder);
                builder.Append(",NP" + _navigationProperty.Name);
                builder.Append(",AT", _navigationProperty.DeclaringType);
            }

            private Type GetElementType()
            {
                // POCO support requires that we allow ICollection<T> collections.  This allows a POCO collection
                // to be projected in a LINQ query.
                Type elementType;
                if (!EntityUtil.TryGetICollectionElementType(ClrType, out elementType))
                {
                    throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.ELinq_UnexpectedTypeForNavigationProperty(
                        _navigationProperty,
                        typeof(EntityCollection<>), typeof(ICollection<>),
                        ClrType));
                }
                return elementType;
            }            
        }
    }

    internal enum InitializerMetadataKind
    {
        Grouping,
        ProjectionNew,
        ProjectionInitializer,
        EntityCollection,
    }
}