From 97351a4e6e99bb44b8ea980a4fb5cefe6d2f8859 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Tue, 29 Oct 2019 09:55:54 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20Aop.AuditValue=20?= =?UTF-8?q?=E5=AE=A1=E8=AE=A1=E8=BF=87=E7=9A=84=E5=80=BC=EF=BC=8CIUpdate.U?= =?UTF-8?q?pdateColumns=20=E5=8D=B3=E4=BD=BF=E4=B8=8D=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E8=AF=A5=E5=88=97=E4=B9=9F=E4=BC=9A=E6=9B=B4=E6=96=B0=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/DbSet/DbSet.cs | 4 ++-- FreeSql.DbContext/FreeSql.DbContext.xml | 14 +++++++++++ .../Sqlite/Curd/SqliteSelectTest.cs | 4 +++- FreeSql.Tests/FreeSql.Tests/UnitTest1.cs | 13 ++++++++++ .../Internal/CommonProvider/InsertProvider.cs | 20 ++++++++++------ .../Internal/CommonProvider/UpdateProvider.cs | 24 +++++++++++++------ 6 files changed, 62 insertions(+), 17 deletions(-) diff --git a/FreeSql.DbContext/DbSet/DbSet.cs b/FreeSql.DbContext/DbSet/DbSet.cs index 56651882..6150feb4 100644 --- a/FreeSql.DbContext/DbSet/DbSet.cs +++ b/FreeSql.DbContext/DbSet/DbSet.cs @@ -236,7 +236,7 @@ namespace FreeSql if (isThrow) throw new Exception($"不可添加,实体没有主键:{_db.Orm.GetEntityString(_entityType, data)}"); return false; } - FreeSql.Internal.CommonProvider.InsertProvider.AuditDataValue(this, data, _db.Orm, _table); + FreeSql.Internal.CommonProvider.InsertProvider.AuditDataValue(this, data, _db.Orm, _table, null); var key = _db.Orm.GetEntityKeyString(_entityType, data, true); if (string.IsNullOrEmpty(key)) { @@ -294,7 +294,7 @@ namespace FreeSql if (isThrow) throw new Exception($"不可更新,实体没有主键:{_db.Orm.GetEntityString(_entityType, data)}"); return false; } - FreeSql.Internal.CommonProvider.UpdateProvider.AuditDataValue(this, data, _db.Orm, _table); + FreeSql.Internal.CommonProvider.UpdateProvider.AuditDataValue(this, data, _db.Orm, _table, null); var key = _db.Orm.GetEntityKeyString(_entityType, data, false); if (string.IsNullOrEmpty(key)) { diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 8b5be7c7..2711e35d 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -99,6 +99,13 @@ 清空状态数据 + + + 根据 lambda 条件删除数据 + + + + 添加 @@ -124,6 +131,13 @@ + + + 根据 lambda 条件删除数据 + + + + 添加或更新 diff --git a/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs b/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs index 15702301..2a29e6a0 100644 --- a/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs @@ -141,6 +141,8 @@ namespace FreeSql.Tests.Sqlite public bool? testBool1 { get; set; } public bool? testBool2 { get; set; } + + public TestDtoLeftJoin Obj { get; set; } } class TestDtoLeftJoin { @@ -167,7 +169,7 @@ namespace FreeSql.Tests.Sqlite var testDto3 = select.Limit(10).ToList(a => new TestDto { }); var testDto4 = select.Limit(10).ToList(a => new TestDto() { }); - var testDto11 = select.LeftJoin((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto { id = a.Id, name = a.Title }); + var testDto11 = select.From((_, 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((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto()); var testDto33 = select.LeftJoin((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto { }); var testDto44 = select.LeftJoin((a, b) => b.Guid == a.TypeGuid).Limit(10).ToList(a => new TestDto() { }); diff --git a/FreeSql.Tests/FreeSql.Tests/UnitTest1.cs b/FreeSql.Tests/FreeSql.Tests/UnitTest1.cs index 214e0646..df1dff9a 100644 --- a/FreeSql.Tests/FreeSql.Tests/UnitTest1.cs +++ b/FreeSql.Tests/FreeSql.Tests/UnitTest1.cs @@ -408,6 +408,7 @@ namespace FreeSql.Tests { public Guid? Id { get; set; } public string xxx { get; set; } + public string yyy { get; set; } } public class TestAddEnum { @@ -420,6 +421,18 @@ namespace FreeSql.Tests [Fact] 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().Limit(5).ToList(); + //g.mysql.GetGuidRepository().UpdateDiy.SetSource(tttee).UpdateColumns(a => new { a.yyy }).NoneParameter().ExecuteAffrows(); + //g.mysql.GlobalFilter // .Apply("test1", a => a.Id == TenrantId.Value) // .Apply("test2", a => a.Id == 111) diff --git a/FreeSql/Internal/CommonProvider/InsertProvider.cs b/FreeSql/Internal/CommonProvider/InsertProvider.cs index 1130da06..f0389815 100644 --- a/FreeSql/Internal/CommonProvider/InsertProvider.cs +++ b/FreeSql/Internal/CommonProvider/InsertProvider.cs @@ -19,6 +19,7 @@ namespace FreeSql.Internal.CommonProvider protected CommonExpression _commonExpression; protected List _source = new List(); protected Dictionary _ignore = new Dictionary(StringComparer.CurrentCultureIgnoreCase); + protected Dictionary _auditValueChangedDict = new Dictionary(StringComparer.CurrentCultureIgnoreCase); protected TableInfo _table; protected Func _tableRule; protected bool _noneParameter, _insertIdentity; @@ -52,6 +53,7 @@ namespace FreeSql.Internal.CommonProvider _insertIdentity = false; _source.Clear(); _ignore.Clear(); + _auditValueChangedDict.Clear(); _params = null; IgnoreCanInsert(); } @@ -85,7 +87,7 @@ namespace FreeSql.Internal.CommonProvider { if (source != null) { - AuditDataValue(this, source, _orm, _table); + AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); _source.Add(source); } return this; @@ -94,7 +96,7 @@ namespace FreeSql.Internal.CommonProvider { if (source != null) { - AuditDataValue(this, source, _orm, _table); + AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); _source.AddRange(source); } return this; @@ -104,19 +106,19 @@ namespace FreeSql.Internal.CommonProvider if (source != null) { source = source.Where(a => a != null).ToList(); - AuditDataValue(this, source, _orm, _table); + AuditDataValue(this, source, _orm, _table, _auditValueChangedDict); _source.AddRange(source); } return this; } - public static void AuditDataValue(object sender, IEnumerable data, IFreeSql orm, TableInfo table) + public static void AuditDataValue(object sender, IEnumerable data, IFreeSql orm, TableInfo table, Dictionary changedDict) { if (data?.Any() != true) return; 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 changedDict) { if (data == null) return; 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); orm.Aop.AuditValue(sender, auditArgs); if (auditArgs.IsChanged) + { 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); _ignore.Clear(); 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); return this; } diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index d98b6392..c4ddd9d2 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -20,6 +20,7 @@ namespace FreeSql.Internal.CommonProvider protected CommonExpression _commonExpression; protected List _source = new List(); protected Dictionary _ignore = new Dictionary(StringComparer.CurrentCultureIgnoreCase); + protected Dictionary _auditValueChangedDict = new Dictionary(StringComparer.CurrentCultureIgnoreCase); protected TableInfo _table; protected Func _tableRule; protected StringBuilder _where = new StringBuilder(); @@ -59,6 +60,7 @@ namespace FreeSql.Internal.CommonProvider { _source.Clear(); _ignore.Clear(); + _auditValueChangedDict.Clear(); _where.Clear(); _set.Clear(); _setIncr.Clear(); @@ -281,12 +283,12 @@ namespace FreeSql.Internal.CommonProvider var cols = columns.ToDictionary(a => a); _ignore.Clear(); 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); return this; } - public static void AuditDataValue(object sender, IEnumerable data, IFreeSql orm, TableInfo table) + public static void AuditDataValue(object sender, IEnumerable data, IFreeSql orm, TableInfo table, Dictionary changedDict) { if (data?.Any() != true) 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); orm.Aop.AuditValue(sender, auditArgs); if (auditArgs.IsChanged) + { 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 changedDict) { if (orm.Aop.AuditValue == 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); orm.Aop.AuditValue(sender, auditArgs); if (auditArgs.IsChanged) + { 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 SetSource(IEnumerable source) { 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)); return this; } @@ -486,9 +496,9 @@ namespace FreeSql.Internal.CommonProvider cwsb.Append(" \r\nWHEN "); ToSqlWhen(cwsb, _table.Primarys, d); cwsb.Append(" THEN "); - var value = col.GetMapValue(d); - cwsb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, value))); - if (value == null || value == DBNull.Value) nulls++; + var val = col.GetMapValue(d); + cwsb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, col.Attribute.MapType, val))); + if (val == null || val == DBNull.Value) nulls++; } cwsb.Append(" END"); if (nulls == _source.Count) sb.Append("NULL");