Clover.NET coverage report - Coverage

Coverage timestamp: Friday, May 20, 2005 9:17:00 PM

File Stats: LOC: 336   Methods: 30
NCLOC: 199 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Configuration\ResultMapping\ResultProperty.cs 100.0 % 91.8 % 90.0 % 92.1 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * iBATIS.NET Data Mapper
9   * Copyright (C) 2004 - Gilles Bayon
10   *
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   * http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
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   /// <summary>
44   /// Summary description for ResultProperty.
45   /// </summary>
46   [Serializable]
47   [XmlRoot("result")]
48   public class ResultProperty
49   {
50   #region Const
51  
52   /// <summary>
53   ///
54   /// </summary>
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   /// <summary>
91   /// Specify the custom type handlers to used.
92   /// </summary>
93   /// <remarks>Will be an alias to a class wchic implement ITypeHandlerCallback</remarks>
94   [XmlAttribute("typeHandler")]
95   public string CallBackName
96   {
97 25840 get { return _callBackName; }
98 680 set { _callBackName = value; }
99   }
100  
101   /// <summary>
102   /// Tell us if we must lazy load this property..
103   /// </summary>
104   [XmlAttribute("lazyLoad")]
105   public bool IsLazyLoad
106   {
107 38 get { return _isLazyLoad; }
108 510 set { _isLazyLoad = value; }
109   }
110  
111   /// <summary>
112   /// The typeHandler used to work with the result property.
113   /// </summary>
114   [XmlIgnore]
115   public ITypeHandler TypeHandler
116   {
117 1237 get { return _typeHandler; }
118 298 set { _typeHandler = value; }
119   }
120  
121   /// <summary>
122   /// Give an entry in the 'DbType' enumeration
123   /// </summary>
124   /// <example >
125   /// For Sql Server, give an entry of SqlDbType : Bit, Decimal, Money...
126   /// <br/>
127   /// For Oracle, give an OracleType Enumeration : Byte, Int16, Number...
128   /// </example>
129   [XmlAttribute("dbType")]
130   public string DbType
131   {
132 0 get { return _dbType; }
133 2040 set { _dbType = value; }
134   }
135  
136  
137   /// <summary>
138   /// Specify the CLR type of the result.
139   /// </summary>
140   /// <remarks>
141   /// The type attribute is used to explicitly specify the property type of the property to be set.
142   /// Normally this can be derived from a property through reflection, but certain mappings such as
143   /// HashTable cannot provide the type to the framework.
144   /// </remarks>
145   [XmlAttribute("type")]
146   public string CLRType
147   {
148 0 get { return _clrType; }
149 2040 set { _clrType = value; }
150   }
151  
152   /// <summary>
153   /// Column Index
154   /// </summary>
155   [XmlAttribute("columnIndex")]
156   public int ColumnIndex
157   {
158 311 get { return _columnIndex; }
159 2580 set { _columnIndex = value; }
160   }
161  
162   /// <summary>
163   /// The name of the statement to retrieve the property
164   /// </summary>
165   [XmlAttribute("select")]
166   public string Select
167   {
168 1260 get { return _select; }
169 1700 set { _select = value; }
170   }
171  
172   /// <summary>
173   /// The name of a nested ResultMap to set the property
174   /// </summary>
175   [XmlAttribute("resultMapping")]
176   public string NestedResultMapName
177   {
178 31450 get { return _nestedResultMapName; }
179 170 set { _nestedResultMapName = value; }
180   }
181  
182   /// <summary>
183   /// Column Name
184   /// </summary>
185   [XmlAttribute("column")]
186   public string ColumnName
187   {
188 1733 get { return _columnName; }
189 24918 set { _columnName = value; }
190   }
191  
192   /// <summary>
193   /// The property name used to identify the property amongst the others.
194   /// </summary>
195   [XmlAttribute("property")]
196   public string PropertyName
197   {
198 57039 get { return _propertyName; }
199 24778 set { _propertyName = value; }
200   }
201  
202   /// <summary>
203   ///
204   /// </summary>
205   [XmlIgnore]
206   public PropertyInfo PropertyInfo
207   {
208 3624 get { return _propertyInfo; }
209   }
210  
211   /// <summary>
212   /// Tell if a nullValue is defined.
213   /// </summary>
214   [XmlIgnore]
215   public bool HasNullValue
216   {
217 102 get { return (_nullValue.Length>0); }
218   }
219  
220   /// <summary>
221   /// Null value replacement.
222   /// </summary>
223   /// <example>"no_email@provided.com"</example>
224   [XmlAttribute("nullValue")]
225   public string NullValue
226   {
227 0 get { return _nullValue; }
228 1190 set { _nullValue = value; }
229   }
230  
231   /// <summary>
232   /// A nested ResultMap use to set a property
233   /// </summary>
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   /// <summary>
244   /// Do not use direclty, only for serialization.
245   /// </summary>
246 25118 public ResultProperty()
247   {
248   }
249   #endregion
250  
251   #region Methods
252  
253   /// <summary>
254   /// Initialize the PropertyInfo of the result property.
255   /// </summary>
256   /// <param name="resultClass"></param>
257   /// <param name="configScope"></param>
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   /// <summary>
287   /// Initialize the PropertyInfo of the result property
288   /// for AutoMapper
289   /// </summary>
290   /// <param name="propertyInfo">A PropertyInfoot.</param>
291   /// <param name="typeHandlerFactory"></param>
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   /// <summary>
300   ///
301   /// </summary>
302   /// <param name="dataReader"></param>
303   /// <returns></returns>
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