forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractEntityJoinWalker.cs
181 lines (154 loc) · 5.95 KB
/
AbstractEntityJoinWalker.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
using System;
using System.Collections.Generic;
using NHibernate.Criterion;
using NHibernate.Engine;
using NHibernate.Persister.Entity;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Loader
{
public abstract class AbstractEntityJoinWalker : JoinWalker
{
private readonly IOuterJoinLoadable persister;
private readonly string alias;
public AbstractEntityJoinWalker(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
: base(factory, enabledFilters)
{
this.persister = persister;
alias = GenerateRootAlias(persister.EntityName);
}
public AbstractEntityJoinWalker(string rootSqlAlias, IOuterJoinLoadable persister, ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
: base(factory, enabledFilters)
{
this.persister = persister;
alias = rootSqlAlias;
}
protected virtual void InitAll(SqlString whereString, SqlString orderByString, LockMode lockMode)
{
AddAssociations();
ProcessJoins();
IList<OuterJoinableAssociation> allAssociations = new List<OuterJoinableAssociation>(associations);
var rootAssociation = CreateRootAssociation();
allAssociations.Add(rootAssociation);
InitPersisters(allAssociations, lockMode);
InitStatementString(rootAssociation, null, whereString, orderByString, null, null, lockMode);
}
protected virtual OuterJoinableAssociation CreateRootAssociation()
{
return CreateAssociation(persister.EntityType, Alias);
}
//Since v5.1
[Obsolete("Please use InitProjection(SqlString projectionString, SqlString whereString, SqlString orderByString, SqlString groupByString, SqlString havingString, IDictionary<string, IFilter> enabledFilters, LockMode lockMode, IList<EntityProjection> entityProjections) instead")]
protected void InitProjection(SqlString projectionString, SqlString whereString, SqlString orderByString, SqlString groupByString, SqlString havingString, IDictionary<string, IFilter> enabledFilters, LockMode lockMode)
{
InitProjection(projectionString, whereString, orderByString, groupByString, havingString, enabledFilters, lockMode, Array.Empty<EntityProjection>());
}
protected void InitProjection(SqlString projectionString, SqlString whereString, SqlString orderByString, SqlString groupByString, SqlString havingString, IDictionary<string, IFilter> enabledFilters, LockMode lockMode, IList<EntityProjection> entityProjections)
{
AddAssociations();
int countEntities = entityProjections.Count;
if (countEntities > 0)
{
var associations = new OuterJoinableAssociation[countEntities];
var eagerProps = new bool[countEntities];
var suffixes = new string[countEntities];
var fetchLazyProperties = new ISet<string>[countEntities];
for (var i = 0; i < countEntities; i++)
{
var e = entityProjections[i];
associations[i] = CreateAssociation(e.Persister.EntityMetamodel.EntityType, e.TableAlias);
if (e.FetchLazyProperties)
{
eagerProps[i] = true;
}
if (e.FetchLazyPropertyGroups?.Count > 0)
{
fetchLazyProperties[i] = new HashSet<string>(e.FetchLazyPropertyGroups);
}
suffixes[i] = e.ColumnAliasSuffix;
}
InitPersisters(associations, lockMode);
Suffixes = suffixes;
EagerPropertyFetches = eagerProps;
EntityFetchLazyProperties = fetchLazyProperties;
}
else
{
Persisters = Array.Empty<ILoadable>();
Suffixes = Array.Empty<string>();
}
InitStatementString(null, projectionString, whereString, orderByString, groupByString, havingString, lockMode);
}
protected virtual void AddAssociations()
{
WalkEntityTree(persister, Alias);
}
private OuterJoinableAssociation CreateAssociation(EntityType entityType, string tableAlias)
{
return new OuterJoinableAssociation(
entityType,
null,
null,
tableAlias,
JoinType.LeftOuterJoin,
null,
Factory,
CollectionHelper.EmptyDictionary<string, IFilter>());
}
private void InitStatementString(OuterJoinableAssociation rootAssociation, SqlString projection, SqlString condition, SqlString orderBy, SqlString groupBy, SqlString having, LockMode lockMode)
{
SqlString selectClause = projection;
if (selectClause == null)
{
int joins = CountEntityPersisters(associations);
Suffixes = BasicLoader.GenerateSuffixes(joins + 1);
var suffix = Suffixes[joins];
selectClause = new SqlString(rootAssociation.GetSelectFragment(suffix, null, null) + SelectString(associations));
}
JoinFragment ojf = MergeOuterJoins(associations);
SqlSelectBuilder select = new SqlSelectBuilder(Factory)
.SetLockMode(lockMode, alias)
.SetSelectClause(selectClause)
.SetFromClause(Dialect.AppendLockHint(lockMode, persister.FromTableFragment(alias)) +persister.FromJoinFragment(alias, true, true))
.SetWhereClause(condition)
.SetOuterJoins(ojf.ToFromFragmentString,ojf.ToWhereFragmentString + WhereFragment)
.SetOrderByClause(
projection == null
? OrderBy(associations, orderBy)
: orderBy)
.SetGroupByClause(groupBy)
.SetHavingClause(having);
if (Factory.Settings.IsCommentsEnabled)
select.SetComment(Comment);
SqlString = select.ToSqlString();
}
/// <summary>
/// The superclass deliberately excludes collections
/// </summary>
protected override bool IsJoinedFetchEnabled(IAssociationType type, FetchMode config, CascadeStyle cascadeStyle)
{
return IsJoinedFetchEnabledInMapping(config, type);
}
/// <summary>
/// Don't bother with the discriminator, unless overridden by subclass
/// </summary>
protected virtual SqlString WhereFragment
{
get { return persister.WhereJoinFragment(alias, true, true); }
}
public abstract string Comment { get; }
protected IOuterJoinLoadable Persister
{
get { return persister; }
}
protected string Alias
{
get { return alias; }
}
public override string ToString()
{
return GetType().FullName + '(' + Persister.EntityName + ')';
}
}
}