Browse Source

Diff Log

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
bd6de96e4e
  1. 35
      Src/Asp.Net/SqlServerTest/Demos/9_Aop.cs
  2. 2
      Src/Asp.Net/SqlServerTest/Models/Student.cs
  3. 1
      Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs
  4. 59
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs
  5. 4
      Src/Asp.Net/SqlSugar/Entities/DiffLogModel.cs
  6. 2
      Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs

35
Src/Asp.Net/SqlServerTest/Demos/9_Aop.cs

@ -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());
@ -23,11 +25,11 @@ namespace OrmTest.Demo
}; };
db.Aop.OnError = (exp) => db.Aop.OnError = (exp) =>
{ {
}; };
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();
@ -39,22 +41,43 @@ namespace OrmTest.Demo
catch (Exception) catch (Exception)
{ {
} }
//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();
} }
} }
} }

2
Src/Asp.Net/SqlServerTest/Models/Student.cs

@ -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; }

1
Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs

@ -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; } }

59
Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs

@ -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;
}
} }
} }

4
Src/Asp.Net/SqlSugar/Entities/DiffLogModel.cs

@ -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; }
} }
} }

2
Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs

@ -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…
Cancel
Save