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.Serialization;
|
34
|
|
using IBatisNet.Common.Exceptions;
|
35
|
|
using IBatisNet.Common.Utilities.Objects;
|
36
|
|
using IBatisNet.DataMapper.Scope;
|
37
|
|
using IBatisNet.DataMapper.TypeHandlers;
|
38
|
|
|
39
|
|
#endregion
|
40
|
|
|
41
|
|
namespace IBatisNet.DataMapper.Configuration.ResultMapping
|
42
|
|
{
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
[Serializable]
|
47
|
|
[XmlRoot("result")]
|
48
|
|
public class ResultProperty
|
49
|
|
{
|
50
|
|
#region Const
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
public const int UNKNOWN_COLUMN_INDEX = -999999;
|
56
|
|
|
57
|
|
#endregion
|
58
|
|
|
59
|
|
#region Fields
|
60
|
|
[NonSerialized]
|
61
|
|
private PropertyInfo _propertyInfo;
|
62
|
|
[NonSerialized]
|
63
|
|
private string _nullValue = string.Empty;
|
64
|
|
[NonSerialized]
|
65
|
|
private string _propertyName = string.Empty;
|
66
|
|
[NonSerialized]
|
67
|
|
private string _columnName = string.Empty;
|
68
|
|
[NonSerialized]
|
69
|
|
private int _columnIndex = UNKNOWN_COLUMN_INDEX;
|
70
|
|
[NonSerialized]
|
71
|
|
private string _select = string.Empty;
|
72
|
|
[NonSerialized]
|
73
|
|
private string _nestedResultMapName = string.Empty;
|
74
|
|
[NonSerialized]
|
75
|
|
private ResultMap _nestedResultMap = null;
|
76
|
|
[NonSerialized]
|
77
|
|
private string _dbType = null;
|
78
|
|
[NonSerialized]
|
79
|
|
private string _clrType = string.Empty;
|
80
|
|
[NonSerialized]
|
81
|
|
private bool _isLazyLoad = false;
|
82
|
|
[NonSerialized]
|
83
|
|
private ITypeHandler _typeHandler = null;
|
84
|
|
[NonSerialized]
|
85
|
|
private string _callBackName= string.Empty;
|
86
|
|
#endregion
|
87
|
|
|
88
|
|
#region Properties
|
89
|
|
|
90
|
|
|
91
|
|
|
92
|
|
|
93
|
|
|
94
|
|
[XmlAttribute("typeHandler")]
|
95
|
|
public string CallBackName
|
96
|
|
{
|
97
|
25840
|
get { return _callBackName; }
|
98
|
680
|
set { _callBackName = value; }
|
99
|
|
}
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
|
[XmlAttribute("lazyLoad")]
|
105
|
|
public bool IsLazyLoad
|
106
|
|
{
|
107
|
38
|
get { return _isLazyLoad; }
|
108
|
510
|
set { _isLazyLoad = value; }
|
109
|
|
}
|
110
|
|
|
111
|
|
|
112
|
|
|
113
|
|
|
114
|
|
[XmlIgnore]
|
115
|
|
public ITypeHandler TypeHandler
|
116
|
|
{
|
117
|
1237
|
get { return _typeHandler; }
|
118
|
298
|
set { _typeHandler = value; }
|
119
|
|
}
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
|
|
126
|
|
|
127
|
|
|
128
|
|
|
129
|
|
[XmlAttribute("dbType")]
|
130
|
|
public string DbType
|
131
|
|
{
|
132
|
0
|
get { return _dbType; }
|
133
|
2040
|
set { _dbType = value; }
|
134
|
|
}
|
135
|
|
|
136
|
|
|
137
|
|
|
138
|
|
|
139
|
|
|
140
|
|
|
141
|
|
|
142
|
|
|
143
|
|
|
144
|
|
|
145
|
|
[XmlAttribute("type")]
|
146
|
|
public string CLRType
|
147
|
|
{
|
148
|
0
|
get { return _clrType; }
|
149
|
2040
|
set { _clrType = value; }
|
150
|
|
}
|
151
|
|
|
152
|
|
|
153
|
|
|
154
|
|
|
155
|
|
[XmlAttribute("columnIndex")]
|
156
|
|
public int ColumnIndex
|
157
|
|
{
|
158
|
311
|
get { return _columnIndex; }
|
159
|
2580
|
set { _columnIndex = value; }
|
160
|
|
}
|
161
|
|
|
162
|
|
|
163
|
|
|
164
|
|
|
165
|
|
[XmlAttribute("select")]
|
166
|
|
public string Select
|
167
|
|
{
|
168
|
1260
|
get { return _select; }
|
169
|
1700
|
set { _select = value; }
|
170
|
|
}
|
171
|
|
|
172
|
|
|
173
|
|
|
174
|
|
|
175
|
|
[XmlAttribute("resultMapping")]
|
176
|
|
public string NestedResultMapName
|
177
|
|
{
|
178
|
31450
|
get { return _nestedResultMapName; }
|
179
|
170
|
set { _nestedResultMapName = value; }
|
180
|
|
}
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
|
[XmlAttribute("column")]
|
186
|
|
public string ColumnName
|
187
|
|
{
|
188
|
1733
|
get { return _columnName; }
|
189
|
24918
|
set { _columnName = value; }
|
190
|
|
}
|
191
|
|
|
192
|
|
|
193
|
|
|
194
|
|
|
195
|
|
[XmlAttribute("property")]
|
196
|
|
public string PropertyName
|
197
|
|
{
|
198
|
57039
|
get { return _propertyName; }
|
199
|
24778
|
set { _propertyName = value; }
|
200
|
|
}
|
201
|
|
|
202
|
|
|
203
|
|
|
204
|
|
|
205
|
|
[XmlIgnore]
|
206
|
|
public PropertyInfo PropertyInfo
|
207
|
|
{
|
208
|
3624
|
get { return _propertyInfo; }
|
209
|
|
}
|
210
|
|
|
211
|
|
|
212
|
|
|
213
|
|
|
214
|
|
[XmlIgnore]
|
215
|
|
public bool HasNullValue
|
216
|
|
{
|
217
|
102
|
get { return (_nullValue.Length>0); }
|
218
|
|
}
|
219
|
|
|
220
|
|
|
221
|
|
|
222
|
|
|
223
|
|
|
224
|
|
[XmlAttribute("nullValue")]
|
225
|
|
public string NullValue
|
226
|
|
{
|
227
|
0
|
get { return _nullValue; }
|
228
|
1190
|
set { _nullValue = value; }
|
229
|
|
}
|
230
|
|
|
231
|
|
|
232
|
|
|
233
|
|
|
234
|
|
[XmlIgnore]
|
235
|
|
public ResultMap NestedResultMap
|
236
|
|
{
|
237
|
1263
|
get { return _nestedResultMap; }
|
238
|
170
|
set { _nestedResultMap = value; }
|
239
|
|
}
|
240
|
|
#endregion
|
241
|
|
|
242
|
|
#region Constructor (s) / Destructor
|
243
|
|
|
244
|
|
|
245
|
|
|
246
|
25118
|
public ResultProperty()
|
247
|
|
{
|
248
|
|
}
|
249
|
|
#endregion
|
250
|
|
|
251
|
|
#region Methods
|
252
|
|
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
|
|
258
|
24820
|
public void Initialize( ConfigurationScope configScope, Type resultClass )
|
259
|
|
{
|
260
|
24820
|
if ( _propertyName.Length>0 &&_propertyName != "value" && !typeof(IDictionary).IsAssignableFrom(resultClass) )
|
261
|
|
{
|
262
|
21760
|
_propertyInfo = ReflectionInfo.GetInstance(resultClass).GetSetter( _propertyName );
|
263
|
|
}
|
264
|
|
|
265
|
24820
|
if (this.CallBackName.Length >0)
|
266
|
|
{
|
267
|
510
|
configScope.ErrorContext.MoreInfo = "Result property '"+_propertyName+"' check the typeHandler attribute '" + this.CallBackName + "' (must be a ITypeHandlerCallback implementation).";
|
268
|
510
|
try
|
269
|
|
{
|
270
|
510
|
Type type = configScope.SqlMapper.GetType(this.CallBackName);
|
271
|
510
|
ITypeHandlerCallback typeHandlerCallback = (ITypeHandlerCallback) Activator.CreateInstance( type );
|
272
|
510
|
_typeHandler = new CustomTypeHandler(typeHandlerCallback);
|
273
|
|
}
|
274
|
|
catch (Exception e)
|
275
|
|
{
|
276
|
0
|
throw new ConfigurationException("Error occurred during custom type handler configuration. Cause: " + e.Message, e);
|
277
|
|
}
|
278
|
|
}
|
279
|
|
else
|
280
|
|
{
|
281
|
24310
|
configScope.ErrorContext.MoreInfo = "Result property '"+_propertyName+"' set the typeHandler attribute.";
|
282
|
24310
|
_typeHandler = configScope.ResolveTypeHandler( resultClass, _propertyName, _clrType, _dbType);
|
283
|
|
}
|
284
|
|
}
|
285
|
|
|
286
|
|
|
287
|
|
|
288
|
|
|
289
|
|
|
290
|
|
|
291
|
|
|
292
|
268
|
internal void Initialize(TypeHandlerFactory typeHandlerFactory, PropertyInfo propertyInfo )
|
293
|
|
{
|
294
|
268
|
_propertyInfo = propertyInfo;
|
295
|
|
|
296
|
268
|
_typeHandler = typeHandlerFactory.GetTypeHandler(propertyInfo.PropertyType);
|
297
|
|
}
|
298
|
|
|
299
|
|
|
300
|
|
|
301
|
|
|
302
|
|
|
303
|
|
|
304
|
1870
|
public object GetDataBaseValue(IDataReader dataReader)
|
305
|
|
{
|
306
|
1870
|
object value = null;
|
307
|
|
|
308
|
1870
|
if (_columnIndex == UNKNOWN_COLUMN_INDEX)
|
309
|
|
{
|
310
|
1711
|
value = _typeHandler.GetValueByName(this, dataReader);
|
311
|
|
}
|
312
|
|
else
|
313
|
|
{
|
314
|
159
|
value = _typeHandler.GetValueByIndex(this, dataReader);
|
315
|
|
}
|
316
|
|
|
317
|
1870
|
bool wasNull = (value == DBNull.Value);
|
318
|
1870
|
if (wasNull)
|
319
|
|
{
|
320
|
102
|
if (this.HasNullValue)
|
321
|
|
{
|
322
|
45
|
value = _typeHandler.ValueOf(_propertyInfo.PropertyType, _nullValue);
|
323
|
|
}
|
324
|
|
else
|
325
|
|
{
|
326
|
57
|
value = null;
|
327
|
|
}
|
328
|
|
}
|
329
|
|
|
330
|
1870
|
return value;
|
331
|
|
}
|
332
|
|
#endregion
|
333
|
|
}
|
334
|
|
|
335
|
|
}
|
336
|
|
|