forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestedSelectDetector.cs
72 lines (60 loc) · 1.81 KB
/
NestedSelectDetector.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
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Util;
using Remotion.Linq.Clauses.Expressions;
using Remotion.Linq.Parsing;
namespace NHibernate.Linq.NestedSelects
{
internal class NestedSelectDetector : RelinqExpressionVisitor
{
private readonly ISessionFactory sessionFactory;
private readonly ICollection<Expression> _expressions = new List<Expression>();
public NestedSelectDetector(ISessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public ICollection<Expression> Expressions
{
get { return _expressions; }
}
public bool HasSubqueries
{
get { return Expressions.Count > 0; }
}
protected override Expression VisitSubQuery(SubQueryExpression expression)
{
if (expression.QueryModel.ResultOperators.Count == 0)
Expressions.Add(expression);
return base.VisitSubQuery(expression);
}
protected override Expression VisitMember(MemberExpression expression)
{
var memberType = ReflectHelper.GetPropertyOrFieldType(expression.Member);
if (memberType != null && memberType.IsCollectionType()
&& IsChainedFromQuerySourceReference(expression)
&& IsMappedCollection(expression.Member))
{
Expressions.Add(expression);
}
return base.VisitMember(expression);
}
private bool IsMappedCollection(MemberInfo memberInfo)
{
var collectionRole = memberInfo.DeclaringType.FullName + "." + memberInfo.Name;
return sessionFactory.GetCollectionMetadata(collectionRole) != null;
}
private bool IsChainedFromQuerySourceReference(MemberExpression expression)
{
while (expression != null)
{
if (expression.Expression is QuerySourceReferenceExpression)
{
return true;
}
expression = expression.Expression as MemberExpression;
}
return false;
}
}
}