/************************************************************************************
*
* Umbraco Data Layer
* MIT Licensed work
* ©2008 Ruben Verborgh
*
***********************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using umbraco.DataLayer.Utility;
namespace umbraco.DataLayer.Extensions
{
///
/// Class that adds extensions to an existing SQL helper.
///
/// Decorator design pattern.
public class SqlHelperExtender : ISqlHelper
{
#region Private Fields
/// SQL helper this SqlHelperExtender extends.
private readonly ISqlHelper m_SqlHelper;
/// Extensions that are currently in use.
private readonly List m_Extensions = new List();
#endregion
#region Public Properties
///
/// Gets the connection string.
///
/// The connection string.
public string ConnectionString
{
get { return m_SqlHelper.ConnectionString; }
}
///
/// Gets the Umbraco utility associated with this SQL helper.
///
/// The utilities.
public IUtilitySet Utility
{
get { return m_SqlHelper.Utility; }
}
#endregion
#region Public Contructors
///
/// Initializes a new instance of the class.
///
/// The SQL helper.
public SqlHelperExtender(ISqlHelper sqlHelper)
{
if (sqlHelper == null)
throw new ArgumentNullException("sqlHelper");
m_SqlHelper = sqlHelper;
}
///
/// Initializes a new instance of the class.
///
/// The SQL helper.
/// The extensions.
public SqlHelperExtender(ISqlHelper sqlHelper, params ISqlHelperExtension[] extensions)
: this(sqlHelper)
{
foreach (ISqlHelperExtension extension in extensions)
AddExtension(extension);
}
#endregion
#region Public Methods
///
/// Adds the extension to the extension chain.
///
/// The extension.
public void AddExtension(ISqlHelperExtension extension)
{
if (extension == null)
throw new ArgumentNullException("extension");
m_Extensions.Add(extension);
}
///
/// Removes an extension from the extension chain.
///
/// The extension.
public void RemoveExtension(ISqlHelperExtension extension)
{
if (extension == null)
throw new ArgumentNullException("extension");
m_Extensions.Remove(extension);
}
#endregion
#region ISqlHelper Members
///
/// Creates a new parameter for use with this specific implementation of ISqlHelper.
///
/// Name of the parameter.
/// Value of the parameter.
/// A new parameter of the correct type.
/// Abstract factory pattern
public IParameter CreateParameter(string parameterName, object value)
{
return m_SqlHelper.CreateParameter(parameterName, value);
}
///
/// Escapes a string for use in an SQL query.
///
/// The text to be escaped.
/// The escaped value.
/// You should use SQL parameters instead.
public string EscapeString(string text)
{
// Although EscapeString is obsolete, we still need to implement it
#pragma warning disable 618
return m_SqlHelper.EscapeString(text);
#pragma warning restore 618
}
///
/// Executes a command and returns the number of rows affected.
///
/// The command text.
/// The parameters.
///
/// The number of rows affected by the command.
///
public int ExecuteNonQuery(string commandText, params IParameter[] parameters)
{
foreach (ISqlHelperExtension extension in m_Extensions)
extension.OnExecuteNonQuery(m_SqlHelper, ref commandText, ref parameters);
return m_SqlHelper.ExecuteNonQuery(commandText, parameters);
}
///
/// Executes a command and returns a records reader containing the results.
///
/// The command text.
/// The parameters.
///
/// A data reader containing the results of the command.
///
public IRecordsReader ExecuteReader(string commandText, params IParameter[] parameters)
{
foreach (ISqlHelperExtension extension in m_Extensions)
extension.OnExecuteReader(m_SqlHelper, ref commandText, ref parameters);
return m_SqlHelper.ExecuteReader(commandText, parameters);
}
///
/// Executes a command that returns a single value.
///
/// The type of the value.
/// The command text.
/// The parameters.
/// The return value of the command.
/// If a data source error occurs.
public T ExecuteScalar(string commandText, params IParameter[] parameters)
{
foreach (ISqlHelperExtension extension in m_Extensions)
extension.OnExecuteScalar(m_SqlHelper, ref commandText, ref parameters);
return m_SqlHelper.ExecuteScalar(commandText, parameters);
}
///
/// Executes a command that returns an XML value.
///
/// The command text.
/// The parameters.
///
/// An XML reader containing the return value.
///
public XmlReader ExecuteXmlReader(string commandText, params IParameter[] parameters)
{
foreach (ISqlHelperExtension extension in m_Extensions)
extension.OnExecuteXmlReader(m_SqlHelper, ref commandText, ref parameters);
return m_SqlHelper.ExecuteXmlReader(commandText, parameters);
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
m_SqlHelper.Dispose();
}
#endregion
}
}