1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
#region Imports
|
28
|
|
|
29
|
|
using System;
|
30
|
|
using System.Collections;
|
31
|
|
using System.Data;
|
32
|
|
using System.Reflection;
|
33
|
|
using System.Xml;
|
34
|
|
using System.Xml.Serialization;
|
35
|
|
using IBatisNet.Common.Exceptions;
|
36
|
|
using IBatisNet.Common.Utilities.Objects;
|
37
|
|
using IBatisNet.Common.Utilities.TypesResolver;
|
38
|
|
using IBatisNet.DataMapper.Scope;
|
39
|
|
|
40
|
|
#endregion
|
41
|
|
|
42
|
|
namespace IBatisNet.DataMapper.Configuration.ResultMapping
|
43
|
|
{
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
[Serializable]
|
48
|
|
[XmlRoot("resultMap")]
|
49
|
|
public class ResultMap
|
50
|
|
{
|
51
|
|
#region Fields
|
52
|
|
[NonSerialized]
|
53
|
|
private string _id = string.Empty;
|
54
|
|
[NonSerialized]
|
55
|
|
private string _className = string.Empty;
|
56
|
|
[NonSerialized]
|
57
|
|
private string _extendMap = string.Empty;
|
58
|
|
[NonSerialized]
|
59
|
|
private Type _class = null;
|
60
|
|
|
61
|
|
[NonSerialized]
|
62
|
|
private Hashtable _columnsToPropertiesMap = new Hashtable();
|
63
|
|
[NonSerialized]
|
64
|
|
private Discriminator _discriminator = null;
|
65
|
|
[NonSerialized]
|
66
|
|
private string _sqlMapNameSpace = string.Empty;
|
67
|
|
#endregion
|
68
|
|
|
69
|
|
#region Properties
|
70
|
|
|
71
|
|
|
72
|
|
|
73
|
|
|
74
|
|
[XmlIgnore]
|
75
|
|
public string SqlMapNameSpace
|
76
|
|
{
|
77
|
680
|
get
|
78
|
|
{
|
79
|
680
|
return _sqlMapNameSpace;
|
80
|
|
}
|
81
|
4930
|
set
|
82
|
|
{
|
83
|
4930
|
_sqlMapNameSpace = value;
|
84
|
|
}
|
85
|
|
}
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
[XmlIgnore]
|
91
|
|
public Discriminator Discriminator
|
92
|
|
{
|
93
|
6630
|
get
|
94
|
|
{
|
95
|
6630
|
return _discriminator;
|
96
|
|
}
|
97
|
340
|
set
|
98
|
|
{
|
99
|
340
|
_discriminator = value;
|
100
|
|
}
|
101
|
|
}
|
102
|
|
|
103
|
|
|
104
|
|
|
105
|
|
|
106
|
|
[XmlIgnore]
|
107
|
|
public Hashtable ColumnsToPropertiesMap
|
108
|
|
{
|
109
|
6954
|
get { return _columnsToPropertiesMap; }
|
110
|
|
}
|
111
|
|
|
112
|
|
|
113
|
|
|
114
|
|
|
115
|
|
|
116
|
|
[XmlAttribute("id")]
|
117
|
|
public string Id
|
118
|
|
{
|
119
|
14790
|
get { return _id; }
|
120
|
9860
|
set
|
121
|
|
{
|
122
|
9860
|
if ((value == null) || (value.Length < 1))
|
123
|
0
|
throw new ArgumentNullException("The id attribute is mandatory in a ResultMap tag.");
|
124
|
|
|
125
|
9860
|
_id = value;
|
126
|
|
}
|
127
|
|
}
|
128
|
|
|
129
|
|
|
130
|
|
|
131
|
|
|
132
|
|
[XmlAttribute("extends")]
|
133
|
|
public string ExtendMap
|
134
|
|
{
|
135
|
16830
|
get { return _extendMap; }
|
136
|
5950
|
set { _extendMap = value; }
|
137
|
|
}
|
138
|
|
|
139
|
|
|
140
|
|
|
141
|
|
|
142
|
0
|
[XmlIgnore]
|
143
|
|
public Type Class
|
144
|
|
{
|
145
|
|
get { return _class; }
|
146
|
|
}
|
147
|
|
|
148
|
|
|
149
|
|
|
150
|
|
|
151
|
|
|
152
|
|
|
153
|
|
[XmlAttribute("class")]
|
154
|
|
public string ClassName
|
155
|
|
{
|
156
|
0
|
get { return _className; }
|
157
|
4930
|
set
|
158
|
|
{
|
159
|
4930
|
if ((value == null) || (value.Length < 1))
|
160
|
0
|
throw new ArgumentNullException("The class attribute is mandatory in a ResultMap tag.");
|
161
|
|
|
162
|
4930
|
_className = value;
|
163
|
|
}
|
164
|
|
}
|
165
|
|
#endregion
|
166
|
|
|
167
|
|
#region Constructor (s) / Destructor
|
168
|
|
|
169
|
|
|
170
|
|
|
171
|
5029
|
public ResultMap()
|
172
|
|
{
|
173
|
|
}
|
174
|
|
#endregion
|
175
|
|
|
176
|
|
#region Methods
|
177
|
|
|
178
|
|
#region Configuration
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
4930
|
public void Initialize( ConfigurationScope configScope )
|
184
|
|
{
|
185
|
4930
|
try
|
186
|
|
{
|
187
|
4930
|
_class = configScope.SqlMapper.GetType(_className);
|
188
|
|
|
189
|
|
|
190
|
4930
|
GetChildNode(configScope);
|
191
|
|
}
|
192
|
|
catch(Exception e)
|
193
|
|
{
|
194
|
0
|
throw new ConfigurationException(
|
195
|
|
string.Format("Could not configure ResultMap. ResultMap named \"{0}\" not found, failed. \n Cause: {1}", _id, e.Message)
|
196
|
|
);
|
197
|
|
}
|
198
|
|
}
|
199
|
|
|
200
|
|
|
201
|
|
|
202
|
|
|
203
|
|
|
204
|
|
|
205
|
4930
|
private void GetChildNode(ConfigurationScope configScope)
|
206
|
|
{
|
207
|
4930
|
XmlSerializer serializer = null;
|
208
|
4930
|
ResultProperty mapping = null;
|
209
|
4930
|
SubMap subMap = null;
|
210
|
|
|
211
|
|
#region Load the Result Properties
|
212
|
|
|
213
|
4930
|
serializer = new XmlSerializer(typeof(ResultProperty));
|
214
|
4930
|
foreach ( XmlNode resultNode in configScope.NodeContext.SelectNodes("result") )
|
215
|
|
{
|
216
|
24480
|
mapping = (ResultProperty) serializer.Deserialize(new XmlNodeReader(resultNode));
|
217
|
|
|
218
|
24480
|
configScope.ErrorContext.MoreInfo = "initialize result property :"+mapping.PropertyName;
|
219
|
|
|
220
|
|
|
221
|
|
|
222
|
|
|
223
|
|
|
224
|
|
|
225
|
|
|
226
|
24480
|
mapping.Initialize( configScope, _class );
|
227
|
|
|
228
|
24480
|
this.AddResultPropery( mapping );
|
229
|
|
}
|
230
|
|
#endregion
|
231
|
|
|
232
|
|
#region Load the Discriminator Property
|
233
|
|
|
234
|
4930
|
serializer = new XmlSerializer(typeof(Discriminator));
|
235
|
4930
|
XmlNode discriminatorNode = configScope.NodeContext.SelectSingleNode("discriminator");
|
236
|
4930
|
if (discriminatorNode != null)
|
237
|
|
{
|
238
|
340
|
configScope.ErrorContext.MoreInfo = "initialize discriminator";
|
239
|
|
|
240
|
340
|
this.Discriminator = (Discriminator) serializer.Deserialize(new XmlNodeReader(discriminatorNode));
|
241
|
|
|
242
|
340
|
this.Discriminator.SetMapping( configScope, _class );
|
243
|
|
}
|
244
|
|
#endregion
|
245
|
|
|
246
|
|
#region Load the SubMap Properties
|
247
|
|
|
248
|
4930
|
serializer = new XmlSerializer(typeof(SubMap));
|
249
|
4930
|
if (configScope.NodeContext.SelectNodes("subMap").Count>0 && this.Discriminator==null)
|
250
|
|
{
|
251
|
0
|
throw new ConfigurationException("The discriminator is null, but somehow a subMap was reached. This is a bug.");
|
252
|
|
}
|
253
|
4930
|
foreach ( XmlNode resultNode in configScope.NodeContext.SelectNodes("subMap") )
|
254
|
|
{
|
255
|
680
|
configScope.ErrorContext.MoreInfo = "initialize subMap";
|
256
|
680
|
subMap = (SubMap) serializer.Deserialize(new XmlNodeReader(resultNode));
|
257
|
680
|
subMap.ResultMapName = this.SqlMapNameSpace + DomSqlMapBuilder.DOT + subMap.ResultMapName;
|
258
|
680
|
this.Discriminator.Add( subMap );
|
259
|
|
}
|
260
|
|
#endregion
|
261
|
|
}
|
262
|
|
|
263
|
|
#endregion
|
264
|
|
|
265
|
|
|
266
|
|
|
267
|
|
|
268
|
|
|
269
|
232
|
public object CreateInstanceOfResult()
|
270
|
|
{
|
271
|
232
|
TypeCode typeCode = Type.GetTypeCode(_class);
|
272
|
|
|
273
|
232
|
if (typeCode == TypeCode.Object)
|
274
|
|
{
|
275
|
210
|
return Activator.CreateInstance(_class);
|
276
|
|
}
|
277
|
|
else
|
278
|
|
{
|
279
|
22
|
return TypeAliasResolver.InstantiatePrimitiveType(typeCode);
|
280
|
|
}
|
281
|
|
}
|
282
|
|
|
283
|
|
|
284
|
|
|
285
|
|
|
286
|
|
|
287
|
31578
|
public void AddResultPropery(ResultProperty property)
|
288
|
|
{
|
289
|
31578
|
_columnsToPropertiesMap.Add( property.PropertyName, property );
|
290
|
|
}
|
291
|
|
|
292
|
|
|
293
|
|
|
294
|
|
|
295
|
|
|
296
|
|
|
297
|
|
|
298
|
1207
|
public void SetValueOfProperty( ref object target, ResultProperty property, object dataBaseValue )
|
299
|
|
{
|
300
|
1207
|
if (target is Hashtable)
|
301
|
|
{
|
302
|
33
|
((Hashtable) target).Add(property.PropertyName, dataBaseValue);
|
303
|
|
}
|
304
|
|
else
|
305
|
|
{
|
306
|
1174
|
if ( target.GetType() != _class )
|
307
|
|
{
|
308
|
0
|
throw new ArgumentException( "Could not set value of type '"+ target.GetType() +"' in property '"+property.PropertyName+"' of type '"+_class+"'" );
|
309
|
|
}
|
310
|
|
|
311
|
1174
|
if ( property.PropertyInfo != null )
|
312
|
|
{
|
313
|
1152
|
property.PropertyInfo.SetValue( target, dataBaseValue, null );
|
314
|
|
}
|
315
|
|
else
|
316
|
|
{
|
317
|
22
|
target = dataBaseValue;
|
318
|
|
}
|
319
|
|
}
|
320
|
|
}
|
321
|
|
|
322
|
|
|
323
|
|
|
324
|
|
|
325
|
|
|
326
|
|
|
327
|
244
|
public ResultMap ResolveSubMap(IDataReader dataReader)
|
328
|
|
{
|
329
|
244
|
ResultMap subMap = this;
|
330
|
244
|
if (_discriminator != null)
|
331
|
|
{
|
332
|
18
|
ResultProperty mapping = _discriminator.ResultProperty;
|
333
|
18
|
object dataBaseValue = mapping.GetDataBaseValue( dataReader );
|
334
|
18
|
subMap = _discriminator.GetSubMap( dataBaseValue.ToString() );
|
335
|
|
|
336
|
18
|
if (subMap == null)
|
337
|
|
{
|
338
|
6
|
subMap = this;
|
339
|
|
}
|
340
|
12
|
else if (subMap != this)
|
341
|
|
{
|
342
|
12
|
subMap = subMap.ResolveSubMap(dataReader);
|
343
|
|
}
|
344
|
|
}
|
345
|
244
|
return subMap;
|
346
|
|
}
|
347
|
|
|
348
|
|
|
349
|
|
#endregion
|
350
|
|
}
|
351
|
|
}
|
352
|
|
|