v7.3.2018.0614   重构高性能资源池,减少GC压力,增加线程池,让异步任务得到平等竞争CPU的机会
大石头 authored at 2018-06-14 17:56:44
2.90 KiB
X
using System;
using System.Runtime.Serialization;
using System.Security.Permissions;
using XCode.DataAccessLayer;

namespace XCode.Exceptions
{
    /// <summary>数据访问层SQL异常</summary>
    [Serializable]
    public class XSqlException : XDbSessionException
    {
        #region 属性
        /// <summary>SQL语句</summary>
        public String Sql { get; }
        #endregion

        #region 构造
        /// <summary>初始化</summary>
        /// <param name="sql"></param>
        /// <param name="session"></param>
        public XSqlException(String sql, IDbSession session) : base(session) { Sql = sql; }

        /// <summary>初始化</summary>
        /// <param name="sql"></param>
        /// <param name="session"></param>
        /// <param name="message"></param>
        public XSqlException(String sql, IDbSession session, String message) : base(session, message + "[SQL:" + FormatSql(sql) + "]") { Sql = sql; }

        /// <summary>初始化</summary>
        /// <param name="sql"></param>
        /// <param name="session"></param>
        /// <param name="message"></param>
        /// <param name="innerException"></param>
        public XSqlException(String sql, IDbSession session, String message, Exception innerException)
            : base(session, message + "[SQL:" + FormatSql(sql) + "]", innerException)
        {
            Sql = sql;
        }

        /// <summary>初始化</summary>
        /// <param name="sql"></param>
        /// <param name="session"></param>
        /// <param name="innerException"></param>
        public XSqlException(String sql, IDbSession session, Exception innerException)
            : base(session, (innerException != null ? innerException.Message : "") + "[SQL:" + FormatSql(sql) + "]", innerException)
        {
            Sql = sql;
        }
        #endregion

        #region 方法
        static String FormatSql(String sql)
        {
            if (String.IsNullOrEmpty(sql)) return sql;
            sql = sql.Trim();
            if (String.IsNullOrEmpty(sql)) return sql;

            if (sql.Contains(Environment.NewLine))
                return Environment.NewLine + sql + Environment.NewLine;
            else
                return sql;
        }

        /// <summary>从序列化信息中读取Sql</summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            //info.AddValue("sql", Sql);
            // 必须明确指定类型,否则可能因为Sql==null,而导致内部当作Object写入
            info.AddValue("sql", Sql, typeof(String));
        }
        #endregion
    }
}