forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoinHelper.cs
212 lines (182 loc) · 6 KB
/
JoinHelper.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
using NHibernate.Persister.Entity;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Engine
{
public static class JoinHelper
{
public static ILhsAssociationTypeSqlInfo GetLhsSqlInfo(string alias, int property,
IOuterJoinLoadable lhsPersister, IMapping mapping)
{
return new PropertiesLhsAssociationTypeSqlInfo(alias, property, lhsPersister, mapping);
}
public static ILhsAssociationTypeSqlInfo GetIdLhsSqlInfo(string alias, IOuterJoinLoadable lhsPersister, IMapping mapping)
{
return new IdPropertiesLhsAssociationTypeSqlInfo(alias, lhsPersister, mapping);
}
/// <summary>
/// Get the columns of the associated table which are to
/// be used in the join
/// </summary>
public static string[] GetRHSColumnNames(IAssociationType type, ISessionFactoryImplementor factory)
{
return GetRHSColumnNames(type.GetAssociatedJoinable(factory), type);
}
/// <summary>
/// Get the columns of the associated table which are to
/// be used in the join
/// </summary>
public static string[] GetRHSColumnNames(IJoinable joinable, IAssociationType type)
{
string uniqueKeyPropertyName = type.RHSUniqueKeyPropertyName;
return uniqueKeyPropertyName == null
? joinable.KeyColumnNames
: ((IOuterJoinLoadable) joinable).GetPropertyColumnNames(uniqueKeyPropertyName);
}
}
public interface ILhsAssociationTypeSqlInfo
{
/// <summary>
/// Get the aliased columns of the owning entity which are to
/// be used in the join
/// </summary>
string[] GetAliasedColumnNames(IAssociationType type, int begin);
/// <summary>
/// Get the columns of the owning entity which are to
/// be used in the join
/// </summary>
string[] GetColumnNames(IAssociationType type, int begin);
string GetTableName(IAssociationType type);
}
public abstract class AbstractLhsAssociationTypeSqlInfo : ILhsAssociationTypeSqlInfo
{
protected AbstractLhsAssociationTypeSqlInfo(string @alias, IOuterJoinLoadable persister, IMapping mapping)
{
Alias = alias;
Persister = persister;
Mapping = mapping;
}
public string Alias { get; private set; }
public IOuterJoinLoadable Persister { get; private set; }
public IMapping Mapping { get; private set; }
#region Implementation of ILhsAssociationTypeSqlInfo
public string[] GetAliasedColumnNames(IAssociationType type, int begin)
{
if (type.UseLHSPrimaryKey)
{
return StringHelper.Qualify(Alias, Persister.IdentifierColumnNames);
}
else
{
string propertyName = type.LHSPropertyName;
if (propertyName == null)
{
return ArrayHelper.Slice(GetAliasedColumns(), begin, type.GetColumnSpan(Mapping));
}
else
{
return ((IPropertyMapping)Persister).ToColumns(Alias, propertyName); //bad cast
}
}
}
public string[] GetColumnNames(IAssociationType type, int begin)
{
if (type.UseLHSPrimaryKey)
{
return Persister.IdentifierColumnNames;
}
else
{
string propertyName = type.LHSPropertyName;
if (propertyName == null)
{
//slice, to get the columns for this component property
return ArrayHelper.Slice(GetColumns(), begin, type.GetColumnSpan(Mapping));
}
else
{
//property-refs for associations defined on a
//component are not supported, so no need to slice
return Persister.GetPropertyColumnNames(propertyName);
}
}
}
protected abstract string[] GetAliasedColumns();
protected abstract string[] GetColumns();
public abstract string GetTableName(IAssociationType type);
#endregion
}
public class PropertiesLhsAssociationTypeSqlInfo : AbstractLhsAssociationTypeSqlInfo
{
private readonly int propertyIdx;
public PropertiesLhsAssociationTypeSqlInfo(string alias,
int propertyIdx, IOuterJoinLoadable persister, IMapping mapping)
: base(alias, persister, mapping)
{
this.propertyIdx = propertyIdx;
}
#region Overrides of AbstractLhsAssociationTypeSqlInfo
protected override string[] GetAliasedColumns()
{
return Persister.ToColumns(Alias, propertyIdx);
}
protected override string[] GetColumns()
{
return Persister.GetSubclassPropertyColumnNames(propertyIdx);
}
public override string GetTableName(IAssociationType type)
{
if (type.UseLHSPrimaryKey)
{
return Persister.TableName;
}
else
{
string propertyName = type.LHSPropertyName;
if (propertyName == null)
{
//if there is no property-ref, assume the join
//is to the subclass table (ie. the table of the
//subclass that the association belongs to)
return Persister.GetSubclassPropertyTableName(propertyIdx);
}
else
{
//handle a property-ref
string propertyRefTable = Persister.GetPropertyTableName(propertyName);
if (propertyRefTable == null)
{
//it is possible that the tree-walking in OuterJoinLoader can get to
//an association defined by a subclass, in which case the property-ref
//might refer to a property defined on a subclass of the current class
//in this case, the table name is not known - this temporary solution
//assumes that the property-ref refers to a property of the subclass
//table that the association belongs to (a reasonable guess)
//TODO: fix this, add: IOuterJoinLoadable.getSubclassPropertyTableName(string propertyName)
propertyRefTable = Persister.GetSubclassPropertyTableName(propertyIdx);
}
return propertyRefTable;
}
}
}
#endregion
}
public class IdPropertiesLhsAssociationTypeSqlInfo : AbstractLhsAssociationTypeSqlInfo
{
public IdPropertiesLhsAssociationTypeSqlInfo(string alias, IOuterJoinLoadable persister, IMapping mapping) : base(alias, persister, mapping) {}
#region Overrides of AbstractLhsAssociationTypeSqlInfo
protected override string[] GetAliasedColumns()
{
return Persister.ToIdentifierColumns(Alias);
}
protected override string[] GetColumns()
{
return Persister.IdentifierColumnNames;
}
public override string GetTableName(IAssociationType type)
{
return Persister.TableName;
}
#endregion
}
}