diff --git a/Src/Asp.Net/SqlServerTest/Demos/7_Filter.cs b/Src/Asp.Net/SqlServerTest/Demos/7_Filter.cs index 366be8b24..7812bbd30 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/7_Filter.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/7_Filter.cs @@ -69,7 +69,7 @@ namespace OrmTest.Demo }); //Processing prior to execution of SQL - db.ProcessingEventStartingSQL = (sql, par) => + db.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql = (sql, par) => { if (sql.Contains("{0}")) { diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Mapping .cs b/Src/Asp.Net/SqlServerTest/UnitTest/Mapping .cs index 54f1872ab..7df8851b1 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Mapping .cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Mapping .cs @@ -57,8 +57,7 @@ namespace OrmTest.UnitTest public SqlSugarClient GetInstance2() { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { InitKeyType = InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true }); - db.Ado.IsEnableLogEvent = true; - db.LogEventStarting = (sql, pars) => + db.CurrentConnectionConfig.AopEvents.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars)); Console.WriteLine(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Query/JoinQuery.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Query/JoinQuery.cs index 2a9b3caec..e1a6dfe45 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Query/JoinQuery.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Query/JoinQuery.cs @@ -50,7 +50,7 @@ namespace OrmTest.UnitTest { var join7 = db.Queryable((st, sc) => new object[] { JoinType.Left,st.SchoolId==sc.Id - }).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = SqlFunc.AggregateMin(sc.Id*1) }).ToSql(); + }).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = SqlFunc.AggregateMin(sc.Id * 1) }).ToSql(); string sql = @"SELECT [st].[Name] AS [Name] , MIN(( [sc].[Id] * @Id0 )) AS [SchoolId] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) "; base.Check(sql, new List() { @@ -64,8 +64,8 @@ namespace OrmTest.UnitTest using (var db = GetInstance()) { db.MappingTables.Add("School", "SchoolTable"); - var join5= db.Queryable((st, sc) => st.SchoolId == sc.Id).Select(st => st) - .GroupBy(st=> new{ st.Id,st.Name }) + var join5 = db.Queryable((st, sc) => st.SchoolId == sc.Id).Select(st => st) + .GroupBy(st => new { st.Id, st.Name }) .ToSql(); string sql = @"SELECT st.* FROM [STudent] st ,[SchoolTable] sc WHERE ( [st].[SchoolId] = [sc].[Id] )GROUP BY [st].[ID],[st].[Name] "; base.Check(sql, null, join5.Key, null, "join 5 Error"); @@ -77,7 +77,7 @@ namespace OrmTest.UnitTest using (var db = GetInstance()) { db.MappingTables.Add("School", "SchoolTable"); - var join4 = db.Queryable((st, sc) => st.SchoolId == sc.Id).Select(st=>st).ToSql(); + var join4 = db.Queryable((st, sc) => st.SchoolId == sc.Id).Select(st => st).ToSql(); string sql = @"SELECT st.* FROM [STudent] st ,[SchoolTable] sc WHERE ( [st].[SchoolId] = [sc].[Id] ) "; base.Check(sql, null, join4.Key, null, "join 4 Error"); } @@ -94,7 +94,7 @@ namespace OrmTest.UnitTest .AddParameters(new { id = 1 }) .Select("st.*").ToSql(); string sql = @"SELECT st.* FROM [Student] st Left JOIN [School] sh ON sh.id=st.schoolid WHERE st.id>@id "; - base.Check(sql,new List() {new SugarParameter("@id",1)}, join3.Key, join3.Value, "join 3 Error"); + base.Check(sql, new List() { new SugarParameter("@id", 1) }, join3.Key, join3.Value, "join 3 Error"); } } @@ -128,12 +128,19 @@ namespace OrmTest.UnitTest public new SqlSugarClient GetInstance() { - SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer }); - db.Ado.IsEnableLogEvent = true; - db.LogEventStarting = (sql, pars) => + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { - Console.WriteLine(sql + " " + pars); - }; + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + AopEvents = new AopEvents() + { + OnLogExecuting = (sql, pars) => + { + Console.WriteLine(sql + " " + pars); + } + } + }); + return db; } } diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs index 38b0a3193..f3bd00644 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Query/SelectQuery.cs @@ -30,7 +30,7 @@ namespace OrmTest.UnitTest using (var db = GetInstance()) { //db.Database.IsEnableLogEvent = true; - db.LogEventStarting = (sql, pars) => + db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql + " " + pars); }; @@ -80,9 +80,8 @@ namespace OrmTest.UnitTest new SugarParameter("@Id0",0) }, t3.Key, t3.Value, "select t3 Error"); - - db.Ado.IsEnableLogEvent = true; - db.LogEventStarting = (sql, pars) => + + db.Aop.OnLogExecuting = (sql, pars) => { base.Check(" SELECT COUNT(1) FROM (SELECT [st].[ID] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) Left JOIN [School] sc2 ON ( [sc2].[Id] = [sc].[Id] ) GROUP BY [st].[ID] ) CountTable ", null, sql, null, "select t4 Error"); diff --git a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 67017cc02..696f6bafb 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -57,14 +57,14 @@ namespace SqlSugar } public virtual int CommandTimeOut { get; set; } public virtual CommandType CommandType { get; set; } - public virtual bool IsEnableLogEvent { get => this.Context.IsEnableLogEvent; set => this.Context.IsEnableLogEvent = value; } + public virtual bool IsEnableLogEvent {get;set;} public virtual bool IsClearParameters { get; set; } - public virtual Action LogEventStarting=> this.Context.LogEventStarting; - public virtual Action LogEventCompleted => this.Context.LogEventCompleted; - public virtual Func> ProcessingEventStartingSQL => this.Context.ProcessingEventStartingSQL; + public virtual Action LogEventStarting=> this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuting; + public virtual Action LogEventCompleted => this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted; + public virtual Func> ProcessingEventStartingSQL => this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql; protected virtual Func FormatSql { get; set; } - public virtual Action ErrorEvent => this.Context.ErrorEvent; - public virtual Action DiffLogEvent => this.Context.DiffLogEvent; + public virtual Action ErrorEvent => this.Context.CurrentConnectionConfig.AopEvents.OnError; + public virtual Action DiffLogEvent => this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent; public virtual List SlaveConnections { get; set; } public virtual IDbConnection MasterConnection { get; set; } #endregion diff --git a/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs index 0035ad43a..1e1b5610d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs @@ -11,13 +11,13 @@ namespace SqlSugar public AopProvider(SqlSugarContext context) { this.Context = context; - this.Context.IsEnableLogEvent = true; + this.Context.Ado.IsEnableLogEvent = true; } private SqlSugarContext Context { get; set; } - public Action OnDiffLogEvent { set { this.Context.DiffLogEvent = value; } } - public Action OnError { set { this.Context.ErrorEvent = value; } } - public Action OnLogExecuting { set { this.Context.LogEventStarting = value; } } - public Action OnLogExecuted { set { this.Context.LogEventCompleted = value; } } - public Func> OnExecutingChangeSql { set { this.Context.ProcessingEventStartingSQL = value; } } + public Action OnDiffLogEvent { set { this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent = value; } } + public Action OnError { set { this.Context.CurrentConnectionConfig.AopEvents.OnError = value; } } + public Action OnLogExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuting= value; } } + public Action OnLogExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted = value; } } + public Func> OnExecutingChangeSql { set { this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql = value; } } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs index 7f9897f3d..8582289e6 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs @@ -410,8 +410,8 @@ namespace SqlSugar parameters = new List(); diffModel.AfterData = null; diffModel.Time = this.Context.Ado.SqlExecutionTime; - if (this.Context.DiffLogEvent != null) - this.Context.DiffLogEvent(diffModel); + if (this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent != null) + this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent(diffModel); this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; } if (this.RemoveCacheFunc != null) { diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index d95bb06f0..a2eaf145c 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -477,8 +477,8 @@ namespace SqlSugar parameters = new List(); diffModel.AfterData = GetDiffTable(sql, result); diffModel.Time = this.Context.Ado.SqlExecutionTime; - if (this.Context.DiffLogEvent != null) - this.Context.DiffLogEvent(diffModel); + if (this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent != null) + this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent(diffModel); this.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; } if (this.RemoveCacheFunc != null) diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 51b3bff27..6b9fc74b3 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -278,13 +278,13 @@ namespace SqlSugar public IUpdateable SetColumnsIF(bool isUpdateColumns, Expression> columns) { if (isUpdateColumns) - UpdateColumns(columns); + SetColumns(columns); return this; } public IUpdateable SetColumnsIF(bool isUpdateColumns, Expression> columns) { if (isUpdateColumns) - UpdateColumns(columns); + SetColumns(columns); return this; } @@ -690,8 +690,8 @@ namespace SqlSugar parameters = new List(); diffModel.AfterData = GetDiffTable(sql, parameters); diffModel.Time = this.Context.Ado.SqlExecutionTime; - if (this.Context.DiffLogEvent != null) - this.Context.DiffLogEvent(diffModel); + if (this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent != null) + this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent(diffModel); this.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation; } if (this.RemoveCacheFunc != null) diff --git a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs index c171f9bcc..5f38997e8 100644 --- a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs +++ b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs @@ -37,8 +37,7 @@ namespace SqlSugar /// Configure External Services replace default services,For example, Redis storage /// [JsonIgnore] - public ConfigureExternalServices ConfigureExternalServices = _DefaultServices; - private static ConfigureExternalServices _DefaultServices = new ConfigureExternalServices(); + public ConfigureExternalServices ConfigureExternalServices = new ConfigureExternalServices(); /// /// If SlaveConnectionStrings has value,ConnectionString is write operation, SlaveConnectionStrings is read operation. /// All operations within a transaction is ConnectionString @@ -52,8 +51,18 @@ namespace SqlSugar /// Used for debugging errors or BUG,Used for debugging, which has an impact on Performance /// public SugarDebugger Debugger { get; set; } + + [JsonIgnore] + public AopEvents AopEvents = new AopEvents(); + } + public class AopEvents + { + public Action OnDiffLogEvent { get; set; } + public Action OnError { get; set; } + public Action OnLogExecuting { get; set; } + public Action OnLogExecuted { get; set; } + public Func> OnExecutingChangeSql { get; set; } } - public class ConfigureExternalServices { diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs index cee77e19d..c401ff120 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/ContextMethods.cs @@ -277,10 +277,6 @@ namespace SqlSugar newClient.IgnoreColumns = this.TranslateCopy(Context.IgnoreColumns); if (isCopyEvents) { - newClient.Ado.IsEnableLogEvent = Context.Ado.IsEnableLogEvent; - newClient.LogEventStarting = Context.LogEventStarting; - newClient.LogEventCompleted = Context.LogEventCompleted; - newClient.ProcessingEventStartingSQL = Context.ProcessingEventStartingSQL; newClient.QueryFilter = Context.QueryFilter; } return newClient; diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs index e59c25c0c..dc0c71ba7 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs @@ -23,7 +23,6 @@ namespace SqlSugar } } - public bool IsEnableLogEvent { get; set; } public ConnectionConfig CurrentConnectionConfig { get; set; } public Dictionary TempItems { get; set; } public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } } @@ -33,15 +32,12 @@ namespace SqlSugar public MappingColumnList MappingColumns { get; set; } public IgnoreColumnList IgnoreColumns { get; set; } public IgnoreColumnList IgnoreInsertColumns { get; set; } - public Action LogEventStarting { get; set; } - public Action LogEventCompleted { get; set; } - public Func> ProcessingEventStartingSQL { get; set; } - public Action ErrorEvent { get; set; } - public Action DiffLogEvent { get; set; } - public QueueList _Queues = new QueueList(); + + #endregion - #region Fields + #region Fields + public QueueList _Queues; protected ISqlBuilder _SqlBuilder; protected ISqlSugarClient _Context { get; set; } protected EntityMaintenance _EntityProvider; diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarContext.cs b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarContext.cs index 71ff9e911..23c01e142 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarContext.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarContext.cs @@ -912,7 +912,7 @@ namespace SqlSugar } this.Queues.Add(sql, parsmeters); } - public QueueList Queues { get =>_Queues; set => _Queues = value; } + public QueueList Queues { get =>_Queues??new QueueList(); set => _Queues = value; } private T SaveQueuesProvider(bool isTran, Func, T> func) { diff --git a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs index e8fb55b76..61d604624 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs @@ -19,6 +19,7 @@ namespace SqlSugar SqlSugarContext Context { get; set; } void ExecuteBefore(string sql, SugarParameter[] pars); void ExecuteAfter(string sql, SugarParameter[] pars); + bool IsEnableLogEvent{get;set;} IDataParameterCollection DataReaderParameters { get; set; } CommandType CommandType { get; set; } diff --git a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs index 277321eaa..82cabfc4e 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs @@ -12,11 +12,7 @@ namespace SqlSugar MappingColumnList MappingColumns { get; set; } IgnoreColumnList IgnoreColumns { get; set; } IgnoreColumnList IgnoreInsertColumns { get; set; } - Action LogEventStarting { get; set; } - Action LogEventCompleted { get; set; } - Func> ProcessingEventStartingSQL { get; set; } - Action ErrorEvent { get; set; } - Action DiffLogEvent { get; set; } + QueueList Queues { get; set; } IAdo Ado { get; } AopProvider Aop { get; } diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index 586ad2241..9a0da29af 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -21,11 +21,7 @@ namespace SqlSugar private MappingColumnList _MappingColumns; private IgnoreColumnList _IgnoreColumns; private IgnoreColumnList _IgnoreInsertColumns; - private Action _LogEventStarting; - private Action _LogEventCompleted; - private Func> _ProcessingEventStartingSQL; - private Action _ErrorEvent; - private Action _DiffLogEvent; + #endregion #region Api @@ -67,11 +63,7 @@ namespace SqlSugar public MappingColumnList MappingColumns { get => _MappingColumns; set => _MappingColumns = value; } public IgnoreColumnList IgnoreColumns { get => _IgnoreColumns; set => _IgnoreColumns = value; } public IgnoreColumnList IgnoreInsertColumns { get => _IgnoreInsertColumns; set => _IgnoreInsertColumns = value; } - public Action LogEventStarting { get => _LogEventStarting; set => _LogEventStarting = value; } - public Action LogEventCompleted { get => _LogEventCompleted; set => _LogEventCompleted = value; } - public Func> ProcessingEventStartingSQL { get => _ProcessingEventStartingSQL; set => _ProcessingEventStartingSQL = value; } - public Action ErrorEvent { get => _ErrorEvent; set => _ErrorEvent = value; } - public Action DiffLogEvent { get => _DiffLogEvent; set => _DiffLogEvent = value; } + public ConnectionConfig CurrentConnectionConfig { get => _CurrentConnectionConfig; set => _CurrentConnectionConfig = value; } @@ -637,11 +629,6 @@ namespace SqlSugar _Context.MappingTables = _MappingTables; _Context.IgnoreColumns = _IgnoreColumns; _Context.IgnoreInsertColumns = _IgnoreInsertColumns; - _Context.DiffLogEvent = _DiffLogEvent; - _Context.LogEventCompleted = _LogEventCompleted; - _Context.LogEventStarting = _LogEventStarting; - _Context.ErrorEvent = _ErrorEvent; - _Context.ProcessingEventStartingSQL = _ProcessingEventStartingSQL; return _Context; } else @@ -670,11 +657,7 @@ namespace SqlSugar result.MappingTables = _MappingTables; result.IgnoreColumns = _IgnoreColumns; result.IgnoreInsertColumns = _IgnoreInsertColumns; - result.DiffLogEvent = _DiffLogEvent; - result.LogEventCompleted = _LogEventCompleted; - result.LogEventStarting = _LogEventStarting; - result.ErrorEvent = _ErrorEvent; - result.ProcessingEventStartingSQL = _ProcessingEventStartingSQL; + return result; }