1
+ using System ;
2
+ using System . Text ;
3
+ using NHibernate . Persister . Collection ;
4
+ using NHibernate . Persister . Entity ;
5
+ using NHibernate . Type ;
6
+ using NHibernate . Util ;
7
+
8
+ namespace NHibernate . Hql . Ast . ANTLR . Tree
9
+ {
10
+ public class ComponentJoin : FromElement
11
+ {
12
+ private readonly string columns ;
13
+ private readonly string componentPath ;
14
+ private readonly string componentProperty ;
15
+ private readonly ComponentType componentType ;
16
+
17
+ public ComponentJoin ( FromClause fromClause , FromElement origin , string alias , string componentPath , ComponentType componentType )
18
+ : base ( fromClause , origin , alias )
19
+ {
20
+ this . componentPath = componentPath ;
21
+ this . componentType = componentType ;
22
+ componentProperty = StringHelper . Unqualify ( componentPath ) ;
23
+ fromClause . AddJoinByPathMap ( componentPath , this ) ;
24
+ InitializeComponentJoin ( new ComponentFromElementType ( this ) ) ;
25
+
26
+ string [ ] cols = origin . GetPropertyMapping ( "" ) . ToColumns ( TableAlias , componentProperty ) ;
27
+ columns = string . Join ( ", " , cols ) ;
28
+ }
29
+
30
+ public string ComponentPath
31
+ {
32
+ get { return componentPath ; }
33
+ }
34
+
35
+ public ComponentType ComponentType
36
+ {
37
+ get { return componentType ; }
38
+ }
39
+
40
+ public string ComponentProperty
41
+ {
42
+ get { return componentProperty ; }
43
+ }
44
+
45
+ public override IType DataType
46
+ {
47
+ get { return ComponentType ; }
48
+ set { base . DataType = value ; }
49
+ }
50
+
51
+ public override string GetIdentityColumn ( )
52
+ {
53
+ return columns ;
54
+ }
55
+
56
+ public override string GetDisplayText ( )
57
+ {
58
+ return "ComponentJoin{path=" + ComponentPath + ", type=" + componentType . ReturnedClass + "}" ;
59
+ }
60
+
61
+ #region Nested type: ComponentFromElementType
62
+
63
+ public class ComponentFromElementType : FromElementType
64
+ {
65
+ private readonly ComponentJoin fromElement ;
66
+ private readonly IPropertyMapping propertyMapping ;
67
+
68
+ public ComponentFromElementType ( ComponentJoin fromElement )
69
+ : base ( fromElement )
70
+ {
71
+ this . fromElement = fromElement ;
72
+ propertyMapping = new ComponentPropertyMapping ( this ) ;
73
+ }
74
+
75
+ public ComponentJoin FromElement
76
+ {
77
+ get { return fromElement ; }
78
+ }
79
+
80
+ public override IType DataType
81
+ {
82
+ get { return fromElement . ComponentType ; }
83
+ }
84
+
85
+ public override IQueryableCollection QueryableCollection
86
+ {
87
+ get { return null ; }
88
+ set { base . QueryableCollection = value ; }
89
+ }
90
+
91
+ public override IPropertyMapping GetPropertyMapping ( string propertyName )
92
+ {
93
+ return propertyMapping ;
94
+ }
95
+
96
+ public override IType GetPropertyType ( string propertyName , string propertyPath )
97
+ {
98
+ int index = fromElement . ComponentType . GetPropertyIndex ( propertyName ) ;
99
+ return fromElement . ComponentType . Subtypes [ index ] ;
100
+ }
101
+
102
+ public override string RenderScalarIdentifierSelect ( int i )
103
+ {
104
+ String [ ] cols = GetBasePropertyMapping ( ) . ToColumns ( fromElement . TableAlias , fromElement . ComponentProperty ) ;
105
+ var buf = new StringBuilder ( ) ;
106
+ // For property references generate <tablealias>.<columnname> as <projectionalias>
107
+ for ( int j = 0 ; j < cols . Length ; j ++ )
108
+ {
109
+ string column = cols [ j ] ;
110
+ if ( j > 0 )
111
+ {
112
+ buf . Append ( ", " ) ;
113
+ }
114
+ buf . Append ( column ) . Append ( " as " ) . Append ( NameGenerator . ScalarName ( i , j ) ) ;
115
+ }
116
+ return buf . ToString ( ) ;
117
+ }
118
+
119
+ protected IPropertyMapping GetBasePropertyMapping ( )
120
+ {
121
+ return fromElement . Origin . GetPropertyMapping ( "" ) ;
122
+ }
123
+
124
+ #region Nested type: ComponentPropertyMapping
125
+
126
+ private class ComponentPropertyMapping : IPropertyMapping
127
+ {
128
+ private readonly ComponentFromElementType fromElementType ;
129
+
130
+ public ComponentPropertyMapping ( ComponentFromElementType fromElementType )
131
+ {
132
+ this . fromElementType = fromElementType ;
133
+ }
134
+
135
+ #region IPropertyMapping Members
136
+
137
+ public IType Type
138
+ {
139
+ get { return fromElementType . FromElement . ComponentType ; }
140
+ }
141
+
142
+ public IType ToType ( string propertyName )
143
+ {
144
+ return fromElementType . GetBasePropertyMapping ( ) . ToType ( GetPropertyPath ( propertyName ) ) ;
145
+ }
146
+
147
+ public bool TryToType ( string propertyName , out IType type )
148
+ {
149
+ return fromElementType . GetBasePropertyMapping ( ) . TryToType ( GetPropertyPath ( propertyName ) , out type ) ;
150
+ }
151
+
152
+ public string [ ] ToColumns ( string alias , string propertyName )
153
+ {
154
+ return fromElementType . GetBasePropertyMapping ( ) . ToColumns ( alias , GetPropertyPath ( propertyName ) ) ;
155
+ }
156
+
157
+ public string [ ] ToColumns ( string propertyName )
158
+ {
159
+ return fromElementType . GetBasePropertyMapping ( ) . ToColumns ( GetPropertyPath ( propertyName ) ) ;
160
+ }
161
+
162
+ #endregion
163
+
164
+ private string GetPropertyPath ( string propertyName )
165
+ {
166
+ return fromElementType . FromElement . ComponentPath + '.' + propertyName ;
167
+ }
168
+ }
169
+
170
+ #endregion
171
+ }
172
+
173
+ #endregion
174
+ }
175
+ }
0 commit comments