#region Apache Notice /***************************************************************************** * $Revision: 575902 $ * $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 #region Using using System; using System.Collections; using System.Data; using System.Diagnostics; using System.Xml.Serialization; using Apache.Ibatis.Common.Contracts; using Apache.Ibatis.Common.Exceptions; using Apache.Ibatis.Common.Utilities; using Apache.Ibatis.Common.Utilities.Objects; using Apache.Ibatis.Common.Utilities.Objects.Members; using Apache.Ibatis.DataMapper.DataExchange; using Apache.Ibatis.DataMapper.TypeHandlers; #endregion namespace Apache.Ibatis.DataMapper.Model.ParameterMapping { /// /// Summary description for ParameterProperty. /// [Serializable] [DebuggerDisplay("ParameterProperty: {propertyName}-{columnName}")] public class ParameterProperty { #region Fields [NonSerialized] private readonly string nullValue = null; [NonSerialized] private readonly string propertyName = string.Empty; [NonSerialized] private ParameterDirection direction = ParameterDirection.Input; [NonSerialized] private readonly string directionAttribute = string.Empty; [NonSerialized] private readonly string dbType = null; [NonSerialized] private readonly int size = -1; [NonSerialized] private readonly byte scale = 0; [NonSerialized] private readonly byte precision = 0; [NonSerialized] private readonly string columnName = string.Empty; // used only for store procedure [NonSerialized] private ITypeHandler typeHandler = null; [NonSerialized] private readonly string clrType = string.Empty; [NonSerialized] private readonly string callBackName = string.Empty; [NonSerialized] private readonly IGetAccessor getAccessor = null; [NonSerialized] private readonly bool isComplexMemberName = false; #endregion #region Properties /// /// Indicate if we have a complex member name as [avouriteLineItem.Id] /// public bool IsComplexMemberName { get { return isComplexMemberName; } } /// /// Specify the custom type handlers to used. /// /// Will be an alias to a class wchic implement ITypeHandlerCallback public string CallBackName { get { return callBackName; } } /// /// Specify the CLR type of the parameter. /// /// /// The type attribute is used to explicitly specify the property type to be read. /// Normally this can be derived from a property through reflection, but certain mappings such as /// HashTable cannot provide the type to the framework. /// public string CLRType { get { return clrType; } } /// /// The typeHandler used to work with the parameter. /// public ITypeHandler TypeHandler { get { return typeHandler; } set { typeHandler = value; } } /// /// Column Name for output parameter /// in store proccedure. /// public string ColumnName { get { return columnName; } } /// /// Column size. /// [XmlAttribute("size")] public int Size { get { return size; } } /// /// Column Scale. /// public byte Scale { get { return scale; } } /// /// Column Precision. /// public byte Precision { get { return precision; } } /// /// Give an entry in the 'DbType' enumeration /// /// /// For Sql Server, give an entry of SqlDbType : Bit, Decimal, Money... ///
/// For Oracle, give an OracleType Enumeration : Byte, Int16, Number... ///
public string DbType { get { return dbType; } } /// /// The direction attribute of the XML parameter. /// /// Input, Output, InputOutput public string DirectionAttribute { get { return directionAttribute; } } /// /// Indicate the direction of the parameter. /// /// Input, Output, InputOutput public ParameterDirection Direction { get { return direction; } set { direction = value; } } /// /// Property name used to identify the property amongst the others. /// /// EmailAddress public string PropertyName { get { return propertyName; } } /// /// Tell if a nullValue is defined._nullValue!=null /// public bool HasNullValue { get { return (nullValue!=null); } } /// /// Null value replacement. /// /// "no_email@provided.com" public string NullValue { get { return nullValue; } } /// /// Defines a field/property get accessor /// public IGetAccessor GetAccessor { get { return getAccessor; } } #endregion /// /// Initializes a new instance of the class. /// /// Name of the property. /// Name of the column. /// Name of the call back. /// Type of the CLR. /// Type of the db. /// The direction attribute. /// The null value. /// The precision. /// The scale. /// The size. /// The parameter class. /// The data exchange factory. public ParameterProperty( string propertyName, string columnName, string callBackName, string clrType, string dbType, string directionAttribute, string nullValue, Byte precision, Byte scale, int size, Type parameterClass, DataExchangeFactory dataExchangeFactory ) { Contract.Require.That(propertyName, Is.Not.Null & Is.Not.Empty).When("retrieving argument propertyName in ParameterProperty constructor"); this.columnName = columnName; this.callBackName = callBackName; this.clrType = clrType; this.dbType = dbType; this.nullValue = nullValue; this.precision = precision; this.scale = scale; this.size = size; #region Direction if (directionAttribute.Length > 0) { direction = (ParameterDirection)Enum.Parse(typeof(ParameterDirection), directionAttribute, true); this.directionAttribute = direction.ToString(); } #endregion #region Property Name this.propertyName = propertyName; if (propertyName.IndexOf('.') < 0) { isComplexMemberName = false; } else // complex member name FavouriteLineItem.Id { isComplexMemberName = true; } #endregion #region GetAccessor if (!typeof(IDictionary).IsAssignableFrom(parameterClass) // Hashtable parameter map && parameterClass != null // value property && !dataExchangeFactory.TypeHandlerFactory.IsSimpleType(parameterClass)) // value property { if (!isComplexMemberName) { IGetAccessorFactory getAccessorFactory = dataExchangeFactory.AccessorFactory.GetAccessorFactory; getAccessor = getAccessorFactory.CreateGetAccessor(parameterClass, propertyName); } else // complex member name FavouriteLineItem.Id { string memberName = propertyName.Substring(propertyName.LastIndexOf('.') + 1); string parentName = propertyName.Substring(0, propertyName.LastIndexOf('.')); Type parentType = ObjectProbe.GetMemberTypeForGetter(parameterClass, parentName); IGetAccessorFactory getAccessorFactory = dataExchangeFactory.AccessorFactory.GetAccessorFactory; getAccessor = getAccessorFactory.CreateGetAccessor(parentType, memberName); } } #endregion #region TypeHandler if (CallBackName.Length > 0) { try { Type type = dataExchangeFactory.TypeHandlerFactory.GetType(CallBackName); ITypeHandlerCallback typeHandlerCallback = (ITypeHandlerCallback)Activator.CreateInstance(type); typeHandler = new CustomTypeHandler(typeHandlerCallback); } catch (Exception e) { throw new ConfigurationException("Error occurred during custom type handler configuration. Cause: " + e.Message, e); } } else { if (CLRType.Length == 0) // Unknown { if (getAccessor != null && dataExchangeFactory.TypeHandlerFactory.IsSimpleType(getAccessor.MemberType)) { // Primitive typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(getAccessor.MemberType, dbType); } else { typeHandler = dataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } } else // If we specify a CLR type, use it { Type type = TypeUtils.ResolveType(CLRType); if (dataExchangeFactory.TypeHandlerFactory.IsSimpleType(type)) { // Primitive typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } else { // .NET object type = ObjectProbe.GetMemberTypeForGetter(parameterClass, PropertyName); typeHandler = dataExchangeFactory.TypeHandlerFactory.GetTypeHandler(type, dbType); } } } #endregion } #region Methods /// /// Determines whether the specified is equal to the current . /// /// The to compare with the current . /// /// true if the specified is equal to the current ; otherwise, false. /// public override bool Equals(object obj) { //Check for null and compare run-time types. if (obj == null || GetType() != obj.GetType()) return false; ParameterProperty p = (ParameterProperty)obj; return (PropertyName == p.PropertyName); } /// /// Serves as a hash function for a particular type. is suitable for use in hashing algorithms and data structures like a hash table. /// /// /// A hash code for the current . /// public override int GetHashCode() { return propertyName.GetHashCode(); } #endregion } }