forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionsHelper.cs
973 lines (867 loc) · 31.6 KB
/
ExpressionsHelper.cs
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using NHibernate.Engine;
using NHibernate.Linq;
using NHibernate.Linq.Clauses;
using NHibernate.Linq.Expressions;
using NHibernate.Linq.Functions;
using NHibernate.Linq.Visitors;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
using NHibernate.Type;
using Remotion.Linq.Clauses;
using Remotion.Linq.Clauses.Expressions;
using Remotion.Linq.Clauses.ResultOperators;
using TransparentIdentifierRemovingExpressionVisitor = NHibernate.Linq.Visitors.TransparentIdentifierRemovingExpressionVisitor;
namespace NHibernate.Util
{
public static class ExpressionsHelper
{
public static MemberInfo DecodeMemberAccessExpression<TEntity, TResult>(Expression<Func<TEntity, TResult>> expression)
{
if (expression.Body.NodeType != ExpressionType.MemberAccess)
{
throw new HibernateException(
string.Format("Invalid expression type: Expected ExpressionType.MemberAccess, Found {0}", expression.Body.NodeType));
}
return ((MemberExpression)expression.Body).Member;
}
#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
/// <summary>
/// Try to retrieve <see cref="GetMemberBinder"/> from a reduced <see cref="ExpressionType.Dynamic"/> expression.
/// </summary>
/// <param name="expression">The reduced dynamic expression.</param>
/// <param name="memberBinder">The out binder parameter.</param>
/// <returns>Whether the binder was found.</returns>
internal static bool TryGetDynamicMemberBinder(InvocationExpression expression, out GetMemberBinder memberBinder)
{
// This is an ugly workaround for dynamic expressions in .NET Core. In .NET Core a dynamic expression is reduced
// when first visited by a expression visitor that is not a DynamicExpressionVisitor, where in .NET Framework it is never reduced.
// As RelinqExpressionVisitor does not extend DynamicExpressionVisitor, we will always have a reduced dynamic expression in .NET Core.
// Unfortunately we can not tap into the expression tree earlier to intercept the dynamic expression
if (expression.Arguments.Count == 2 &&
expression.Arguments[0] is ConstantExpression constant &&
constant.Value is CallSite site &&
site.Binder is GetMemberBinder binder)
{
memberBinder = binder;
return true;
}
memberBinder = null;
return false;
}
#endif
/// <summary>
/// Check whether the given expression represent a variable.
/// </summary>
/// <param name="expression">The expression to check.</param>
/// <param name="path">The path of the variable.</param>
/// <param name="closureContext">The closure context where the variable is stored.</param>
/// <returns>Whether the expression represents a variable.</returns>
internal static bool IsVariable(Expression expression, out string path, out object closureContext)
{
Expression childExpression;
string currentPath;
switch (expression)
{
case MemberExpression memberExpression:
childExpression = memberExpression.Expression;
currentPath = memberExpression.Member.Name;
break;
case ConstantExpression constantExpression:
path = null;
if (constantExpression.Type.Attributes.HasFlag(TypeAttributes.NestedPrivate) &&
Attribute.IsDefined(constantExpression.Type, typeof(CompilerGeneratedAttribute), inherit: true))
{
closureContext = constantExpression.Value;
return true;
}
closureContext = null;
return false;
default:
path = null;
closureContext = null;
return false;
}
if (!IsVariable(childExpression, out path, out closureContext))
{
return false;
}
path = path != null ? $"{currentPath}_{path}" : currentPath;
return true;
}
/// <summary>
/// Get the mapped type for the given expression.
/// </summary>
/// <param name="parameters">The query parameters.</param>
/// <param name="expression">The expression.</param>
/// <returns>The mapped type of the expression or <see langword="null"/> when the mapped type was not
/// found and the <paramref name="expression"/> type is <see cref="object"/>.</returns>
internal static IType GetType(VisitorParameters parameters, Expression expression)
{
if (expression is ConstantExpression constantExpression &&
parameters.ConstantToParameterMap.TryGetValue(constantExpression, out var param))
{
return param.Type;
}
if (TryGetMappedType(parameters.SessionFactory, expression, out var type, out _, out _, out _))
{
return type;
}
return expression.Type == typeof(object) ? null : TypeFactory.HeuristicType(expression.Type);
}
/// <summary>
/// Try to get the mapped nullability from the given expression.
/// </summary>
/// <param name="sessionFactory">The session factory.</param>
/// <param name="expression">The expression to evaluate.</param>
/// <param name="nullable">Output parameter that represents whether the <paramref name="expression"/> is nullable.</param>
/// <returns>Whether the mapped nullability was found.</returns>
internal static bool TryGetMappedNullability(
ISessionFactoryImplementor sessionFactory,
Expression expression,
out bool nullable)
{
if (!TryGetMappedType(
sessionFactory,
expression,
out _,
out var entityPersister,
out var componentType,
out var memberPath))
{
nullable = false;
return false;
}
// The source entity is always not null, as it gets translated to the entity identifier
if (memberPath == null)
{
nullable = false;
return true;
}
int index;
if (componentType != null)
{
var propertyNullability = componentType.PropertyNullability;
if (propertyNullability == null)
{
nullable = false;
return false;
}
index = Array.IndexOf(
componentType.PropertyNames,
memberPath.Substring(memberPath.LastIndexOf('.') + 1));
nullable = propertyNullability[index];
return true;
}
if (entityPersister.EntityMetamodel.GetIdentifierPropertyType(memberPath) != null)
{
nullable = false; // Identifier is always not-null
return true;
}
var propIndex = entityPersister.EntityMetamodel.GetPropertyIndexOrNull(memberPath);
if (propIndex == null)
{
nullable = false;
return false;
}
index = propIndex.Value;
nullable = entityPersister.PropertyNullability[index];
return true;
}
/// <summary>
/// Try to get the mapped type from the given expression. When the <paramref name="expression"/> type is
/// <see cref="ExpressionType.Convert"/>, the <paramref name="mappedType"/> will be set based on the expression type
/// only when the mapping for <see cref="UnaryExpression.Operand"/> was found, otherwise <see langword="null"/>
/// will be returned.
/// </summary>
/// <param name="sessionFactory">The session factory to retrieve <see cref="IEntityPersister"/> types.</param>
/// <param name="expression">The expression to evaluate.</param>
/// <param name="mappedType">Output parameter that represents the mapped type of <paramref name="memberPath"/>.</param>
/// <param name="entityPersister">
/// Output parameter that represents the entity persister of the entity where <paramref name="memberPath"/> is defined.
/// This parameter will not be set when <paramref name="memberPath"/> represents a property in a collection composite element.
/// </param>
/// <param name="component">
/// Output parameter that represents the component type where <paramref name="memberPath"/> is defined.
/// This parameter will not be set when <paramref name="memberPath"/> does not represent a property in a component.
/// </param>
/// <param name="memberPath">
/// Output parameter that represents the path of the mapped member, which in most cases is the member name. In case
/// when the mapped member is defined inside a component the path will be prefixed with the name of the component member and a dot.
/// (e.g. Component.Property).</param>
/// <returns>Whether the mapped type was found.</returns>
/// <remarks>
/// When the <paramref name="expression"/> contains an expression of type <see cref="ExpressionType.Convert"/>, the
/// result may not be correct when casting to an entity that is mapped with multiple entity names.
/// When the <paramref name="expression"/> is polymorphic, the first implementor will be returned.
/// When the <paramref name="expression"/> contains a <see cref="ConditionalExpression"/>, the first found entity name
/// will be returned from <see cref="ConditionalExpression.IfTrue"/> or <see cref="ConditionalExpression.IfFalse"/>.
/// When the <paramref name="expression"/> contains a <see cref="ExpressionType.Coalesce"/> expression, the first found entity name
/// will be returned from <see cref="BinaryExpression.Left"/> or <see cref="BinaryExpression.Right"/>.
/// </remarks>
internal static bool TryGetMappedType(
ISessionFactoryImplementor sessionFactory,
Expression expression,
out IType mappedType,
out IEntityPersister entityPersister,
out IAbstractComponentType component,
out string memberPath)
{
// In order to get the correct entity name from the expression we first have to find the constant expression that contains the
// IEntityNameProvider instance, from which we can retrieve the starting entity name. Once we have it, we have to traverse all
// expressions that we had to traverse in order to find the IEntityNameProvider instance, but in reverse order (bottom to top)
// and keep tracking the entity name until we reach to top.
memberPath = null;
mappedType = null;
entityPersister = null;
component = null;
// Try to retrieve the starting entity name with all members that were traversed in that process.
if (!MemberMetadataExtractor.TryGetAllMemberMetadata(expression, out var metadataResults))
{
// Failed to find the starting entity name, due to:
// - Unsupported expression
// - The expression didn't contain the IEntityNameProvider instance
return false;
}
// Due to coalesce and conditional expressions we can have multiple paths to traverse, in that case find the first path
// for which we are able to determine the mapped type.
foreach (var metadataResult in metadataResults)
{
if (ProcessMembersMetadataResult(
metadataResult,
sessionFactory,
out mappedType,
out entityPersister,
out component,
out memberPath))
{
return true;
}
}
return false;
}
private static bool ProcessMembersMetadataResult(
MemberMetadataResult metadataResult,
ISessionFactoryImplementor sessionFactory,
out IType mappedType,
out IEntityPersister entityPersister,
out IAbstractComponentType component,
out string memberPath)
{
if (!TryGetEntityPersister(metadataResult.EntityName, null, sessionFactory, out var currentEntityPersister))
{
// Failed to find the starting entity name, due to:
// - Querying a type that is not related to any entity e.g. s.Query<NotRelatedType>().Where(a => a.Type == "A")
memberPath = null;
mappedType = null;
entityPersister = null;
component = null;
return false;
}
if (metadataResult.MemberPaths.Count == 0) // The expression do not contain any member expressions
{
if (metadataResult.ConvertType != null)
{
mappedType = TryGetEntityPersister(
currentEntityPersister,
metadataResult.ConvertType,
sessionFactory,
out var convertPersister)
? convertPersister.EntityMetamodel.EntityType // ((Subclass)q)
: TypeFactory.GetDefaultTypeFor(metadataResult.ConvertType); // ((NotMapped)q)
}
else
{
mappedType = currentEntityPersister.EntityMetamodel.EntityType; // q
}
memberPath = null;
component = null;
entityPersister = currentEntityPersister;
return mappedType != null;
}
// If there was a cast right after the constant expression that contains the IEntityNameProvider instance, we have
// to update the entity persister according to it, otherwise use the value returned by TryGetAllMemberMetadata method.
if (metadataResult.ConvertType != null)
{
if (!TryGetEntityPersister(
currentEntityPersister,
metadataResult.ConvertType,
sessionFactory,
out var convertPersister)) // ((NotMapped)q).Id
{
memberPath = null;
mappedType = null;
entityPersister = null;
component = null;
return false;
}
currentEntityPersister = convertPersister; // ((Subclass)q).Id
}
return TraverseMembers(
sessionFactory,
metadataResult.MemberPaths,
currentEntityPersister,
out mappedType,
out entityPersister,
out component,
out memberPath);
}
private static bool TraverseMembers(
ISessionFactoryImplementor sessionFactory,
Stack<MemberMetadata> memberPaths,
IEntityPersister currentEntityPersister,
out IType mappedType,
out IEntityPersister entityPersister,
out IAbstractComponentType component,
out string memberPath)
{
// Traverse the members that were traversed by the TryGetAllMemberMetadata method in the reverse order and try to keep
// tracking the entity persister until all members are traversed.
var member = memberPaths.Pop();
var currentType = GetPropertyType(currentEntityPersister, member.Path);
IAbstractComponentType currentComponentType = null;
while (memberPaths.Count > 0 && currentType != null)
{
memberPath = member.Path;
var convertType = member.ConvertType;
member = memberPaths.Pop();
switch (currentType)
{
case IAssociationType associationType:
ProcessAssociationType(
associationType,
sessionFactory,
member,
convertType,
out currentType,
out currentEntityPersister,
out currentComponentType);
break;
case IAbstractComponentType componentType:
currentComponentType = componentType;
currentType = TryGetComponentPropertyType(componentType, member.Path);
if (currentEntityPersister != null)
{
// q.Component.Prop
member = new MemberMetadata(
memberPath + "." + member.Path,
member.ConvertType,
member.HasIndexer);
}
break;
default:
// q.Prop.NotMappedProp
currentType = null;
currentEntityPersister = null;
currentComponentType = null;
break;
}
}
// When traversed to the top of the expression, return the current tracking values
if (memberPaths.Count == 0)
{
memberPath = currentEntityPersister != null || currentComponentType != null ? member.Path : null;
mappedType = GetType(currentEntityPersister, currentType, member, sessionFactory);
entityPersister = currentEntityPersister;
component = currentComponentType;
return mappedType != null;
}
// Member not mapped
memberPath = null;
mappedType = null;
entityPersister = null;
component = null;
return false;
}
private static IType GetPropertyType(IEntityPersister currentEntityPersister, string path)
{
((IPropertyMapping) currentEntityPersister).TryToType(path, out var type);
return type;
}
private static IType TryGetComponentPropertyType(IAbstractComponentType componentType, string memberPath)
{
var index = Array.IndexOf(componentType.PropertyNames, memberPath);
return index < 0
? null // q.OneToManyCompositeElement[0].NotMappedProp
: componentType.Subtypes[index]; // q.OneToManyCompositeElement[0].Prop
}
private static void ProcessAssociationType(
IAssociationType associationType,
ISessionFactoryImplementor sessionFactory,
MemberMetadata member,
System.Type convertType,
out IType memberType,
out IEntityPersister memberPersister,
out IAbstractComponentType memberComponent)
{
if (associationType.IsCollectionType)
{
// Check manually for entity association as GetAssociatedEntityName throws when there is none.
var queryableCollection =
(IQueryableCollection) associationType.GetAssociatedJoinable(sessionFactory);
if (!queryableCollection.ElementType.IsEntityType) // q.OneToManyCompositeElement[0].Member, q.OneToManyElement[0].Member
{
memberPersister = null;
// Can be <composite-element> or <element>
switch (queryableCollection.ElementType)
{
case IAbstractComponentType componentType: // q.OneToManyCompositeElement[0].Member
memberComponent = componentType;
memberType = TryGetComponentPropertyType(componentType, member.Path);
return;
default: // q.OneToManyElement[0].Member
memberType = null;
memberComponent = null;
return;
}
}
// q.OneToMany[0].Member
TryGetEntityPersister(
associationType.GetAssociatedEntityName(sessionFactory),
convertType,
sessionFactory,
out memberPersister);
}
else if (associationType.IsAnyType)
{
// ((Address)q.AnyType).Member, q.AnyType.Member
// Unfortunately we cannot detect the exact entity name as cast does not provide it,
// so the only option is to guess it.
TryGetEntityPersister(convertType, sessionFactory, out memberPersister);
}
else // q.ManyToOne.Member
{
TryGetEntityPersister(
associationType.GetAssociatedEntityName(sessionFactory),
convertType,
sessionFactory,
out memberPersister);
}
memberComponent = null;
memberType = memberPersister != null
? GetPropertyType(memberPersister, member.Path)
: null; // q.AnyType.Member, ((NotMappedClass)q.ManyToOne)
}
private static bool TryGetEntityPersister(
string currentEntityName,
System.Type convertedType,
ISessionFactoryImplementor sessionFactory,
out IEntityPersister persister)
{
var currentEntityPersister = sessionFactory.TryGetEntityPersister(currentEntityName);
if (currentEntityPersister == null)
{
// When dealing with a polymorphic query it is not important which entity name we pick
// as they all need to have the same mapped types for members of the type that is queried.
// If one of the entites has a different type mapped (e.g. enum mapped as string instead of numeric),
// the query will fail to execute as currently the ParameterMetadata is bound to IQueryPlan and not to IQueryTranslator
// (e.g. s.Query<IEntity>().Where(a => a.MyEnum == MyEnum.Option)).
currentEntityName = sessionFactory.GetImplementors(currentEntityName).FirstOrDefault();
if (currentEntityName == null)
{
persister = null;
return false;
}
currentEntityPersister = sessionFactory.GetEntityPersister(currentEntityName);
}
return TryGetEntityPersister(currentEntityPersister, convertedType, sessionFactory, out persister);
}
private static bool TryGetEntityPersister(
IEntityPersister currentEntityPersister,
System.Type convertedType,
ISessionFactoryImplementor sessionFactory,
out IEntityPersister persister)
{
if (convertedType == null)
{
persister = currentEntityPersister;
return true;
}
if (currentEntityPersister.EntityMetamodel.HasSubclasses)
{
// When a class is casted to a subclass e.g. ((PizzaOrder)c.Order).PizzaName, we
// can only guess the entity name of it, as there can be many entity names mapped
// to the same subclass.
persister = currentEntityPersister.EntityMetamodel.SubclassEntityNames
.Select(sessionFactory.GetEntityPersister)
.FirstOrDefault(p => p.MappedClass == convertedType);
if (persister != null)
return true;
}
else if (TryGetEntityPersister(convertedType, sessionFactory, out persister))
return true;
// Assume type conversion doesn't change entity type
// TODO: Consider removing convertedType related logic above and always return currentEntityPersister
persister = currentEntityPersister;
return true;
}
private static bool TryGetEntityPersister(
System.Type convertedType,
ISessionFactoryImplementor sessionFactory,
out IEntityPersister persister)
{
if (convertedType == null)
{
persister = null;
return false;
}
var entityName = sessionFactory.TryGetGuessEntityName(convertedType);
if (entityName == null)
{
persister = null;
return false;
}
persister = sessionFactory.GetEntityPersister(entityName);
return true;
}
private static IType GetType(
IEntityPersister currentEntityPersister,
IType currentType,
MemberMetadata member,
ISessionFactoryImplementor sessionFactory)
{
// Not mapped
if (currentType == null)
{
return null;
}
IEntityPersister persister;
if (!member.HasIndexer || currentEntityPersister == null)
{
if (member.ConvertType == null)
{
return currentType; // q.Prop, q.OneToManyCompositeElement[0].Prop
}
return TryGetEntityPersister(member.ConvertType, sessionFactory, out persister)
? persister.EntityMetamodel.EntityType // (Entity)q.Prop, (Entity)q.OneToManyCompositeElement[0].Prop
: TypeFactory.GetDefaultTypeFor(member.ConvertType); // (long)q.Prop, (long)q.OneToManyCompositeElement[0].Prop
}
if (!(currentType is IAssociationType associationType))
{
// q.Prop[0]
return null;
}
var queryableCollection = (IQueryableCollection) associationType.GetAssociatedJoinable(sessionFactory);
if (member.ConvertType == null)
{
// q.OneToMany[0]
return queryableCollection.ElementType;
}
return TryGetEntityPersister(member.ConvertType, sessionFactory, out persister)
? persister.EntityMetamodel.EntityType // (Entity)q.OneToMany[0]
: TypeFactory.GetDefaultTypeFor(member.ConvertType); // (long)q.OneToMany[0]
}
private class GroupingKeyFlattener : NhExpressionVisitor
{
private bool _flattened;
public static Expression FlattenGroupingKey(Expression expression)
{
var visitor = new GroupingKeyFlattener();
expression = visitor.Visit(expression);
if (visitor._flattened)
{
expression = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(expression);
// When the grouping key is an array we have to unwrap it (e.g. group.Key[0] == variable)
if (expression.NodeType == ExpressionType.ArrayIndex &&
expression is BinaryExpression binaryExpression &&
binaryExpression.Left is NewArrayExpression newArray &&
binaryExpression.Right is ConstantExpression indexExpression &&
indexExpression.Value is int index)
{
return newArray.Expressions[index];
}
}
return expression;
}
protected override Expression VisitMember(MemberExpression node)
{
if (node.TryGetGroupResultOperator(out var groupBy))
{
_flattened = true;
return groupBy.KeySelector;
}
return base.VisitMember(node);
}
}
private class MemberMetadataExtractor : NhExpressionVisitor
{
private readonly List<MemberMetadataResult> _childrenResults = new List<MemberMetadataResult>();
private readonly Stack<MemberMetadata> _memberPaths;
private System.Type _convertType;
private bool _hasIndexer;
private string _entityName;
private MemberMetadataExtractor(Stack<MemberMetadata> memberPaths, System.Type convertType, bool hasIndexer)
{
_memberPaths = memberPaths;
_convertType = convertType;
_hasIndexer = hasIndexer;
}
/// <summary>
/// Traverses the expression from top to bottom until the first <see cref="ConstantExpression"/> containing an IEntityNameProvider
/// instance is found.
/// </summary>
/// <param name="expression">The expression to traverse.</param>
/// <param name="results">Output parameter that represents a collection, where each item contains information about all
/// <see cref="MemberExpression"/> that were traversed until the first <see cref="ConstantExpression"/> containing an
/// <see cref="IEntityNameProvider"/> instance is found. The number of items depends on how many different paths exist
/// in the <paramref name="expression"/> that contains a <see cref="IEntityNameProvider"/> instance. When <see cref="IEntityNameProvider"/>
/// is not found or one of the expressions is not supported the parameter will be set to <see langword="null"/>.</param>
/// <returns>Whether <paramref name="results"/> was populated.</returns>
public static bool TryGetAllMemberMetadata(Expression expression, out List<MemberMetadataResult> results)
{
if (TryGetAllMemberMetadata(expression, new Stack<MemberMetadata>(), null, false, out var result))
{
results = result.GetAllResults().ToList();
return true;
}
results = null;
return false;
}
private static bool TryGetAllMemberMetadata(
Expression expression,
Stack<MemberMetadata> memberPaths,
System.Type convertType,
bool hasIndexer,
out MemberMetadataResult results)
{
expression = GroupingKeyFlattener.FlattenGroupingKey(expression);
var extractor = new MemberMetadataExtractor(memberPaths, convertType, hasIndexer);
extractor.Accept(expression);
results = extractor._entityName != null || extractor._childrenResults.Count > 0
? new MemberMetadataResult(
extractor._childrenResults,
extractor._memberPaths,
extractor._entityName,
extractor._convertType)
: null;
return results != null;
}
private void Accept(Expression expression)
{
base.Visit(expression);
}
protected override Expression VisitMember(MemberExpression node)
{
_memberPaths.Push(new MemberMetadata(node.Member.Name, _convertType, _hasIndexer));
_convertType = null;
_hasIndexer = false;
return base.Visit(node.Expression);
}
#if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
protected override Expression VisitInvocation(InvocationExpression node)
{
if (TryGetDynamicMemberBinder(node, out var binder))
{
_memberPaths.Push(new MemberMetadata(binder.Name, _convertType, _hasIndexer));
_convertType = null;
_hasIndexer = false;
return base.Visit(node.Arguments[1]);
}
return base.VisitInvocation(node);
}
#endif
protected override Expression VisitDynamic(DynamicExpression node)
{
if (node.Binder is GetMemberBinder binder)
{
_memberPaths.Push(new MemberMetadata(binder.Name, _convertType, _hasIndexer));
_convertType = null;
_hasIndexer = false;
return base.Visit(node.Arguments[0]);
}
return Visit(node);
}
protected override Expression VisitQuerySourceReference(QuerySourceReferenceExpression node)
{
if (node.ReferencedQuerySource is IFromClause fromClause)
{
// Types will be different when OfType method is used (e.g. Query<A>().OfType<B>())
if (fromClause.ItemType != node.Type)
{
_convertType = node.Type;
}
return base.Visit(fromClause.FromExpression);
}
if (node.ReferencedQuerySource is JoinClause joinClause)
{
return base.Visit(joinClause.InnerSequence);
}
if (node.ReferencedQuerySource is NhOuterJoinClause outerJoinClause)
{
return base.Visit(outerJoinClause.JoinClause.InnerSequence);
}
// Not supported expression
_entityName = null;
return node;
}
protected override Expression VisitSubQuery(SubQueryExpression expression)
{
var ofTypeOperator = expression.QueryModel.ResultOperators.OfType<OfTypeResultOperator>().FirstOrDefault();
if (ofTypeOperator != null)
{
_convertType = ofTypeOperator.SearchedItemType;
}
base.Visit(expression.QueryModel.SelectClause.Selector);
return expression;
}
protected override Expression VisitUnary(UnaryExpression node)
{
// Store only the outermost cast, when there are multiple casts for the same member
if (_convertType == null)
{
_convertType = node.Type;
}
return base.Visit(node.Operand);
}
protected internal override Expression VisitNhNominated(NhNominatedExpression node)
{
return base.Visit(node.Expression);
}
protected override Expression VisitConstant(ConstantExpression node)
{
_entityName = node.Value is IEntityNameProvider entityNameProvider
? entityNameProvider.EntityName
: null; // Not a NhQueryable<T>
return node;
}
protected override Expression VisitBinary(BinaryExpression node)
{
if (node.NodeType == ExpressionType.ArrayIndex)
{
_hasIndexer = true;
return base.Visit(node.Left);
}
if (node.NodeType == ExpressionType.Coalesce &&
(TryGetMembersMetadata(node.Left) | TryGetMembersMetadata(node.Right)))
{
return node;
}
return Visit(node);
}
protected override Expression VisitConditional(ConditionalExpression node)
{
if (TryGetMembersMetadata(node.IfTrue) | TryGetMembersMetadata(node.IfFalse))
{
return node;
}
return Visit(node);
}
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (ListIndexerGenerator.IsMethodSupported(node.Method))
{
_hasIndexer = true;
return base.Visit(
node.Object == null
? Enumerable.First(node.Arguments) // q.Children.ElementAt(0)
: node.Object // q.Children[0]
);
}
if (VisitorUtil.TryGetPotentialDynamicComponentDictionaryMember(node, out var memberName))
{
_memberPaths.Push(new MemberMetadata(memberName, _convertType, _hasIndexer));
_convertType = null;
_hasIndexer = false;
return base.Visit(node.Object);
}
return Visit(node);
}
public override Expression Visit(Expression node)
{
// Not supported expression
_entityName = null;
return node;
}
private bool TryGetMembersMetadata(Expression expression)
{
if (TryGetAllMemberMetadata(expression, Clone(_memberPaths), _convertType, _hasIndexer, out var result))
{
_childrenResults.Add(result);
return true;
}
return false;
}
private static Stack<T> Clone<T>(Stack<T> original)
{
var arr = new T[original.Count];
original.CopyTo(arr, 0);
Array.Reverse(arr);
return new Stack<T>(arr);
}
}
private struct MemberMetadata
{
public MemberMetadata(string path, System.Type convertType, bool hasIndexer)
{
Path = path;
ConvertType = convertType;
HasIndexer = hasIndexer;
}
public string Path { get; }
public System.Type ConvertType { get; }
public bool HasIndexer { get; }
}
private class MemberMetadataResult
{
public MemberMetadataResult(
List<MemberMetadataResult> childrenResults,
Stack<MemberMetadata> memberPaths,
string entityName,
System.Type convertType)
{
ChildrenResults = childrenResults;
MemberPaths = memberPaths;
EntityName = entityName;
ConvertType = convertType;
}
/// <summary>
/// Metadata about all <see cref="MemberExpression"/> that were traversed.
/// </summary>
public Stack<MemberMetadata> MemberPaths { get; }
/// <summary>
/// <see cref="UnaryExpression"/> type that was used on a <see cref="ConstantExpression"/> containing
/// an <see cref="IEntityNameProvider"/>.
/// </summary>
public System.Type ConvertType { get; }
/// <summary>
/// The entity name from <see cref="IEntityNameProvider.EntityName"/>.
/// </summary>
public string EntityName { get; }
/// <summary>
/// Direct children of the current metadata result.
/// </summary>
public List<MemberMetadataResult> ChildrenResults { get; }
/// <summary>
/// Gets all leaf (bottom) children that have the entity name set.
/// </summary>
/// <returns></returns>
public IEnumerable<MemberMetadataResult> GetAllResults()
{
return GetAllResults(this);
}
private static IEnumerable<MemberMetadataResult> GetAllResults(MemberMetadataResult result)
{
if (result.ChildrenResults.Count == 0)
{
yield return result;
}
else
{
foreach (var childResult in result.ChildrenResults)
{
foreach (var childChildrenResult in GetAllResults(childResult))
{
yield return childChildrenResult;
}
}
}
}
}
}
}