Clover.NET coverage report - Coverage

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

File Stats: LOC: 1282   Methods: 56
NCLOC: 751 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
SqlMapper.cs 50.0 % 71.9 % 75.0 % 67.5 %
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   using System.IO;
31   using System.Text;
32   using System.Collections;
33   using System.Collections.Specialized;
34   using System.Threading;
35   using System.Xml;
36  
37   using IBatisNet.Common;
38   using IBatisNet.Common.Exceptions;
39   using IBatisNet.Common.Utilities;
40   using IBatisNet.DataMapper.SessionContainer;
41  
42   using IBatisNet.DataMapper.Configuration;
43   using IBatisNet.DataMapper.Configuration.Alias;
44   using IBatisNet.DataMapper.Configuration.ParameterMapping;
45   using IBatisNet.DataMapper.Configuration.ResultMapping;
46   using IBatisNet.DataMapper.Configuration.Cache;
47   using IBatisNet.DataMapper.Exceptions;
48   using IBatisNet.DataMapper.MappedStatements;
49   using IBatisNet.DataMapper.TypeHandlers;
50  
51   #endregion
52  
53   namespace IBatisNet.DataMapper
54   {
55   /// <summary>
56   /// Summary description for SqlMap.
57   /// </summary>
58   public class SqlMapper
59   {
60  
61   /// <summary>
62   ///
63   /// </summary>
64   public delegate void RowDelegate(object obj, IList list);
65  
66   #region Contants
67   private const string DEFAULT_FILE_CONFIG_NAME = "sqlmap.config";
68   #endregion
69  
70   #region Fields
71   //(MappedStatement Name, MappedStatement)
72   private HybridDictionary _mappedStatements = new HybridDictionary();
73   //(ResultMap name, ResultMap)
74   private HybridDictionary _resultMaps = new HybridDictionary();
75   //(ParameterMap name, ParameterMap)
76   private HybridDictionary _parameterMaps = new HybridDictionary();
77   // DataSource
78   private DataSource _dataSource = null;
79   //(typeAlias name, type alias)
80   private HybridDictionary _typeAliasMaps = new HybridDictionary();
81   //(CacheModel name, cache))
82   private HybridDictionary _cacheMaps = new HybridDictionary();
83   private TypeHandlerFactory _typeHandlerFactory = null;
84  
85   private bool _cacheModelsEnabled = false;
86   private bool _useEmbedStatementParams = false;
87  
88   /// <summary>
89   /// Container session unique for each thread.
90   /// </summary>
91   private ISessionContainer _sessionContainer = null;
92  
93   #endregion
94  
95   #region Properties
96  
97   /// <summary>
98   /// Returns the DalSession instance
99   /// currently being used by the SqlMap.
100   /// </summary>
101   public IDalSession LocalSession
102   {
103 14 get { return _sessionContainer.LocalSession; }
104   }
105  
106  
107   /// <summary>
108   /// Tell if the session is started.
109   /// </summary>
110   /// <returns></returns>
111   public bool IsSessionStarted
112   {
113 20 get { return (_sessionContainer.LocalSession != null); }
114   }
115  
116   /// <summary>
117   /// A flag that determines whether cache models were enabled
118   /// when this SqlMap was built.
119   /// </summary>
120 0 public bool IsCacheModelsEnabled
121   {
122   get { return _cacheModelsEnabled; }
123   }
124  
125   /// <summary>
126   /// A flag that determines whether statements use
127   /// embedded parameters.
128   /// </summary>
129   public bool UseEmbedStatementParams
130   {
131 26520 get { return _useEmbedStatementParams; }
132   }
133  
134   /// <summary>
135   /// The TypeHandlerFactory
136   /// </summary>
137   internal TypeHandlerFactory TypeHandlerFactory
138   {
139 134620 get { return _typeHandlerFactory; }
140   }
141   #endregion
142  
143   #region Constructor (s) / Destructor
144   /// <summary>
145   /// Create a new SqlMap
146   /// </summary>
147 170 internal SqlMapper(TypeHandlerFactory typeHandlerFactory)
148   {
149 170 _typeHandlerFactory = typeHandlerFactory;
150 170 _sessionContainer = SessionContainerFactory.GetSessionContainer(IBatisNet.Common.Utilities.HashCodeProvider.GetIdentityHashCode(this).ToString());
151   }
152   #endregion
153  
154   #region Methods
155   /// <summary>
156   /// Set the falg to tell us if cache models were enabled
157   /// or not.
158   /// </summary>
159 170 internal void SetCacheModelsEnabled(bool value)
160   {
161 170 _cacheModelsEnabled = value;
162   }
163  
164   /// <summary>
165   /// Sets the flag indicating if statements should used embedded
166   /// parameters
167   /// </summary>
168 170 internal void SetUseEmbedStatementParams(bool value)
169   {
170 170 _useEmbedStatementParams = value;
171   }
172  
173   #region Configure
174  
175   /// <summary>
176   /// Configure an SqlMap.
177   /// </summary>
178   /// <param name="document">An xml sql map onfiguration document.</param>
179   /// <returns>the SqlMap</returns>
180 0 static public SqlMapper Configure( XmlDocument document )
181   {
182   return new DomSqlMapBuilder().Build( document, false );
183   }
184  
185   /// <summary>
186   /// Configure an SqlMap from
187   /// default file named SqlMap.config.
188   /// </summary>
189   /// <returns>An SqlMap</returns>
190 0 static public SqlMapper Configure()
191   {
192   return Configure( Resources.GetConfigAsXmlDocument(DEFAULT_FILE_CONFIG_NAME) );
193   }
194  
195  
196   /// <summary>
197   /// Configure an SqlMap from via a file.
198   /// </summary>
199   /// <param name="fileName">File name.</param>
200   /// <returns>An SqlMap</returns>
201 0 public static SqlMapper Configure(string fileName)
202   {
203   XmlDocument document = Resources.GetConfigAsXmlDocument(fileName);
204   return new DomSqlMapBuilder().Build( document, false);
205   }
206  
207  
208   /// <summary>
209   /// Configure and monitor the default configuration file for modifications
210   /// and automatically reconfigure SqlMap.
211   /// </summary>
212   /// <returns>An SqlMap</returns>
213 0 public static SqlMapper ConfigureAndWatch(ConfigureHandler configureDelegate)
214   {
215   return ConfigureAndWatch( DEFAULT_FILE_CONFIG_NAME, configureDelegate ) ;
216   }
217  
218  
219   /// <summary>
220   /// Configure and monitor the configuration file for modifications
221   /// and automatically reconfigure SqlMap.
222   /// </summary>
223   /// <param name="fileName">File name.</param>
224   ///<param name="configureDelegate">
225   /// Delegate called when the file has changed, to rebuild the dal.
226   /// </param>
227   /// <returns>An SqlMap</returns>
228 170 public static SqlMapper ConfigureAndWatch( string fileName, ConfigureHandler configureDelegate )
229   {
230 170 XmlDocument document = Resources.GetConfigAsXmlDocument( fileName );
231  
232 170 ConfigWatcherHandler.ClearFilesMonitored();
233 170 ConfigWatcherHandler.AddFileToWatch( Resources.GetFileInfo( fileName ) );
234  
235 170 TimerCallback callBakDelegate = new TimerCallback( SqlMapper.OnConfigFileChange );
236  
237 170 StateConfig state = new StateConfig();
238 170 state.fileName = fileName;
239 170 state.configureHandler = configureDelegate;
240  
241 170 new ConfigWatcherHandler( callBakDelegate, state );
242  
243 170 return new DomSqlMapBuilder().Build( document, true );
244   }
245  
246   /// <summary>
247   /// Callback called when the SqlMap.config changed.
248   /// </summary>
249   /// <param name="obj">The state config.</param>
250 0 public static void OnConfigFileChange(object obj)
251   {
252   StateConfig state = (StateConfig)obj;
253  
254   //SqlMap sqlMap = ConfigureAndWatch( state.fileName, state.configureHandler );
255   //state.configureHandler( sqlMap );
256   state.configureHandler( null );
257   }
258  
259   #endregion
260  
261   #region Manage Connection, Transaction
262  
263   /// <summary>
264   ///
265   /// </summary>
266   /// <returns></returns>
267 1 public IDalSession OpenConnection()
268   {
269 1 if (_sessionContainer.LocalSession != null)
270   {
271 0 throw new DataMapperException("SqlMap could not invoke OpenConnection(). A connection is already started. Call CloseConnection first.");
272   }
273 1 SqlMapSession session = new SqlMapSession(this);
274 1 _sessionContainer.Store(session);
275 1 session.OpenConnection();
276 1 return session;
277   }
278  
279   /// <summary>
280   /// Open a connection
281   /// </summary>
282 1 public void CloseConnection()
283   {
284 1 if (_sessionContainer.LocalSession == null)
285   {
286 0 throw new DataMapperException("SqlMap could not invoke CloseConnection(). No connection was started. Call OpenConnection() first.");
287   }
288 1 try
289   {
290 1 IDalSession session = _sessionContainer.LocalSession;
291 1 session.CloseConnection();
292   }
293   catch(Exception ex)
294   {
295 0 throw new DataMapperException("SqlMapper could not CloseConnection(). Cause :"+ex.Message, ex);
296   }
297   finally
298   {
299 1 _sessionContainer.Dispose();
300   }
301   }
302  
303  
304   /// <summary>
305   /// Begins a database transaction.
306   /// </summary>
307 5 public IDalSession BeginTransaction()
308   {
309 5 if (_sessionContainer.LocalSession != null)
310   {
311 1 throw new DataMapperException("SqlMap could not invoke BeginTransaction(). A Transaction is already started. Call CommitTransaction() or RollbackTransaction first.");
312   }
313 4 SqlMapSession session = new SqlMapSession(this);
314 4 _sessionContainer.Store(session);
315 4 session.BeginTransaction();
316 4 return session ;
317   }
318  
319   /// <summary>
320   /// Begins a database transaction on the currect session
321   /// </summary>
322   /// <param name="openConnection">Open a connection.</param>
323 0 public IDalSession BeginTransaction(bool openConnection)
324   {
325   IDalSession session = null;
326  
327   if (openConnection)
328   {
329   session = this.BeginTransaction();
330   }
331   else
332   {
333   session = _sessionContainer.LocalSession;
334   if (session == null)
335   {
336   throw new DataMapperException("SqlMap could not invoke BeginTransaction(). A session must be Open. Call OpenConnection() first.");
337   }
338   session.BeginTransaction(openConnection);
339   }
340  
341   return session;
342   }
343  
344   /// <summary>
345   /// Begins a database transaction with the specified isolation level.
346   /// </summary>
347   /// <param name="isolationLevel">
348   /// The isolation level under which the transaction should run.
349   /// </param>
350 0 public IDalSession BeginTransaction(IsolationLevel isolationLevel)
351   {
352   if (_sessionContainer.LocalSession != null)
353   {
354   throw new DataMapperException("SqlMap could not invoke BeginTransaction(). A Transaction is already started. Call CommitTransaction() or RollbackTransaction first.");
355   }
356   SqlMapSession session = new SqlMapSession(this);
357   _sessionContainer.Store(session);
358   session.BeginTransaction(isolationLevel);
359   return session;
360   }
361  
362   /// <summary>
363   /// Start a database transaction on the current session
364   /// with the specified isolation level.
365   /// </summary>
366   /// <param name="openConnection">Open a connection.</param>
367   /// <param name="isolationLevel">
368   /// The isolation level under which the transaction should run.
369   /// </param>
370 0 public IDalSession BeginTransaction(bool openConnection, IsolationLevel isolationLevel)
371   {
372   IDalSession session = null;
373  
374   if (openConnection)
375   {
376   session = this.BeginTransaction(isolationLevel);
377   }
378   else
379   {
380   session = _sessionContainer.LocalSession;
381   if (session == null)
382   {
383   throw new DataMapperException("SqlMap could not invoke BeginTransaction(). A session must be Open. Call OpenConnection() first.");
384   }
385   session.BeginTransaction(openConnection, isolationLevel);
386   }
387   return session;
388   }
389  
390   /// <summary>
391   /// Commits the database transaction.
392   /// </summary>
393   /// <remarks>
394   /// Will close the connection.
395   /// </remarks>
396 4 public void CommitTransaction()
397   {
398 4 if (_sessionContainer.LocalSession == null)
399   {
400 1 throw new DataMapperException("SqlMap could not invoke CommitTransaction(). No Transaction was started. Call BeginTransaction() first.");
401   }
402 3 try
403   {
404 3 IDalSession session = _sessionContainer.LocalSession;
405 3 session.CommitTransaction();
406   }
407   finally
408   {
409 3 _sessionContainer.Dispose();
410   }
411   }
412  
413   /// <summary>
414   /// Commits the database transaction.
415   /// </summary>
416   /// <param name="closeConnection">Close the connection</param>
417 0 public void CommitTransaction(bool closeConnection)
418   {
419   if (_sessionContainer.LocalSession == null)
420   {
421   throw new DataMapperException("SqlMap could not invoke CommitTransaction(). No Transaction was started. Call BeginTransaction() first.");
422   }
423   try
424   {
425   IDalSession session = _sessionContainer.LocalSession;
426   session.CommitTransaction(closeConnection);
427   }
428   finally
429   {
430   if (closeConnection)
431   {
432   _sessionContainer.Dispose();
433   }
434   }
435   }
436  
437   /// <summary>
438   /// Rolls back a transaction from a pending state.
439   /// </summary>
440   /// <remarks>
441   /// Will close the connection.
442   /// </remarks>
443 1 public void RollBackTransaction()
444   {
445 1 if (_sessionContainer.LocalSession == null)
446   {
447 0 throw new DataMapperException("SqlMap could not invoke RollBackTransaction(). No Transaction was started. Call BeginTransaction() first.");
448   }
449 1 try
450   {
451 1 IDalSession session = _sessionContainer.LocalSession;
452 1 session.RollBackTransaction();
453   }
454   finally
455   {
456 1 _sessionContainer.Dispose();
457   }
458   }
459  
460   /// <summary>
461   /// Rolls back a transaction from a pending state.
462   /// </summary>
463   /// <param name="closeConnection">Close the connection</param>
464 0 public void RollBackTransaction(bool closeConnection)
465   {
466   if (_sessionContainer.LocalSession == null)
467   {
468   throw new DataMapperException("SqlMap could not invoke RollBackTransaction(). No Transaction was started. Call BeginTransaction() first.");
469   }
470   try
471   {
472   IDalSession session = _sessionContainer.LocalSession;
473   session.RollBackTransaction(closeConnection);
474   }
475   finally
476   {
477   if (closeConnection)
478   {
479   _sessionContainer.Dispose();
480   }
481   }
482   }
483  
484   #endregion
485  
486   #region QueryForObject
487  
488   /// <summary>
489   /// Executes a Sql SELECT statement that returns that returns data
490   /// to populate a single object instance.
491   /// <p/>
492   /// The parameter object is generally used to supply the input
493   /// data for the WHERE clause parameter(s) of the SELECT statement.
494   /// </summary>
495   /// <param name="statementName">The name of the sql statement to execute.</param>
496   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
497   /// <returns> The single result object populated with the result set data.</returns>
498 125 public object QueryForObject(string statementName, object parameterObject)
499   {
500 125 bool isSessionLocal = false;
501 125 IDalSession session = _sessionContainer.LocalSession;
502 125 object result;
503  
504 125 if (session == null)
505   {
506 122 session = new SqlMapSession(this.DataSource);
507 122 session.OpenConnection();
508 122 isSessionLocal = true;
509   }
510  
511 125 IMappedStatement statement = GetMappedStatement(statementName);
512  
513 125 try
514   {
515 125 result = statement.ExecuteQueryForObject(session, parameterObject);
516   }
517   catch
518   {
519 0 throw;
520   }
521   finally
522   {
523 125 if ( isSessionLocal )
524   {
525 122 session.CloseConnection();
526   }
527   }
528  
529 125 return result;
530   }
531  
532  
533   /// <summary>
534   /// Executes a Sql SELECT statement that returns a single object of the type of the
535   /// resultObject parameter.
536   /// </summary>
537   /// <param name="statementName">The name of the sql statement to execute.</param>
538   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
539   /// <param name="resultObject">An object of the type to be returned.</param>
540   /// <returns>The single result object populated with the result set data.</returns>
541 1 public object QueryForObject(string statementName, object parameterObject, object resultObject)
542   {
543 1 bool isSessionLocal = false;
544 1 IDalSession session = _sessionContainer.LocalSession;
545 1 object result = null;
546  
547 1 if (session == null)
548   {
549 1 session = new SqlMapSession(this.DataSource);
550 1 session.OpenConnection();
551 1 isSessionLocal = true;
552   }
553  
554 1 IMappedStatement statement = GetMappedStatement(statementName);
555  
556 1 try
557   {
558 1 result = statement.ExecuteQueryForObject(session, parameterObject, resultObject);
559   }
560   catch
561   {
562 0 throw;
563   }
564   finally
565   {
566 1 if ( isSessionLocal )
567   {
568 1 session.CloseConnection();
569   }
570   }
571  
572 1 return result;
573   }
574  
575  
576   #endregion
577  
578   #region QueryForMap, QueryForDictionary
579  
580   /// <summary>
581   /// Alias to QueryForMap, .NET spirit.
582   /// Feature idea by Ted Husted.
583   /// </summary>
584   /// <param name="statementName">The name of the sql statement to execute.</param>
585   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
586   /// <param name="keyProperty">The property of the result object to be used as the key.</param>
587   /// <returns>A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.</returns>
588 0 public IDictionary QueryForDictionary(string statementName, object parameterObject, string keyProperty)
589   {
590   return QueryForMap( statementName, parameterObject, keyProperty);
591   }
592  
593   /// <summary>
594   /// Alias to QueryForMap, .NET spirit.
595   /// Feature idea by Ted Husted.
596   /// </summary>
597   /// <param name="statementName">The name of the sql statement to execute.</param>
598   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
599   /// <param name="keyProperty">The property of the result object to be used as the key.</param>
600   /// <param name="valueProperty">The property of the result object to be used as the value (or null)</param>
601   /// <returns>A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.</returns>
602   ///<exception cref="DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
603 0 public IDictionary QueryForDictionary(string statementName, object parameterObject, string keyProperty, string valueProperty)
604   {
605   return QueryForMap( statementName, parameterObject, keyProperty, valueProperty);
606   }
607  
608   /// <summary>
609   /// Executes the SQL and retuns all rows selected in a map that is keyed on the property named
610   /// in the keyProperty parameter. The value at each key will be the entire result object.
611   /// </summary>
612   /// <param name="statementName">The name of the sql statement to execute.</param>
613   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
614   /// <param name="keyProperty">The property of the result object to be used as the key.</param>
615   /// <returns>A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.</returns>
616 2 public IDictionary QueryForMap(string statementName, object parameterObject, string keyProperty)
617   {
618 2 return QueryForMap(statementName, parameterObject, keyProperty, null);
619   }
620  
621  
622   /// <summary>
623   /// Executes the SQL and retuns all rows selected in a map that is keyed on the property named
624   /// in the keyProperty parameter. The value at each key will be the value of the property specified
625   /// in the valueProperty parameter. If valueProperty is null, the entire result object will be entered.
626   /// </summary>
627   /// <param name="statementName">The name of the sql statement to execute.</param>
628   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
629   /// <param name="keyProperty">The property of the result object to be used as the key.</param>
630   /// <param name="valueProperty">The property of the result object to be used as the value (or null)</param>
631   /// <returns>A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.</returns>
632   ///<exception cref="DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
633 3 public IDictionary QueryForMap(string statementName, object parameterObject, string keyProperty, string valueProperty)
634   {
635 3 bool isSessionLocal = false;
636 3 IDalSession session = _sessionContainer.LocalSession;
637 3 IDictionary map = null;
638  
639 3 if (session == null)
640   {
641 3 session = new SqlMapSession(this.DataSource);
642 3 session.OpenConnection();
643 3 isSessionLocal = true;
644   }
645  
646 3 IMappedStatement statement = GetMappedStatement(statementName);
647  
648 3 try
649   {
650 3 map = statement.ExecuteQueryForMap(session, parameterObject, keyProperty, valueProperty);
651   }
652   catch
653   {
654 0 throw;
655   }
656   finally
657   {
658 3 if ( isSessionLocal )
659   {
660 3 session.CloseConnection();
661   }
662   }
663  
664 3 return map;
665   }
666  
667  
668   #endregion
669  
670   #region QueryForList
671  
672   /// <summary>
673   /// Executes a Sql SELECT statement that returns data to populate
674   /// a number of result objects.
675   /// <p/>
676   /// The parameter object is generally used to supply the input
677   /// data for the WHERE clause parameter(s) of the SELECT statement.
678   /// </summary>
679   /// <param name="statementName">The name of the sql statement to execute.</param>
680   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
681   /// <returns>A List of result objects.</returns>
682 63 public IList QueryForList(string statementName, object parameterObject)
683   {
684 63 bool isSessionLocal = false;
685 63 IDalSession session = _sessionContainer.LocalSession;
686 63 IList list;
687  
688 63 if (session == null)
689   {
690 63 session = new SqlMapSession(this.DataSource);
691 63 session.OpenConnection();
692 63 isSessionLocal = true;
693   }
694  
695 63 IMappedStatement statement = GetMappedStatement(statementName);
696  
697 63 try
698   {
699 63 list = statement.ExecuteQueryForList(session, parameterObject);
700   }
701   catch
702   {
703 0 throw;
704   }
705   finally
706   {
707 63 if ( isSessionLocal )
708   {
709 63 session.CloseConnection();
710   }
711   }
712  
713 63 return list;
714   }
715  
716  
717   /// <summary>
718   /// Executes the SQL and retuns all rows selected.
719   /// <p/>
720   /// The parameter object is generally used to supply the input
721   /// data for the WHERE clause parameter(s) of the SELECT statement.
722   /// </summary>
723   /// <param name="statementName">The name of the sql statement to execute.</param>
724   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
725   /// <param name="skipResults">The number of rows to skip over.</param>
726   /// <param name="maxResults">The maximum number of rows to return.</param>
727   /// <returns>A List of result objects.</returns>
728 1 public IList QueryForList(string statementName, object parameterObject, int skipResults, int maxResults)
729   {
730 1 bool isSessionLocal = false;
731 1 IDalSession session = _sessionContainer.LocalSession;
732 1 IList list;
733  
734 1 if (session == null)
735   {
736 1 session = new SqlMapSession(this.DataSource);
737 1 session.OpenConnection();
738 1 isSessionLocal = true;
739   }
740  
741 1 IMappedStatement statement = GetMappedStatement(statementName);
742  
743 1 try
744   {
745 1 list = statement.ExecuteQueryForList(session, parameterObject, skipResults, maxResults);
746   }
747   catch
748   {
749 0 throw;
750   }
751   finally
752   {
753 1 if ( isSessionLocal )
754   {
755 1 session.CloseConnection();
756   }
757   }
758  
759 1 return list;
760   }
761  
762  
763   /// <summary>
764   /// Executes a Sql SELECT statement that returns data to populate
765   /// a number of result objects.
766   /// <p/>
767   /// The parameter object is generally used to supply the input
768   /// data for the WHERE clause parameter(s) of the SELECT statement.
769   /// </summary>
770   /// <param name="statementName">The name of the sql statement to execute.</param>
771   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
772   /// <param name="resultObject">An Ilist object used to hold the objects.</param>
773   /// <returns>A List of result objects.</returns>
774 1 public void QueryForList(string statementName, object parameterObject, IList resultObject)
775   {
776 1 bool isSessionLocal = false;
777 1 IDalSession session = _sessionContainer.LocalSession;
778  
779 1 if (resultObject == null)
780   {
781 0 throw new DataMapperException("resultObject parameter must be instantiated before being passed to SqlMapper.QueryForList");
782   }
783  
784 1 if (session == null)
785   {
786 1 session = new SqlMapSession(this.DataSource);
787 1 session.OpenConnection();
788 1 isSessionLocal = true;
789   }
790  
791 1 IMappedStatement statement = GetMappedStatement(statementName);
792  
793 1 try
794   {
795 1 statement.ExecuteQueryForList(session, parameterObject, resultObject);
796   }
797   catch
798   {
799 0 throw;
800   }
801   finally
802   {
803 1 if ( isSessionLocal )
804   {
805 1 session.CloseConnection();
806   }
807   }
808   }
809  
810  
811   #endregion
812  
813   #region QueryForPaginatedList
814   /// <summary>
815   /// Executes the SQL and retuns a subset of the results in a dynamic PaginatedList that can be used to
816   /// automatically scroll through results from a database table.
817   /// </summary>
818   /// <param name="statementName">The name of the sql statement to execute.</param>
819   /// <param name="parameterObject">The object used to set the parameters in the SQL</param>
820   /// <param name="pageSize">The maximum number of objects to store in each page</param>
821   /// <returns>A PaginatedList of beans containing the rows</returns>
822 4 public PaginatedList QueryForPaginatedList(String statementName, object parameterObject, int pageSize)
823   {
824 4 IMappedStatement statement = GetMappedStatement(statementName);
825 4 return new PaginatedList(statement, parameterObject, pageSize);
826   }
827   #endregion
828  
829   #region QueryWithRowDelegate
830  
831   /// <summary>
832   /// Runs a query with a custom object that gets a chance to deal
833   /// with each row as it is processed.
834   /// <p/>
835   /// The parameter object is generally used to supply the input
836   /// data for the WHERE clause parameter(s) of the SELECT statement.
837   /// </summary>
838   /// <param name="statementName">The name of the sql statement to execute.</param>
839   /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
840   /// <param name="rowDelegate"></param>
841   /// <returns>A List of result objects.</returns>
842 1 public IList QueryWithRowDelegate(string statementName, object parameterObject, RowDelegate rowDelegate)
843   {
844 1 bool isSessionLocal = false;
845 1 IDalSession session = _sessionContainer.LocalSession;
846 1 IList list = null;
847  
848 1 if (session == null)
849   {
850 1 session = new SqlMapSession(this.DataSource);
851 1 session.OpenConnection();
852 1 isSessionLocal = true;
853   }
854  
855 1 IMappedStatement statement = GetMappedStatement(statementName);
856  
857 1 try
858   {
859 1 list = statement.ExecuteQueryForRowDelegate(session, parameterObject, rowDelegate);
860   }
861   catch
862   {
863 0 throw;
864   }
865   finally
866   {
867 1 if ( isSessionLocal )
868   {
869 1 session.CloseConnection();
870   }
871   }
872  
873 1 return list;
874   }
875  
876  
877   #endregion
878  
879   #region Query Insert, Update, Delete
880  
881   /// <summary>
882   /// Executes a Sql INSERT statement.
883   /// Insert is a bit different from other update methods, as it
884   /// provides facilities for returning the primary key of the
885   /// newly inserted row (rather than the effected rows). This
886   /// functionality is of course optional.
887   /// <p/>
888   /// The parameter object is generally used to supply the input
889   /// data for the INSERT values.
890   /// </summary>
891   /// <param name="statementName">The name of the statement to execute.</param>
892   /// <param name="parameterObject">The parameter object.</param>
893   /// <returns> The primary key of the newly inserted row.
894   /// This might be automatically generated by the RDBMS,
895   /// or selected from a sequence table or other source.
896   /// </returns>
897 57 public object Insert(string statementName, object parameterObject)
898   {
899 57 bool isSessionLocal = false;
900 57 IDalSession session = _sessionContainer.LocalSession;
901 57 object generatedKey = null;
902  
903 57 if (session == null)
904   {
905 54 session = new SqlMapSession(this.DataSource);
906 54 session.OpenConnection();
907 54 isSessionLocal = true;
908   }
909  
910 57 IMappedStatement statement = GetMappedStatement(statementName);
911  
912 57 try
913   {
914 57 generatedKey = statement.ExecuteInsert(session, parameterObject);
915   }
916   catch
917   {
918 0 throw;
919   }
920   finally
921   {
922 57 if ( isSessionLocal )
923   {
924 54 session.CloseConnection();
925   }
926   }
927  
928 57 return generatedKey;
929   }
930  
931  
932   /// <summary>
933   /// Executes a Sql UPDATE statement.
934   /// Update can also be used for any other update statement type,
935   /// such as inserts and deletes. Update returns the number of
936   /// rows effected.
937   /// <p/>
938   /// The parameter object is generally used to supply the input
939   /// data for the UPDATE values as well as the WHERE clause parameter(s).
940   /// </summary>
941   /// <param name="statementName">The name of the statement to execute.</param>
942   /// <param name="parameterObject">The parameter object.</param>
943   /// <returns>The number of rows effected.</returns>
944   // /// <exception cref="IBatisNet.Common.Exceptions.DalConcurrentException">
945   // /// If no rows are effected throw this exception.
946   // /// </exception>
947 10 public int Update(string statementName, object parameterObject)
948   {
949 10 bool isSessionLocal = false;
950 10 IDalSession session = _sessionContainer.LocalSession;
951 10 int rows = 0; // the number of rows affected
952  
953 10 if (session == null)
954   {
955 9 session = new SqlMapSession(this.DataSource);
956 9 session.OpenConnection();
957 9 isSessionLocal = true;
958   }
959  
960 10 IMappedStatement statement = GetMappedStatement(statementName);
961  
962 10 try
963   {
964 10 rows = statement.ExecuteUpdate(session, parameterObject);
965   }
966   catch
967   {
968 1 throw;
969   }
970   finally
971   {
972 10 if ( isSessionLocal )
973   {
974 9 session.CloseConnection();
975   }
976   }
977  
978   // // check that statement affected a row
979   // if( rows == 0 )
980   // {
981   // // throw concurrency error if no record was affected
982   // throw new ConcurrentException();
983   // }
984  
985 9 return rows;
986   }
987  
988  
989   /// <summary>
990   /// Executes a Sql DELETE statement.
991   /// Delete returns the number of rows effected.
992   /// </summary>
993   /// <param name="statementName">The name of the statement to execute.</param>
994   /// <param name="parameterObject">The parameter object.</param>
995   /// <returns>The number of rows effected.</returns>
996 3 public int Delete(string statementName, object parameterObject)
997   {
998 3 bool isSessionLocal = false;
999 3 IDalSession session = _sessionContainer.LocalSession;
1000 3 int rows = 0; // the number of rows affected
1001  
1002 3 if (session == null)
1003   {
1004 3 session = new SqlMapSession(this.DataSource);
1005 3 session.OpenConnection();
1006 3 isSessionLocal = true;
1007   }
1008  
1009 3 IMappedStatement statement = GetMappedStatement(statementName);
1010  
1011 3 try
1012   {
1013 3 rows = statement.ExecuteUpdate(session, parameterObject);
1014   }
1015   catch
1016   {
1017 0 throw;
1018   }
1019   finally
1020   {
1021 3 if ( isSessionLocal )
1022   {
1023 3 session.CloseConnection();
1024   }
1025   }
1026  
1027 3 return rows;
1028   }
1029  
1030  
1031   #endregion
1032  
1033   #region Get/Add ParemeterMap, ResultMap, MappedStatement, TypeAlias, DataSource, CacheModel
1034  
1035   /// <summary>
1036   /// Gets a MappedStatement by name
1037   /// </summary>
1038   /// <param name="name"> The name of the statement</param>
1039   /// <returns> The MappedStatement</returns>
1040 643 public MappedStatement GetMappedStatement(string name)
1041   {
1042 643 if (_mappedStatements.Contains(name) == false)
1043   {
1044 0 throw new DataMapperException("This SQL map does not contain a MappedStatement named " + name);
1045   }
1046 643 return (MappedStatement) _mappedStatements[name];
1047   }
1048  
1049   /// <summary>
1050   /// Adds a (named) MappedStatement.
1051   /// </summary>
1052   /// <param name="key"> The key name</param>
1053   /// <param name="mappedStatement">The statement to add</param>
1054 26520 internal void AddMappedStatement(string key, MappedStatement mappedStatement)
1055   {
1056 26520 if (_mappedStatements.Contains(key) == true)
1057   {
1058 0 throw new DataMapperException("This SQL map already contains a MappedStatement named " + mappedStatement.Name);
1059   }
1060 26520 _mappedStatements.Add(key, mappedStatement);
1061   }
1062  
1063   /// <summary>
1064   /// The MappedStatements collection
1065   /// </summary>
1066   internal HybridDictionary MappedStatements
1067   {
1068 170 get { return _mappedStatements; }
1069   }
1070  
1071  
1072   /// <summary>
1073   /// Get a ParameterMap by name
1074   /// </summary>
1075   /// <param name="name">The name of the ParameterMap</param>
1076   /// <returns>The ParameterMap</returns>
1077 4080 internal ParameterMap GetParameterMap(string name)
1078   {
1079 4080 if (!_parameterMaps.Contains(name))
1080   {
1081 0 throw new DataMapperException("This SQL map does not contain an ParameterMap named " + name + ". ");
1082   }
1083 4080 return (ParameterMap) _parameterMaps[name];
1084   }
1085  
1086  
1087   /// <summary>
1088   /// Adds a (named) ParameterMap.
1089   /// </summary>
1090   /// <param name="parameterMap">the ParameterMap to add</param>
1091 3400 internal void AddParameterMap(ParameterMap parameterMap)
1092   {
1093 3400 if (_parameterMaps.Contains(parameterMap.Id) == true)
1094   {
1095 0 throw new DataMapperException("This SQL map already contains an ParameterMap named " + parameterMap.Id);
1096   }
1097 3400 _parameterMaps.Add(parameterMap.Id, parameterMap);
1098   }
1099  
1100  
1101   /// <summary>
1102   /// Gets a ResultMap by name
1103   /// </summary>
1104   /// <param name="name">The name of the result map</param>
1105   /// <returns>The ResultMap</returns>
1106 10710 internal ResultMap GetResultMap(string name)
1107   {
1108 10710 if (_resultMaps.Contains(name) == false)
1109   {
1110 0 throw new DataMapperException("This SQL map does not contain an ResultMap named " + name);
1111   }
1112 10710 return (ResultMap) _resultMaps[name];
1113   }
1114  
1115   /// <summary>
1116   /// Adds a (named) ResultMap
1117   /// </summary>
1118   /// <param name="resultMap">The ResultMap to add</param>
1119 4930 internal void AddResultMap(ResultMap resultMap)
1120   {
1121 4930 if (_resultMaps.Contains(resultMap.Id) == true)
1122   {
1123 0 throw new DataMapperException("This SQL map already contains an ResultMap named " + resultMap.Id);
1124   }
1125 4930 _resultMaps.Add(resultMap.Id, resultMap);
1126   }
1127  
1128   /// <summary>
1129   /// The ParameterMap collection
1130   /// </summary>
1131   internal HybridDictionary ParameterMaps
1132   {
1133 3910 get { return _parameterMaps; }
1134   }
1135  
1136   /// <summary>
1137   /// The ResultMap collection
1138   /// </summary>
1139   internal HybridDictionary ResultMaps
1140   {
1141 6290 get { return _resultMaps; }
1142   }
1143  
1144  
1145   /// <summary>
1146   /// The DataSource
1147   /// </summary>
1148   public DataSource DataSource
1149   {
1150 21740 get { return _dataSource; }
1151 170 set { _dataSource = value; }
1152   }
1153  
1154   /// <summary>
1155   /// Gets a named TypeAlias from the list of available TypeAlias
1156   /// </summary>
1157   /// <param name="name">The name of the TypeAlias.</param>
1158   /// <returns>The TypeAlias.</returns>
1159 36040 internal TypeAlias GetTypeAlias(string name)
1160   {
1161 36040 if (_typeAliasMaps.Contains(name) == true)
1162   {
1163 16830 return (TypeAlias) _typeAliasMaps[name];
1164   }
1165   else
1166   {
1167 19210 return null;
1168   }
1169   }
1170  
1171   /// <summary>
1172   /// Adds a named TypeAlias to the list of available TypeAlias.
1173   /// </summary>
1174   /// <param name="key">The key name.</param>
1175   /// <param name="typeAlias"> The TypeAlias.</param>
1176 2720 internal void AddTypeAlias(string key, TypeAlias typeAlias)
1177   {
1178 2720 if (_typeAliasMaps.Contains(key) == true)
1179   {
1180 0 throw new DataMapperException(" Alias name conflict occurred. The type alias '" + key + "' is already mapped to the value '"+typeAlias.ClassName+"'.");
1181   }
1182 2720 _typeAliasMaps.Add(key, typeAlias);
1183   }
1184  
1185  
1186   /// <summary>
1187   /// Gets the type object from the specific class name.
1188   /// </summary>
1189   /// <param name="className">The supplied class name.</param>
1190   /// <returns>The correpsonding type.
1191   /// </returns>
1192 36040 internal Type GetType(string className)
1193   {
1194 36040 Type type = null;
1195 36040 TypeAlias typeAlias = this.GetTypeAlias(className) as TypeAlias;
1196  
1197 36040 if (typeAlias != null)
1198   {
1199 16830 type = typeAlias.Class;
1200   }
1201   else
1202   {
1203 19210 type = Resources.TypeForName(className);
1204   }
1205  
1206 36040 return type;
1207   }
1208  
1209   /// <summary>
1210   /// Flushes all cached objects that belong to this SqlMap
1211   /// </summary>
1212 1 public void FlushCaches()
1213   {
1214 1 IDictionaryEnumerator enumerator = _cacheMaps.GetEnumerator();
1215 2 while (enumerator.MoveNext())
1216   {
1217 1 ((CacheModel)enumerator.Value).Flush();
1218   }
1219   }
1220  
1221   /// <summary>
1222   /// Adds a (named) cache.
1223   /// </summary>
1224   /// <param name="cache">The cache to add</param>
1225 170 internal void AddCache(CacheModel cache)
1226   {
1227 170 if (_cacheMaps.Contains(cache.Id))
1228   {
1229 0 throw new DataMapperException("This SQL map already contains an Cache named " + cache.Id);
1230   }
1231 170 _cacheMaps.Add(cache.Id, cache);
1232   }
1233  
1234   /// <summary>
1235   /// Gets a cache by name
1236   /// </summary>
1237   /// <param name="name">The name of the cache to get</param>
1238   /// <returns>The cache object</returns>
1239 510 internal CacheModel GetCache(string name)
1240   {
1241 510 if (!_cacheMaps.Contains(name))
1242   {
1243 0 throw new DataMapperException("This SQL map does not contain an Cache named " + name);
1244   }
1245 510 return (CacheModel) _cacheMaps[name];
1246   }
1247  
1248   /// <summary>
1249   ///
1250   /// </summary>
1251   /// <returns></returns>
1252 0 public string GetDataCacheStats()
1253   {
1254   StringBuilder buffer = new StringBuilder("\nCache Data Statistiques");
1255   buffer.Append("\n=======================\n");
1256   IDictionaryEnumerator enumerator = _mappedStatements.GetEnumerator();
1257   while (enumerator.MoveNext())
1258   {
1259   MappedStatement statement = (MappedStatement) enumerator.Value;
1260   buffer.Append(statement.Name);
1261   buffer.Append(": ");
1262   double hitRatio = statement.GetDataCacheHitRatio();
1263   if (hitRatio != -1)
1264   {
1265   buffer.Append(Math.Round(hitRatio * 100));
1266   buffer.Append("%");
1267   }
1268   else
1269   {
1270   buffer.Append("No Cache.");
1271   }
1272   buffer.Append("\n");
1273   }
1274   return buffer.ToString();
1275   }
1276  
1277   #endregion
1278  
1279   #endregion
1280   }
1281   }
1282