- 优化 Aop.AuditValue 审计过的值,IUpdate.UpdateColumns 即使不指定该列也会更新;

This commit is contained in:
28810 2019-10-29 09:55:54 +08:00
parent f7633e9f67
commit 97351a4e6e
6 changed files with 62 additions and 17 deletions

View File

@ -236,7 +236,7 @@ namespace FreeSql
if (isThrow) throw new Exception($"不可添加,实体没有主键:{_db.Orm.GetEntityString(_entityType, data)}"); if (isThrow) throw new Exception($"不可添加,实体没有主键:{_db.Orm.GetEntityString(_entityType, data)}");
return false; return false;
} }
FreeSql.Internal.CommonProvider.InsertProvider<TEntity>.AuditDataValue(this, data, _db.Orm, _table); FreeSql.Internal.CommonProvider.InsertProvider<TEntity>.AuditDataValue(this, data, _db.Orm, _table, null);
var key = _db.Orm.GetEntityKeyString(_entityType, data, true); var key = _db.Orm.GetEntityKeyString(_entityType, data, true);
if (string.IsNullOrEmpty(key)) if (string.IsNullOrEmpty(key))
{ {
@ -294,7 +294,7 @@ namespace FreeSql
if (isThrow) throw new Exception($"不可更新,实体没有主键:{_db.Orm.GetEntityString(_entityType, data)}"); if (isThrow) throw new Exception($"不可更新,实体没有主键:{_db.Orm.GetEntityString(_entityType, data)}");
return false; return false;
} }
FreeSql.Internal.CommonProvider.UpdateProvider<TEntity>.AuditDataValue(this, data, _db.Orm, _table); FreeSql.Internal.CommonProvider.UpdateProvider<TEntity>.AuditDataValue(this, data, _db.Orm, _table, null);
var key = _db.Orm.GetEntityKeyString(_entityType, data, false); var key = _db.Orm.GetEntityKeyString(_entityType, data, false);
if (string.IsNullOrEmpty(key)) if (string.IsNullOrEmpty(key))
{ {

View File

@ -99,6 +99,13 @@
清空状态数据 清空状态数据
</summary> </summary>
</member> </member>
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)"> <member name="M:FreeSql.DbSet`1.Add(`0)">
<summary> <summary>
添加 添加
@ -124,6 +131,13 @@
</summary> </summary>
<param name="data"></param> <param name="data"></param>
</member> </member>
<member name="M:FreeSql.DbSet`1.Remove(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.AddOrUpdate(`0)"> <member name="M:FreeSql.DbSet`1.AddOrUpdate(`0)">
<summary> <summary>
添加或更新 添加或更新

View File

@ -141,6 +141,8 @@ namespace FreeSql.Tests.Sqlite
public bool? testBool1 { get; set; } public bool? testBool1 { get; set; }
public bool? testBool2 { get; set; } public bool? testBool2 { get; set; }
public TestDtoLeftJoin Obj { get; set; }
} }
class TestDtoLeftJoin class TestDtoLeftJoin
{ {
@ -167,7 +169,7 @@ namespace FreeSql.Tests.Sqlite
var testDto3 = select.Limit(10).ToList(a => new TestDto { }); var testDto3 = select.Limit(10).ToList(a => new TestDto { });
var testDto4 = select.Limit(10).ToList(a => new TestDto() { }); var testDto4 = select.Limit(10).ToList(a => new TestDto() { });
var testDto11 = select.LeftJoin<TestDtoLeftJoin>((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto { id = a.Id, name = a.Title }); var testDto11 = select.From<TestDtoLeftJoin>((_, b) => _.LeftJoin(a => b.Guid == a.TypeGuid)).Limit(10).ToList((a, b) => new TestDto { id = a.Id, name = a.Title, Obj = b });
var testDto22 = select.LeftJoin<TestDtoLeftJoin>((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto()); var testDto22 = select.LeftJoin<TestDtoLeftJoin>((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto());
var testDto33 = select.LeftJoin<TestDtoLeftJoin>((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto { }); var testDto33 = select.LeftJoin<TestDtoLeftJoin>((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto { });
var testDto44 = select.LeftJoin<TestDtoLeftJoin>((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto() { }); var testDto44 = select.LeftJoin<TestDtoLeftJoin>((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto() { });

View File

@ -408,6 +408,7 @@ namespace FreeSql.Tests
{ {
public Guid? Id { get; set; } public Guid? Id { get; set; }
public string xxx { get; set; } public string xxx { get; set; }
public string yyy { get; set; }
} }
public class TestAddEnum public class TestAddEnum
{ {
@ -420,6 +421,18 @@ namespace FreeSql.Tests
[Fact] [Fact]
public void Test1() public void Test1()
{ {
//g.mysql.Aop.AuditValue += (_, e) =>
//{
// if (e.AuditValueType == FreeSql.Aop.AuditValueType.Update)
// {
// if (e.Property.Name == "xxx")
// e.Value = "xxx";
// }
//};
//var tttee = g.mysql.Select<TestGuidId>().Limit(5).ToList();
//g.mysql.GetGuidRepository<TestGuidId>().UpdateDiy.SetSource(tttee).UpdateColumns(a => new { a.yyy }).NoneParameter().ExecuteAffrows();
//g.mysql.GlobalFilter //g.mysql.GlobalFilter
// .Apply<TestAddEnum>("test1", a => a.Id == TenrantId.Value) // .Apply<TestAddEnum>("test1", a => a.Id == TenrantId.Value)
// .Apply<AuthorTest>("test2", a => a.Id == 111) // .Apply<AuthorTest>("test2", a => a.Id == 111)

View File

@ -19,6 +19,7 @@ namespace FreeSql.Internal.CommonProvider
protected CommonExpression _commonExpression; protected CommonExpression _commonExpression;
protected List<T1> _source = new List<T1>(); protected List<T1> _source = new List<T1>();
protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase); protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
protected Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
protected TableInfo _table; protected TableInfo _table;
protected Func<string, string> _tableRule; protected Func<string, string> _tableRule;
protected bool _noneParameter, _insertIdentity; protected bool _noneParameter, _insertIdentity;
@ -52,6 +53,7 @@ namespace FreeSql.Internal.CommonProvider
_insertIdentity = false; _insertIdentity = false;
_source.Clear(); _source.Clear();
_ignore.Clear(); _ignore.Clear();
_auditValueChangedDict.Clear();
_params = null; _params = null;
IgnoreCanInsert(); IgnoreCanInsert();
} }
@ -85,7 +87,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (source != null) if (source != null)
{ {
AuditDataValue(this, source, _orm, _table); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.Add(source); _source.Add(source);
} }
return this; return this;
@ -94,7 +96,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (source != null) if (source != null)
{ {
AuditDataValue(this, source, _orm, _table); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.AddRange(source); _source.AddRange(source);
} }
return this; return this;
@ -104,19 +106,19 @@ namespace FreeSql.Internal.CommonProvider
if (source != null) if (source != null)
{ {
source = source.Where(a => a != null).ToList(); source = source.Where(a => a != null).ToList();
AuditDataValue(this, source, _orm, _table); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.AddRange(source); _source.AddRange(source);
} }
return this; return this;
} }
public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table) public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
{ {
if (data?.Any() != true) return; if (data?.Any() != true) return;
foreach (var d in data) foreach (var d in data)
AuditDataValue(sender, d, orm, table); AuditDataValue(sender, d, orm, table, changedDict);
} }
public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table) public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
{ {
if (data == null) return; if (data == null) return;
foreach (var col in table.Columns.Values) foreach (var col in table.Columns.Values)
@ -129,7 +131,11 @@ namespace FreeSql.Internal.CommonProvider
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Insert, col, table.Properties[col.CsName], val); var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Insert, col, table.Properties[col.CsName], val);
orm.Aop.AuditValue(sender, auditArgs); orm.Aop.AuditValue(sender, auditArgs);
if (auditArgs.IsChanged) if (auditArgs.IsChanged)
{
col.SetMapValue(data, val = auditArgs.Value); col.SetMapValue(data, val = auditArgs.Value);
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
changedDict.Add(col.Attribute.Name, true);
}
} }
} }
} }
@ -375,7 +381,7 @@ namespace FreeSql.Internal.CommonProvider
var cols = _commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, columns?.Body, false, null).ToDictionary(a => a, a => true); var cols = _commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, columns?.Body, false, null).ToDictionary(a => a, a => true);
_ignore.Clear(); _ignore.Clear();
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
if (cols.ContainsKey(col.Attribute.Name) == false) if (cols.ContainsKey(col.Attribute.Name) == false && _auditValueChangedDict.ContainsKey(col.Attribute.Name) == false)
_ignore.Add(col.Attribute.Name, true); _ignore.Add(col.Attribute.Name, true);
return this; return this;
} }

View File

@ -20,6 +20,7 @@ namespace FreeSql.Internal.CommonProvider
protected CommonExpression _commonExpression; protected CommonExpression _commonExpression;
protected List<T1> _source = new List<T1>(); protected List<T1> _source = new List<T1>();
protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase); protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
protected Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
protected TableInfo _table; protected TableInfo _table;
protected Func<string, string> _tableRule; protected Func<string, string> _tableRule;
protected StringBuilder _where = new StringBuilder(); protected StringBuilder _where = new StringBuilder();
@ -59,6 +60,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
_source.Clear(); _source.Clear();
_ignore.Clear(); _ignore.Clear();
_auditValueChangedDict.Clear();
_where.Clear(); _where.Clear();
_set.Clear(); _set.Clear();
_setIncr.Clear(); _setIncr.Clear();
@ -281,12 +283,12 @@ namespace FreeSql.Internal.CommonProvider
var cols = columns.ToDictionary(a => a); var cols = columns.ToDictionary(a => a);
_ignore.Clear(); _ignore.Clear();
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
if (cols.ContainsKey(col.Attribute.Name) == false && cols.ContainsKey(col.CsName) == false) if (cols.ContainsKey(col.Attribute.Name) == false && cols.ContainsKey(col.CsName) == false && _auditValueChangedDict.ContainsKey(col.Attribute.Name) == false)
_ignore.Add(col.Attribute.Name, true); _ignore.Add(col.Attribute.Name, true);
return this; return this;
} }
public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table) public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
{ {
if (data?.Any() != true) return; if (data?.Any() != true) return;
if (orm.Aop.AuditValue == null) return; if (orm.Aop.AuditValue == null) return;
@ -299,11 +301,15 @@ namespace FreeSql.Internal.CommonProvider
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val); var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val);
orm.Aop.AuditValue(sender, auditArgs); orm.Aop.AuditValue(sender, auditArgs);
if (auditArgs.IsChanged) if (auditArgs.IsChanged)
{
col.SetMapValue(d, val = auditArgs.Value); col.SetMapValue(d, val = auditArgs.Value);
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
changedDict.Add(col.Attribute.Name, true);
}
} }
} }
} }
public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table) public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
{ {
if (orm.Aop.AuditValue == null) return; if (orm.Aop.AuditValue == null) return;
if (data == null) return; if (data == null) return;
@ -313,7 +319,11 @@ namespace FreeSql.Internal.CommonProvider
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val); var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val);
orm.Aop.AuditValue(sender, auditArgs); orm.Aop.AuditValue(sender, auditArgs);
if (auditArgs.IsChanged) if (auditArgs.IsChanged)
{
col.SetMapValue(data, val = auditArgs.Value); col.SetMapValue(data, val = auditArgs.Value);
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
changedDict.Add(col.Attribute.Name, true);
}
} }
} }
@ -321,7 +331,7 @@ namespace FreeSql.Internal.CommonProvider
public IUpdate<T1> SetSource(IEnumerable<T1> source) public IUpdate<T1> SetSource(IEnumerable<T1> source)
{ {
if (source == null || source.Any() == false) return this; if (source == null || source.Any() == false) return this;
AuditDataValue(this, source, _orm, _table); AuditDataValue(this, source, _orm, _table, _auditValueChangedDict);
_source.AddRange(source.Where(a => a != null)); _source.AddRange(source.Where(a => a != null));
return this; return this;
} }
@ -486,9 +496,9 @@ namespace FreeSql.Internal.CommonProvider
cwsb.Append(" \r\nWHEN "); cwsb.Append(" \r\nWHEN ");
ToSqlWhen(cwsb, _table.Primarys, d); ToSqlWhen(cwsb, _table.Primarys, d);
cwsb.Append(" THEN "); cwsb.Append(" THEN ");
var value = col.GetMapValue(d); var val = col.GetMapValue(d);
cwsb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, value))); cwsb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val)));
if (value == null || value == DBNull.Value) nulls++; if (val == null || val == DBNull.Value) nulls++;
} }
cwsb.Append(" END"); cwsb.Append(" END");
if (nulls == _source.Count) sb.Append("NULL"); if (nulls == _source.Count) sb.Append("NULL");