using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace parkMonitor.Database2
{
    /// <summary>
    /// 数据库访问类
    /// </summary>
    class DBAccess
    {
        public string sql { set; get; }
        public Connection conn { set; get; }
        public CommandTypes commType { set; get; }

        public List<string> strs { set; get; }
        /// <summary>
        /// 类的构造函数
        /// </summary>
        public DBAccess()
        {
            sql = null;
            conn = null;
        }
        /// <summary>
        /// 类的构造函数
        /// </summary>
        public DBAccess(string sql, Connection conn, CommandTypes commType)
        {
            this.sql = sql;
            this.conn = conn;
            this.commType = commType;

        }
        /// <summary>
        /// 类的构造函数
        /// </summary>
        public DBAccess(Connection conn, CommandTypes commType, List<string> strs)
        {
            this.sql = null;
            this.conn = conn;
            this.commType = commType;
            this.strs = strs;
        }
        /// <summary>
        /// 类的构造函数
        /// </summary>
        public DBAccess(string sql, Connection conn, CommandTypes commType, Dictionary<string, string> args)
        {

            this.sql = sql;
            this.conn = conn;
            this.commType = commType;

        }
        /// <summary>
        /// 数据库查询、插入、更新处理
        /// </summary>
        /// <returns></returns>
        public object GetResult()
        {
            object result = null;
            try
            {
                AbstractCommand abscommand = SimpleCommandFactory.CreateCommandAndExcute(commType);
                abscommand.sql = sql;
                abscommand.conn = conn;
                result = abscommand.ExecuteSqlCommand();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 数据库事务处理
        /// </summary>
        /// <param name="strs"></param>
        /// <returns></returns>
        public TransactionResult DealTransaction(List<string> strs)
        {
            TransactionResult result = null;
            try
            {
                TransactionCommand abscommand = (TransactionCommand)SimpleCommandFactory.CreateCommandAndExcute(commType);
                abscommand.strs = strs;
                abscommand.conn = conn;
                abscommand.sql = sql;
                result = (TransactionResult)abscommand.ExecuteSqlCommand();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
