Clover.NET coverage report - Coverage

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

File Stats: LOC: 250   Methods: 2
NCLOC: 147 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Commands\DefaultPreparedCommand.cs 75.0 % 98.3 % 100.0 % 90.3 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * iBATIS.NET Data Mapper
9   * Copyright (C) 2005 - 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   using System.Data;
29   using System.Collections;
30   using System.Text;
31  
32   using log4net;
33  
34   using IBatisNet.Common;
35   using IBatisNet.Common.Utilities.Objects;
36  
37   using IBatisNet.DataMapper.Configuration.Statements;
38   using IBatisNet.DataMapper.Configuration.ParameterMapping;
39   using IBatisNet.DataMapper.Exceptions;
40   using IBatisNet.DataMapper.Scope;
41   #endregion
42  
43   namespace IBatisNet.DataMapper.Commands
44   {
45   /// <summary>
46   /// Summary description for DefaultPreparedCommand.
47   /// </summary>
48   internal class DefaultPreparedCommand : IPreparedCommand
49   {
50  
51   #region Fields
52   private static readonly ILog _logger = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
53   #endregion
54  
55   #region IPreparedCommand Members
56  
57   /// <summary>
58   /// Create an IDbCommand for the IDalSession and the current SQL Statement
59   /// and fill IDbCommand IDataParameter's with the parameterObject.
60   /// </summary>
61   /// <param name="request"></param>
62   /// <param name="session">The IDalSession</param>
63   /// <param name="statement">The IStatement</param>
64   /// <param name="parameterObject">
65   /// The parameter object that will fill the sql parameter
66   /// </param>
67   /// <returns>An IDbCommand with all the IDataParameter filled.</returns>
68 301 public IDbCommand Create(RequestScope request, IDalSession session, IStatement statement, object parameterObject )
69   {
70   // the IDbConnection & the IDbTransaction are assign in the CreateCommand
71 301 IDbCommand command = session.CreateCommand(statement.CommandType);
72  
73 301 command.CommandText = request.PreparedStatement.PreparedSql;
74  
75 301 if (_logger.IsDebugEnabled)
76   {
77 301 _logger.Debug("PreparedStatement : [" + command.CommandText + "]");
78   }
79  
80 301 ApplyParameterMap( session, command, request, statement, parameterObject );
81  
82 300 return command;
83   }
84  
85  
86   /// <summary>
87   ///
88   /// </summary>
89   /// <param name="session"></param>
90   /// <param name="command"></param>
91   /// <param name="request"></param>
92   /// <param name="statement"></param>
93   /// <param name="parameterObject"></param>
94 301 protected virtual void ApplyParameterMap
95   ( IDalSession session, IDbCommand command,
96   RequestScope request, IStatement statement, object parameterObject )
97   {
98 301 ArrayList properties = request.PreparedStatement.DbParametersName;
99 301 ArrayList parameters = request.PreparedStatement.DbParameters;
100 301 StringBuilder paramLogList = new StringBuilder(); // Log info
101 301 StringBuilder typeLogList = new StringBuilder(); // Log info
102  
103 800 for ( int i = 0; i < properties.Count; ++i )
104   {
105 500 IDataParameter sqlParameter = (IDataParameter)parameters[i];
106 500 string propertyName = (string)properties[i];
107 500 IDataParameter parameterCopy = command.CreateParameter();
108 500 ParameterProperty property = request.ParameterMap.GetProperty(i);
109  
110   #region Logging
111 500 if (_logger.IsDebugEnabled)
112   {
113 500 paramLogList.Append(sqlParameter.ParameterName);
114 500 paramLogList.Append("=[");
115 500 typeLogList.Append(sqlParameter.ParameterName);
116 500 typeLogList.Append("=[");
117   }
118   #endregion
119  
120 500 if (command.CommandType == CommandType.StoredProcedure)
121   {
122   #region store procedure command
123  
124   // A store procedure must always use a ParameterMap
125   // to indicate the mapping order of the properties to the columns
126 24 if (request.ParameterMap == null) // Inline Parameters
127   {
128 0 throw new DataMapperException("A procedure statement tag must alway have a parameterMap attribute, which is not the case for the procedure '"+statement.Id+"'.");
129   }
130   else // Parameters via ParameterMap
131   {
132 24 if (property.DirectionAttribute.Length == 0)
133   {
134 21 property.Direction = sqlParameter.Direction;
135   }
136  
137   // DbDataParameter dataParameter = (IDbDataParameter)parameters[i];
138   // property.Precision = dataParameter.Precision;
139   // property.Scale = dataParameter.Scale;
140   // property.Size = dataParameter.Size;
141  
142 24 sqlParameter.Direction = property.Direction;
143   }
144   #endregion
145   }
146  
147   #region Logging
148 500 if (_logger.IsDebugEnabled)
149   {
150 500 paramLogList.Append( property.PropertyName );
151 500 paramLogList.Append( "," );
152   }
153   #endregion
154  
155 500 request.ParameterMap.SetParameter(property, parameterCopy, parameterObject );
156  
157   // // Fix JIRA 20
158   // property.TypeHandler.SetParameter(property, parameterCopy, parameterValue, property.DbType);
159  
160 499 parameterCopy.Direction = sqlParameter.Direction;
161  
162   // With a ParameterMap, we could specify the ParameterDbTypeProperty
163 499 if (statement.ParameterMap != null)
164   {
165 445 if (request.ParameterMap.GetProperty(i).DbType != null &&
166   request.ParameterMap.GetProperty(i).DbType.Length >0)
167   {
168 64 string dbTypePropertyName = session.DataSource.Provider.ParameterDbTypeProperty;
169  
170 64 ObjectProbe.SetPropertyValue(parameterCopy, dbTypePropertyName, ObjectProbe.GetPropertyValue(sqlParameter, dbTypePropertyName));
171   }
172   else
173   {
174   //parameterCopy.DbType = sqlParameter.DbType;
175   }
176   }
177   else
178   {
179   //parameterCopy.DbType = sqlParameter.DbType;
180   }
181  
182  
183   #region Logging
184 499 if (_logger.IsDebugEnabled)
185   {
186 499 if (parameterCopy.Value == System.DBNull.Value)
187   {
188 29 paramLogList.Append("null");
189 29 paramLogList.Append( "], " );
190 29 typeLogList.Append("System.DBNull, null");
191 29 typeLogList.Append( "], " );
192   }
193   else
194   {
195  
196 470 paramLogList.Append( parameterCopy.Value.ToString() );
197 470 paramLogList.Append( "], " );
198  
199   // sqlParameter.DbType could be null (as with Npgsql)
200   // if PreparedStatementFactory did not find a dbType for the parameter in:
201   // line 225: "if (property.DbType.Length >0)"
202   // Use parameterCopy.DbType
203  
204   //typeLogList.Append( sqlParameter.DbType.ToString() );
205 470 typeLogList.Append( parameterCopy.DbType.ToString() );
206 470 typeLogList.Append( ", " );
207 470 typeLogList.Append( parameterCopy.Value.GetType().ToString() );
208 470 typeLogList.Append( "], " );
209   }
210   }
211   #endregion
212  
213   // JIRA-49 Fixes (size, precision, and scale)
214 499 if (session.DataSource.Provider.SetDbParameterSize)
215   {
216 499 if (((IDbDataParameter)sqlParameter).Size > 0)
217   {
218 13 ((IDbDataParameter)parameterCopy).Size = ((IDbDataParameter)sqlParameter).Size;
219   }
220   }
221  
222 499 if (session.DataSource.Provider.SetDbParameterPrecision)
223   {
224 499 ((IDbDataParameter)parameterCopy).Precision = ((IDbDataParameter)sqlParameter).Precision;
225   }
226  
227 499 if (session.DataSource.Provider.SetDbParameterScale)
228   {
229 499 ((IDbDataParameter)parameterCopy).Scale = ((IDbDataParameter)sqlParameter).Scale;
230   }
231  
232 499 parameterCopy.ParameterName = sqlParameter.ParameterName;
233  
234 499 command.Parameters.Add( parameterCopy );
235   }
236  
237   #region Logging
238  
239 300 if (_logger.IsDebugEnabled && properties.Count>0)
240   {
241 219 _logger.Debug("Parameters: [" + paramLogList.ToString(0, paramLogList.Length - 2) + "]");
242 219 _logger.Debug("Types: [" + typeLogList.ToString(0, typeLogList.Length - 2) + "]");
243   }
244   #endregion
245   }
246  
247   #endregion
248   }
249   }
250