Clover.NET coverage report - Coverage

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

File Stats: LOC: 249   Methods: 10
NCLOC: 123 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Utilities\DBHelperParameterCache.cs 56.3 % 75.6 % 60.0 % 69.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   using System;
28   using System.Collections;
29   using System.Data;
30   using System.Data.Common;
31   using System.Reflection;
32  
33   using IBatisNet.Common.Exceptions;
34  
35   namespace IBatisNet.Common.Utilities
36   {
37   /// <summary>
38   /// DBHelperParameterCache provides functions to leverage a
39   /// static cache of procedure parameters, and the
40   /// ability to discover parameters for stored procedures at run-time.
41   /// </summary>
42   public class DBHelperParameterCache
43   {
44   //Since this class provides only static methods, make the default constructor private to prevent
45   //instances from being created.
46 0 private DBHelperParameterCache() {}
47  
48   #region Private fields
49   private static Hashtable _paramCache = Hashtable.Synchronized(new Hashtable());
50   #endregion
51  
52   #region private methods
53  
54   /// <summary>
55   /// Resolve at run time the appropriate set of Parameters for a stored procedure
56   /// </summary>
57   /// <param name="session">a valid session</param>
58   /// <param name="spName">the name of the stored procedure</param>
59   /// <param name="includeReturnValueParameter">whether or not to include their return value parameter</param>
60   /// <returns></returns>
61 4 private static IDataParameter[] DiscoverSpParameterSet(IDalSession session, string spName, bool includeReturnValueParameter)
62   {
63 4 using (IDbConnection connection = session.DataSource.Provider.GetConnection())
64   {
65 4 connection.ConnectionString = session.DataSource.ConnectionString;
66 4 connection.Open();
67 4 return InternalDiscoverSpParameterSet(session.DataSource.Provider, connection, spName, includeReturnValueParameter);
68   }
69   }
70  
71   /// <summary>
72   /// resolve at run time the appropriate set of Parameters for a stored procedure
73   /// </summary>
74   /// <param name="provider"></param>
75   /// <param name="connection">a valid open IDbConnection</param>
76   /// <param name="spName">the name of the stored procedure</param>
77   /// <param name="includeReturnValueParameter">whether or not to include their return value parameter</param>
78   /// <returns></returns>
79 4 private static IDataParameter[] InternalDiscoverSpParameterSet(Provider provider,
80   IDbConnection connection, string spName, bool includeReturnValueParameter)
81   {
82 4 using (IDbCommand cmd = connection.CreateCommand())
83   {
84 4 cmd.CommandType = CommandType.StoredProcedure;
85 4 cmd.CommandText = spName;
86  
87 4 DeriveParameters(provider, cmd);
88  
89 4 if (cmd.Parameters.Count > 0) {
90 4 IDataParameter firstParameter = (IDataParameter)cmd.Parameters[0];
91 4 if (firstParameter.Direction == ParameterDirection.ReturnValue) {
92 4 if (!includeReturnValueParameter) {
93 4 cmd.Parameters.RemoveAt(0);
94   }
95   }
96   }
97  
98  
99 4 IDataParameter[] discoveredParameters = new IDataParameter[cmd.Parameters.Count];
100  
101 4 cmd.Parameters.CopyTo(discoveredParameters, 0);
102  
103 4 return discoveredParameters;
104   }
105   }
106  
107 4 private static void DeriveParameters(Provider provider, IDbCommand command)
108   {
109 4 Type commandBuilderType;
110  
111   // Find the CommandBuilder
112 4 if (provider == null)
113 0 throw new ArgumentNullException("provider");
114 4 if ((provider.CommandBuilderClass == null) || (provider.CommandBuilderClass.Length < 1))
115 0 throw new Exception(String.Format(
116   "CommandBuilderClass not defined for provider \"{0}\".",
117   provider.Name));
118 4 commandBuilderType = provider.GetCommandBuilderType();
119  
120   // Invoke the static DeriveParameter method on the CommandBuilder class
121   // NOTE: OracleCommandBuilder has no DeriveParameter method
122 4 try
123   {
124 4 commandBuilderType.InvokeMember("DeriveParameters",
125   BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null,
126   new object[] {command});
127   }
128   catch(Exception ex)
129   {
130 0 throw new IBatisNetException("Could not retrieve parameters for the store procedure named "+command.CommandText, ex);
131   }
132   }
133  
134   /// <summary>
135   /// Deep copy of cached IDataParameter array.
136   /// </summary>
137   /// <param name="originalParameters"></param>
138   /// <returns></returns>
139 6 private static IDataParameter[] CloneParameters(IDataParameter[] originalParameters)
140   {
141 6 IDataParameter[] clonedParameters = new IDataParameter[originalParameters.Length];
142  
143 27 for (int i = 0, j = originalParameters.Length; i < j; i++)
144   {
145 21 clonedParameters[i] = (IDataParameter) ((ICloneable) originalParameters[i]).Clone();
146   }
147  
148 6 return clonedParameters;
149   }
150  
151   #endregion private methods, variables, and constructors
152  
153   #region caching functions
154  
155   /// <summary>
156   /// Add parameter array to the cache
157   /// </summary>
158   /// <param name="connectionString">a valid connection string for an IDbConnection</param>
159   /// <param name="commandText">the stored procedure name or SQL command</param>
160   /// <param name="commandParameters">an array of IDataParameters to be cached</param>
161 0 public static void CacheParameterSet(string connectionString, string commandText, params IDataParameter[] commandParameters)
162   {
163   string hashKey = connectionString + ":" + commandText;
164  
165   _paramCache[hashKey] = commandParameters;
166   }
167  
168  
169   // FM Added
170   /// <summary>
171   /// Clear the parameter cache.
172   /// </summary>
173 0 public static void Clear()
174   {
175   _paramCache.Clear();
176   }
177  
178   /// <summary>
179   /// retrieve a parameter array from the cache
180   /// </summary>
181   /// <param name="connectionString">a valid connection string for an IDbConnection</param>
182   /// <param name="commandText">the stored procedure name or SQL command</param>
183   /// <returns>an array of IDataParameters</returns>
184 0 public static IDataParameter[] GetCachedParameterSet(string connectionString, string commandText)
185   {
186   string hashKey = connectionString + ":" + commandText;
187  
188   IDataParameter[] cachedParameters = (IDataParameter[]) _paramCache[hashKey];
189  
190   if (cachedParameters == null)
191   {
192   return null;
193   }
194   else
195   {
196   return CloneParameters(cachedParameters);
197   }
198   }
199  
200   #endregion caching functions
201  
202   #region Parameter Discovery Functions
203  
204   /// <summary>
205   /// Retrieves the set of IDataParameters appropriate for the stored procedure
206   /// </summary>
207   /// <remarks>
208   /// This method will query the database for this information, and then store it in a cache for future requests.
209   /// </remarks>
210   /// <param name="session">a valid session</param>
211   /// <param name="spName">the name of the stored procedure</param>
212   /// <returns>an array of IDataParameters</returns>
213 6 public static IDataParameter[] GetSpParameterSet(IDalSession session, string spName)
214   {
215 6 return GetSpParameterSet(session, spName, false);
216   }
217  
218   /// <summary>
219   /// Retrieves the set of IDataParameters appropriate for the stored procedure
220   /// </summary>
221   /// <remarks>
222   /// This method will query the database for this information, and then store it in a cache for future requests.
223   /// </remarks>
224   /// <param name="session">a valid csession</param>
225   /// <param name="spName">the name of the stored procedure</param>
226   /// <param name="includeReturnValueParameter">a bool value indicating whether the return value parameter should be included in the results</param>
227   /// <returns>an array of IDataParameters</returns>
228 6 public static IDataParameter[] GetSpParameterSet(IDalSession session
229   , string spName, bool includeReturnValueParameter)
230   {
231 6 string hashKey = session.DataSource.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter":"");
232  
233 6 IDataParameter[] cachedParameters;
234  
235 6 cachedParameters = (IDataParameter[]) _paramCache[hashKey];
236  
237 6 if (cachedParameters == null)
238   {
239 4 _paramCache[hashKey] = DiscoverSpParameterSet(session, spName, includeReturnValueParameter);
240 4 cachedParameters = (IDataParameter[]) _paramCache[hashKey];
241   }
242  
243 6 return CloneParameters(cachedParameters);
244   }
245  
246   #endregion Parameter Discovery Functions
247   }
248   }
249