From bd6de96e4eab7edb8c84ca15ae4a6b132a3099b3 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 13 Nov 2018 01:51:36 +0800 Subject: [PATCH] Diff Log --- Src/Asp.Net/SqlServerTest/Demos/9_Aop.cs | 35 +++++++++-- Src/Asp.Net/SqlServerTest/Models/Student.cs | 2 +- .../Abstract/AopProvider/AopProvider.cs | 1 + .../UpdateProvider/UpdateableProvider.cs | 59 ++++++++++++++++++- Src/Asp.Net/SqlSugar/Entities/DiffLogModel.cs | 4 +- Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs | 2 +- 6 files changed, 92 insertions(+), 11 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demos/9_Aop.cs b/Src/Asp.Net/SqlServerTest/Demos/9_Aop.cs index 37444e997..917edb588 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/9_Aop.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/9_Aop.cs @@ -13,6 +13,8 @@ namespace OrmTest.Demo public static void Init() { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true }); + + db.Aop.OnLogExecuted = (sql, pars) => { Console.Write("time:" + db.Ado.SqlExecutionTime.ToString()); @@ -23,11 +25,11 @@ namespace OrmTest.Demo }; db.Aop.OnError = (exp) => { - + }; db.Aop.OnExecutingChangeSql = (sql, pars) => { - return new KeyValuePair(sql,pars); + return new KeyValuePair(sql, pars); }; db.Queryable().ToList(); @@ -39,22 +41,43 @@ namespace OrmTest.Demo catch (Exception) { - + } //diff log demo - db.Ado.DiffLogEvent = it => + db.Aop.OnDiffLogEvent = it => { var editBeforeData = it.BeforeData; var editAfterData = it.AfterDate; var sql = it.Sql; var parameter = it.Parameters; + var data = it.BusinessData; }; - db.Updateable().EnableDiffLogEvent().ExecuteCommand(); + + var id = db.Queryable().First().Id; + db.Updateable(new Student() + { + Id = id, + CreateTime = DateTime.Now, + Name = "before", + SchoolId = 1 + }) + .EnableDiffLogEvent(new { title = "update Student", Modular = 1, Operator = "admin" }).ExecuteCommand(); + + + db.Updateable(new Student() + { + Id = id, + CreateTime = DateTime.Now, + Name = "after", + SchoolId = 2 + }) + .EnableDiffLogEvent(new { title= "update Student", Modular=1, Operator="admin" }) + .ExecuteCommand(); } -} + } } \ No newline at end of file diff --git a/Src/Asp.Net/SqlServerTest/Models/Student.cs b/Src/Asp.Net/SqlServerTest/Models/Student.cs index fe6c6362a..2ff409787 100644 --- a/Src/Asp.Net/SqlServerTest/Models/Student.cs +++ b/Src/Asp.Net/SqlServerTest/Models/Student.cs @@ -11,7 +11,7 @@ namespace OrmTest.Models [SugarTable("STudent")] public class Student { - [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "ID")] + [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "ID",ColumnDescription ="主键")] public int Id { get; set; } public int? SchoolId { get; set; } public string Name { get; set; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs index 95020c962..2b51c0002 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs @@ -14,6 +14,7 @@ namespace SqlSugar this.Context.Ado.IsEnableLogEvent = true; } private SqlSugarClient Context { get; set; } + public Action OnDiffLogEvent { set { this.Context.Ado.DiffLogEvent = value; } } public Action OnError { set { this.Context.Ado.ErrorEvent = value; } } public Action OnLogExecuting { set { this.Context.Ado.LogEventStarting = value; } } public Action OnLogExecuted { set { this.Context.Ado.LogEventCompleted = value; } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 19d68e128..a95cf0596 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Linq.Expressions; using System.Text; @@ -28,6 +29,7 @@ namespace SqlSugar public bool IsAs { get; set; } public bool IsEnableDiffLogEvent { get; set; } public DiffLogModel diffModel { get; set; } + public virtual int ExecuteCommand() { PreToSql(); @@ -36,7 +38,9 @@ namespace SqlSugar string sql = UpdateBuilder.ToSqlString(); ValidateVersion(); RestoreMapping(); - var result= this.Ado.ExecuteCommand(sql, UpdateBuilder.Parameters == null ? null : UpdateBuilder.Parameters.ToArray()); + Before(sql); + var result = this.Ado.ExecuteCommand(sql, UpdateBuilder.Parameters == null ? null : UpdateBuilder.Parameters.ToArray()); + After(sql); return result; } @@ -89,9 +93,12 @@ namespace SqlSugar return this; } - public IUpdateable EnableDiffLogEvent() { + public IUpdateable EnableDiffLogEvent(object businessData = null) + { + diffModel = new DiffLogModel(); this.IsEnableDiffLogEvent = true; + diffModel.BusinessData = businessData; return this; } @@ -545,5 +552,53 @@ namespace SqlSugar } } } + private void After(string sql) + { + if (this.IsEnableDiffLogEvent) + { + var parameters = UpdateBuilder.Parameters.ToList(); + diffModel.AfterDate = GetDiffTable(sql, parameters); + diffModel.Time = this.Context.Ado.SqlExecutionTime; + this.Context.Ado.DiffLogEvent(diffModel); + } + } + + private void Before(string sql) + { + if (this.IsEnableDiffLogEvent) + { + var parameters = UpdateBuilder.Parameters; + diffModel.BeforeData = GetDiffTable(sql, parameters); + diffModel.Sql = sql; + diffModel.Parameters = parameters.ToArray(); + } + } + + private List GetDiffTable(string sql, List parameters) + { + List result = new List(); + var whereSql = Regex.Replace(sql, ".* WHERE ", "", RegexOptions.Singleline); + var dt = this.Context.Queryable().Where(whereSql).AddParameters(parameters).ToDataTable(); + if (dt.Rows != null && dt.Rows.Count > 0) + { + foreach (DataRow row in dt.Rows) + { + DiffLogTableInfo item = new DiffLogTableInfo(); + item.TableDescription = this.EntityInfo.TableDescription; + item.TableName = this.EntityInfo.DbTableName; + item.Columns = new List(); + foreach (DataColumn col in dt.Columns) + { + DiffLogColumnInfo addItem = new DiffLogColumnInfo(); + addItem.Value = row[col.ColumnName]; + addItem.ColumnName = col.ColumnName; + addItem.ColumnDescription = this.EntityInfo.Columns.First(it => it.DbColumnName.Equals(col.ColumnName, StringComparison.CurrentCultureIgnoreCase)).ColumnDescription; + item.Columns.Add(addItem); + } + result.Add(item); + } + } + return result; + } } } diff --git a/Src/Asp.Net/SqlSugar/Entities/DiffLogModel.cs b/Src/Asp.Net/SqlSugar/Entities/DiffLogModel.cs index 576a344c3..c549fb3f4 100644 --- a/Src/Asp.Net/SqlSugar/Entities/DiffLogModel.cs +++ b/Src/Asp.Net/SqlSugar/Entities/DiffLogModel.cs @@ -11,7 +11,8 @@ namespace SqlSugar public List BeforeData { get; set; } public SugarParameter[] Parameters { get; set; } public string Sql { get; set; } - public TimeSpan Time { get; set; } + public TimeSpan? Time { get; set; } + public object BusinessData { get; set; } } public class DiffLogTableInfo { @@ -23,5 +24,6 @@ namespace SqlSugar public string ColumnName { get; set; } public string ColumnDescription { get; set; } + public object Value { get; set; } } } diff --git a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs index b71881c10..e73eed875 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs @@ -46,7 +46,7 @@ namespace SqlSugar IUpdateable IgnoreColumns(Expression> columns); IUpdateable IgnoreColumns(Func ignoreColumMethod); IUpdateable IsEnableUpdateVersionValidation(); - IUpdateable EnableDiffLogEvent(); + IUpdateable EnableDiffLogEvent(object businessData = null); IUpdateable ReSetValue(Expression> setValueExpression); IUpdateable RemoveDataCache(); KeyValuePair> ToSql();