Clover.NET coverage report - Coverage

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

File Stats: LOC: 633   Methods: 52
NCLOC: 370 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Provider.cs 46.7 % 69.7 % 76.9 % 68.0 %
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 Using
28  
29   using System;
30   using System.Data;
31   using System.Reflection;
32   using System.Xml.Serialization;
33   using IBatisNet.Common.Exceptions;
34   using IBatisNet.Common.Utilities.TypesResolver;
35   //using log4net;
36  
37   #endregion
38  
39   namespace IBatisNet.Common
40   {
41   /// <summary>
42   /// Information about a data provider.
43   /// </summary>
44   [Serializable]
45   [XmlRoot("provider")]
46   public class Provider
47   {
48   #region Fields
49   [NonSerialized]
50   private string _assemblyName = string.Empty;
51   [NonSerialized]
52   private string _connectionClass = string.Empty;
53   [NonSerialized]
54   private string _commandClass = string.Empty;
55   [NonSerialized]
56   private string _dataParameter = string.Empty;
57   [NonSerialized]
58   private Type _dataParameterType = null;
59  
60   [NonSerialized]
61   private string _parameterDbTypeClass = string.Empty;
62   [NonSerialized]
63   private Type _parameterDbType = null;
64  
65   [NonSerialized]
66   private string _parameterDbTypeProperty = string.Empty;
67   [NonSerialized]
68   private string _dataAdapterClass = string.Empty;
69   [NonSerialized]
70   private string _commandBuilderClass = string.Empty;
71  
72   [NonSerialized]
73   private string _name = string.Empty;
74   [NonSerialized]
75   private string _description = string.Empty;
76   [NonSerialized]
77   private bool _isDefault = false;
78   [NonSerialized]
79   private bool _isEnabled = true;
80   [NonSerialized]
81   private IDbConnection _templateConnection = null;
82   [NonSerialized]
83   private IDbCommand _templateCommand= null;
84   [NonSerialized]
85   private IDbDataAdapter _templateDataAdapter= null;
86   [NonSerialized]
87   private Type _commandBuilderType = null;
88   [NonSerialized]
89   private string _parameterPrefix = string.Empty;
90   [NonSerialized]
91   private bool _useParameterPrefixInSql = true;
92   [NonSerialized]
93   private bool _useParameterPrefixInParameter = true;
94   [NonSerialized]
95   private bool _usePositionalParameters = false;
96   [NonSerialized]
97   private bool _templateConnectionIsICloneable = false;
98   [NonSerialized]
99   private bool _templateCommandIsICloneable = false;
100   [NonSerialized]
101   private bool _templateDataAdapterIsICloneable = false;
102   [NonSerialized]
103   private bool _setDbParameterSize = true;
104   [NonSerialized]
105   private bool _setDbParameterPrecision = true;
106   [NonSerialized]
107   private bool _setDbParameterScale = true;
108   [NonSerialized]
109   private bool _useDeriveParameters = true;
110  
111   // private static readonly ILog _connectionLogger = LogManager.GetLogger("System.Data.IDbConnection");
112  
113   #endregion
114  
115   #region Properties
116   /// <summary>
117   /// The name of the assembly which conatins the definition of the provider.
118   /// </summary>
119   /// <example>Examples : "System.Data", "Microsoft.Data.Odbc"</example>
120   [XmlAttribute("assemblyName")]
121   public string AssemblyName
122   {
123 0 get { return _assemblyName; }
124 2040 set
125   {
126 2040 if ((value == null) || (value.Length < 1))
127 0 throw new ArgumentNullException("AssemblyName");
128 2040 _assemblyName = value;
129   }
130   }
131  
132  
133   /// <summary>
134   /// Tell us if it is the default data source.
135   /// Default false.
136   /// </summary>
137   [XmlAttribute("default")]
138   public bool IsDefault
139   {
140 1360 get { return _isDefault; }
141 340 set {_isDefault = value;}
142   }
143  
144  
145   /// <summary>
146   /// Tell us if this provider is enabled.
147   /// Default true.
148   /// </summary>
149   [XmlAttribute("enabled")]
150   public bool IsEnabled
151   {
152 2040 get { return _isEnabled; }
153 1870 set {_isEnabled = value;}
154   }
155  
156  
157   /// <summary>
158   /// The connection class name to use.
159   /// </summary>
160   /// <example>
161   /// "System.Data.OleDb.OleDbConnection",
162   /// "System.Data.SqlClient.SqlConnection",
163   /// "Microsoft.Data.Odbc.OdbcConnection"
164   /// </example>
165   [XmlAttribute("connectionClass")]
166   public string ConnectionClass
167   {
168 0 get { return _connectionClass; }
169 2040 set
170   {
171 2040 if ((value == null) || (value.Length < 1))
172 0 throw new ArgumentNullException("The connectionClass attribute is mandatory in the provider " + _name);
173 2040 _connectionClass = value;
174   }
175   }
176  
177   /// <summary>
178   /// Does this ConnectionProvider require the use of a Named Prefix in the SQL
179   /// statement.
180   /// </summary>
181   /// <remarks>
182   /// The OLE DB/ODBC .NET Provider does not support named parameters for
183   /// passing parameters to an SQL Statement or a stored procedure called
184   /// by an IDbCommand when CommandType is set to Text.
185   ///
186   /// For example, SqlClient requires select * from simple where simple_id = @simple_id
187   /// If this is false, like with the OleDb or Obdc provider, then it is assumed that
188   /// the ? can be a placeholder for the parameter in the SQL statement when CommandType
189   /// is set to Text.
190   /// </remarks>
191   [XmlAttribute("useParameterPrefixInSql")]
192   public bool UseParameterPrefixInSql
193   {
194 0 get { return _useParameterPrefixInSql; }
195 2040 set { _useParameterPrefixInSql = value;}
196   }
197  
198   /// <summary>
199   /// Does this ConnectionProvider require the use of the Named Prefix when trying
200   /// to reference the Parameter in the Command's Parameter collection.
201   /// </summary>
202   /// <remarks>
203   /// This is really only useful when the UseParameterPrefixInSql = true.
204   /// When this is true the code will look like IDbParameter param = cmd.Parameters["@paramName"],
205   /// if this is false the code will be IDbParameter param = cmd.Parameters["paramName"] - ie - Oracle.
206   /// </remarks>
207   [XmlAttribute("useParameterPrefixInParameter")]
208   public bool UseParameterPrefixInParameter
209   {
210 64909 get { return _useParameterPrefixInParameter; }
211 2040 set { _useParameterPrefixInParameter = value; }
212   }
213  
214   /// <summary>
215   /// The OLE DB/OBDC .NET Provider uses positional parameters that are marked with a
216   /// question mark (?) instead of named parameters.
217   /// </summary>
218   [XmlAttribute("usePositionalParameters")]
219   public bool UsePositionalParameters
220   {
221 122732 get { return _usePositionalParameters; }
222 2040 set { _usePositionalParameters = value; }
223   }
224  
225   /// <summary>
226   /// Used to indicate whether or not the provider
227   /// supports parameter size.
228   /// </summary>
229   /// <remarks>
230   /// See JIRA-49 about SQLite.Net provider not supporting parameter size.
231   /// </remarks>
232   [XmlAttribute("setDbParameterSize")]
233   public bool SetDbParameterSize
234   {
235 32683 get { return _setDbParameterSize; }
236 170 set { _setDbParameterSize = value; }
237   }
238  
239   /// <summary>
240   /// Used to indicate whether or not the provider
241   /// supports parameter precision.
242   /// </summary>
243   /// <remarks>
244   /// See JIRA-49 about SQLite.Net provider not supporting parameter precision.
245   /// </remarks>
246   [XmlAttribute("setDbParameterPrecision")]
247   public bool SetDbParameterPrecision
248   {
249 32683 get { return _setDbParameterPrecision; }
250 170 set { _setDbParameterPrecision = value; }
251   }
252  
253   /// <summary>
254   /// Used to indicate whether or not the provider
255   /// supports a parameter scale.
256   /// </summary>
257   /// <remarks>
258   /// See JIRA-49 about SQLite.Net provider not supporting parameter scale.
259   /// </remarks>
260   [XmlAttribute("setDbParameterScale")]
261   public bool SetDbParameterScale
262   {
263 32683 get { return _setDbParameterScale; }
264 170 set { _setDbParameterScale = value; }
265   }
266  
267   /// <summary>
268   /// Used to indicate whether or not the provider
269   /// supports DeriveParameters method for procedure.
270   /// </summary>
271   [XmlAttribute("useDeriveParameters")]
272   public bool UseDeriveParameters
273   {
274 6 get { return _useDeriveParameters; }
275 340 set { _useDeriveParameters = value; }
276   }
277  
278   /// <summary>
279   /// The command class name to use.
280   /// </summary>
281   /// <example>
282   /// "System.Data.SqlClient.SqlCommand"
283   /// </example>
284   [XmlAttribute("commandClass")]
285   public string CommandClass
286   {
287 0 get { return _commandClass; }
288 2040 set
289   {
290 2040 if ((value == null) || (value.Length < 1))
291 0 throw new ArgumentNullException("The commandClass attribute is mandatory in the provider " + _name);
292 2040 _commandClass = value;
293   }
294   }
295  
296  
297   /// <summary>
298   /// The parameter class name to use.
299   /// </summary>
300   /// <example>
301   /// "System.Data.SqlClient.SqlParameter"
302   /// </example>
303   [XmlAttribute("parameterClass")]
304   public string ParameterClass
305   {
306 0 get { return _dataParameter; }
307 2040 set
308   {
309 2040 if ((value == null) || (value.Length < 1))
310 0 throw new ArgumentNullException("The parameterClass attribute is mandatory in the provider " + _name);
311 2040 _dataParameter = value;
312   }
313   }
314  
315  
316   /// <summary>
317   /// The ParameterDbType class name to use.
318   /// </summary>
319   /// <example>
320   /// "System.Data.SqlDbType"
321   /// </example>
322   [XmlAttribute("parameterDbTypeClass")]
323   public string ParameterDbTypeClass
324   {
325 0 get { return _parameterDbTypeClass; }
326 2040 set
327   {
328 2040 if ((value == null) || (value.Length < 1))
329 0 throw new ArgumentNullException("The parameterDbTypeClass attribute is mandatory in the provider " + _name);
330 2040 _parameterDbTypeClass = value;
331   }
332   }
333  
334  
335   /// <summary>
336   /// The ParameterDbTypeProperty class name to use.
337   /// </summary>
338   /// <example >
339   /// SqlDbType in SqlParamater.SqlDbType,
340   /// OracleType in OracleParameter.OracleType.
341   /// </example>
342   [XmlAttribute("parameterDbTypeProperty")]
343   public string ParameterDbTypeProperty
344   {
345 16263 get { return _parameterDbTypeProperty; }
346 2040 set
347   {
348 2040 if ((value == null) || (value.Length < 1))
349 0 throw new ArgumentNullException("The parameterDbTypeProperty attribute is mandatory in the provider " + _name);
350 2040 _parameterDbTypeProperty = value;
351   }
352   }
353  
354   /// <summary>
355   /// The dataAdapter class name to use.
356   /// </summary>
357   /// <example >
358   /// "System.Data.SqlDbType"
359   /// </example>
360   [XmlAttribute("dataAdapterClass")]
361   public string DataAdapterClass
362   {
363 0 get { return _dataAdapterClass; }
364 2040 set
365   {
366 2040 if ((value == null) || (value.Length < 1))
367 0 throw new ArgumentNullException("The dataAdapterClass attribute is mandatory in the provider " + _name);
368 2040 _dataAdapterClass = value;
369   }
370   }
371  
372   /// <summary>
373   /// The commandBuilder class name to use.
374   /// </summary>
375   /// <example >
376   /// "System.Data.OleDb.OleDbCommandBuilder",
377   /// "System.Data.SqlClient.SqlCommandBuilder",
378   /// "Microsoft.Data.Odbc.OdbcCommandBuilder"
379   /// </example>
380   [XmlAttribute("commandBuilderClass")]
381   public string CommandBuilderClass
382   {
383 8 get { return _commandBuilderClass; }
384 2040 set
385   {
386 2040 if ((value == null) || (value.Length < 1))
387 0 throw new ArgumentNullException("The commandBuilderClass attribute is mandatory in the provider " + _name);
388 2040 _commandBuilderClass = value;
389   }
390   }
391  
392  
393   /// <summary>
394   /// Name used to identify the provider amongst the others.
395   /// </summary>
396   [XmlAttribute("name")]
397   public string Name
398   {
399 36467 get { return _name; }
400 2040 set
401   {
402 2040 if ((value == null) || (value.Length < 1))
403 0 throw new ArgumentNullException("The name attribute is mandatory in the provider.");
404  
405 2040 _name = value;
406   }
407   }
408  
409   /// <summary>
410   /// Description.
411   /// </summary>
412   [XmlAttribute("description")]
413   public string Description
414   {
415 566 get { return _description; }
416 2040 set { _description = value;}
417   }
418  
419   /// <summary>
420   /// Parameter prefix use in store procedure.
421   /// </summary>
422   /// <example> @ for Sql Server.</example>
423   [XmlAttribute("parameterPrefix")]
424   public string ParameterPrefix
425   {
426 20471 get { return _parameterPrefix; }
427 2040 set
428   {
429 2040 if ((value == null) || (value.Length < 1))
430   {
431 340 _parameterPrefix = "";
432   //throw new ArgumentNullException("The parameterPrefix attribute is mandatory in the provider " + _name);
433   }
434   else
435   {
436 1700 _parameterPrefix = value;
437   }
438   }
439   }
440  
441   /// <summary>
442   /// Check if this provider is Odbc ?
443   /// </summary>
444   [XmlIgnore]
445   public bool IsObdc
446   {
447 6 get
448   {
449 6 return (_connectionClass.IndexOf(".Odbc.")>0);
450   }
451   }
452   #endregion
453  
454   #region Constructor (s) / Destructor
455   /// <summary>
456   /// Do not use direclty, only for serialization.
457   /// </summary>
458 2040 [Obsolete("This public constructor with no parameter is not really obsolete, but is reserved for serialization.", false)]
459   public Provider()
460   {
461   }
462   #endregion
463  
464   #region Methods
465   /// <summary>
466   /// Init the provider.
467   /// </summary>
468 1360 public void Initialisation()
469   {
470 1360 Assembly assembly;
471 1360 Type type;
472 1360 CachedTypeResolver cachedTypeResolver = new CachedTypeResolver();
473  
474 1360 try
475   {
476 1360 assembly = Assembly.Load(_assemblyName);
477  
478   // Build the Command template
479 1360 type = assembly.GetType(_commandClass, true);
480 1360 _templateCommand = (IDbCommand)type.GetConstructor(Type.EmptyTypes).Invoke(null);
481   // Build the DataAdapter template
482 1360 type = assembly.GetType(_dataAdapterClass, true);
483 1360 _templateDataAdapter = (IDbDataAdapter)type.GetConstructor(Type.EmptyTypes).Invoke(null);
484   // Build the connection template
485 1360 type = assembly.GetType(_connectionClass, true);
486 1360 _templateConnection = (IDbConnection)type.GetConstructor(Type.EmptyTypes).Invoke(null);
487   // Get the IDataParameter type
488 1360 _dataParameterType = assembly.GetType(_dataParameter, true);
489   // Get the CommandBuilder Type
490 1360 _commandBuilderType = assembly.GetType(_commandBuilderClass, true);
491 1360 if (_parameterDbTypeClass.IndexOf(',')>0)
492   {
493 0 _parameterDbType = cachedTypeResolver.Resolve(_parameterDbTypeClass);
494   }
495   else
496   {
497 1360 _parameterDbType = assembly.GetType(_parameterDbTypeClass, true);
498   }
499  
500 1360 _templateConnectionIsICloneable = _templateConnection is ICloneable;
501 1360 _templateCommandIsICloneable = _templateCommand is ICloneable;
502 1360 _templateDataAdapterIsICloneable = _templateDataAdapter is ICloneable;
503   }
504   catch(Exception e)
505   {
506 0 throw new ConfigurationException(
507   string.Format("Could not configure providers. Provider named \"{0}\" not found, failed. Cause: {1}", _name, e.Message)
508   );
509   }
510   }
511  
512   /// <summary>
513   /// Get a connection object for this provider.
514   /// </summary>
515   /// <returns>An 'IDbConnection' object.</returns>
516 995 public IDbConnection GetConnection()
517   {
518   // Cannot do that because on
519   // IDbCommand.Connection = cmdConnection
520   // .NET cast the cmdConnection to the real type (as SqlConnection)
521   // and we pass a proxy --> exception invalid cast !
522   // if (_connectionLogger.IsDebugEnabled)
523   // {
524   // connection = (IDbConnection)IDbConnectionProxy.NewInstance(connection, this);
525   // }
526 995 if (_templateConnectionIsICloneable)
527   {
528 995 return (IDbConnection) ((ICloneable)_templateConnection).Clone();
529   }
530   else
531   {
532 0 return (IDbConnection) Activator.CreateInstance(_templateConnection.GetType());
533   }
534   }
535  
536   /// <summary>
537   /// Get a command object for this provider.
538   /// </summary>
539   /// <returns>An 'IDbCommand' object.</returns>
540 33193 public IDbCommand GetCommand()
541   {
542 33193 if (_templateCommandIsICloneable)
543   {
544 33193 return (IDbCommand) ((ICloneable)_templateCommand).Clone();
545   }
546   else
547   {
548 0 return (IDbCommand) Activator.CreateInstance(_templateCommand.GetType());
549   }
550   }
551  
552   /// <summary>
553   /// Get a dataAdapter object for this provider.
554   /// </summary>
555   /// <returns>An 'IDbDataAdapter' object.</returns>
556 0 public IDbDataAdapter GetDataAdapter()
557   {
558   if (_templateDataAdapterIsICloneable)
559   {
560   return (IDbDataAdapter) ((ICloneable)_templateDataAdapter).Clone();
561   }
562   else
563   {
564   return (IDbDataAdapter) Activator.CreateInstance(_templateDataAdapter.GetType());
565   }
566   }
567  
568   /// <summary>
569   /// Get a IDataParameter object for this provider.
570   /// </summary>
571   /// <returns>An 'IDataParameter' object.</returns>
572 0 public IDataParameter GetDataParameter()
573   {
574   return (IDataParameter) Activator.CreateInstance(_dataParameterType);
575   }
576  
577   /// <summary>
578   /// Get the CommandBuilder Type for this provider.
579   /// </summary>
580   /// <returns>An object.</returns>
581 4 public Type GetCommandBuilderType()
582   {
583 4 return _commandBuilderType;
584   }
585  
586   /// <summary>
587   /// Get the ParameterDb Type for this provider.
588   /// </summary>
589   /// <returns>An object.</returns>
590   public Type ParameterDbType
591   {
592 16199 get { return _parameterDbType; }
593   }
594  
595   /// <summary>
596   /// Equals implemantation.
597   /// </summary>
598   /// <param name="obj">The test object.</param>
599   /// <returns>A boolean.</returns>
600 0 public override bool Equals(object obj)
601   {
602   if ((obj != null) && (obj is Provider))
603   {
604   Provider that = (Provider) obj;
605   return ((this._name == that._name) &&
606   (this._assemblyName == that._assemblyName) &&
607   (this._connectionClass == that._connectionClass));
608   }
609   return false;
610   }
611  
612   /// <summary>
613   /// A hashcode for the provider.
614   /// </summary>
615   /// <returns>An integer.</returns>
616 0 public override int GetHashCode()
617   {
618   return (_name.GetHashCode() ^ _assemblyName.GetHashCode() ^ _connectionClass.GetHashCode());
619   }
620  
621   /// <summary>
622   /// ToString implementation.
623   /// </summary>
624   /// <returns>A string that describes the provider.</returns>
625 0 public override string ToString()
626   {
627   return "Provider " + _name;
628   }
629   #endregion
630  
631   }
632   }
633