using System;
using System.Data.Common;

namespace LJLib.DAL.SQL
{
    internal interface ISqlEngine
    {
        DateTime GetServerTime(DbCommand cmd);
        void AddWithValue(object cmd, string name, object value);
    }

    internal abstract class SqlEngine<T> : ISqlEngine where T : DbCommand
    {
        public DateTime GetServerTime(DbCommand cmd)
        {
            var cmdinternal = cmd as T;
            if (cmdinternal == null)
            {
                throw  new Exception(string.Format("数据库引擎{0}与{1}不兼容", this.GetType().FullName, typeof(T).FullName));
            }
            return GetServerTimeInternal(cmdinternal);
        }

        protected abstract DateTime GetServerTimeInternal(T cmd);


        public void AddWithValue(object cmd, string name, object value)
        {
            var cmdinternal = cmd as T;
            if (cmdinternal == null)
            {
                throw new Exception(string.Format("数据库引擎{0}与{1}不兼容", this.GetType().FullName, typeof(T).FullName));
            }
            AddWithValueInternal(cmdinternal, name, value);
        }

        protected abstract void AddWithValueInternal(T cmd, string name, object value);

    }
}
