#region Apache Notice
/*****************************************************************************
* $Header: $
* $Revision: 575902 $
* $Date$
*
* 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
#region Using
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using Apache.Ibatis.Common.Contracts;
using Apache.Ibatis.Common.Logging;
using Apache.Ibatis.DataMapper.DataExchange;
using Apache.Ibatis.DataMapper.TypeHandlers;
#endregion
namespace Apache.Ibatis.DataMapper.Model.ParameterMapping
{
///
/// Summary description for ParameterMap.
///
[DebuggerDisplay("ParameterMap: {Id}-{ClassName}")]
public class ParameterMap
{
#region private
private static readonly ILog logger = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType );
private readonly string id = string.Empty;
// Properties list
private readonly ParameterPropertyCollection properties = new ParameterPropertyCollection();
// Same list as properties but without doubled (Test UpdateAccountViaParameterMap2)
private readonly ParameterPropertyCollection propertiesList = new ParameterPropertyCollection();
//(property Name, property)
private readonly IDictionary propertiesMap = new Dictionary(); // Corrected ?? Support Request 1043181, move to HashTable
private readonly string extendMap = string.Empty;
private readonly bool usePositionalParameters = false;
private readonly string className = string.Empty;
private readonly Type parameterClass = null;
private readonly IDataExchange dataExchange = null;
#endregion
#region Properties
///
/// Gets or sets a value indicating whether this ParameterMap has output parameter.
///
///
/// true if this instance has output parameter; otherwise, false.
///
public bool HasOutputParameter { get; set;}
///
/// The parameter class name.
///
public string ClassName
{
get { return className; }
}
///
/// The parameter type class.
///
public Type Class
{
get { return parameterClass; }
}
///
/// Identifier used to identify the ParameterMap amongst the others.
///
public string Id
{
get { return id; }
}
///
/// The collection of ParameterProperty
///
public ParameterPropertyCollection Properties
{
get { return properties; }
}
///
/// Gets the properties list.
///
/// The properties list.
public ParameterPropertyCollection PropertiesList
{
get { return propertiesList; }
}
///
/// Extend Parametermap attribute
///
/// The id of a ParameterMap
public string ExtendMap
{
get { return extendMap; }
}
///
/// Sets the IDataExchange
///
public IDataExchange DataExchange
{
get { return dataExchange; }
}
#endregion
#region Constructor (s) / Destructor
///
/// Initializes a new instance of the class.
///
/// The id.
/// Name of the class.
/// The extend map.
/// The type.
/// The data exchange.
/// if set to true [use positional parameters].
public ParameterMap(
string id,
string className,
string extendMap,
Type type,
IDataExchange dataExchange,
bool usePositionalParameters)
{
Contract.Require.That(id, Is.Not.Null & Is.Not.Empty).When("retrieving argument id in ParameterMap constructor");
Contract.Require.That(dataExchange, Is.Not.Null & Is.Not.Empty).When("retrieving argument dataExchange in ParameterMap constructor for id " + id );
HasOutputParameter = false;
if (logger.IsInfoEnabled)
{
if (string.IsNullOrEmpty(className))
{
logger.Info("The class attribute is recommended for better performance in a ParameterMap tag '" + id + "'.");
}
}
this.id = id;
this.className = className;
this.usePositionalParameters = usePositionalParameters;
parameterClass = type;
this.dataExchange = dataExchange;
this.extendMap = extendMap;
}
#endregion
#region Methods
///
/// Get the ParameterProperty at index.
///
/// Index
/// A ParameterProperty
public ParameterProperty GetProperty(int index)
{
if (usePositionalParameters) //obdc/oledb
{
return properties[index];
}
return propertiesList[index];
}
///
/// Get a ParameterProperty by his name.
///
/// The name of the ParameterProperty
/// A ParameterProperty
public ParameterProperty GetProperty(string name)
{
return propertiesMap[name];
}
///
/// Add a ParameterProperty to the ParameterProperty list.
///
///
public void AddParameterProperty(ParameterProperty property)
{
if (property.Direction == ParameterDirection.Output ||
property.Direction == ParameterDirection.InputOutput)
{
HasOutputParameter = true;
}
// These mappings will replace any mappings that this map
// had for any of the keys currently in the specified map.
propertiesMap[property.PropertyName] = property;
properties.Add( property );
if (propertiesList.Contains(property) == false)
{
propertiesList.Add( property );
}
}
///
/// Insert a ParameterProperty ine the ParameterProperty list at the specified index..
///
///
/// The zero-based index at which ParameterProperty should be inserted.
///
/// The ParameterProperty to insert.
public void InsertParameterProperty(int index, ParameterProperty property)
{
if (property.Direction == ParameterDirection.Output ||
property.Direction == ParameterDirection.InputOutput)
{
HasOutputParameter = true;
}
// These mappings will replace any mappings that this map
// had for any of the keys currently in the specified map.
propertiesMap[property.PropertyName] = property;
properties.Insert( index, property );
if (propertiesList.Contains(property) == false)
{
propertiesList.Insert( index, property );
}
}
///
/// Retrieve the index for array property
///
///
///
public int GetParameterIndex(string propertyName)
{
int index = -1;
index = Convert.ToInt32(propertyName.Replace("[", "").Replace("]", ""));
return index;
}
///
/// Get all Parameter Property Name
///
/// A string array
public string[] GetPropertyNameArray()
{
string[] propertyNameArray = new string[propertiesMap.Count];
for (int index=0;index
/// Set parameter value, replace the null value if any.
///
///
///
///
public void SetParameter(ParameterProperty mapping, IDataParameter dataParameter, object parameterValue)
{
object value = dataExchange.GetData(mapping, parameterValue);
ITypeHandler typeHandler = mapping.TypeHandler;
// Apply Null Value
if (mapping.HasNullValue)
{
if (typeHandler.Equals(value, mapping.NullValue))
{
value = null;
}
}
typeHandler.SetParameter(dataParameter, value, mapping.DbType);
}
///
/// Set output parameter value.
///
/// The target.
/// The mapping.
/// The data base value.
public void SetOutputParameter(ref object target, ParameterProperty mapping, object dataBaseValue )
{
dataExchange.SetData(ref target, mapping, dataBaseValue);
}
#endregion
}
}