Author: gbayon Date: Wed Jun 11 11:40:13 2008 New Revision: 666779 URL: http://svn.apache.org/viewvc?rev=666779&view=rev Log: Fix IBATISNET-272
Added: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/DataTableStrategy.cs (with props) Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.Common/Utilities/TypesResolver/TypeRegistry.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Fixtures/Mapping/StatementTest.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Maps/Account.xml ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Apache.Ibatis.DataMapper.20005.csproj ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/DataMapper.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/IDataMapper.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/CachingStatement.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/IMappedStatement.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatement.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/MapStrategy.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/ResultClassStrategy.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/ResultMapping/ResultProperty.cs ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/TypeHandlers/TypeHandlerFactory.cs Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.Common/Utilities/TypesResolver/TypeRegistry.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.Common/Utilities/TypesResolver/TypeRegistry.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.Common/Utilities/TypesResolver/TypeRegistry.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.Common/Utilities/TypesResolver/TypeRegistry.cs Wed Jun 11 11:40:13 2008 @@ -30,6 +30,7 @@ using System.Collections; using System.Collections.Specialized; using System.Collections.Generic; +using System.Data; #endregion @@ -129,6 +130,15 @@ /// </summary> public const string HashtableAlias3 = "hashmap"; + /// <summary> + /// The alias around the 'DataTable' type. + /// </summary> + public const string DataTableAlias1 = "datatable"; + /// <summary> + /// Another alias around the 'DataTable' type. + /// </summary> + public const string DataTableAlias2 = "data"; + /// <summary> /// The alias around the 'short' type. /// </summary> @@ -316,7 +326,7 @@ #endregion #region Fields - private static IDictionary<string, Type> _types = new Dictionary<string, Type>(); + private static readonly IDictionary<string, Type> types = new Dictionary<string, Type>(); #endregion #region Constructor (s) / Destructor @@ -338,89 +348,92 @@ static TypeRegistry() { // Initialize a dictionary with some fully qualifiaed name - _types[ArrayListAlias1] = typeof (ArrayList); - _types[ArrayListAlias2] = typeof (ArrayList); + types[DataTableAlias1] = typeof(DataTable); + types[DataTableAlias2] = typeof(DataTable); + + types[ArrayListAlias1] = typeof (ArrayList); + types[ArrayListAlias2] = typeof (ArrayList); - _types[BoolAlias] = typeof (bool); - _types[BooleanAlias] = typeof (bool); + types[BoolAlias] = typeof (bool); + types[BooleanAlias] = typeof (bool); - _types[ByteAlias] = typeof (byte); + types[ByteAlias] = typeof (byte); - _types[CharAlias] = typeof (char); + types[CharAlias] = typeof (char); - _types[DateAlias1] = typeof (DateTime); - _types[DateAlias2] = typeof (DateTime); + types[DateAlias1] = typeof (DateTime); + types[DateAlias2] = typeof (DateTime); - _types[DecimalAlias] = typeof (decimal); + types[DecimalAlias] = typeof (decimal); - _types[DoubleAlias] = typeof (double); + types[DoubleAlias] = typeof (double); - _types[FloatAlias] = typeof (float); - _types[SingleAlias] = typeof (float); + types[FloatAlias] = typeof (float); + types[SingleAlias] = typeof (float); - _types[GuidAlias] = typeof (Guid); + types[GuidAlias] = typeof (Guid); - _types[HashtableAlias1] = typeof (Hashtable); - _types[HashtableAlias2] = typeof (Hashtable); - _types[HashtableAlias3] = typeof (Hashtable); + types[HashtableAlias1] = typeof (Hashtable); + types[HashtableAlias2] = typeof (Hashtable); + types[HashtableAlias3] = typeof (Hashtable); - _types[Int16Alias1] = typeof (short); - _types[Int16Alias2] = typeof (short); + types[Int16Alias1] = typeof (short); + types[Int16Alias2] = typeof (short); - _types[Int32Alias1] = typeof (int); - _types[Int32Alias2] = typeof (int); - _types[Int32Alias3] = typeof (int); + types[Int32Alias1] = typeof (int); + types[Int32Alias2] = typeof (int); + types[Int32Alias3] = typeof (int); - _types[Int64Alias1] = typeof (long); - _types[Int64Alias2] = typeof (long); + types[Int64Alias1] = typeof (long); + types[Int64Alias2] = typeof (long); - _types[UInt16Alias1] = typeof (ushort); - _types[UInt16Alias2] = typeof (ushort); + types[UInt16Alias1] = typeof (ushort); + types[UInt16Alias2] = typeof (ushort); - _types[UInt32Alias1] = typeof (uint); - _types[UInt32Alias2] = typeof (uint); + types[UInt32Alias1] = typeof (uint); + types[UInt32Alias2] = typeof (uint); - _types[UInt64Alias1] = typeof (ulong); - _types[UInt64Alias2] = typeof (ulong); + types[UInt64Alias1] = typeof (ulong); + types[UInt64Alias2] = typeof (ulong); - _types[SByteAlias] = typeof (sbyte); + types[SByteAlias] = typeof (sbyte); - _types[StringAlias] = typeof (string); + types[StringAlias] = typeof (string); - _types[TimeSpanAlias] = typeof (string); + types[TimeSpanAlias] = typeof (string); - _types[NullableInt32Alias] = typeof(int?); - _types[NullableInt32ArrayAlias] = typeof(int?[]); + types[NullableInt32Alias] = typeof(int?); + types[NullableInt32ArrayAlias] = typeof(int?[]); - _types[NullableDecimalAlias] = typeof(decimal?); - _types[NullableDecimalArrayAlias] = typeof(decimal?[]); + types[NullableDecimalAlias] = typeof(decimal?); + types[NullableDecimalArrayAlias] = typeof(decimal?[]); - _types[NullableCharAlias] = typeof(char?); - _types[NullableCharArrayAlias] = typeof(char?[]); + types[NullableCharAlias] = typeof(char?); + types[NullableCharArrayAlias] = typeof(char?[]); - _types[NullableInt64Alias] = typeof(long?); - _types[NullableInt64ArrayAlias] = typeof(long?[]); + types[NullableInt64Alias] = typeof(long?); + types[NullableInt64ArrayAlias] = typeof(long?[]); - _types[NullableInt16Alias] = typeof(short?); - _types[NullableInt16ArrayAlias] = typeof(short?[]); + types[NullableInt16Alias] = typeof(short?); + types[NullableInt16ArrayAlias] = typeof(short?[]); - _types[NullableUInt32Alias] = typeof(uint?); - _types[NullableUInt32ArrayAlias] = typeof(uint?[]); + types[NullableUInt32Alias] = typeof(uint?); + types[NullableUInt32ArrayAlias] = typeof(uint?[]); - _types[NullableUInt64Alias] = typeof(ulong?); - _types[NullableUInt64ArrayAlias] = typeof(ulong?[]); + types[NullableUInt64Alias] = typeof(ulong?); + types[NullableUInt64ArrayAlias] = typeof(ulong?[]); - _types[NullableUInt16Alias] = typeof(ushort?); - _types[NullableUInt16ArrayAlias] = typeof(ushort?[]); + types[NullableUInt16Alias] = typeof(ushort?); + types[NullableUInt16ArrayAlias] = typeof(ushort?[]); - _types[NullableDoubleAlias] = typeof(double?); - _types[NullableDoubleArrayAlias] = typeof(double?[]); + types[NullableDoubleAlias] = typeof(double?); + types[NullableDoubleArrayAlias] = typeof(double?[]); - _types[NullableFloatAlias] = typeof(float?); - _types[NullableFloatArrayAlias] = typeof(float?[]); + types[NullableFloatAlias] = typeof(float?); + types[NullableFloatArrayAlias] = typeof(float?[]); - _types[NullableBoolAlias] = typeof(bool?); - _types[NullableBoolArrayAlias] = typeof(bool?[]); + types[NullableBoolAlias] = typeof(bool?); + types[NullableBoolArrayAlias] = typeof(bool?[]); } #endregion @@ -446,7 +459,7 @@ public static Type ResolveType(string alias) { Type type = null; - _types.TryGetValue(alias.ToLower(), out type); + types.TryGetValue(alias.ToLower(), out type); return type; } Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Fixtures/Mapping/StatementTest.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Fixtures/Mapping/StatementTest.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Fixtures/Mapping/StatementTest.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Fixtures/Mapping/StatementTest.cs Wed Jun 11 11:40:13 2008 @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Data; using Apache.Ibatis.Common.Exceptions; using Apache.Ibatis.Common.Utilities; using Apache.Ibatis.DataMapper.Model; @@ -7,6 +8,7 @@ using Apache.Ibatis.DataMapper.SqlClient.Test.Domain; using Apache.Ibatis.DataMapper.SqlClient.Test.Fixtures; using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; namespace Apache.Ibatis.DataMapper.SqlClient.Test.Fixtures.Mapping { @@ -43,6 +45,60 @@ #region Object Query tests + + [Test] + public void QueryForDatatable_with_resulclass_should_work() + { + DataTable dataTable = dataMapper.QueryForDataTable("SimpleAccountDataTable", 1); + + DataRow[] currentRows = dataTable.Select(null, null, DataViewRowState.CurrentRows); + + //if (currentRows.Length < 1) + // Console.WriteLine("No Current Rows Found"); + //else + //{ + // foreach (DataColumn column in dataTable.Columns) + // Console.Write("\t{0}", column.ColumnName); + + // Console.WriteLine("\tRowState"); + + // foreach (DataRow row in currentRows) + // { + // foreach (DataColumn column in dataTable.Columns) + // Console.Write("\t{0}", row[column]); + + // Console.WriteLine("\t" + row.RowState); + // } + //} + + + Assert.That(dataTable, Is.Not.Null); + Assert.That(currentRows.Length, Is.GreaterThan(0)); + Assert.That(dataTable.Columns[0].ColumnName, Is.EqualTo("Account_ID")); + Assert.That(dataTable.Columns[1].ColumnName, Is.EqualTo("Account_FirstName")); + Assert.That(currentRows[0][0], Is.EqualTo(1)); + Assert.That(currentRows[0][1], Is.EqualTo("Joe")); + } + + [Test] + public void QueryForDatatable_with_resulmap_should_work() + { + DataTable dataTable = dataMapper.QueryForDataTable("SimpleAccountDataTableViaResultMap", 1); + + DataRow[] currentRows = dataTable.Select(null, null, DataViewRowState.CurrentRows); + + Assert.That(dataTable, Is.Not.Null); + Assert.That(currentRows.Length, Is.GreaterThan(0)); + Assert.That(dataTable.Columns[0].ColumnName, Is.EqualTo("Id")); + Assert.That(dataTable.Columns[0].DataType, Is.EqualTo(typeof(int))); + + Assert.That(dataTable.Columns[1].ColumnName, Is.EqualTo("FirstName")); + Assert.That(dataTable.Columns[1].DataType, Is.EqualTo(typeof(string))); + + Assert.That(currentRows[0][0], Is.EqualTo(1)); + Assert.That(currentRows[0][1], Is.EqualTo("Joe")); + } + [Test] public void Statement_with_SqlSource_should_work() { Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Maps/Account.xml URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Maps/Account.xml?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Maps/Account.xml (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper.SqlClient.Test.2005/Maps/Account.xml Wed Jun 11 11:40:13 2008 @@ -30,6 +30,11 @@ </cacheModels> <resultMaps> + + <resultMap id="Account-result-DataTable" class="dataTable"> + <result property="Id" column="Account_ID"/> + <result property="FirstName" column="Account_FirstName"/> + </resultMap> <resultMap id="Category-result" class="Category2"> <result property="Id" column="Category_ID"/> @@ -140,6 +145,17 @@ --> <statements> + <!-- Query for DataTable --> + <select id="SimpleAccountDataTableViaResultMap" parameterClass="int" resultMap="Account-result-DataTable"> + select * + from Accounts + </select> + + <select id="SimpleAccountDataTable" parameterClass="int" resultClass="data"> + select * + from Accounts + </select> + <!-- external sql source --> <select id="SimpleSqlSource" sqlSource="SimpleSqlSource" resultMap="account-result"/> <select id="SqlSourceWithParameter" parameterClass="Integer" sqlSource="SqlSourceWithParameter" resultMap="account-result"/> Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Apache.Ibatis.DataMapper.20005.csproj URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Apache.Ibatis.DataMapper.20005.csproj?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Apache.Ibatis.DataMapper.20005.csproj (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Apache.Ibatis.DataMapper.20005.csproj Wed Jun 11 11:40:13 2008 @@ -120,6 +120,7 @@ <Compile Include="Configuration\Module\ResultMapModuleBuilder.cs" /> <Compile Include="Configuration\Module\TypeAliasModuleBuilder.cs" /> <Compile Include="Configuration\Module\TypeHandlerModuleBuilder.cs" /> + <Compile Include="MappedStatements\ResultStrategy\DataTableStrategy.cs" /> <Compile Include="Session\DataMapperLocalSessionScope.cs" /> <Compile Include="IMapperFactory.cs" /> <Compile Include="Configuration\Interpreters\Config\AbstractInterpreter.cs" /> Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/DataMapper.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/DataMapper.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/DataMapper.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/DataMapper.cs Wed Jun 11 11:40:13 2008 @@ -26,7 +26,7 @@ using System.Collections; using System.Collections.Generic; - +using System.Data; using Apache.Ibatis.Common.Contracts; using Apache.Ibatis.DataMapper.Session; using Apache.Ibatis.DataMapper.Model; @@ -486,6 +486,22 @@ return Update(statementId, parameterObject); } + /// <summary> + /// Executes a SQL SELECT statement that returns data + /// to populate a DataTable. + /// If a resultMap is specified, the column name will be the result property name. + /// </summary> + /// <param name="statementId">The statement id.</param> + /// <param name="parameterObject">The parameter object.</param> + /// <returns>A DataTable</returns> + public DataTable QueryForDataTable(string statementId, object parameterObject) + { + using (DataMapperLocalSessionScope sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory)) + { + IMappedStatement statement = modelStore.GetMappedStatement(statementId); + return statement.ExecuteQueryForDataTable(sessionScope.Session, parameterObject); + } + } #endregion #region IModelStoreAccessor Members Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/IDataMapper.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/IDataMapper.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/IDataMapper.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/IDataMapper.cs Wed Jun 11 11:40:13 2008 @@ -25,6 +25,7 @@ using System.Collections; using System.Collections.Generic; +using System.Data; using Apache.Ibatis.DataMapper.Exceptions; namespace Apache.Ibatis.DataMapper @@ -295,6 +296,15 @@ /// <param name="rowDelegate"></param> /// <returns>A List of result objects.</returns> IList<T> QueryWithRowDelegate<T>(string statementName, object parameterObject, RowDelegate<T> rowDelegate); - + + /// <summary> + /// Executes a SQL SELECT statement that returns data + /// to populate a DataTable. + /// If a resultMap is specified, the column name will be the result property name. + /// </summary> + /// <param name="statementId">The statement id.</param> + /// <param name="parameterObject">The parameter object.</param> + /// <returns>A DataTable</returns> + DataTable QueryForDataTable(string statementId, object parameterObject); } } Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/CachingStatement.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/CachingStatement.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/CachingStatement.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/CachingStatement.cs Wed Jun 11 11:40:13 2008 @@ -99,6 +99,17 @@ get { return mappedStatement.ModelStore; } } + /// <summary> + /// Executes an SQL statement that returns DataTable. + /// </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>The object</returns> + public DataTable ExecuteQueryForDataTable(ISession session, object parameterObject) + { + return mappedStatement.ExecuteQueryForDataTable(session, parameterObject); + } + /// <summary> /// Executes the SQL and retuns all rows selected in a map that is keyed on the property named /// in the keyProperty parameter. The value at each key will be the value of the property specified Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/IMappedStatement.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/IMappedStatement.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/IMappedStatement.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/IMappedStatement.cs Wed Jun 11 11:40:13 2008 @@ -27,6 +27,7 @@ #region Imports using System.Collections; using System.Collections.Generic; +using System.Data; using Apache.Ibatis.DataMapper.Data; using Apache.Ibatis.DataMapper.Model; using Apache.Ibatis.DataMapper.Model.Statements; @@ -261,7 +262,15 @@ /// <returns></returns> IList<T> ExecuteQueryForRowDelegate<T>(ISession session, object parameterObject, RowDelegate<T> rowDelegate); #endregion - - + + #region ExecuteQueryForDataTable + /// <summary> + /// Executes an SQL statement that returns DataTable. + /// </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>The object</returns> + DataTable ExecuteQueryForDataTable(ISession session, object parameterObject); + #endregion } } Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatement.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatement.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatement.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatement.cs Wed Jun 11 11:40:13 2008 @@ -1008,6 +1008,67 @@ #endregion + #region ExecuteQueryForDataTable + /// <summary> + /// Executes an SQL statement that returns DataTable. + /// </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>The object</returns> + public virtual DataTable ExecuteQueryForDataTable(ISession session, object parameterObject) + { + DataTable dataTable = null; + + object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject); + + RequestScope request = statement.Sql.GetRequestScope(this, param, session); + preparedCommand.Create(request, session, Statement, param); + + dataTable = RunQueryForForDataTable(request, session, param); + + return RaisePostEvent<DataTable, PostSelectEventArgs>(PostSelectEvent, param, dataTable); + } + + /// <summary> + /// Runs the query for for data table. + /// </summary> + /// <param name="request">The request.</param> + /// <param name="session">The session.</param> + /// <param name="parameterObject">The parameter object.</param> + /// <returns></returns> + internal DataTable RunQueryForForDataTable(RequestScope request, ISession session, object parameterObject) + { + DataTable dataTable = null; + + using (IDbCommand command = request.IDbCommand) + { + IDataReader reader = command.ExecuteReader(); + + try + { + // Get Results + while (reader.Read()) + { + dataTable = (DataTable)resultStrategy.Process(request, ref reader, dataTable); + } + } + finally + { + reader.Close(); + reader.Dispose(); + } + + // do we need ?? + //ExecuteDelayedLoad(request); + + // do we need ?? + //RetrieveOutputParameters(request, session, command, parameterObject); + } + + return dataTable; + } + #endregion + #endregion /// <summary> Added: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/DataTableStrategy.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/DataTableStrategy.cs?rev=666779&view=auto ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/DataTableStrategy.cs (added) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/DataTableStrategy.cs Wed Jun 11 11:40:13 2008 @@ -0,0 +1,153 @@ +#region Apache Notice +/***************************************************************************** + * $Revision: 374175 $ + * $LastChangedDate$ + * $LastChangedBy$ + * + * 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.Data; +using Apache.Ibatis.DataMapper.Model.ResultMapping; +using Apache.Ibatis.DataMapper.Scope; +using Apache.Ibatis.DataMapper.TypeHandlers; +using System; + +namespace Apache.Ibatis.DataMapper.MappedStatements.ResultStrategy +{ + /// <summary> + /// <see cref="IResultStrategy"/> implementation when + /// a 'resultClass' attribute is specified and + /// the type of the result object is <see cref="DataTable"/>. + /// </summary> + public class DataTableStrategy : IResultStrategy + { + #region IResultStrategy Members + + /// <summary> + /// Processes the specified <see cref="IDataReader"/>. + /// </summary> + /// <param name="request">The request.</param> + /// <param name="reader">The reader.</param> + /// <param name="resultObject">The result object.</param> + /// <returns></returns> + public object Process(RequestScope request, ref IDataReader reader, object resultObject) + { + IResultMap resultMap = request.CurrentResultMap.ResolveSubMap(reader); + DataTable dataTable = resultObject as DataTable; + + if (dataTable == null) + { + dataTable = new DataTable(resultMap.Id); + if (resultMap is AutoResultMap) + { + for (int index = 0; index < reader.FieldCount; index++) + { + string columnName = reader.GetName(index); + Type type = reader.GetFieldType(index); + + DataColumn column = new DataColumn(); + column.DataType = type; + column.ColumnName = columnName; + dataTable.Columns.Add(column); + } + } + else + { + for (int index = 0; index < resultMap.Properties.Count; index++) + { + ResultProperty property = resultMap.Properties[index]; + DataColumn column = new DataColumn(); + if (property.CLRType.Length > 0) + { + column.DataType = property.MemberType; + } + //column.DataType = reader.GetFieldType(reader.GetOrdinal(property.PropertyName)); + column.ColumnName = property.PropertyName; + dataTable.Columns.Add(column); + } + } + } + + DataRow row = null; + if (resultMap is AutoResultMap) + { + row = dataTable.NewRow(); + for (int index = 0; index < reader.FieldCount; index++) + { + string propertyName = reader.GetName(index); + int columnIndex = index; + ITypeHandler typeHandler = request.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(reader.GetFieldType(index)); + + ResultProperty property = new ResultProperty( + propertyName, + string.Empty, + columnIndex, + string.Empty, + string.Empty, + string.Empty, + false, + string.Empty, + null, + string.Empty, + typeof(DataTable), + request.DataExchangeFactory, + typeHandler); + + object value = property.GetDataBaseValue(reader); + if (value == null) + { + value = DBNull.Value; + } + row[property.PropertyName] = value; + } + } + else + { + // We must set the DataType before adding any data to the DataTable + for (int index = 0; index < resultMap.Properties.Count; index++) + { + ResultProperty property = resultMap.Properties[index]; + object value = property.GetDataBaseValue(reader); + if (value != null && property.CLRType.Length == 0) + { + dataTable.Columns[property.PropertyName].DataType = value.GetType(); + } + } + row = dataTable.NewRow(); + for (int index = 0; index < resultMap.Properties.Count; index++) + { + ResultProperty property = resultMap.Properties[index]; + object value = property.GetDataBaseValue(reader); + if (value == null) + { + value = DBNull.Value; + } + row[property.PropertyName] = value; + + } + } + dataTable.Rows.Add(row); + + return dataTable; + } + + #endregion + } +} Propchange: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/DataTableStrategy.cs ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/DataTableStrategy.cs ------------------------------------------------------------------------------ svn:keywords = Id LastChangedDate LastChangedBy Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/MapStrategy.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/MapStrategy.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/MapStrategy.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/MapStrategy.cs Wed Jun 11 11:40:13 2008 @@ -38,6 +38,7 @@ private static readonly IResultStrategy resultMapStrategy = null; private static readonly IResultStrategy groupByStrategy = null; private static readonly IResultStrategy cirularStrategy = null; + private static readonly IResultStrategy dataTableStrategy = null; /// <summary> /// Initializes the <see cref="MapStrategy"/> class. @@ -47,6 +48,7 @@ resultMapStrategy = new ResultMapStrategy(); groupByStrategy = new GroupByStrategy(); cirularStrategy = new CirularStrategy(); + dataTableStrategy = new DataTableStrategy(); } #region IResultStrategy Members @@ -69,6 +71,10 @@ { return cirularStrategy.Process(request, ref reader, resultObject); } + else if (typeof(DataTable).IsAssignableFrom(resultMap.Class)) + { + return dataTableStrategy.Process(request, ref reader, resultObject); + } else { return resultMapStrategy.Process(request, ref reader, resultObject); Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/ResultClassStrategy.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/ResultClassStrategy.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/ResultClassStrategy.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/ResultStrategy/ResultClassStrategy.cs Wed Jun 11 11:40:13 2008 @@ -35,20 +35,23 @@ /// </summary> public sealed class ResultClassStrategy : IResultStrategy { - private static IResultStrategy _simpleTypeStrategy = null; - private static IResultStrategy _dictionaryStrategy = null; - private static IResultStrategy _listStrategy = null; - private static IResultStrategy _autoMapStrategy = null; + private static IResultStrategy simpleTypeStrategy = null; + private static IResultStrategy dictionaryStrategy = null; + private static IResultStrategy listStrategy = null; + private static IResultStrategy autoMapStrategy = null; + private static IResultStrategy dataTableStrategy = null; /// <summary> /// Initializes a new instance of the <see cref="ResultClassStrategy"/> class. /// </summary> public ResultClassStrategy() { - _simpleTypeStrategy = new SimpleTypeStrategy(); - _dictionaryStrategy = new DictionaryStrategy(); - _listStrategy = new ListStrategy(); - _autoMapStrategy = new AutoMapStrategy(); + simpleTypeStrategy = new SimpleTypeStrategy(); + dictionaryStrategy = new DictionaryStrategy(); + listStrategy = new ListStrategy(); + autoMapStrategy = new AutoMapStrategy(); + dataTableStrategy = new DataTableStrategy(); + } #region IResultStrategy Members @@ -65,19 +68,23 @@ // Check if the ResultClass is a 'primitive' Type if (request.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(request.CurrentResultMap.Class)) { - return _simpleTypeStrategy.Process(request, ref reader, resultObject); + return simpleTypeStrategy.Process(request, ref reader, resultObject); } else if (typeof(IDictionary).IsAssignableFrom(request.CurrentResultMap.Class)) { - return _dictionaryStrategy.Process(request, ref reader, resultObject); + return dictionaryStrategy.Process(request, ref reader, resultObject); } else if (typeof(IList).IsAssignableFrom(request.CurrentResultMap.Class)) { - return _listStrategy.Process(request, ref reader, resultObject); + return listStrategy.Process(request, ref reader, resultObject); } - else + else if (typeof(DataTable).IsAssignableFrom(request.CurrentResultMap.Class)) + { + return dataTableStrategy.Process(request, ref reader, resultObject); + } + else { - return _autoMapStrategy.Process(request, ref reader, resultObject); + return autoMapStrategy.Process(request, ref reader, resultObject); } } Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/ResultMapping/ResultProperty.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/ResultMapping/ResultProperty.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/ResultMapping/ResultProperty.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/ResultMapping/ResultProperty.cs Wed Jun 11 11:40:13 2008 @@ -395,7 +395,8 @@ if ( propertyName.Length>0 && propertyName != "value" && - !typeof(IDictionary).IsAssignableFrom(resultClass) ) + !typeof(IDictionary).IsAssignableFrom(resultClass)&& + !typeof(DataTable).IsAssignableFrom(resultClass)) { #region SetAccessor if (!isComplexMemberName) Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/TypeHandlers/TypeHandlerFactory.cs URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/TypeHandlers/TypeHandlerFactory.cs?rev=666779&r1=666778&r2=666779&view=diff ============================================================================== --- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/TypeHandlers/TypeHandlerFactory.cs (original) +++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/TypeHandlers/TypeHandlerFactory.cs Wed Jun 11 11:40:13 2008 @@ -29,6 +29,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Data; using System.Reflection; using Apache.Ibatis.Common.Exceptions; using Apache.Ibatis.Common.Logging; @@ -356,9 +357,9 @@ { handler = GetUnkownTypeHandler(); } - else if (typeof(IDictionary).IsAssignableFrom(classType)) + else if (typeof(IDictionary).IsAssignableFrom(classType) || typeof(DataTable).IsAssignableFrom(classType)) { - // IDictionary + // IDictionary or DataTable if (memberType == null || memberType.Length == 0) { handler = GetUnkownTypeHandler(); @@ -431,9 +432,9 @@ { handler = GetUnkownTypeHandler(); } - else if (typeof(IDictionary).IsAssignableFrom(argumentType)) + else if (typeof(IDictionary).IsAssignableFrom(argumentType) || typeof(DataTable).IsAssignableFrom(argumentType)) { - // IDictionary + // IDictionary or if (clrType == null || clrType.Length == 0) { handler = GetUnkownTypeHandler();