forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOuterJoinableAssociation.cs
263 lines (231 loc) · 7.68 KB
/
OuterJoinableAssociation.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Loader
{
public sealed class OuterJoinableAssociation : IJoin
{
private readonly IAssociationType joinableType;
private readonly IJoinable joinable;
private readonly string lhsAlias; // belong to other persister
private readonly string[] lhsColumns; // belong to other persister
private readonly string rhsAlias;
private readonly string[] rhsColumns;
private readonly JoinType joinType;
private readonly SqlString on;
private readonly IDictionary<string, IFilter> enabledFilters;
private readonly SelectMode _selectMode;
public OuterJoinableAssociation(
IAssociationType joinableType,
String lhsAlias,
String[] lhsColumns,
String rhsAlias,
JoinType joinType,
SqlString withClause,
ISessionFactoryImplementor factory,
IDictionary<string, IFilter> enabledFilters,
SelectMode selectMode) : this(joinableType, lhsAlias, lhsColumns, rhsAlias, joinType, withClause, factory, enabledFilters)
{
_selectMode = selectMode;
}
public OuterJoinableAssociation(IAssociationType joinableType, String lhsAlias, String[] lhsColumns, String rhsAlias,
JoinType joinType, SqlString withClause, ISessionFactoryImplementor factory,
IDictionary<string, IFilter> enabledFilters)
{
this.joinableType = joinableType;
this.lhsAlias = lhsAlias;
this.lhsColumns = lhsColumns;
this.rhsAlias = rhsAlias;
this.joinType = joinType;
joinable = joinableType.GetAssociatedJoinable(factory);
rhsColumns = JoinHelper.GetRHSColumnNames(joinable, joinableType);
on = new SqlString(joinableType.GetOnCondition(rhsAlias, factory, enabledFilters));
if (SqlStringHelper.IsNotEmpty(withClause))
on = on.Append(" and ( ", withClause, " )");
this.enabledFilters = enabledFilters; // needed later for many-to-many/filter application
}
public JoinType JoinType
{
get { return joinType; }
}
public string RHSAlias
{
get { return rhsAlias; }
}
public SqlString On
{
get { return on; }
}
private bool IsEntityType
{
get { return joinableType.IsEntityType; }
}
public IAssociationType JoinableType
{
get { return joinableType; }
}
public string RHSUniqueKeyName
{
get { return joinableType.RHSUniqueKeyPropertyName; }
}
public bool IsCollection
{
get { return joinableType.IsCollectionType; }
}
public IJoinable Joinable
{
get { return joinable; }
}
public SelectMode SelectMode
{
get { return _selectMode; }
}
public ISet<string> EntityFetchLazyProperties { get; set; }
internal bool ForceFilter { get; set; }
string[] IJoin.LHSColumns => lhsColumns;
string IJoin.Alias => RHSAlias;
IAssociationType IJoin.AssociationType => JoinableType;
string[] IJoin.RHSColumns => rhsColumns;
public int GetOwner(IList<OuterJoinableAssociation> associations)
{
if (IsEntityType || IsCollection)
{
return GetPosition(lhsAlias, associations);
}
else
{
return -1;
}
}
/// <summary>
/// Get the position of the join with the given alias in the
/// list of joins
/// </summary>
private static int GetPosition(string lhsAlias, IEnumerable<OuterJoinableAssociation> associations)
{
int result = 0;
foreach (OuterJoinableAssociation oj in associations)
{
if (oj.Joinable.ConsumesEntityAlias())
{
if (oj.rhsAlias.Equals(lhsAlias))
{
return result;
}
result++;
}
}
return -1;
}
public void AddJoins(JoinFragment outerjoin)
{
outerjoin.AddJoin(joinable.TableName, rhsAlias, lhsColumns, rhsColumns, joinType, on);
outerjoin.AddJoins(joinable.FromJoinFragment(rhsAlias, false, true),
joinable.WhereJoinFragment(rhsAlias, false, true));
}
public void ValidateJoin(string path)
{
if (rhsColumns == null || lhsColumns == null || lhsColumns.Length != rhsColumns.Length || lhsColumns.Length == 0)
{
throw new MappingException("invalid join columns for association: " + path);
}
}
public bool IsManyToManyWith(OuterJoinableAssociation other)
{
if (joinable.IsCollection)
{
IQueryableCollection persister = (IQueryableCollection) joinable;
if (persister.IsManyToMany)
{
return persister.ElementType == other.JoinableType;
}
}
return false;
}
public void AddManyToManyJoin(JoinFragment outerjoin, IQueryableCollection collection)
{
string manyToManyFilter = collection.GetManyToManyFilterFragment(rhsAlias, enabledFilters);
SqlString condition = string.Empty.Equals(manyToManyFilter)
? on
: SqlStringHelper.IsEmpty(on) ? new SqlString(manyToManyFilter) :
on.Append(" and ", manyToManyFilter);
outerjoin.AddJoin(joinable.TableName, rhsAlias, lhsColumns, rhsColumns, joinType, condition);
outerjoin.AddJoins(joinable.FromJoinFragment(rhsAlias, false, true),
joinable.WhereJoinFragment(rhsAlias, false, true));
}
internal bool ShouldFetchCollectionPersister()
{
if (!Joinable.IsCollection)
return false;
switch (SelectMode)
{
case SelectMode.Undefined:
return JoinType == JoinType.LeftOuterJoin;
case SelectMode.Fetch:
case SelectMode.FetchLazyProperties:
return true;
case SelectMode.ChildFetch:
case SelectMode.JoinOnly:
return false;
}
throw new ArgumentOutOfRangeException(nameof(SelectMode), SelectMode.ToString());
}
internal string GetSelectFragment(string entitySuffix, string collectionSuffix, OuterJoinableAssociation next)
{
switch (SelectMode)
{
case SelectMode.Undefined:
case SelectMode.Fetch:
#pragma warning disable 618
return Joinable.SelectFragment(
next?.Joinable,
next?.RHSAlias,
RHSAlias,
entitySuffix,
collectionSuffix,
ShouldFetchCollectionPersister());
#pragma warning restore 618
case SelectMode.FetchLazyProperties:
#pragma warning disable 618
return ReflectHelper.CastOrThrow<ISupportSelectModeJoinable>(Joinable, "fetch lazy properties")
.SelectFragment(
next?.Joinable,
next?.RHSAlias,
RHSAlias,
entitySuffix,
collectionSuffix,
ShouldFetchCollectionPersister(),
true);
#pragma warning restore 618
case SelectMode.FetchLazyPropertyGroup:
return ReflectHelper.CastOrThrow<ISupportLazyPropsJoinable>(Joinable, "fetch lazy property")
.SelectFragment(
next?.Joinable,
next?.RHSAlias,
RHSAlias,
collectionSuffix,
ShouldFetchCollectionPersister(),
new EntityLoadInfo(entitySuffix)
{
LazyProperties = EntityFetchLazyProperties,
IncludeLazyProps = SelectMode == SelectMode.FetchLazyProperties,
});
case SelectMode.ChildFetch:
// Skip ChildFetch for many-to-many as element id is added by element persister.
if (Joinable.IsCollection && ((IQueryableCollection) Joinable).IsManyToMany)
return string.Empty;
return ReflectHelper.CastOrThrow<ISupportSelectModeJoinable>(Joinable, "child fetch select mode")
.IdentifierSelectFragment(RHSAlias, entitySuffix);
case SelectMode.JoinOnly:
return string.Empty;
default:
throw new ArgumentOutOfRangeException(nameof(SelectMode), $"{SelectMode} is unexpected.");
}
}
}
}