1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Linq ;
4
3
using System . Text ;
5
4
using NHibernate . Hql . Ast . ANTLR . Tree ;
6
5
using NHibernate . Persister . Collection ;
@@ -41,7 +40,7 @@ public override string ToString()
41
40
return buf . Append ( '}' ) . ToString ( ) ;
42
41
}
43
42
44
- private sealed class Join
43
+ private sealed class Join : IJoin
45
44
{
46
45
private readonly IAssociationType associationType ;
47
46
private readonly IJoinable joinable ;
@@ -50,7 +49,7 @@ private sealed class Join
50
49
private readonly string [ ] lhsColumns ;
51
50
52
51
public Join ( ISessionFactoryImplementor factory , IAssociationType associationType , string alias , JoinType joinType ,
53
- string [ ] lhsColumns )
52
+ string [ ] lhsColumns )
54
53
{
55
54
this . associationType = associationType ;
56
55
this . joinable = associationType . GetAssociatedJoinable ( factory ) ;
@@ -182,7 +181,7 @@ internal JoinFragment ToJoinFragment(
182
181
last = join . Joinable ;
183
182
}
184
183
185
- if ( rootJoinable == null && ProcessAsTableGroupJoin ( includeAllSubclassJoins , withClauses , joinFragment ) )
184
+ if ( rootJoinable == null && TableGroupJoinHelper . ProcessAsTableGroupJoin ( joins , withClauses , includeAllSubclassJoins , joinFragment , alias => IsIncluded ( alias ) , factory ) )
186
185
{
187
186
return joinFragment ;
188
187
}
@@ -253,117 +252,6 @@ private SqlString GetWithClause(IDictionary<string, IFilter> enabledFilters, ref
253
252
return SqlStringHelper . JoinParts ( " and " , withConditions ) ;
254
253
}
255
254
256
- private bool ProcessAsTableGroupJoin ( bool includeAllSubclassJoins , SqlString [ ] withClauseFragments , JoinFragment joinFragment )
257
- {
258
- if ( ! NeedsTableGroupJoin ( joins , withClauseFragments , includeAllSubclassJoins ) )
259
- return false ;
260
-
261
- var first = joins [ 0 ] ;
262
- string joinString = ANSIJoinFragment . GetJoinString ( first . JoinType ) ;
263
- joinFragment . AddFromFragmentString (
264
- new SqlString (
265
- joinString ,
266
- " (" ,
267
- first . Joinable . TableName ,
268
- " " ,
269
- first . Alias
270
- ) ) ;
271
-
272
- foreach ( var join in joins )
273
- {
274
- if ( join != first )
275
- joinFragment . AddJoin (
276
- join . Joinable . TableName ,
277
- join . Alias ,
278
- join . LHSColumns ,
279
- JoinHelper . GetRHSColumnNames ( join . AssociationType , factory ) ,
280
- join . JoinType ,
281
- SqlString . Empty ) ;
282
-
283
- AddSubclassJoins (
284
- joinFragment ,
285
- join . Alias ,
286
- join . Joinable ,
287
- // TODO (from hibernate): Think about if this could be made always true
288
- // NH Specific: made always true (original check: join.JoinType == JoinType.InnerJoin)
289
- true ,
290
- includeAllSubclassJoins
291
- ) ;
292
- }
293
-
294
- var tableGroupWithClause = GetTableGroupJoinWithClause ( withClauseFragments , first ) ;
295
- joinFragment . AddFromFragmentString ( tableGroupWithClause ) ;
296
- return true ;
297
- }
298
-
299
- private SqlString GetTableGroupJoinWithClause ( SqlString [ ] withClauseFragments , Join first )
300
- {
301
- SqlStringBuilder fromFragment = new SqlStringBuilder ( ) ;
302
- fromFragment . Add ( ")" ) . Add ( " on " ) ;
303
-
304
- String [ ] lhsColumns = first . LHSColumns ;
305
- var isAssociationJoin = lhsColumns . Length > 0 ;
306
- if ( isAssociationJoin )
307
- {
308
- String rhsAlias = first . Alias ;
309
- String [ ] rhsColumns = JoinHelper . GetRHSColumnNames ( first . AssociationType , factory ) ;
310
- for ( int j = 0 ; j < lhsColumns . Length ; j ++ )
311
- {
312
- fromFragment . Add ( lhsColumns [ j ] ) ;
313
- fromFragment . Add ( "=" ) ;
314
- fromFragment . Add ( rhsAlias ) ;
315
- fromFragment . Add ( "." ) ;
316
- fromFragment . Add ( rhsColumns [ j ] ) ;
317
- if ( j < lhsColumns . Length - 1 )
318
- {
319
- fromFragment . Add ( " and " ) ;
320
- }
321
- }
322
- }
323
-
324
- for ( var i = 0 ; i < withClauseFragments . Length ; i ++ )
325
- {
326
- var withClause = withClauseFragments [ i ] ;
327
- if ( SqlStringHelper . IsEmpty ( withClause ) )
328
- continue ;
329
-
330
- if ( withClause . StartsWithCaseInsensitive ( " and " ) )
331
- {
332
- if ( ! isAssociationJoin )
333
- {
334
- withClause = withClause . Substring ( 4 ) ;
335
- }
336
- }
337
- else if ( isAssociationJoin )
338
- {
339
- fromFragment . Add ( " and " ) ;
340
- }
341
-
342
- fromFragment . Add ( withClause ) ;
343
- }
344
-
345
- return fromFragment . ToSqlString ( ) ;
346
- }
347
-
348
- private bool NeedsTableGroupJoin ( List < Join > joins , SqlString [ ] withClauseFragments , bool includeSubclasses )
349
- {
350
- // If the rewrite is disabled or we don't have a with clause, we don't need a table group join
351
- if ( /*!collectionJoinSubquery ||*/ withClauseFragments . All ( x => SqlStringHelper . IsEmpty ( x ) ) )
352
- {
353
- return false ;
354
- }
355
- // If we only have one join, a table group join is only necessary if subclass columns are used in the with clause
356
- if ( joins . Count == 1 )
357
- {
358
- return joins [ 0 ] . Joinable is AbstractEntityPersister persister && persister . HasSubclassJoins ( includeSubclasses ) ;
359
- //NH Specific: No alias processing
360
- //return isSubclassAliasDereferenced( joins[ 0], withClauseFragment );
361
- }
362
-
363
- //NH Specific: No alias processing (see hibernate JoinSequence.NeedsTableGroupJoin)
364
- return true ;
365
- }
366
-
367
255
private bool IsManyToManyRoot ( IJoinable joinable )
368
256
{
369
257
if ( joinable != null && joinable . IsCollection )
0 commit comments