forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicCollectionJoinWalker.cs
93 lines (77 loc) · 3.54 KB
/
BasicCollectionJoinWalker.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Persister.Collection;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Loader.Collection
{
/// <summary>
/// Walker for collections of values and many-to-many associations
/// </summary>
public class BasicCollectionJoinWalker : CollectionJoinWalker
{
private readonly IQueryableCollection collectionPersister;
public BasicCollectionJoinWalker(IQueryableCollection collectionPersister, int batchSize,
SqlString subquery, ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
: base(factory, enabledFilters)
{
this.collectionPersister = collectionPersister;
string alias = GenerateRootAlias(collectionPersister.Role);
WalkCollectionTree(collectionPersister, alias);
IList<OuterJoinableAssociation> allAssociations = new List<OuterJoinableAssociation>(associations);
// NH Different behavior : passing enabledFilters instead empty-filter
allAssociations.Add(
new OuterJoinableAssociation(collectionPersister.CollectionType, null, null, alias, JoinType.LeftOuterJoin, null, Factory,
enabledFilters));
InitPersisters(allAssociations, LockMode.None);
InitStatementString(alias, batchSize, subquery);
}
private void InitStatementString(string alias, int batchSize, SqlString subquery)
{
int joins = CountEntityPersisters(associations);
int collectionJoins = CountCollectionPersisters(associations) + 1;
Suffixes = BasicLoader.GenerateSuffixes(joins);
CollectionSuffixes = BasicLoader.GenerateSuffixes(joins, collectionJoins);
SqlStringBuilder whereString = WhereString(alias, collectionPersister.KeyColumnNames, subquery, batchSize);
string manyToManyOrderBy = string.Empty;
string filter = collectionPersister.FilterFragment(alias, EnabledFilters);
if (collectionPersister.IsManyToMany)
{
// from the collection of associations, locate OJA for the
// ManyToOne corresponding to this persister to fully
// define the many-to-many; we need that OJA so that we can
// use its alias here
// TODO : is there a better way here?
IAssociationType associationType = (IAssociationType)collectionPersister.ElementType;
foreach (OuterJoinableAssociation oja in associations)
{
if (oja.JoinableType == associationType)
{
// we found it
filter += collectionPersister.GetManyToManyFilterFragment(oja.RHSAlias, EnabledFilters);
manyToManyOrderBy += collectionPersister.GetManyToManyOrderByString(oja.RHSAlias);
}
}
}
whereString.Insert(0, StringHelper.MoveAndToBeginning(filter));
JoinFragment ojf = MergeOuterJoins(associations);
SqlSelectBuilder select =
new SqlSelectBuilder(Factory)
.SetSelectClause(collectionPersister.GetSelectFragment(alias, CollectionSuffixes[0]).ToSqlStringFragment(false)
+ SelectString(associations))
.SetFromClause(collectionPersister.TableName, alias)
.SetWhereClause(whereString.ToSqlString())
.SetOuterJoins(ojf.ToFromFragmentString, ojf.ToWhereFragmentString);
select.SetOrderByClause(OrderBy(associations, MergeOrderings(collectionPersister.GetSQLOrderByString(alias), manyToManyOrderBy)));
if (Factory.Settings.IsCommentsEnabled)
select.SetComment("load collection " + collectionPersister.Role);
SqlString = select.ToSqlString();
}
public override string ToString()
{
return GetType().FullName + '(' + collectionPersister.Role + ')';
}
}
}