From 6ca226a8e40193df016a8a98643fed83f313fae6 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Thu, 26 Sep 2019 15:45:40 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20ColumnAttribute=20?= =?UTF-8?q?=E5=8F=AF=E6=8F=92=E5=85=A5(CanInsert)=E3=80=81=E5=8F=AF?= =?UTF-8?q?=E6=9B=B4=E6=96=B0(CanUpdate)=EF=BC=9B#99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataAnnotations/MySqlFluentTest.cs | 41 ++++++++++++++++++- FreeSql/DataAnnotations/ColumnAttribute.cs | 10 +++++ FreeSql/DataAnnotations/ColumnFluent.cs | 21 ++++++++++ FreeSql/FreeSql.xml | 34 +++++++++++++++ .../Internal/CommonProvider/InsertProvider.cs | 13 ++++++ .../Internal/CommonProvider/UpdateProvider.cs | 13 ++++++ FreeSql/Internal/CommonUtils.cs | 6 +++ 7 files changed, 137 insertions(+), 1 deletion(-) diff --git a/FreeSql.Tests/FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs b/FreeSql.Tests/FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs index 2dac212c8..5f7369633 100644 --- a/FreeSql.Tests/FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs @@ -125,7 +125,6 @@ public void IsIgnore() Assert.NotNull(find); Assert.Equal(item.id, find.id); } - class TestIsIgnore { public Guid id { get; set; } @@ -133,5 +132,45 @@ class TestIsIgnore [Column(IsIgnore = true)] public bool isignore { get; set; } } + + [Fact] + public void CanInsert_CanUpdate() + { + var item = new TestCanInsert { title = "testtitle", testfield1 = 1000, testfield2 = 1000 }; + var sql = g.mysql.Insert(item).ToSql().Replace("\r\n", ""); + Assert.Equal("INSERT INTO `TestCanInsert`(`id`, `title`, `testfield2`) VALUES(?id_0, ?title_0, ?testfield2_0)", sql); + + Assert.Equal(1, g.mysql.Insert(item).ExecuteAffrows()); + var find = g.mysql.Select().Where(a => a.id == item.id).First(); + Assert.NotNull(find); + Assert.Equal(item.id, find.id); + Assert.Equal(item.title, find.title); + Assert.NotEqual(item.testfield1, find.testfield1); + Assert.Equal(0, find.testfield1); + Assert.Equal(item.testfield2, find.testfield2); + + item.title = "testtitle_update"; + item.testfield2 = 0; + sql = g.mysql.Update().SetSource(item).ToSql().Replace("\r\n", ""); + Assert.Equal($"UPDATE `TestCanInsert` SET `id` = ?p_0, `title` = ?p_1, `testfield1` = ?p_2 WHERE (`id` = '{item.id}')", sql); + + Assert.Equal(1, g.mysql.Update().SetSource(item).ExecuteAffrows()); + find = g.mysql.Select().Where(a => a.id == item.id).First(); + Assert.NotNull(find); + Assert.Equal(item.id, find.id); + Assert.Equal(item.title, find.title); + Assert.Equal(item.testfield1, find.testfield1); + Assert.NotEqual(item.testfield2, find.testfield2); + Assert.Equal(1000, find.testfield1); + } + class TestCanInsert + { + public Guid id { get; set; } + public string title { get; set; } + [Column(CanInsert = false)] + public long testfield1 { get; set; } + [Column(CanUpdate = false)] + public long testfield2 { get; set; } + } } } diff --git a/FreeSql/DataAnnotations/ColumnAttribute.cs b/FreeSql/DataAnnotations/ColumnAttribute.cs index 9040fe966..969558729 100644 --- a/FreeSql/DataAnnotations/ColumnAttribute.cs +++ b/FreeSql/DataAnnotations/ColumnAttribute.cs @@ -94,5 +94,15 @@ public string Unique /// <0时排后面,...-3,-2,-1 /// public short Position { get => _Position ?? 0; set => _Position = value; } + + internal bool? _CanInsert, _CanUpdate; + /// + /// 该字段是否可以插入,默认值true,指定为false插入时该字段会被忽略 + /// + public bool CanInsert { get => _CanInsert ?? true; set => _CanInsert = value; } + /// + /// 该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略 + /// + public bool CanUpdate { get => _CanUpdate ?? true; set => _CanUpdate = value; } } } diff --git a/FreeSql/DataAnnotations/ColumnFluent.cs b/FreeSql/DataAnnotations/ColumnFluent.cs index bd7313747..0f80bb028 100644 --- a/FreeSql/DataAnnotations/ColumnFluent.cs +++ b/FreeSql/DataAnnotations/ColumnFluent.cs @@ -113,5 +113,26 @@ public ColumnFluent Position(short value) _column.Position = value; return this; } + + /// + /// 该字段是否可以插入,默认值true,指定为false插入时该字段会被忽略 + /// + /// + /// + public ColumnFluent CanInsert(bool value) + { + _column.CanInsert = value; + return this; + } + /// + /// 该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略 + /// + /// + /// + public ColumnFluent CanUpdate(bool value) + { + _column.CanUpdate = value; + return this; + } } } diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 1bab48234..3d2a21c9f 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -73,6 +73,16 @@ <0时排后面,...-3,-2,-1 + + + 该字段是否可以插入,默认值true,指定为false插入时该字段会被忽略 + + + + + 该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略 + + 数据库列名 @@ -140,6 +150,20 @@ + + + 该字段是否可以插入,默认值true,指定为false插入时该字段会被忽略 + + + + + + + 该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略 + + + + 手工绑定 OneToMany、ManyToOne 导航关系 @@ -2418,6 +2442,16 @@ + + + AsType, Ctor, ClearData 三处地方需要重新加载 + + + + + AsType, Ctor, ClearData 三处地方需要重新加载 + + 通过属性的注释文本,通过 xml 读取 diff --git a/FreeSql/Internal/CommonProvider/InsertProvider.cs b/FreeSql/Internal/CommonProvider/InsertProvider.cs index 8247d3a50..c425d3798 100644 --- a/FreeSql/Internal/CommonProvider/InsertProvider.cs +++ b/FreeSql/Internal/CommonProvider/InsertProvider.cs @@ -34,14 +34,26 @@ public InsertProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression co _table = _commonUtils.GetTableByEntity(typeof(T1)); _noneParameter = _orm.CodeFirst.IsNoneCommandParameter; if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure(); + IgnoreCanInsert(); } + /// + /// AsType, Ctor, ClearData 三处地方需要重新加载 + /// + protected void IgnoreCanInsert() + { + if (_table == null || _table.Type == typeof(object)) return; + foreach (var col in _table.Columns.Values) + if (col.Attribute.CanInsert == false) + _ignore.Add(col.Attribute.Name, true); + } protected void ClearData() { _insertIdentity = false; _source.Clear(); _ignore.Clear(); _params = null; + IgnoreCanInsert(); } public IInsert WithTransaction(DbTransaction transaction) @@ -569,6 +581,7 @@ public IInsert AsType(Type entityType) var newtb = _commonUtils.GetTableByEntity(entityType); _table = newtb ?? throw new Exception("IInsert.AsType 参数错误,请传入正确的实体类型"); if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType); + IgnoreCanInsert(); return this; } diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index e29c443aa..b021f6b1d 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -40,8 +40,19 @@ public UpdateProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression co _noneParameter = _orm.CodeFirst.IsNoneCommandParameter; this.Where(_commonUtils.WhereObject(_table, "", dywhere)); if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure(); + IgnoreCanUpdate(); } + /// + /// AsType, Ctor, ClearData 三处地方需要重新加载 + /// + protected void IgnoreCanUpdate() + { + if (_table == null || _table.Type == typeof(object)) return; + foreach (var col in _table.Columns.Values) + if (col.Attribute.CanUpdate == false) + _ignore.Add(col.Attribute.Name, true); + } protected void ClearData() { _source.Clear(); @@ -51,6 +62,7 @@ protected void ClearData() _setIncr.Clear(); _params.Clear(); _paramsSource.Clear(); + IgnoreCanUpdate(); } public IUpdate WithTransaction(DbTransaction transaction) @@ -610,6 +622,7 @@ public IUpdate AsType(Type entityType) var newtb = _commonUtils.GetTableByEntity(entityType); _table = newtb ?? throw new Exception("IUpdate.AsType 参数错误,请传入正确的实体类型"); if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType); + IgnoreCanUpdate(); return this; } diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index 6f0658957..e976465d1 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -128,6 +128,8 @@ public ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto) if (trycol._Uniques != null) attr._Uniques = trycol._Uniques; if (trycol.MapType != null) attr.MapType = trycol.MapType; if (trycol._Position != null) attr._Position = trycol.Position; + if (trycol._CanInsert != null) attr._CanInsert = trycol.CanInsert; + if (trycol._CanUpdate != null) attr._CanUpdate = trycol.CanUpdate; if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue; } var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false); @@ -146,6 +148,8 @@ public ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto) if (tryattr._Uniques != null) attr._Uniques = tryattr._Uniques; if (tryattr.MapType != null) attr.MapType = tryattr.MapType; if (tryattr._Position != null) attr._Position = tryattr.Position; + if (tryattr._CanInsert != null) attr._CanInsert = tryattr.CanInsert; + if (tryattr._CanUpdate != null) attr._CanUpdate = tryattr.CanUpdate; if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue; } ColumnAttribute ret = null; @@ -160,6 +164,8 @@ public ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto) if (attr._Uniques != null) ret = attr; if (attr.MapType != null) ret = attr; if (attr._Position != null) ret = attr; + if (attr._CanInsert != null) ret = attr; + if (attr._CanUpdate != null) ret = attr; if (attr.DbDefautValue != null) ret = attr; if (ret != null && ret.MapType == null) ret.MapType = proto.PropertyType; return ret;