mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Code optimization
This commit is contained in:
parent
d110859eaf
commit
26548f60f9
@ -59,7 +59,7 @@ namespace SqlSugar
|
||||
public virtual Action<string, SugarParameter[]> LogEventCompleted { get; set; }
|
||||
public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }
|
||||
protected virtual Func<string,string> FormatSql { get; set; }
|
||||
public virtual Action<Exception> ErrorEvent { get; set; }
|
||||
public virtual Action<SqlSugarException> ErrorEvent { get; set; }
|
||||
public virtual Action<DiffLogModel> DiffLogEvent { get; set; }
|
||||
public virtual List<IDbConnection> SlaveConnections { get; set; }
|
||||
public virtual IDbConnection MasterConnection { get; set; }
|
||||
@ -300,7 +300,7 @@ namespace SqlSugar
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ErrorEvent != null)
|
||||
ErrorEvent(ex);
|
||||
ExecuteErrorEvent(sql, parameters, ex);
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
@ -333,7 +333,7 @@ namespace SqlSugar
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ErrorEvent != null)
|
||||
ErrorEvent(ex);
|
||||
ExecuteErrorEvent(sql, parameters, ex);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
@ -360,7 +360,7 @@ namespace SqlSugar
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ErrorEvent != null)
|
||||
ErrorEvent(ex);
|
||||
ExecuteErrorEvent(sql, parameters, ex);
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
@ -390,7 +390,7 @@ namespace SqlSugar
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ErrorEvent != null)
|
||||
ErrorEvent(ex);
|
||||
ExecuteErrorEvent(sql,parameters,ex);
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
@ -786,6 +786,11 @@ namespace SqlSugar
|
||||
var result = Regex.IsMatch(sqlLower, "[ ]*select[ ]") && !Regex.IsMatch(sqlLower, "[ ]*insert[ ]|[ ]*update[ ]|[ ]*delete[ ]");
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ExecuteErrorEvent(string sql, SugarParameter[] parameters, Exception ex)
|
||||
{
|
||||
ErrorEvent(new SqlSugarException(this.Context,ex, sql, parameters));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ namespace SqlSugar
|
||||
}
|
||||
private SqlSugarClient Context { get; set; }
|
||||
public Action<DiffLogModel> OnDiffLogEvent { set { this.Context.Ado.DiffLogEvent = value; } }
|
||||
public Action<Exception> OnError { set { this.Context.Ado.ErrorEvent = value; } }
|
||||
public Action<SqlSugarException> OnError { set { this.Context.Ado.ErrorEvent = value; } }
|
||||
public Action<string, SugarParameter[]> OnLogExecuting { set { this.Context.Ado.LogEventStarting = value; } }
|
||||
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.Ado.LogEventCompleted = value; } }
|
||||
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { set { this.Context.Ado.ProcessingEventStartingSQL = value; } }
|
||||
|
@ -323,7 +323,7 @@ namespace SqlSugar
|
||||
var isAny = invalidTypes.Contains(bindProperyTypeName);
|
||||
if (isAny)
|
||||
{
|
||||
throw new UtilExceptions(string.Format("{0} can't convert {1} to {2}", propertyName, validPropertyType, bindProperyTypeName));
|
||||
throw new SqlSugarException(string.Format("{0} can't convert {1} to {2}", propertyName, validPropertyType, bindProperyTypeName));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
@ -26,7 +26,7 @@ namespace SqlSugar
|
||||
Action<string, SugarParameter []> LogEventStarting { get; set; }
|
||||
Action<string, SugarParameter []> LogEventCompleted { get; set; }
|
||||
Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }
|
||||
Action<Exception> ErrorEvent { get; set; }
|
||||
Action<SqlSugarException> ErrorEvent { get; set; }
|
||||
Action<DiffLogModel> DiffLogEvent { get; set; }
|
||||
bool IsClearParameters { get; set; }
|
||||
int CommandTimeOut { get; set; }
|
||||
|
@ -9,25 +9,25 @@ namespace SqlSugar
|
||||
public static void ThrowNotSupportedException(string message)
|
||||
{
|
||||
message = message.IsNullOrEmpty() ? new NotSupportedException().Message : message;
|
||||
throw new UtilExceptions("SqlSugarException.NotSupportedException:" + message);
|
||||
throw new SqlSugarException("SqlSugarException.NotSupportedException:" + message);
|
||||
}
|
||||
|
||||
public static void ArgumentNullException(object checkObj, string message)
|
||||
{
|
||||
if (checkObj == null)
|
||||
throw new UtilExceptions("SqlSugarException.ArgumentNullException:" + message);
|
||||
throw new SqlSugarException("SqlSugarException.ArgumentNullException:" + message);
|
||||
}
|
||||
|
||||
public static void ArgumentNullException(object [] checkObj, string message)
|
||||
{
|
||||
if (checkObj == null|| checkObj.Length==0)
|
||||
throw new UtilExceptions("SqlSugarException.ArgumentNullException:" + message);
|
||||
throw new SqlSugarException("SqlSugarException.ArgumentNullException:" + message);
|
||||
}
|
||||
|
||||
public static void Exception(bool isException, string message, params string[] args)
|
||||
{
|
||||
if (isException)
|
||||
throw new UtilExceptions(string.Format(message, args));
|
||||
throw new SqlSugarException(string.Format(message, args));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,58 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class UtilExceptions : Exception
|
||||
public class SqlSugarException : Exception
|
||||
{
|
||||
public UtilExceptions(string message)
|
||||
public string Sql { get; set; }
|
||||
public object Parametres { get; set; }
|
||||
public new Exception InnerException;
|
||||
public new string StackTrace;
|
||||
public new MethodBase TargetSite;
|
||||
public new string Source;
|
||||
|
||||
public SqlSugarException(string message)
|
||||
: base(message){}
|
||||
|
||||
public UtilExceptions(SqlSugarClient context,string message, string sql)
|
||||
: base(GetMessage(context, message, sql)) {}
|
||||
|
||||
public UtilExceptions(SqlSugarClient context, string message, string sql, object pars)
|
||||
: base(GetMessage(context,message, sql, pars)){}
|
||||
|
||||
public UtilExceptions(SqlSugarClient context, string message, object pars)
|
||||
: base(GetMessage(context,message, pars)){}
|
||||
|
||||
private static string GetMessage(SqlSugarClient context, string message, object pars)
|
||||
{
|
||||
var parsStr = string.Empty; ;
|
||||
if (pars != null)
|
||||
{
|
||||
parsStr = context.Utilities.SerializeObject(pars);
|
||||
}
|
||||
var reval = GetLineMessage("message", message) + GetLineMessage("function", parsStr);
|
||||
return reval;
|
||||
|
||||
public SqlSugarException(SqlSugarClient context,string message, string sql)
|
||||
: base(message) {
|
||||
this.Sql = sql;
|
||||
}
|
||||
private static string GetMessage(SqlSugarClient context, string message, string sql, object pars)
|
||||
{
|
||||
if (pars == null)
|
||||
{
|
||||
return GetMessage(context,message, sql);
|
||||
}
|
||||
else
|
||||
{
|
||||
var reval = GetLineMessage("message ", message) + GetLineMessage("ORM Sql", sql) + GetLineMessage("函数参数 ", JsonConvert.SerializeObject(pars));
|
||||
return reval;
|
||||
}
|
||||
|
||||
public SqlSugarException(SqlSugarClient context, string message, string sql, object pars)
|
||||
: base(message) {
|
||||
this.Sql = sql;
|
||||
this.Parametres = pars;
|
||||
}
|
||||
private static string GetMessage(string message, string sql)
|
||||
|
||||
public SqlSugarException(SqlSugarClient context, Exception ex, string sql, object pars)
|
||||
: base(ex.Message)
|
||||
{
|
||||
var reval = GetLineMessage("message ", message) + GetLineMessage("ORM Sql", sql);
|
||||
return reval;
|
||||
this.Sql = sql;
|
||||
this.Parametres = pars;
|
||||
this.InnerException = ex.InnerException;
|
||||
this.StackTrace = ex.StackTrace;
|
||||
this.TargetSite = ex.TargetSite;
|
||||
this.Source = ex.Source;
|
||||
}
|
||||
private static string GetLineMessage(string key, string value)
|
||||
{
|
||||
return string.Format("{0} : '{1}' \r\n", key, value);
|
||||
|
||||
public SqlSugarException(SqlSugarClient context, string message, object pars)
|
||||
: base(message) {
|
||||
this.Parametres = pars;
|
||||
}
|
||||
}
|
||||
public class VersionExceptions : UtilExceptions
|
||||
public class VersionExceptions : SqlSugarException
|
||||
{
|
||||
public VersionExceptions(string message)
|
||||
: base(message){ }
|
||||
|
Loading…
Reference in New Issue
Block a user