#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.Collections; #if dotnet2 using System.Collections.Generic; #endif using System.Data; using IBatisNet.DataMapper.Configuration.ResultMapping; using IBatisNet.DataMapper.Scope; namespace IBatisNet.DataMapper.MappedStatements.ResultStrategy { /// /// implementation when /// a 'groupBy' attribute is specified on the resultMap tag. /// /// N+1 Select solution public sealed class GroupByStrategy : BaseStrategy, IResultStrategy { #region IResultStrategy Members /// /// Processes the specified . /// /// The request. /// The reader. /// The result object. /// The result object public object Process(RequestScope request, ref IDataReader reader, object resultObject) { object outObject = resultObject; IResultMap resultMap = request.CurrentResultMap.ResolveSubMap(reader); string uniqueKey = GetUniqueKey(resultMap, request, reader); // Gets the [key, result object] already build IDictionary buildObjects = request.GetUniqueKeys(resultMap); if (buildObjects != null && buildObjects.Contains(uniqueKey)) { // Unique key is already known, so get the existing result object and process additional results. outObject = buildObjects[uniqueKey]; // process resulMapping attribute wich point to a groupBy attribute for (int index = 0; index < resultMap.Properties.Count; index++) { ResultProperty resultProperty = resultMap.Properties[index]; if (resultProperty.PropertyStrategy is PropertStrategy.GroupByStrategy) { resultProperty.PropertyStrategy.Set(request, resultMap, resultProperty, ref outObject, reader, null); } } outObject = SKIP; } else if (uniqueKey == null || buildObjects == null || !buildObjects.Contains(uniqueKey)) { // Unique key is NOT known, so create a new result object and process additional results. // Fix IBATISNET-241 if (outObject == null) { // temp ?, we don't support constructor tag with groupBy attribute outObject = resultMap.CreateInstanceOfResult(null); } for (int index = 0; index < resultMap.Properties.Count; index++) { ResultProperty resultProperty = resultMap.Properties[index]; resultProperty.PropertyStrategy.Set(request, resultMap, resultProperty, ref outObject, reader, null); } if (buildObjects == null) { buildObjects = new Hashtable(); request.SetUniqueKeys(resultMap, buildObjects); } buildObjects[uniqueKey] = outObject; } return outObject; } #endregion } }