Skip to content

Commit 25dca03

Browse files
AkkermanAkkerman
Akkerman
authored and
Akkerman
committed
NH-3777: Fixed JoinWalker to expose additional constraints to the topological sort.
Before, the only constraints exposed to the topological sort where constraints that were found in the explicit "On"-SqlString of the underlying OuterJoinableAssociation and not the implicit dependency of the join itself (LHS depends on RHS)
1 parent c7b34d8 commit 25dca03

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/NHibernate/Loader/JoinWalker.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using NHibernate.SqlCommand;
99
using NHibernate.Type;
1010
using NHibernate.Util;
11+
using System.Linq;
1112

1213
namespace NHibernate.Loader
1314
{
@@ -158,7 +159,7 @@ private void AddAssociationToJoinTree(IAssociationType type, string[] aliasedLhs
158159
OuterJoinableAssociation assoc =
159160
new OuterJoinableAssociation(type, alias, aliasedLhsColumns, subalias, joinType, GetWithClause(path), Factory, enabledFilters);
160161
assoc.ValidateJoin(path);
161-
AddAssociation(subalias, assoc);
162+
AddAssociation(alias, assoc);
162163

163164
int nextDepth = currentDepth + 1;
164165

@@ -197,6 +198,7 @@ private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
197198
{
198199
var dependentField = dependentAlias.DependsOn[j].ToLower();
199200
int end;
201+
200202
if (_indexes.TryGetValue(dependentField, out end))
201203
{
202204
g.AddEdge(i, end);
@@ -211,25 +213,30 @@ private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
211213
/// <summary>
212214
/// Adds an association and extracts the aliases the association's 'with clause' is dependent on
213215
/// </summary>
214-
private void AddAssociation(string subalias, OuterJoinableAssociation association)
216+
private void AddAssociation(string lhsAlias, OuterJoinableAssociation association)
215217
{
216-
subalias = subalias.ToLower();
218+
string rhsAlias = association.RHSAlias.ToLower();
217219

218220
var dependencies = new List<string>();
221+
222+
// the association always depends on the lhsAlias (note: the "On"-SqlString does not seem to include this part of the join SQL)
223+
dependencies.Add(lhsAlias.ToLower());
224+
225+
// analyze the "On"-SqlString to find other aliases the association depends on.
219226
var on = association.On.ToString();
220227
if (!String.IsNullOrEmpty(on))
221228
{
222229
foreach (Match match in aliasRegex.Matches(on))
223230
{
224231
string alias = match.Groups[1].Value;
225-
if (alias == subalias) continue;
232+
if (alias == rhsAlias) continue;
226233
dependencies.Add(alias.ToLower());
227234
}
228235
}
229236

230237
_dependentAliases.Add(new DependentAlias
231238
{
232-
Alias = subalias,
239+
Alias = rhsAlias,
233240
DependsOn = dependencies.ToArray()
234241
});
235242

@@ -908,4 +915,4 @@ public string SelectString(IList<OuterJoinableAssociation> associations)
908915
}
909916
}
910917
}
911-
}
918+
}

0 commit comments

Comments
 (0)