#region Apache Notice
/*****************************************************************************
* $Revision: 374175 $
* $LastChangedDate$
* $LastChangedBy$
*
* iBATIS.NET Data Mapper
* Copyright (C) 2008/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.Collections.Generic;
using System.Data;
using Apache.Ibatis.DataMapper.Model.ResultMapping;
using Apache.Ibatis.DataMapper.Exceptions;
using Apache.Ibatis.DataMapper.Scope;
using Apache.Ibatis.Common.Contracts;
namespace Apache.Ibatis.DataMapper.MappedStatements.PropertyStrategy
{
///
/// implementation when a 'select' attribute exists
/// on a
///
public class SelectStrategy : IPropertyStrategy
{
private readonly IPropertyStrategy selectStrategy = null;
///
/// Initializes a new instance of the class.
///
/// The mapping.
/// The select array strategy.
/// The select generic list strategy.
/// The select list strategy.
/// The select object strategy.
public SelectStrategy(ResultProperty mapping,
IPropertyStrategy selectArrayStrategy,
IPropertyStrategy selectGenericListStrategy,
IPropertyStrategy selectListStrategy,
IPropertyStrategy selectObjectStrategy)
{
Contract.Require.That(selectArrayStrategy, Is.Not.Null).When("retrieving argument selectArrayStrategy in SelectStrategy constructor");
Contract.Require.That(selectGenericListStrategy, Is.Not.Null).When("retrieving argument selectGenericListStrategy in SelectStrategy constructor");
Contract.Require.That(selectListStrategy, Is.Not.Null).When("retrieving argument selectListStrategy in SelectStrategy constructor");
Contract.Require.That(selectObjectStrategy, Is.Not.Null).When("retrieving argument selectObjectStrategy in SelectStrategy constructor");
// Collection object or .NET object
if (mapping.SetAccessor.MemberType.BaseType == typeof(Array))
{
selectStrategy = selectArrayStrategy;
}
else if (mapping.SetAccessor.MemberType.IsGenericType &&
typeof(IList<>).IsAssignableFrom(mapping.SetAccessor.MemberType.GetGenericTypeDefinition()) )
{
selectStrategy = selectGenericListStrategy;
}
// 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;
}
else // The ResultProperty is map to a .Net object
{
selectStrategy = selectObjectStrategy;
}
}
#region IPropertyStrategy members
///
/// Sets value of the specified on the target object
/// when a 'select' attribute exists
/// on the are empties.
///
/// The request.
/// The result map.
/// The ResultProperty.
/// The target.
/// The reader.
/// The keys
public void Set(RequestScope request, IResultMap resultMap,
ResultProperty mapping, ref object target, IDataReader reader, object selectKeys)
{
string paramString = mapping.ColumnName;
object keys = null;
bool wasNull = false;
#region Finds the select keys.
if (paramString.IndexOf(',')>0 || paramString.IndexOf('=')>0) // composite parameters key
{
IDictionary keyMap = new Hashtable();
keys = keyMap;
// define which character is seperating fields
char[] splitter = {'=',','};
string[] paramTab = paramString.Split(splitter);
if (paramTab.Length % 2 != 0)
{
throw new DataMapperException("Invalid composite key string format in '"+mapping.PropertyName+". It must be: property1=column1,property2=column2,...");
}
IEnumerator enumerator = paramTab.GetEnumerator();
while (!wasNull && enumerator.MoveNext())
{
string hashKey = ((string)enumerator.Current).Trim();
if (paramString.Contains("="))// old 1.x style multiple params
{
enumerator.MoveNext();
}
object hashValue = reader.GetValue(reader.GetOrdinal(((string)enumerator.Current).Trim()));
keyMap.Add(hashKey, hashValue );
wasNull = (hashValue == DBNull.Value);
}
}
else // single parameter key
{
keys = reader.GetValue(reader.GetOrdinal(paramString));
wasNull = reader.IsDBNull(reader.GetOrdinal(paramString));
}
#endregion
if (wasNull)
{
// set the value of an object property to null
mapping.Set(target, null);
}
else // Collection object or .Net object
{
selectStrategy.Set(request, resultMap, mapping, ref target, reader, keys);
}
}
///
/// Gets the value of the specified that must be set on the target object.
///
/// The request.
/// The result map.
/// The mapping.
/// The reader.
/// The target object
public object Get(RequestScope request, IResultMap resultMap, ResultProperty mapping, ref object target, IDataReader reader)
{
throw new NotSupportedException("Get method on ResultMapStrategy is not supported");
}
#endregion
}
}