﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="EmbeddedDate.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.Sql
{
    using System.Data.Common;
    using System.Reflection;

    using Dhgms.DataManager.Model.Helper.Database;
    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Base class for an SQL task that processes a date from an embedded script
    /// </summary>
    /// <typeparam name="TDatabaseHelperClass">
    /// The type for the database helper
    /// </typeparam>
    /// <typeparam name="TParameterClass">
    /// The type for the SQL Parameters
    /// </typeparam>
    public abstract class EmbeddedDate<TDatabaseHelperClass, TParameterClass> :
        Embedded<SingleResultDateTime, TDatabaseHelperClass, TParameterClass>
        where TDatabaseHelperClass : DatabaseHelperBase<TParameterClass>, new()
        where TParameterClass : DbParameter
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="EmbeddedDate{TDatabaseHelperClass,TParameterClass}"/> class. 
        /// </summary>
        /// <param name="taskName">
        /// </param>
        /// <param name="connectionString">
        /// </param>
        /// <param name="assembly">
        /// </param>
        /// <param name="resourceNamespace">
        /// </param>
        /// <param name="resourceFileName">
        /// </param>
        /// <param name="assumeSqlSafe">
        /// Whether to skip the SQL injection safety check.  Use only if you know the code you are passing in is safe and contains a quoted string that is preventing it passing the saftey check
        /// </param>
        protected EmbeddedDate(
            string taskName, 
            string connectionString, 
            Assembly assembly, 
            string resourceNamespace, 
            string resourceFileName, 
            bool assumeSqlSafe)
            : base(taskName, connectionString, assembly, resourceNamespace, resourceFileName, assumeSqlSafe)
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// The OnProcess event for doing the donkey work
        /// </summary>
        /// <param name="result">
        /// The result.
        /// </param>
        protected override void OnProcess(SingleResultDateTime result)
        {
            result.Result = this.DbHelper.GetDate(
                this.ConnectionString, this.GetSql(), this.GetParameters(), this.AssumeSqlSafe);
        }

        #endregion
    }
}