mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-16 07:57:33 +08:00
Diff Log
This commit is contained in:
parent
3055a3de16
commit
bd6de96e4e
@ -13,6 +13,8 @@ namespace OrmTest.Demo
|
|||||||
public static void Init()
|
public static void Init()
|
||||||
{
|
{
|
||||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
|
||||||
|
|
||||||
|
|
||||||
db.Aop.OnLogExecuted = (sql, pars) =>
|
db.Aop.OnLogExecuted = (sql, pars) =>
|
||||||
{
|
{
|
||||||
Console.Write("time:" + db.Ado.SqlExecutionTime.ToString());
|
Console.Write("time:" + db.Ado.SqlExecutionTime.ToString());
|
||||||
@ -27,7 +29,7 @@ namespace OrmTest.Demo
|
|||||||
};
|
};
|
||||||
db.Aop.OnExecutingChangeSql = (sql, pars) =>
|
db.Aop.OnExecutingChangeSql = (sql, pars) =>
|
||||||
{
|
{
|
||||||
return new KeyValuePair<string, SugarParameter[]>(sql,pars);
|
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
|
||||||
};
|
};
|
||||||
|
|
||||||
db.Queryable<CMStudent>().ToList();
|
db.Queryable<CMStudent>().ToList();
|
||||||
@ -45,16 +47,37 @@ namespace OrmTest.Demo
|
|||||||
|
|
||||||
//diff log demo
|
//diff log demo
|
||||||
|
|
||||||
db.Ado.DiffLogEvent = it =>
|
db.Aop.OnDiffLogEvent = it =>
|
||||||
{
|
{
|
||||||
var editBeforeData = it.BeforeData;
|
var editBeforeData = it.BeforeData;
|
||||||
var editAfterData = it.AfterDate;
|
var editAfterData = it.AfterDate;
|
||||||
var sql = it.Sql;
|
var sql = it.Sql;
|
||||||
var parameter = it.Parameters;
|
var parameter = it.Parameters;
|
||||||
|
var data = it.BusinessData;
|
||||||
};
|
};
|
||||||
|
|
||||||
db.Updateable<Student>().EnableDiffLogEvent().ExecuteCommand();
|
|
||||||
|
var id = db.Queryable<Student>().First().Id;
|
||||||
|
db.Updateable<Student>(new Student()
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
CreateTime = DateTime.Now,
|
||||||
|
Name = "before",
|
||||||
|
SchoolId = 1
|
||||||
|
})
|
||||||
|
.EnableDiffLogEvent(new { title = "update Student", Modular = 1, Operator = "admin" }).ExecuteCommand();
|
||||||
|
|
||||||
|
|
||||||
|
db.Updateable<Student>(new Student()
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
CreateTime = DateTime.Now,
|
||||||
|
Name = "after",
|
||||||
|
SchoolId = 2
|
||||||
|
})
|
||||||
|
.EnableDiffLogEvent(new { title= "update Student", Modular=1, Operator="admin" })
|
||||||
|
.ExecuteCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,7 +11,7 @@ namespace OrmTest.Models
|
|||||||
[SugarTable("STudent")]
|
[SugarTable("STudent")]
|
||||||
public class 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 Id { get; set; }
|
||||||
public int? SchoolId { get; set; }
|
public int? SchoolId { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
@ -14,6 +14,7 @@ namespace SqlSugar
|
|||||||
this.Context.Ado.IsEnableLogEvent = true;
|
this.Context.Ado.IsEnableLogEvent = true;
|
||||||
}
|
}
|
||||||
private SqlSugarClient Context { get; set; }
|
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<Exception> OnError { set { this.Context.Ado.ErrorEvent = value; } }
|
||||||
public Action<string, SugarParameter[]> OnLogExecuting { set { this.Context.Ado.LogEventStarting = value; } }
|
public Action<string, SugarParameter[]> OnLogExecuting { set { this.Context.Ado.LogEventStarting = value; } }
|
||||||
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.Ado.LogEventCompleted = value; } }
|
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.Ado.LogEventCompleted = value; } }
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -28,6 +29,7 @@ namespace SqlSugar
|
|||||||
public bool IsAs { get; set; }
|
public bool IsAs { get; set; }
|
||||||
public bool IsEnableDiffLogEvent { get; set; }
|
public bool IsEnableDiffLogEvent { get; set; }
|
||||||
public DiffLogModel diffModel { get; set; }
|
public DiffLogModel diffModel { get; set; }
|
||||||
|
|
||||||
public virtual int ExecuteCommand()
|
public virtual int ExecuteCommand()
|
||||||
{
|
{
|
||||||
PreToSql();
|
PreToSql();
|
||||||
@ -36,7 +38,9 @@ namespace SqlSugar
|
|||||||
string sql = UpdateBuilder.ToSqlString();
|
string sql = UpdateBuilder.ToSqlString();
|
||||||
ValidateVersion();
|
ValidateVersion();
|
||||||
RestoreMapping();
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,9 +93,12 @@ namespace SqlSugar
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IUpdateable<T> EnableDiffLogEvent() {
|
public IUpdateable<T> EnableDiffLogEvent(object businessData = null)
|
||||||
|
{
|
||||||
|
|
||||||
|
diffModel = new DiffLogModel();
|
||||||
this.IsEnableDiffLogEvent = true;
|
this.IsEnableDiffLogEvent = true;
|
||||||
|
diffModel.BusinessData = businessData;
|
||||||
return this;
|
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<DiffLogTableInfo> GetDiffTable(string sql, List<SugarParameter> parameters)
|
||||||
|
{
|
||||||
|
List<DiffLogTableInfo> result = new List<DiffLogTableInfo>();
|
||||||
|
var whereSql = Regex.Replace(sql, ".* WHERE ", "", RegexOptions.Singleline);
|
||||||
|
var dt = this.Context.Queryable<T>().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<DiffLogColumnInfo>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,8 @@ namespace SqlSugar
|
|||||||
public List<DiffLogTableInfo> BeforeData { get; set; }
|
public List<DiffLogTableInfo> BeforeData { get; set; }
|
||||||
public SugarParameter[] Parameters { get; set; }
|
public SugarParameter[] Parameters { get; set; }
|
||||||
public string Sql { 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
|
public class DiffLogTableInfo
|
||||||
{
|
{
|
||||||
@ -23,5 +24,6 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public string ColumnName { get; set; }
|
public string ColumnName { get; set; }
|
||||||
public string ColumnDescription { get; set; }
|
public string ColumnDescription { get; set; }
|
||||||
|
public object Value { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ namespace SqlSugar
|
|||||||
IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns);
|
IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns);
|
||||||
IUpdateable<T> IgnoreColumns(Func<string, bool> ignoreColumMethod);
|
IUpdateable<T> IgnoreColumns(Func<string, bool> ignoreColumMethod);
|
||||||
IUpdateable<T> IsEnableUpdateVersionValidation();
|
IUpdateable<T> IsEnableUpdateVersionValidation();
|
||||||
IUpdateable<T> EnableDiffLogEvent();
|
IUpdateable<T> EnableDiffLogEvent(object businessData = null);
|
||||||
IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression);
|
IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression);
|
||||||
IUpdateable<T> RemoveDataCache();
|
IUpdateable<T> RemoveDataCache();
|
||||||
KeyValuePair<string,List<SugarParameter>> ToSql();
|
KeyValuePair<string,List<SugarParameter>> ToSql();
|
||||||
|
Loading…
Reference in New Issue
Block a user