Clover.NET coverage report - Coverage

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

File Stats: LOC: 494   Methods: 22
NCLOC: 319 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
SqlMapSession.cs 29.7 % 50.0 % 54.5 % 43.9 %
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   using System;
29   using System.Data;
30  
31   using IBatisNet.Common;
32   using IBatisNet.DataMapper.Exceptions;
33  
34   using log4net;
35   #endregion
36  
37  
38   namespace IBatisNet.DataMapper
39   {
40   /// <summary>
41   /// Summary description for SqlMapSession.
42   /// </summary>
43   [Serializable]
44   public class SqlMapSession : IDalSession
45   {
46   #region Fields
47   private static readonly ILog _logger = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
48   private SqlMapper _sqlMapper = null;
49   private DataSource _dataSource = null;
50  
51   #endregion
52  
53   #region Constructor (s) / Destructor
54   /// <summary>
55   ///
56   /// </summary>
57   /// <param name="sqlMapper"></param>
58 5 public SqlMapSession(SqlMapper sqlMapper)
59   {
60 5 _dataSource = sqlMapper.DataSource;
61 5 _sqlMapper = sqlMapper;
62   }
63  
64   /// <summary>
65   ///
66   /// </summary>
67   /// <param name="dataSource"></param>
68 20678 public SqlMapSession(DataSource dataSource)
69   {
70 20678 _dataSource = dataSource;
71   }
72   #endregion
73  
74   #region IDalSession Members
75  
76   #region Fields
77   private bool _isOpenTransaction = false;
78   /// <summary>
79   /// Changes the vote to commit (true) or to abort (false) in transsaction
80   /// </summary>
81   private bool _consistent = false;
82  
83   /// <summary>
84   /// Holds value of connection
85   /// </summary>
86   private IDbConnection _connection = null;
87  
88   /// <summary>
89   /// Holds value of transaction
90   /// </summary>
91   private IDbTransaction _transaction = null;
92   #endregion
93  
94   #region Properties
95   /// <summary>
96   ///
97   /// </summary>
98   public DataSource DataSource
99   {
100 329702 get { return _dataSource; }
101   }
102  
103   /// <summary>
104   ///
105   /// </summary>
106 0 public IDbConnection Connection
107   {
108   get { return _connection; }
109   }
110  
111   /// <summary>
112   ///
113   /// </summary>
114 0 public IDbTransaction Transaction
115   {
116   get { return _transaction; }
117   }
118  
119   /// <summary>
120   /// Changes the vote for transaction to commit (true) or to abort (false).
121   /// </summary>
122   private bool Consistent
123   {
124 1 set
125   {
126 1 _consistent = value;
127   }
128   }
129   #endregion
130  
131   #region Methods
132   /// <summary>
133   /// Complete (commit) a transaction
134   /// </summary>
135   /// <remarks>
136   /// Use in 'using' syntax.
137   /// </remarks>
138 1 public void Complete()
139   {
140 1 this.Consistent = true;
141   }
142  
143   /// <summary>
144   /// Open the connection
145   /// </summary>
146 283 public void OpenConnection()
147   {
148 283 if (_connection == null)
149   {
150 283 _connection = _dataSource.Provider.GetConnection();
151 283 _connection.ConnectionString = _dataSource.ConnectionString;
152 283 try
153   {
154 283 _connection.Open();
155 283 if (_logger.IsDebugEnabled)
156   {
157 283 _logger.Debug( string.Format("Open Connection \"{0}\" to \"{1}\".", _connection.GetHashCode().ToString(), _dataSource.Provider.Description) );
158   }
159   }
160   catch(Exception ex)
161   {
162 0 throw new DataMapperException( string.Format("Unable to open connection to \"{0}\".", _dataSource.Provider.Description), ex );
163   }
164   }
165 0 else if (_connection.State != ConnectionState.Open)
166   {
167   try
168   {
169   _connection.Open();
170   if (_logger.IsDebugEnabled)
171   {
172   _logger.Debug(string.Format("Open Connection \"{0}\" to \"{1}\".", _connection.GetHashCode().ToString(), _dataSource.Provider.Description) );
173   }
174   }
175   catch(Exception ex)
176   {
177   throw new DataMapperException(string.Format("Unable to open connection to \"{0}\".", _dataSource.Provider.Description), ex );
178   }
179   }
180   }
181  
182   /// <summary>
183   /// Close the connection
184   /// </summary>
185 283 public void CloseConnection()
186   {
187 283 if ( (_connection != null) && (_connection.State != ConnectionState.Closed) )
188   {
189 283 _connection.Close();
190 283 if (_logger.IsDebugEnabled)
191   {
192  
193 283 _logger.Debug(string.Format("Close Connection \"{0}\" to \"{1}\".", _connection.GetHashCode().ToString(), _dataSource.Provider.Description));
194   }
195 283 _connection.Dispose();
196   }
197 283 _connection = null;
198   }
199  
200   /// <summary>
201   /// Begins a database transaction.
202   /// </summary>
203 4 public void BeginTransaction()
204   {
205 4 if (_connection == null || _connection.State != ConnectionState.Open)
206   {
207 4 OpenConnection();
208   }
209 4 _transaction = _connection.BeginTransaction();
210 4 if (_logger.IsDebugEnabled)
211   {
212 4 _logger.Debug("Begin Transaction.");
213   }
214 4 _isOpenTransaction = true;
215   }
216  
217   /// <summary>
218   /// Begins a database transaction
219   /// </summary>
220   /// <param name="openConnection">Open a connection.</param>
221 0 public void BeginTransaction(bool openConnection)
222   {
223   if (openConnection)
224   {
225   this.BeginTransaction();
226   }
227   else
228   {
229   if (_connection == null || _connection.State != ConnectionState.Open)
230   {
231   throw new DataMapperException("SqlMapSession could not invoke BeginTransaction(). A Connection must be started. Call OpenConnection() first.");
232   }
233   _transaction = _connection.BeginTransaction();
234   if (_logger.IsDebugEnabled)
235   {
236   _logger.Debug("Begin Transaction.");
237   }
238   _isOpenTransaction = true;
239   }
240   }
241  
242   /// <summary>
243   /// Begins a database transaction with the specified isolation level.
244   /// </summary>
245   /// <param name="isolationLevel">
246   /// The isolation level under which the transaction should run.
247   /// </param>
248 0 public void BeginTransaction(IsolationLevel isolationLevel)
249   {
250   if (_connection == null || _connection.State != ConnectionState.Open)
251   {
252   OpenConnection();
253   }
254   _transaction = _connection.BeginTransaction(isolationLevel);
255   if (_logger.IsDebugEnabled)
256   {
257   _logger.Debug("Begin Transaction.");
258   }
259   _isOpenTransaction = true;
260   }
261  
262   /// <summary>
263   /// Begins a transaction on the current connection
264   /// with the specified IsolationLevel value.
265   /// </summary>
266   /// <param name="isolationLevel">The transaction isolation level for this connection.</param>
267   /// <param name="openConnection">Open a connection.</param>
268 0 public void BeginTransaction(bool openConnection, IsolationLevel isolationLevel)
269   {
270   if (openConnection)
271   {
272   this.BeginTransaction(isolationLevel);
273   }
274   else
275   {
276   if (_connection == null || _connection.State != ConnectionState.Open)
277   {
278   throw new DataMapperException("SqlMapSession could not invoke StartTransaction(). A Connection must be started. Call OpenConnection() first.");
279   }
280   _transaction = _connection.BeginTransaction(isolationLevel);
281   if (_logger.IsDebugEnabled)
282   {
283   _logger.Debug("Begin Transaction.");
284   }
285   _isOpenTransaction = true;
286   }
287   }
288  
289  
290   /// <summary>
291   /// Commits the database transaction.
292   /// </summary>
293   /// <remarks>
294   /// Will close the connection.
295   /// </remarks>
296 3 public void CommitTransaction()
297   {
298 3 if (_logger.IsDebugEnabled)
299   {
300 3 _logger.Debug("Commit Transaction.");
301   }
302 3 _transaction.Commit();
303 3 _transaction.Dispose();
304 3 if (_connection.State != ConnectionState.Closed)
305   {
306 3 this.CloseConnection();
307   }
308   }
309  
310   /// <summary>
311   /// Commits the database transaction.
312   /// </summary>
313   /// <param name="closeConnection">Close the connection</param>
314 0 public void CommitTransaction(bool closeConnection)
315   {
316   if (closeConnection)
317   {
318   this.CommitTransaction();
319   }
320   else
321   {
322   _transaction.Commit();
323   if (_logger.IsDebugEnabled)
324   {
325   _logger.Debug("Commit Transaction.");
326   }
327   _transaction.Dispose();
328   }
329   }
330  
331   /// <summary>
332   /// Rolls back a transaction from a pending state.
333   /// </summary>
334   /// <remarks>
335   /// Will close the connection.
336   /// </remarks>
337 1 public void RollBackTransaction()
338   {
339 1 _transaction.Rollback();
340 1 if (_logger.IsDebugEnabled)
341   {
342 1 _logger.Debug("RollBack Transaction.");
343   }
344 1 _transaction.Dispose();
345 1 _transaction = null;
346 1 if (_connection.State != ConnectionState.Closed)
347   {
348 1 this.CloseConnection();
349   }
350   }
351  
352   /// <summary>
353   /// Rolls back a transaction from a pending state.
354   /// </summary>
355   /// <param name="closeConnection">Close the connection</param>
356 0 public void RollBackTransaction(bool closeConnection)
357   {
358   if (closeConnection)
359   {
360   this.RollBackTransaction();
361   }
362   else
363   {
364   if (_logger.IsDebugEnabled)
365   {
366   _logger.Debug("RollBack Transaction.");
367   }
368   _transaction.Rollback();
369   _transaction.Dispose();
370   _transaction = null;
371   }
372   }
373  
374   /// <summary>
375   /// Create a command object
376   /// </summary>
377   /// <param name="commandType"></param>
378   /// <returns></returns>
379 32485 public IDbCommand CreateCommand(CommandType commandType)
380   {
381 32485 IDbCommand command = null;
382  
383 32485 command = _dataSource.Provider.GetCommand();
384   // Assign CommandType
385 32485 command.CommandType = commandType;
386   // Assign connection
387 32485 command.Connection = _connection;
388   // Assign transaction
389 32485 if (_transaction != null)
390   {
391 6 try
392   {
393 6 command.Transaction = _transaction;
394   }
395   catch
396   {}
397   }
398   // Assign connection timeout
399 32485 if (_connection!= null)
400   {
401 355 try // MySql provider doesn't suppport it !
402   {
403 355 command.CommandTimeout = _connection.ConnectionTimeout;
404   }
405   catch(NotSupportedException e)
406   {
407 0 if (_logger.IsInfoEnabled)
408   {
409   _logger.Info(e.Message);
410   }
411   }
412   }
413  
414 32485 return command;
415   }
416  
417   /// <summary>
418   /// Create an IDataParameter
419   /// </summary>
420   /// <returns>An IDataParameter.</returns>
421 0 public IDataParameter CreateDataParameter()
422   {
423   IDataParameter dataParameter = null;
424  
425   dataParameter = _dataSource.Provider.GetDataParameter();
426  
427   return dataParameter;
428   }
429  
430   /// <summary>
431   ///
432   /// </summary>
433   /// <returns></returns>
434 0 public IDbDataAdapter CreateDataAdapter()
435   {
436   return _dataSource.Provider.GetDataAdapter();
437   }
438  
439   /// <summary>
440   ///
441   /// </summary>
442   /// <param name="command"></param>
443   /// <returns></returns>
444 0 public IDbDataAdapter CreateDataAdapter(IDbCommand command)
445   {
446   IDbDataAdapter dataAdapter = null;
447  
448   dataAdapter = _dataSource.Provider.GetDataAdapter();
449   dataAdapter.SelectCommand = command;
450  
451   return dataAdapter;
452   }
453   #endregion
454  
455   #endregion
456  
457   #region IDisposable Members
458  
459   /// <summary>
460   /// Releasing, or resetting resources.
461   /// </summary>
462 2 public void Dispose()
463   {
464 2 if (_logger.IsDebugEnabled)
465   {
466 2 _logger.Debug("Dispose SqlMapSession");
467   }
468 2 if (_isOpenTransaction == false)
469   {
470 1 if (_connection.State != ConnectionState.Closed)
471   {
472 1 _sqlMapper.CloseConnection();
473   }
474   }
475   else
476   {
477 1 if (_consistent)
478   {
479 1 _sqlMapper.CommitTransaction();
480   }
481   else
482   {
483 0 if (_connection.State != ConnectionState.Closed)
484   {
485   _sqlMapper.RollBackTransaction();
486   }
487   }
488   }
489   }
490  
491   #endregion
492   }
493   }
494