Skip to content

Commit 89f8755

Browse files
committed
Skip no longer needed moving ON condition to Where clause in LINQ
1 parent ee0d520 commit 89f8755

File tree

4 files changed

+13
-48
lines changed

4 files changed

+13
-48
lines changed

src/NHibernate.Test/Async/Linq/LinqQuerySamples.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,17 +1112,17 @@ join o in db.Orders on
11121112

11131113
[Category("JOIN")]
11141114
[Test(Description = "This sample explictly joins two tables with a composite key and projects results from both tables.")]
1115-
public void DLinqJoin5dLeftJoinAsync()
1115+
public async Task DLinqJoin5dLeftJoinAsync()
11161116
{
11171117
var q =
11181118
from c in db.Customers
11191119
join o in db.Orders on
1120-
new { c.CustomerId, HasContractTitle = c.ContactTitle != null } equals
1121-
new { o.Customer.CustomerId, HasContractTitle = o.Customer.ContactTitle != null } into orders
1120+
new {c.CustomerId, HasContractTitle = c.ContactTitle != null} equals
1121+
new {o.Customer.CustomerId, HasContractTitle = o.Customer.ContactTitle != null} into orders
11221122
from o in orders.DefaultIfEmpty()
1123-
select new { c.ContactName, o.OrderId };
1123+
select new {c.ContactName, OrderId = (int?) o.OrderId};
11241124

1125-
Assert.ThrowsAsync<NotSupportedException>(() => ObjectDumper.WriteAsync(q));
1125+
await (ObjectDumper.WriteAsync(q));
11261126
}
11271127

11281128
[Category("JOIN")]

src/NHibernate.Test/Linq/LinqQuerySamples.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,12 +1661,12 @@ public void DLinqJoin5dLeftJoin()
16611661
var q =
16621662
from c in db.Customers
16631663
join o in db.Orders on
1664-
new { c.CustomerId, HasContractTitle = c.ContactTitle != null } equals
1665-
new { o.Customer.CustomerId, HasContractTitle = o.Customer.ContactTitle != null } into orders
1664+
new {c.CustomerId, HasContractTitle = c.ContactTitle != null} equals
1665+
new {o.Customer.CustomerId, HasContractTitle = o.Customer.ContactTitle != null} into orders
16661666
from o in orders.DefaultIfEmpty()
1667-
select new { c.ContactName, o.OrderId };
1667+
select new {c.ContactName, OrderId = (int?) o.OrderId};
16681668

1669-
Assert.Throws<NotSupportedException>(() => ObjectDumper.Write(q));
1669+
ObjectDumper.Write(q);
16701670
}
16711671

16721672
[Category("JOIN")]

src/NHibernate/Linq/ReWriters/AddJoinsReWriter.cs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.Specialized;
42
using System.Linq;
53
using System.Linq.Expressions;
64
using NHibernate.Engine;
@@ -22,13 +20,11 @@ public class AddJoinsReWriter : NhQueryModelVisitorBase, IIsEntityDecider, INhQu
2220
private readonly ISessionFactoryImplementor _sessionFactory;
2321
private readonly MemberExpressionJoinDetector _memberExpressionJoinDetector;
2422
private readonly WhereJoinDetector _whereJoinDetector;
25-
private JoinClause _currentJoin;
26-
private bool? _innerJoin;
2723

2824
private AddJoinsReWriter(ISessionFactoryImplementor sessionFactory, QueryModel queryModel)
2925
{
3026
_sessionFactory = sessionFactory;
31-
var joiner = new Joiner(queryModel, AddJoin);
27+
var joiner = new Joiner(queryModel);
3228
_memberExpressionJoinDetector = new MemberExpressionJoinDetector(this, joiner, _sessionFactory);
3329
_whereJoinDetector = new WhereJoinDetector(this, joiner, _sessionFactory);
3430
}
@@ -66,30 +62,17 @@ public override void VisitNhHavingClause(NhHavingClause havingClause, QueryModel
6662

6763
public void VisitNhOuterJoinClause(NhOuterJoinClause nhOuterJoinClause, QueryModel queryModel, int index)
6864
{
69-
VisitJoinClause(nhOuterJoinClause.JoinClause, false);
65+
VisitJoinClause(nhOuterJoinClause.JoinClause);
7066
}
7167

7268
public override void VisitJoinClause(JoinClause joinClause, QueryModel queryModel, int index)
7369
{
74-
VisitJoinClause(joinClause, true);
70+
VisitJoinClause(joinClause);
7571
}
7672

77-
private void VisitJoinClause(JoinClause joinClause, bool innerJoin)
73+
private void VisitJoinClause(JoinClause joinClause)
7874
{
7975
joinClause.InnerSequence = _whereJoinDetector.Transform(joinClause.InnerSequence);
80-
81-
// When associations are located in the outer key (e.g. from a in A join b in B b on a.C.D.Id equals b.Id),
82-
// do nothing and leave them to HQL for adding the missing joins.
83-
84-
// When associations are located in the inner key (e.g. from a in A join b in B b on a.Id equals b.C.D.Id),
85-
// we have to move the condition to the where statement, otherwise the query will be invalid (HQL does not
86-
// support them).
87-
// Link newly created joins with the current join clause in order to later detect which join type to use.
88-
_currentJoin = joinClause;
89-
_innerJoin = innerJoin;
90-
joinClause.InnerKeySelector = _whereJoinDetector.Transform(joinClause.InnerKeySelector);
91-
_currentJoin = null;
92-
_innerJoin = null;
9376
}
9477

9578
// Since v5.3
@@ -107,18 +90,6 @@ public bool IsIdentifier(System.Type type, string propertyName)
10790
return metadata != null && propertyName.Equals(metadata.IdentifierPropertyName);
10891
}
10992

110-
private void AddJoin(QueryModel queryModel, NhJoinClause joinClause)
111-
{
112-
joinClause.ParentJoinClause = _currentJoin;
113-
if (_innerJoin == true)
114-
{
115-
// Match the parent join type
116-
joinClause.MakeInner();
117-
}
118-
119-
queryModel.BodyClauses.Add(joinClause);
120-
}
121-
12293
bool IIsEntityDecider.IsEntity(MemberExpression expression, out bool isIdentifier)
12394
{
12495
isIdentifier =

src/NHibernate/Linq/Visitors/JoinBuilder.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ public class Joiner : IJoiner
2121
private readonly NameGenerator _nameGenerator;
2222
private readonly QueryModel _queryModel;
2323

24-
internal Joiner(QueryModel queryModel, System.Action<QueryModel, NhJoinClause> addJoinMethod)
25-
: this(queryModel)
26-
{
27-
AddJoinMethod = addJoinMethod;
28-
}
29-
3024
internal Joiner(QueryModel queryModel)
3125
{
3226
_nameGenerator = new NameGenerator(queryModel);

0 commit comments

Comments
 (0)