Author: rgrabowski
Date: Sun Jul 16 09:58:24 2006
New Revision: 422475

URL: http://svn.apache.org/viewvc?rev=422475&view=rev
Log:
Initial check-in for supporting QueryForDataTable. I've got it returning a 
DataTable from a <select> statement. I've also included a fixed QueryForObject 
for IBATISNET-104 (CachingStatement opens/closes connection to database if 
there is a cache hit) and the two line fix for IBATISNET-154 (allow SelectKey 
to specify a resultMap instead of just a resultClass).

Added:
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectDataTableStrategy.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectDataTableStrategy.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/DataTableStrategy.cs
Modified:
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/Configuration/Cache/CacheModel.cs
    ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ExecuteEventArgs.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/IBatisNet.DataMapper.csproj
    ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ISqlMapper.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/ArgumentStrategyFactory.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectStrategy.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/CachingStatement.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/IMappedStatement.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/MappedStatement.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/PropertyStrategyFactory.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectStrategy.cs
    
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/ResultClassStrategy.cs
    ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/SqlMapper.cs

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/Configuration/Cache/CacheModel.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/Configuration/Cache/CacheModel.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/Configuration/Cache/CacheModel.cs
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/Configuration/Cache/CacheModel.cs
 Sun Jul 16 09:58:24 2006
@@ -252,7 +252,7 @@
                {
                        if (_logger.IsDebugEnabled) 
                        {
-                               _logger.Debug("Flush cacheModel named "+_id+" 
for statement '"+e.StatementName+"'");
+                               _logger.Debug("Flush cacheModel named "+_id+" 
for statement '"+e.RequestScope.Statement.Id+"'");
                        }
 
                        Flush();

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ExecuteEventArgs.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ExecuteEventArgs.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ExecuteEventArgs.cs 
(original)
+++ ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ExecuteEventArgs.cs 
Sun Jul 16 09:58:24 2006
@@ -2,7 +2,7 @@
 #region Apache Notice
 /*****************************************************************************
  * $Header: $
- * $Revision: $
+ * $Revision$
  * $Date$
  * 
  * iBATIS.NET Data Mapper
@@ -25,29 +25,22 @@
 #endregion
 
 using System;
+using IBatisNet.DataMapper.Scope;
 
 namespace IBatisNet.DataMapper
 {
-       /// <summary>
-       /// Summary description for ExecuteEventArgs.
-       /// </summary>
        public class ExecuteEventArgs : EventArgs
        {
-               private string _statementName = string.Empty;
+               private RequestScope requestScope;
 
-               /// <summary>
-               /// Set or get the statement name
-               /// </summary>
-               public string StatementName
+               public ExecuteEventArgs(RequestScope requestScope)
                {
-                       get 
-                       {
-                               return _statementName;
-                       }
-                       set 
-                       {
-                               _statementName = value;
-                       }
+                       this.requestScope = requestScope;
+               }
+
+               public RequestScope RequestScope
+               {
+                       get { return requestScope; }
                }
        }
-}
+}
\ No newline at end of file

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/IBatisNet.DataMapper.csproj
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/IBatisNet.DataMapper.csproj?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/IBatisNet.DataMapper.csproj
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/IBatisNet.DataMapper.csproj
 Sun Jul 16 09:58:24 2006
@@ -903,6 +903,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = 
"MappedStatements\ArgumentStrategy\SelectDataTableStrategy.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = 
"MappedStatements\ArgumentStrategy\SelectListStrategy.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
@@ -973,6 +978,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = 
"MappedStatements\PropertStrategy\SelectDataTableStrategy.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = 
"MappedStatements\PropertStrategy\SelectListStrategy.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
@@ -994,6 +1004,11 @@
                 />
                 <File
                     RelPath = 
"MappedStatements\ResultStrategy\BaseResultStrategy.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
+                    RelPath = 
"MappedStatements\ResultStrategy\DataTableStrategy.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
                 />

Modified: ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ISqlMapper.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ISqlMapper.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ISqlMapper.cs 
(original)
+++ ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/ISqlMapper.cs Sun 
Jul 16 09:58:24 2006
@@ -187,6 +187,8 @@
         /// <returns></returns>
         IDalSession OpenConnection(string connectionString);
 
+               DataTable QueryForDataTable(string statementName, object 
parameterObject);
+
         /// <summary>
         /// Alias to QueryForMap, .NET spirit.
         ///  Feature idea by Ted Husted.

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/ArgumentStrategyFactory.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/ArgumentStrategyFactory.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/ArgumentStrategyFactory.cs
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/ArgumentStrategyFactory.cs
 Sun Jul 16 09:58:24 2006
@@ -38,6 +38,8 @@
         private static IArgumentStrategy _selectGenericListStrategy = null;
         private static IArgumentStrategy _selectListStrategy = null;
         private static IArgumentStrategy _selectObjectStrategy = null;
+               private static IArgumentStrategy _selectDataTableStrategy = 
null;
+
 
                /// <summary>
                /// Initializes the <see cref="ArgumentStrategyFactory"/> class.
@@ -50,6 +52,7 @@
             _selectArrayStrategy = new SelectArrayStrategy();
             _selectListStrategy = new SelectListStrategy();
             _selectObjectStrategy = new SelectObjectStrategy();
+                       _selectDataTableStrategy = new 
SelectDataTableStrategy();
 #if dotnet2
             _selectGenericListStrategy = new SelectGenericListStrategy();
 #endif
@@ -78,7 +81,8 @@
                     _selectArrayStrategy,
                     _selectGenericListStrategy,
                     _selectListStrategy,
-                    _selectObjectStrategy);
+                    _selectObjectStrategy,
+                                       _selectDataTableStrategy);
                        }
                }
        }

Added: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectDataTableStrategy.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectDataTableStrategy.cs?rev=422475&view=auto
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectDataTableStrategy.cs
 (added)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectDataTableStrategy.cs
 Sun Jul 16 09:58:24 2006
@@ -0,0 +1,20 @@
+using System.Data;
+using IBatisNet.DataMapper.Commands;
+using IBatisNet.DataMapper.Configuration.ResultMapping;
+using IBatisNet.DataMapper.Scope;
+
+namespace IBatisNet.DataMapper.MappedStatements.ArgumentStrategy
+{
+       public sealed class SelectDataTableStrategy : IArgumentStrategy
+       {
+               public object GetValue(RequestScope request, ResultMap 
resultMap, ResultProperty mapping, ref IDataReader reader,
+                                      object keys)
+               {
+                       // Get the select statement
+                       IMappedStatement selectStatement = 
request.MappedStatement.SqlMap.GetMappedStatement(mapping.Select);
+
+                       reader = DataReaderTransformer.Transform(reader, 
request.Session.DataSource.DbProvider);
+                       return 
selectStatement.ExecuteQueryForDataTable(request.Session, keys);
+               }
+       }
+}

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectStrategy.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectStrategy.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectStrategy.cs
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ArgumentStrategy/SelectStrategy.cs
 Sun Jul 16 09:58:24 2006
@@ -58,12 +58,13 @@
             IArgumentStrategy selectArrayStrategy,
             IArgumentStrategy selectGenericListStrategy,
             IArgumentStrategy selectListStrategy,
-            IArgumentStrategy selectObjectStrategy)
+            IArgumentStrategy selectObjectStrategy,
+                       IArgumentStrategy selectDataTableStrategy)
                {
                        // Collection object or .NET object                     
                        if (mapping.MemberType.BaseType == typeof(Array))
                        {
-                _selectStrategy = selectArrayStrategy;
+                               _selectStrategy = selectArrayStrategy;
                        }
 #if dotnet2
             else if (mapping.MemberType.IsGenericType &&
@@ -72,16 +73,19 @@
                 _selectStrategy = selectGenericListStrategy;
             }
 #endif                         
-            // Check if the object to Map implement 'IList' or is IList type
-                       // If yes the ResultProperty is map to a IList object
+                               // Check if the object to Map implement 'IList' 
or is IList type
+                               // If yes the ResultProperty is map to a IList 
object
                        else if ( 
typeof(IList).IsAssignableFrom(mapping.MemberType) )
                        {
-                _selectStrategy = selectListStrategy;
+                               _selectStrategy = selectListStrategy;
+                       }
+                       else if 
(typeof(DataTable).IsAssignableFrom(mapping.MemberType) )
+                       {
+                               _selectStrategy = selectDataTableStrategy;
                        }
-
                        else // The ResultProperty is map to a .Net object
                        {
-                _selectStrategy = selectObjectStrategy;
+                               _selectStrategy = selectObjectStrategy;
                        }
                }
 

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/CachingStatement.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/CachingStatement.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/CachingStatement.cs
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/CachingStatement.cs
 Sun Jul 16 09:58:24 2006
@@ -52,6 +52,8 @@
                /// </summary>
                public event ExecuteEventHandler Execute;
 
+               public event ExecuteEventHandler PreExecute;
+
                /// <summary>
                /// Constructor
                /// </summary>
@@ -145,7 +147,24 @@
                        return map;
                }
 
-               
+               public DataTable ExecuteQueryForDataTable(IDalSession session, 
object parameterObject)
+               {
+                       RequestScope request = 
this.Statement.Sql.GetRequestScope(this, parameterObject, session);
+
+                       _mappedStatement.PreparedCommand.Create( request, 
session, Statement, parameterObject );
+
+                       CacheKey cacheKey = this.GetCacheKey(request);
+                       cacheKey.Update("ExecuteQueryForDataTable");
+                       
+                       DataTable dataTable = Statement.CacheModel[cacheKey] as 
DataTable;
+                       if (dataTable == null) 
+                       {
+                               dataTable = 
_mappedStatement.RunQueryForDataTable( request, session, parameterObject);
+                               this.Statement.CacheModel[cacheKey] = dataTable;
+                       }
+
+                       return dataTable;
+               }
                
                /// <summary>
                /// Execute an update statement. Also used for delete statement.

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/IMappedStatement.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/IMappedStatement.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/IMappedStatement.cs
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/IMappedStatement.cs
 Sun Jul 16 09:58:24 2006
@@ -25,11 +25,11 @@
 #endregion
 
 #region Imports
-using System;
 using System.Collections;
 #if dotnet2
 using System.Collections.Generic;
 #endif
+using System.Data;
 using IBatisNet.Common;
 using IBatisNet.DataMapper.Commands;
 using IBatisNet.DataMapper.Configuration.Statements;
@@ -55,6 +55,8 @@
                /// </summary>
                event ExecuteEventHandler Execute;
 
+               event ExecuteEventHandler PreExecute;
+
                #endregion 
 
                #region Properties
@@ -120,6 +122,20 @@
 
                #endregion
 
+               #region ExecuteQueryForDataTable
+
+               /// <summary>
+               /// Executes the SQL and retuns all rows selected in a 
DataTable with its TableName property to the name of 
+               /// the statement.
+               /// </summary>
+               /// <param name="session">The session used to execute the 
statement</param>
+               /// <param name="parameterObject">The object used to set the 
parameters in the SQL. </param>
+               /// <returns>A DataTable containing the selected rows.</returns>
+               /// <exception 
cref="IBatisNet.DataMapper.Exceptions.DataMapperException">If a transaction is 
not in progress, or the database throws an exception.</exception>
+               DataTable ExecuteQueryForDataTable(IDalSession session, object 
parameterObject);
+
+               #endregion
+
                #region ExecuteUpdate
 
                /// <summary>
@@ -294,5 +310,5 @@
         IList<T> ExecuteQueryForRowDelegate<T>(IDalSession session, object 
parameterObject, SqlMapper.RowDelegate<T> rowDelegate);
 #endif
         #endregion
-    }
+       }
 }

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/MappedStatement.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/MappedStatement.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/MappedStatement.cs
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/MappedStatement.cs
 Sun Jul 16 09:58:24 2006
@@ -62,6 +62,8 @@
        /// </summary>
        public class MappedStatement : IMappedStatement
        {
+               public event ExecuteEventHandler PreExecute;
+
                /// <summary>
                /// Event launch on exceute query
                /// </summary>
@@ -249,6 +251,7 @@
                        
                        using ( IDbCommand command = request.IDbCommand )
                        {
+                               RaisePreExecuteEvent(request);
                                using ( IDataReader reader = 
command.ExecuteReader() )
                                {                               
                                        if ( reader.Read() )
@@ -267,7 +270,7 @@
                                RetrieveOutputParameters(request, session, 
command, parameterObject);
                        }
 
-                       RaiseExecuteEvent();
+                       RaiseExecuteEvent(request);
 
                        return result;
                }
@@ -718,6 +721,7 @@
        
                        using ( IDbCommand command = request.IDbCommand )
                        {
+                               RaisePreExecuteEvent(request);
                                rows = command.ExecuteNonQuery();
 
                                ExecutePostSelect(request);
@@ -725,7 +729,7 @@
                                RetrieveOutputParameters(request, session, 
command, parameterObject);
                        }
 
-                       RaiseExecuteEvent();
+                       RaiseExecuteEvent(request);
 
                        return rows;
                }
@@ -764,6 +768,7 @@
                        {
                                if (_statement is Insert)
                                {
+                                       RaisePreExecuteEvent(request);
                                        command.ExecuteNonQuery();
                                }
                 else if (_statement is Procedure && (_statement.ResultClass != 
null) &&
@@ -773,6 +778,7 @@
                     returnValueParameter.Direction = 
ParameterDirection.ReturnValue;
                     command.Parameters.Add(returnValueParameter);
                     
+                                       RaisePreExecuteEvent(request);
                     command.ExecuteNonQuery ( );
                     generatedKey = returnValueParameter.Value;
 
@@ -782,6 +788,7 @@
                            else
 
                                {
+                                       RaisePreExecuteEvent(request);
                                        generatedKey = command.ExecuteScalar();
                                        if ( (_statement.ResultClass!=null) && 
                                                
_sqlMap.TypeHandlerFactory.IsSimpleType(_statement.ResultClass) )
@@ -806,13 +813,22 @@
                                RetrieveOutputParameters(request, session, 
command, parameterObject);
                        }
 
-                       RaiseExecuteEvent();
+                       RaiseExecuteEvent(request);
 
                        return generatedKey;
                }
 
                #endregion
 
+               public DataTable ExecuteQueryForDataTable(IDalSession session, 
object parameterObject)
+               {
+                       RequestScope request = 
_statement.Sql.GetRequestScope(this, parameterObject, session);
+
+                       _preparedCommand.Create(request, session, 
this.Statement, parameterObject);
+
+                       return RunQueryForDataTable(request, session, 
parameterObject);
+               }
+
                #region ExecuteQueryForMap
        
                /// <summary>
@@ -900,6 +916,23 @@
 
                }
 
+               public DataTable RunQueryForDataTable(RequestScope request, 
IDalSession session, object parameterObject)
+               {
+                       DataTable dataTable = null;
+
+                       using (IDbCommand command = request.IDbCommand)
+                       {
+                               using (IDataReader reader = 
command.ExecuteReader())
+                               {
+                                       while (reader.Read() )
+                                       {
+                                               dataTable = 
(DataTable)_resultStrategy.Process(request, reader, dataTable);
+                                       }
+                               }
+                       }
+
+                       return dataTable;
+               }
                
                #endregion
 
@@ -917,19 +950,28 @@
                                
PostSelectStrategyFactory.Get(postSelect.Method).Execute(postSelect, request);
                        }
                }
-       
 
                /// <summary>
                /// Raise an event ExecuteEventArgs
                /// (Used when a query is executed)
                /// </summary>
-               private void RaiseExecuteEvent()
+               private void RaisePreExecuteEvent(RequestScope requestScope)
+               {
+                       if (PreExecute != null)
+                       {
+                               PreExecute(this, new 
ExecuteEventArgs(requestScope));
+                       }
+               }
+
+               /// <summary>
+               /// Raise an event ExecuteEventArgs
+               /// (Used when a query is executed)
+               /// </summary>
+               private void RaiseExecuteEvent(RequestScope requestScope)
                {
-                       ExecuteEventArgs e = new ExecuteEventArgs();
-                       e.StatementName = _statement.Id;
                        if (Execute != null)
                        {
-                               Execute(this, e);
+                               Execute(this, new 
ExecuteEventArgs(requestScope));
                        }
                }
 
@@ -950,6 +992,5 @@
        
 
                #endregion
-
        }
 }

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/PropertyStrategyFactory.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/PropertyStrategyFactory.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/PropertyStrategyFactory.cs
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/PropertyStrategyFactory.cs
 Sun Jul 16 09:58:24 2006
@@ -39,6 +39,7 @@
         private static IPropertyStrategy _selectGenericListStrategy = null;
         private static IPropertyStrategy _selectListStrategy = null;
         private static IPropertyStrategy _selectObjectStrategy = null;
+        private static IPropertyStrategy _selectDataTableStrategy = null;
 
                /// <summary>
                /// Initializes the <see cref="PropertyStrategyFactory"/> class.
@@ -51,6 +52,7 @@
             _selectArrayStrategy = new SelectArrayStrategy();
             _selectListStrategy = new SelectListStrategy();
             _selectObjectStrategy = new SelectObjectStrategy();
+            _selectDataTableStrategy = new SelectDataTableStrategy();
 #if dotnet2
             _selectGenericListStrategy = new SelectGenericListStrategy();
 #endif
@@ -79,7 +81,8 @@
                     _selectArrayStrategy,
                     _selectGenericListStrategy,
                     _selectListStrategy,
-                    _selectObjectStrategy);
+                    _selectObjectStrategy,
+                                       _selectDataTableStrategy);
                        }
                }
        }

Added: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectDataTableStrategy.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectDataTableStrategy.cs?rev=422475&view=auto
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectDataTableStrategy.cs
 (added)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectDataTableStrategy.cs
 Sun Jul 16 09:58:24 2006
@@ -0,0 +1,73 @@
+#region Apache Notice
+/*****************************************************************************
+ * $Revision: 374175 $
+ * $LastChangedDate: 2006-04-25 19:40:27 +0200 (mar., 25 avr. 2006) $
+ * $LastChangedBy: gbayon $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2006/2005 - The Apache Software Foundation
+ *  
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ 
********************************************************************************/
+#endregion
+
+using System;
+using System.Collections;
+using System.Data;
+using IBatisNet.DataMapper.Configuration.ResultMapping;
+using IBatisNet.DataMapper.Scope;
+
+namespace IBatisNet.DataMapper.MappedStatements.PropertyStrategy
+{
+       /// <summary>
+       /// <see cref="IPropertyStrategy"/> implementation when a 'select' 
attribute exists
+       /// on an <see cref="ResultProperty"/> and the object property is 
+       /// different from an <see cref="Array"/> or an <see cref="IList"/>.
+       /// </summary>
+       public sealed class SelectDataTableStrategy : IPropertyStrategy
+       {
+
+               #region IPropertyStrategy members
+
+               ///<summary>
+               /// Sets value of the specified <see cref="ResultProperty"/> on 
the target object
+               /// when a 'select' attribute exists and fills an object 
property.
+               /// on the <see cref="ResultProperty"/> are empties.
+               /// </summary>
+               /// <param name="request">The request.</param>
+               /// <param name="resultMap">The result map.</param>
+               /// <param name="mapping">The ResultProperty.</param>
+               /// <param name="target">The target.</param>
+               /// <param name="reader">The current <see 
cref="IDataReader"/></param>
+               /// <param name="keys">The keys</param>
+               public void Set(RequestScope request, ResultMap resultMap, 
+                       ResultProperty mapping, ref object target, IDataReader 
reader, object keys)
+               {
+                       // Get the select statement
+                       IMappedStatement selectStatement = 
request.MappedStatement.SqlMap.GetMappedStatement(mapping.Select);
+
+                       PostBindind postSelect = new PostBindind();
+                       postSelect.Statement = selectStatement;
+                       postSelect.Keys = keys;
+                       postSelect.Target = target;
+                       postSelect.ResultProperty = mapping;
+
+                       postSelect.Method = 
PostBindind.ExecuteMethod.ExecuteQueryForObject;
+                       request.QueueSelect.Enqueue(postSelect);
+               }
+
+               #endregion
+       }
+}

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectStrategy.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectStrategy.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectStrategy.cs
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/PropertStrategy/SelectStrategy.cs
 Sun Jul 16 09:58:24 2006
@@ -57,12 +57,13 @@
             IPropertyStrategy selectArrayStrategy,
             IPropertyStrategy selectGenericListStrategy,
             IPropertyStrategy selectListStrategy,
-            IPropertyStrategy selectObjectStrategy)
+            IPropertyStrategy selectObjectStrategy,
+                       IPropertyStrategy selectDataTableStrategy)
                {
                        // Collection object or .NET object                     
                        if (mapping.SetAccessor.MemberType.BaseType == 
typeof(Array))
                        {
-                _selectStrategy = selectArrayStrategy;
+                               _selectStrategy = selectArrayStrategy;
                        }
 #if dotnet2
             else if (mapping.SetAccessor.MemberType.IsGenericType &&
@@ -71,16 +72,19 @@
                 _selectStrategy = selectGenericListStrategy;
             }
 #endif                 
-            // Check if the object to Map implement 'IList' or is IList type
-                       // If yes the ResultProperty is map to a IList object
-            else if 
(typeof(IList).IsAssignableFrom(mapping.SetAccessor.MemberType))
+                               // Check if the object to Map implement 'IList' 
or is IList type
+                               // If yes the ResultProperty is map to a IList 
object
+                       else if 
(typeof(IList).IsAssignableFrom(mapping.SetAccessor.MemberType))
                        {
-                _selectStrategy = selectListStrategy;
+                               _selectStrategy = selectListStrategy;
+                       }
+                       else if 
(typeof(DataTable).IsAssignableFrom(mapping.SetAccessor.MemberType))
+                       {
+                               _selectStrategy = selectDataTableStrategy;
                        }
-
                        else // The ResultProperty is map to a .Net object
                        {
-                _selectStrategy = selectObjectStrategy;
+                               _selectStrategy = selectObjectStrategy;
                        }
                }
        

Added: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/DataTableStrategy.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/DataTableStrategy.cs?rev=422475&view=auto
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/DataTableStrategy.cs
 (added)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/DataTableStrategy.cs
 Sun Jul 16 09:58:24 2006
@@ -0,0 +1,82 @@
+#region Apache Notice
+/*****************************************************************************
+ * $Revision: 374175 $
+ * $LastChangedDate: 2006-04-25 19:40:27 +0200 (mar., 25 avr. 2006) $
+ * $LastChangedBy: gbayon $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2006/2005 - The Apache Software Foundation
+ *  
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ 
********************************************************************************/
+#endregion
+
+using System.Collections;
+using System.Data;
+using IBatisNet.DataMapper.Configuration.ResultMapping;
+using IBatisNet.DataMapper.Scope;
+
+namespace IBatisNet.DataMapper.MappedStatements.ResultStrategy
+{
+       /// <summary>
+       /// <see cref="IResultStrategy"/> implementation when 
+       /// a 'resultClass' attribute is specified and
+       /// the type of the result object is <see cref="IDictionary"/>.
+       /// </summary>
+       public sealed class DataTableStrategy : IResultStrategy
+       {
+               #region IResultStrategy Members
+
+               /// <summary>
+               /// Processes the specified <see cref="IDataReader"/> 
+               /// when a 'resultClass' attribute is specified on the 
statement and
+               /// the 'resultClass' attribute is a <see cref="DataTable"/>.
+               /// </summary>
+               /// <param name="request">The request.</param>
+               /// <param name="reader">The reader.</param>
+               /// <param name="resultObject">The result object.</param>
+               public object Process(RequestScope request, IDataReader reader, 
object resultObject)
+               {
+                       object outObject = resultObject; 
+
+                       if (outObject == null) 
+                       {
+                               outObject = new DataTable(request.Statement.Id);
+
+                               DataTable dataColumn = reader.GetSchemaTable();
+                               for (int i = 0; i < dataColumn.Rows.Count; i++)
+                               {
+                                       ((DataTable) 
outObject).Columns.Add(dataColumn.Rows[i][0].ToString());
+                               }
+                       }
+
+                       DataRow dataRow = ((DataTable) outObject).NewRow();
+                       int count = reader.FieldCount;
+                       for (int i = 0; i < count; i++) 
+                       {
+                               ResultProperty property = new ResultProperty();
+                               property.PropertyName = "value";
+                               property.ColumnIndex = i;
+                               property.TypeHandler = 
request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(i));
+                               dataRow[i] = property.GetDataBaseValue(reader);
+                       }   
+                       ((DataTable) outObject).Rows.Add(dataRow);
+     
+                       return outObject;
+               }
+
+               #endregion
+       }
+}
\ No newline at end of file

Modified: 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/ResultClassStrategy.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/ResultClassStrategy.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/ResultClassStrategy.cs
 (original)
+++ 
ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/MappedStatements/ResultStrategy/ResultClassStrategy.cs
 Sun Jul 16 09:58:24 2006
@@ -38,6 +38,7 @@
                private static IResultStrategy _simpleTypeStrategy = null;
                private static IResultStrategy _dictionaryStrategy = null;
                private static IResultStrategy _listStrategy = null;
+               private static IResultStrategy _dataTableStrategy = null;
                private static IResultStrategy _autoMapStrategy = null;
 
                /// <summary>
@@ -48,6 +49,7 @@
                        _simpleTypeStrategy = new SimpleTypeStrategy();
                        _dictionaryStrategy = new DictionaryStrategy();
                        _listStrategy = new ListStrategy();
+                       _dataTableStrategy = new DataTableStrategy();
                        _autoMapStrategy = new AutoMapStrategy();
                }
 
@@ -66,13 +68,17 @@
                        {
                                return _simpleTypeStrategy.Process(request, 
reader, resultObject);
                        }
-            else if 
(typeof(IDictionary).IsAssignableFrom(request.Statement.ResultClass)) 
+                       else if 
(typeof(IDictionary).IsAssignableFrom(request.Statement.ResultClass)) 
                        {
                                return _dictionaryStrategy.Process(request, 
reader, resultObject);
                        }
-            else if 
(typeof(IList).IsAssignableFrom(request.Statement.ResultClass)) 
+                       else if 
(typeof(IList).IsAssignableFrom(request.Statement.ResultClass)) 
                        {
                                return _listStrategy.Process(request, reader, 
resultObject);
+                       }
+                       else if 
(typeof(DataTable).IsAssignableFrom(request.Statement.ResultClass))
+                       {
+                               return _dataTableStrategy.Process(request, 
reader, resultObject);
                        }
                        else
                        {

Modified: ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/SqlMapper.cs
URL: 
http://svn.apache.org/viewvc/ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/SqlMapper.cs?rev=422475&r1=422474&r2=422475&view=diff
==============================================================================
--- ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/SqlMapper.cs 
(original)
+++ ibatis/branches/IBatisNet.DataMapper-QueryForDataTable/SqlMapper.cs Sun Jul 
16 09:58:24 2006
@@ -53,6 +53,7 @@
 
 namespace IBatisNet.DataMapper
 {
+
        /// <summary>
        /// Summary description for SqlMap.
        /// </summary>
@@ -611,7 +612,41 @@
                }
 
                #endregion
-               
+
+               public DataTable QueryForDataTable(string statementName, object 
parameterObject)
+               {
+                       bool isSessionLocal = false;
+                       IDalSession session = _sessionHolder.LocalSession;
+                       DataTable dataTable = null;
+ 
+                       if (session == null) 
+                       {
+                               session = new SqlMapSession(this);
+                               session.OpenConnection();
+                               isSessionLocal = true;
+                       }
+
+                       try 
+                       {
+                               IMappedStatement statement = 
GetMappedStatement(statementName);
+                               dataTable = 
statement.ExecuteQueryForDataTable(session, parameterObject);
+                       } 
+                       catch
+                       {
+                               throw;
+                       }
+                       finally
+                       {
+                               if ( isSessionLocal )
+                               {
+                                       session.CloseConnection();
+                               }
+                       }
+
+                       return dataTable;
+                       
+               }
+
                #region QueryForObject
   
                /// <summary>
@@ -626,22 +661,24 @@
                /// <returns> The single result object populated with the 
result set data.</returns>
                public object QueryForObject(string statementName, object 
parameterObject)
                {
-                       bool isSessionLocal = false;
+                       bool isSessionLocal = true;
+
+                       IMappedStatement statement = 
GetMappedStatement(statementName);
+
                        IDalSession session = _sessionHolder.LocalSession;
-                       object result;
- 
                        if (session == null) 
                        {
-                               session = new SqlMapSession(this);
-                               session.OpenConnection();
                                isSessionLocal = true;
+                               session = new SqlMapSession(this);
+
+                               // delay calling OpenConnection
+                               statement.PreExecute += new 
ExecuteEventHandler(statement_PreExecute);
                        }
 
-                       try 
+                       try
                        {
-                               IMappedStatement statement = 
GetMappedStatement(statementName);
-                               result = 
statement.ExecuteQueryForObject(session, parameterObject);
-                       } 
+                               return statement.ExecuteQueryForObject(session, 
parameterObject);
+                       }
                        catch
                        {
                                throw;
@@ -653,8 +690,6 @@
                                        session.CloseConnection();
                                }
                        }
-
-                       return result;
                }
 
                /// <summary>
@@ -1653,5 +1688,11 @@
                #endregion
 
                #endregion
+
+               private void statement_PreExecute(object sender, 
ExecuteEventArgs e)
+               {
+            e.RequestScope.Session.OpenConnection();
+                       e.RequestScope.IDbCommand.Connection = 
e.RequestScope.Session.Connection;
+               }
        }
 }


Reply via email to