#region Apache Notice
/*****************************************************************************
* $Revision: 469233 $
* $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
#region Using
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Xml.Serialization;
using Apache.Ibatis.Common.Utilities.Objects;
using Apache.Ibatis.DataMapper.Model.Cache;
using Apache.Ibatis.DataMapper.Model.ParameterMapping;
using Apache.Ibatis.DataMapper.Model.ResultMapping;
using Apache.Ibatis.DataMapper.Configuration.Sql;
using Apache.Ibatis.DataMapper.DataExchange;
using Apache.Ibatis.DataMapper.Exceptions;
using Apache.Ibatis.DataMapper.Scope;
#endregion
namespace Apache.Ibatis.DataMapper.Configuration.Statements
{
///
/// Summary description for Statement.
///
[Serializable]
[XmlRoot("statement", Namespace = "http://ibatis.apache.org/mapping")]
public class Statement : IStatement
{
#region Fields
[NonSerialized]
private bool _allowRemapping = false;
[NonSerialized]
private string _id = string.Empty;
// ResultMap
[NonSerialized]
private string _resultMapName = string.Empty;
[NonSerialized]
private ResultMapCollection _resultsMap = new ResultMapCollection();
// ParameterMap
[NonSerialized]
private string _parameterMapName = string.Empty;
[NonSerialized]
private ParameterMap _parameterMap = null;
// Result Class
[NonSerialized]
private string _resultClassName = string.Empty;
[NonSerialized]
private Type _resultClass = null;
// Parameter Class
[NonSerialized]
private string _parameterClassName = string.Empty;
[NonSerialized]
private Type _parameterClass = null;
// List Class
[NonSerialized]
private string _listClassName = string.Empty;
[NonSerialized]
private Type _listClass = null;
// CacheModel
[NonSerialized]
private string _cacheModelName = string.Empty;
[NonSerialized]
private CacheModel _cacheModel = null;
[NonSerialized]
private ISql _sql = null;
[NonSerialized]
private string _extendStatement = string.Empty;
[NonSerialized]
private IFactory _listClassFactory = null;
#endregion
#region Properties
///
/// Allow remapping of dynamic SQL
///
[XmlAttribute("remapResults")]
public bool AllowRemapping
{
get { return _allowRemapping; }
set { _allowRemapping = value; }
}
///
/// Extend statement attribute
///
[XmlAttribute("extends")]
public virtual string ExtendStatement
{
get { return _extendStatement; }
set { _extendStatement = value; }
}
///
/// The CacheModel name to use.
///
[XmlAttribute("cacheModel")]
public string CacheModelName
{
get { return _cacheModelName; }
set { _cacheModelName = value; }
}
///
/// Tell us if a cacheModel is attached to this statement.
///
[XmlIgnore]
public bool HasCacheModel
{
get { return _cacheModelName.Length > 0; }
}
///
/// The CacheModel used by this statement.
///
[XmlIgnore]
public CacheModel CacheModel
{
get { return _cacheModel; }
set { _cacheModel = value; }
}
///
/// The list class name to use for strongly typed collection.
///
[XmlAttribute("listClass")]
public string ListClassName
{
get { return _listClassName; }
set { _listClassName = value; }
}
///
/// The list class type to use for strongly typed collection.
///
[XmlIgnore]
public Type ListClass
{
get { return _listClass; }
}
///
/// The result class name to used.
///
[XmlAttribute("resultClass")]
public string ResultClassName
{
get { return _resultClassName; }
set { _resultClassName = value; }
}
///
/// The result class type to used.
///
[XmlIgnore]
public Type ResultClass
{
get { return _resultClass; }
}
///
/// The parameter class name to used.
///
[XmlAttribute("parameterClass")]
public string ParameterClassName
{
get { return _parameterClassName; }
set { _parameterClassName = value; }
}
///
/// The parameter class type to used.
///
[XmlIgnore]
public Type ParameterClass
{
get { return _parameterClass; }
}
///
/// Name used to identify the statement amongst the others.
///
[XmlAttribute("id")]
public string Id
{
get { return _id; }
set
{
if ((value == null) || (value.Length < 1))
throw new DataMapperException("The id attribute is required in a statement tag.");
_id = value;
}
}
///
/// The sql statement
///
[XmlIgnore]
public ISql Sql
{
get { return _sql; }
set
{
if (value == null)
throw new DataMapperException("The sql statement query text is required in the statement tag " + _id);
_sql = value;
}
}
///
/// The ResultMaps name used by the statement.
///
[XmlAttribute("resultMap")]
public string ResultMapName
{
get { return _resultMapName; }
set { _resultMapName = value; }
}
///
/// The ParameterMap name used by the statement.
///
[XmlAttribute("parameterMap")]
public string ParameterMapName
{
get { return _parameterMapName; }
set { _parameterMapName = value; }
}
///
/// The ResultMap used by the statement.
///
[XmlIgnore]
public ResultMapCollection ResultsMap
{
get { return _resultsMap; }
}
///
/// The parameterMap used by the statement.
///
[XmlIgnore]
public ParameterMap ParameterMap
{
get { return _parameterMap; }
set { _parameterMap = value; }
}
///
/// The type of the statement (text or procedure)
/// Default Text.
///
/// Text or StoredProcedure
[XmlIgnore]
public virtual CommandType CommandType
{
get { return CommandType.Text; }
}
#endregion
#region Methods
///
/// Initialize an statement for the sqlMap.
///
/// The scope of the configuration
internal virtual void Initialize(ConfigurationScope configurationScope)
{
if (_resultMapName.Length > 0)
{
string[] names = _resultMapName.Split(',');
for (int i = 0; i < names.Length; i++)
{
string name = configurationScope.ApplyNamespace(names[i].Trim());
_resultsMap.Add( configurationScope.SqlMapper.GetResultMap(name) );
}
}
if (_parameterMapName.Length > 0)
{
_parameterMap = configurationScope.SqlMapper.GetParameterMap(_parameterMapName);
}
if (_resultClassName.Length > 0)
{
string[] classNames = _resultClassName.Split(',');
for (int i = 0; i < classNames.Length; i++)
{
_resultClass = configurationScope.SqlMapper.TypeHandlerFactory.GetType(classNames[i].Trim());
IFactory resultClassFactory = null;
if (Type.GetTypeCode(_resultClass) == TypeCode.Object &&
(_resultClass.IsValueType == false))
{
resultClassFactory = configurationScope.SqlMapper.ObjectFactory.CreateFactory(_resultClass, Type.EmptyTypes);
}
IDataExchange dataExchange = configurationScope.DataExchangeFactory.GetDataExchangeForClass(_resultClass);
IResultMap autoMap = new AutoResultMap(_resultClass, resultClassFactory, dataExchange);
_resultsMap.Add(autoMap);
}
}
if (_parameterClassName.Length > 0)
{
_parameterClass = configurationScope.SqlMapper.TypeHandlerFactory.GetType(_parameterClassName);
}
if (_listClassName.Length > 0)
{
_listClass = configurationScope.SqlMapper.TypeHandlerFactory.GetType(_listClassName);
_listClassFactory = configurationScope.SqlMapper.ObjectFactory.CreateFactory(_listClass, Type.EmptyTypes);
}
}
///
/// Create an instance of 'IList' class.
///
/// An object which implment IList.
public IList CreateInstanceOfListClass()
{
return (IList)_listClassFactory.CreateInstance(null);
}
///
/// Create an instance of a generic 'IList' class.
///
/// An object which implment IList.
public IList CreateInstanceOfGenericListClass()
{
return (IList)_listClassFactory.CreateInstance(null);
}
#endregion
}
}