#region Apache Notice
/*****************************************************************************
* $Header: $
* $Revision: $
* $Date: $
*
* iBATIS.NET Data Mapper
* Copyright (C) 2004 - Gilles Bayon
*
*
* 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.Data;
using System.Xml.Serialization;
using IBatisNet.Common.Exceptions;
using IBatisNet.Common.Utilities;
using IBatisNet.Common.Utilities.Objects;
using IBatisNet.DataMapper.Scope;
using IBatisNet.DataMapper.TypeHandlers;
#endregion
namespace IBatisNet.DataMapper.Configuration.ParameterMapping
{
///
/// Summary description for ParameterProperty.
///
[Serializable]
[XmlRoot("parameter", Namespace="http://ibatis.apache.org/mapping")]
public class ParameterProperty
{
#region Fields
[NonSerialized]
private string _nullValue = null;//string.Empty;//null;
[NonSerialized]
private string _property = string.Empty;
[NonSerialized]
private ParameterDirection _direction = ParameterDirection.Input;
[NonSerialized]
private string _directionAttribute = string.Empty;
[NonSerialized]
private string _dbType = null;
[NonSerialized]
private int _size = -1;
[NonSerialized]
private byte _scale= 0;
[NonSerialized]
private byte _precision = 0;
[NonSerialized]
private string _columnName = string.Empty; // used only for store procedure
[NonSerialized]
private ITypeHandler _typeHandler = null;
[NonSerialized]
private string _clrType = string.Empty;
[NonSerialized]
private string _callBackName= string.Empty;
#endregion
#region Properties
///
/// Specify the custom type handlers to used.
///
/// Will be an alias to a class wchic implement ITypeHandlerCallback
[XmlAttribute("typeHandler")]
public string CallBackName
{
get { return _callBackName; }
set { _callBackName = value; }
}
///
/// 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.
///
[XmlAttribute("type")]
public string CLRType
{
get { return _clrType; }
set { _clrType = value; }
}
///
/// The typeHandler used to work with the parameter.
///
[XmlIgnore]
public ITypeHandler TypeHandler
{
get { return _typeHandler; }
set { _typeHandler = value; }
}
///
/// Column Name for output parameter
/// in store proccedure.
///
[XmlAttribute("column")]
public string ColumnName
{
get { return _columnName; }
set { _columnName = value; }
}
///
/// Column size.
///
[XmlAttribute("size")]
public int Size
{
get { return _size; }
set { _size = value; }
}
///
/// Column Scale.
///
[XmlAttribute("scale")]
public byte Scale
{
get { return _scale; }
set { _scale = value; }
}
///
/// Column Precision.
///
[XmlAttribute("precision")]
public byte Precision
{
get { return _precision; }
set { _precision = value; }
}
///
/// 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...
///
[XmlAttribute("dbType")]
public string DbType
{
get { return _dbType; }
set { _dbType = value; }
}
///
/// The direction attribute of the XML parameter.
///
/// Input, Output, InputOutput
[XmlAttribute("direction")]
public string DirectionAttribute
{
get { return _directionAttribute; }
set { _directionAttribute = value; }
}
///
/// Indicate the direction of the parameter.
///
/// Input, Output, InputOutput
[XmlIgnore]
public ParameterDirection Direction
{
get { return _direction; }
set
{
_direction = value;
_directionAttribute = _direction.ToString();
}
}
///
/// Property name used to identify the property amongst the others.
///
/// EmailAddress
[XmlAttribute("property")]
public string PropertyName
{
get { return _property; }
set
{
if ((value == null) || (value.Length < 1))
throw new ArgumentNullException("The property attribute is mandatory in a paremeter property.");
_property = value;
}
}
///
/// Tell if a nullValue is defined._nullValue!=null
///
[XmlIgnore]
public bool HasNullValue
{
get { return (_nullValue!=null); }
}
///
/// Null value replacement.
///
/// "no_email@provided.com"
[XmlAttribute("nullValue")]
public string NullValue
{
get { return _nullValue; }
set { _nullValue = value; }
}
#endregion
#region Constructor (s) / Destructor
///
/// Constructor
///
public ParameterProperty()
{
}
#endregion
#region Methods
///
///
///
///
///
public void Initialize(TypeHandlerFactory typeHandlerFactory, ErrorContext errorContext)
{
if(_directionAttribute.Length >0)
{
_direction = (ParameterDirection)Enum.Parse( typeof(ParameterDirection), _directionAttribute, true );
}
errorContext.MoreInfo = "Check the parameter mapping typeHandler attribute '" + this.CallBackName + "' (must be a ITypeHandlerCallback implementation).";
if (this.CallBackName.Length >0)
{
try
{
Type type = typeHandlerFactory.GetType(this.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 (this.CLRType.Length == 0 ) // Unknown
{
_typeHandler = typeHandlerFactory.GetUnkownTypeHandler();
}
else // If we specify a CLR type, use it
{
Type type = Resources.TypeForName(this.CLRType);
if (typeHandlerFactory.IsSimpleType(type))
{
// Primitive
_typeHandler = typeHandlerFactory.GetTypeHandler(type, _dbType);
}
else
{
// .NET object
type = ObjectProbe.GetPropertyTypeForGetter(type, this.PropertyName);
_typeHandler = typeHandlerFactory.GetTypeHandler(type, _dbType);
}
}
}
}
///
///
///
///
internal void Initialize(ErrorContext errorContext)
{
errorContext.MoreInfo = "Initialize an inline parameter property '" + this.PropertyName + "' .";
if(_directionAttribute.Length >0)
{
_direction = (ParameterDirection)Enum.Parse( typeof(ParameterDirection), _directionAttribute, true );
}
}
///
///
///
///
///
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 (this.PropertyName == p.PropertyName);
}
///
///
///
///
public override int GetHashCode()
{
return _property.GetHashCode();
}
#endregion
}
}