@@ -121,8 +121,6 @@ public class DependentAlias
121
121
public string [ ] DependsOn { get ; set ; }
122
122
}
123
123
124
- readonly List < DependentAlias > _dependentAliases = new List < DependentAlias > ( ) ;
125
-
126
124
protected JoinWalker ( ISessionFactoryImplementor factory , IDictionary < string , IFilter > enabledFilters )
127
125
{
128
126
this . factory = factory ;
@@ -181,7 +179,7 @@ private void AddAssociationToJoinTree(IAssociationType type, string[] aliasedLhs
181
179
enabledFilters ,
182
180
GetSelectMode ( path ) ) ;
183
181
assoc . ValidateJoin ( path ) ;
184
- AddAssociation ( subalias , assoc ) ;
182
+ AddAssociation ( assoc ) ;
185
183
186
184
int nextDepth = currentDepth + 1 ;
187
185
@@ -207,13 +205,17 @@ protected virtual SelectMode GetSelectMode(string path)
207
205
/// <summary>
208
206
/// Returns null if sorting is not required, otherwise list of indexes in sorted order
209
207
/// </summary>
210
- private static int [ ] GetTopologicalSortOrder ( List < DependentAlias > fields )
208
+ private static int [ ] GetTopologicalSortOrder ( IList < OuterJoinableAssociation > associations )
211
209
{
210
+ if ( associations . Count < 2 )
211
+ return null ;
212
+
213
+ var fields = GetDependentAliases ( associations ) ;
212
214
if ( ! fields . Exists ( a => a . DependsOn ? . Length > 0 ) )
213
215
return null ;
214
216
215
217
TopologicalSorter g = new TopologicalSorter ( fields . Count ) ;
216
- Dictionary < string , int > indexes = new Dictionary < string , int > ( StringComparer . OrdinalIgnoreCase ) ;
218
+ Dictionary < string , int > indexes = new Dictionary < string , int > ( fields . Count , StringComparer . OrdinalIgnoreCase ) ;
217
219
218
220
// add vertices
219
221
for ( int i = 0 ; i < fields . Count ; i ++ )
@@ -242,31 +244,41 @@ private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
242
244
return g . Sort ( ) ;
243
245
}
244
246
245
- /// <summary>
246
- /// Adds an association and extracts the aliases the association's 'with clause' is dependent on
247
- /// </summary>
248
- private void AddAssociation ( string subalias , OuterJoinableAssociation association )
247
+ private static List < DependentAlias > GetDependentAliases ( IList < OuterJoinableAssociation > associations )
249
248
{
250
- var dependentAlias = new DependentAlias
251
- {
252
- Alias = subalias ,
253
- } ;
254
- _dependentAliases . Add ( dependentAlias ) ;
249
+ var dependentAliases = new List < DependentAlias > ( associations . Count ) ;
255
250
256
- var on = association . On . ToString ( ) ;
257
- if ( ! string . IsNullOrEmpty ( on ) )
251
+ foreach ( var association in associations )
258
252
{
259
- var dependencies = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
260
- foreach ( Match match in aliasRegex . Matches ( on ) )
253
+ var dependentAlias = new DependentAlias
261
254
{
262
- string alias = match . Value ;
263
- if ( string . Equals ( alias , subalias , StringComparison . OrdinalIgnoreCase ) )
264
- continue ;
265
- dependencies . Add ( alias ) ;
255
+ Alias = association . RHSAlias ,
256
+ } ;
257
+
258
+ dependentAliases . Add ( dependentAlias ) ;
259
+
260
+ var on = association . On . ToString ( ) ;
261
+ if ( ! string . IsNullOrEmpty ( on ) )
262
+ {
263
+ var dependencies = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
264
+ foreach ( Match match in aliasRegex . Matches ( on ) )
265
+ {
266
+ string alias = match . Value ;
267
+ if ( string . Equals ( alias , dependentAlias . Alias , StringComparison . OrdinalIgnoreCase ) )
268
+ continue ;
269
+ dependencies . Add ( alias ) ;
270
+ }
271
+ dependentAlias . DependsOn = dependencies . ToArray ( ) ;
266
272
}
267
- dependentAlias . DependsOn = dependencies . ToArray ( ) ;
268
273
}
274
+ return dependentAliases ;
275
+ }
269
276
277
+ /// <summary>
278
+ /// Adds an association
279
+ /// </summary>
280
+ private void AddAssociation ( OuterJoinableAssociation association )
281
+ {
270
282
associations . Add ( association ) ;
271
283
}
272
284
@@ -366,7 +378,7 @@ internal void AddExplicitEntityJoinAssociation(
366
378
Factory ,
367
379
enabledFilters ,
368
380
GetSelectMode ( path ) ) ;
369
- AddAssociation ( tableAlias , assoc ) ;
381
+ AddAssociation ( assoc ) ;
370
382
}
371
383
372
384
private void WalkEntityAssociationTree ( IAssociationType associationType , IOuterJoinLoadable persister ,
@@ -841,7 +853,7 @@ protected JoinFragment MergeOuterJoins(IList<OuterJoinableAssociation> associati
841
853
842
854
private IList < OuterJoinableAssociation > GetSortedAssociations ( IList < OuterJoinableAssociation > associations )
843
855
{
844
- var indexes = GetTopologicalSortOrder ( _dependentAliases ) ;
856
+ var indexes = GetTopologicalSortOrder ( associations ) ;
845
857
if ( indexes == null )
846
858
return associations ;
847
859
0 commit comments