From 0b8f2127f73abd93575be9689b4e40cc202e86fc Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Thu, 14 Dec 2023 20:23:29 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20IUpdate.SetDtoIgnore?= =?UTF-8?q?=20=E5=BF=BD=E7=95=A5Dto=E6=9B=B4=E6=96=B0=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=9B#1688?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/FreeSql.xml | 303 +++++++++++++++++- FreeSql/Interface/Curd/IUpdate.cs | 46 ++- .../Internal/CommonProvider/UpdateProvider.cs | 49 +-- 3 files changed, 342 insertions(+), 56 deletions(-) diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 79f068d6..8f213e5c 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -1104,6 +1104,82 @@ + + + 动态创建实体类型 + + + + + 配置Class + + 类名 + 类标记的特性[Table(Name = "xxx")] [Index(xxxx)] + + + + + 配置属性 + + 属性名称 + 属性类型 + 属性标记的特性-支持多个 + + + + + 配置属性 + + 属性名称 + 属性类型 + 该属性是否重写父类属性 + 属性标记的特性-支持多个 + + + + + 配置属性 + + 属性名称 + 属性类型 + 该属性是否重写父类属性 + 属性默认值 + 属性标记的特性-支持多个 + + + + + 配置父类 + + 父类类型 + + + + + Override属性 + + + + + + Emit动态创建出Class - Type + + + + + + 首字母小写 + + + + + + + 首字母大写 + + + + 获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 null @@ -3163,11 +3239,24 @@ 设置更新的列 SetDto(new { title = "xxx", clicks = 2 }) SetDto(new Dictionary<string, object> { ["title"] = "xxx", ["clicks"] = 2 }) - 注意:标记 [Column(CanUpdate = false)] 的属性不会被更新 + 注意:标记 [Column(CanUpdate = false)] 的属性不会被更新 + 注意:SetDto 与 IUpdate.IgnoreColumns/UpdateColumns 不能同时使用 dto 或 Dictionary<string, object> + + + 设置更新的列 + SetDto(new { title = "xxx", clicks = 2 }) + SetDto(new Dictionary<string, object> { ["title"] = "xxx", ["clicks"] = 2 }) + 注意:标记 [Column(CanUpdate = false)] 的属性不会被更新 + 注意:SetDto 与 IUpdate.IgnoreColumns/UpdateColumns 不能同时使用 + + dto 或 Dictionary<string, object> + 属性值忽略判断, true忽略 + + lambda表达式条件,仅支持实体基础成员(不包含导航对象) @@ -3270,6 +3359,13 @@ + + + 执行SQL语句,返回更新后的记录 + 注意:此方法只有 Postgresql/SqlServer 有效果 + + + 指定事务对象 @@ -3614,6 +3710,177 @@ + + + 测试数据库是否连接正确,本方法执行如下命令: + MySql/SqlServer/PostgreSQL/达梦/人大金仓/神通: SELECT 1 + Oracle: SELECT 1 FROM dual + + 命令超时设置(秒) + + true: 成功, false: 失败 + + + + 查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】 + + + + + + + + + + 查询,ExecuteReaderAsync(dr => {}, "select * from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 查询 + + + + + + + + + 查询,ExecuteArrayAsync("select * from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 查询 + + + + + + + + + 查询,ExecuteDataSetAsync("select * from user where age > @age; select 2", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 查询 + + + + + + + + + 查询,ExecuteDataTableAsync("select * from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 在【主库】执行 + + + + + + + + + 在【主库】执行,ExecuteNonQueryAsync("delete from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 在【主库】执行 + + + + + + + + + 在【主库】执行,ExecuteScalarAsync("select 1 from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new SqlParameter { ParameterName = "age", Value = 25 }) + + + + + + + + + + + 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + + 执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 }) + + + + + + + + + + + + 执行SQL返回对象集合,Query<User, Address>("select * from user where age > @age; select * from address", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + 可自定义解析表达式 @@ -4613,6 +4880,12 @@ 超时 + + + 获取资源 + + + 使用完毕后,归还资源 @@ -4688,6 +4961,12 @@ 资源对象 + + + 从对象池获取对象成功的时候触发,通过该方法统计或初始化对象 + + 资源对象 + 归还对象给对象池的时候触发 @@ -5618,6 +5897,28 @@ 请使用 fsql.InsertDict(dict) 方法插入字典数据 + + + 动态构建Class Type + + + + + + 根据字典,创建 table 对应的实体对象 + + + + + + + + 根据实体对象,创建 table 对应的字典 + + + + + C#: that >= between && that <= and diff --git a/FreeSql/Interface/Curd/IUpdate.cs b/FreeSql/Interface/Curd/IUpdate.cs index 5cf46f3a..b6a1a0c1 100644 --- a/FreeSql/Interface/Curd/IUpdate.cs +++ b/FreeSql/Interface/Curd/IUpdate.cs @@ -109,7 +109,7 @@ namespace FreeSql /// 实体 /// 属性值忽略判断, true忽略 /// - IUpdate SetSourceIgnore(T1 source, Func ignore); + IUpdate SetSourceIgnore(T1 source, Func ignore = null); /// /// 忽略的列,IgnoreColumns(a => a.Name) | IgnoreColumns(a => new{a.Name,a.Time}) | IgnoreColumns(a => new[]{"name","time"}) @@ -186,23 +186,35 @@ namespace FreeSql /// IUpdate SetRaw(string sql, object parms = null); - /// - /// 设置更新的列 - /// SetDto(new { title = "xxx", clicks = 2 }) - /// SetDto(new Dictionary<string, object> { ["title"] = "xxx", ["clicks"] = 2 }) - /// 注意:标记 [Column(CanUpdate = false)] 的属性不会被更新 - /// - /// dto 或 Dictionary<string, object> - /// - IUpdate SetDto(object dto); + /// + /// 设置更新的列 + /// SetDto(new { title = "xxx", clicks = 2 }) + /// SetDto(new Dictionary<string, object> { ["title"] = "xxx", ["clicks"] = 2 }) + /// 注意:标记 [Column(CanUpdate = false)] 的属性不会被更新 + /// 注意:SetDto 与 IUpdate.IgnoreColumns/UpdateColumns 不能同时使用 + /// + /// dto 或 Dictionary<string, object> + /// + IUpdate SetDto(object dto); + /// + /// 设置更新的列 + /// SetDto(new { title = "xxx", clicks = 2 }) + /// SetDto(new Dictionary<string, object> { ["title"] = "xxx", ["clicks"] = 2 }) + /// 注意:标记 [Column(CanUpdate = false)] 的属性不会被更新 + /// 注意:SetDto 与 IUpdate.IgnoreColumns/UpdateColumns 不能同时使用 + /// + /// dto 或 Dictionary<string, object> + /// 属性值忽略判断, true忽略 + /// + IUpdate SetDtoIgnore(object dto, Func ignore = null); - /// - /// lambda表达式条件,仅支持实体基础成员(不包含导航对象) - /// 若想使用导航对象,请使用 ISelect.ToUpdate() 方法 - /// - /// lambda表达式条件 - /// - IUpdate Where(Expression> exp); + /// + /// lambda表达式条件,仅支持实体基础成员(不包含导航对象) + /// 若想使用导航对象,请使用 ISelect.ToUpdate() 方法 + /// + /// lambda表达式条件 + /// + IUpdate Where(Expression> exp); /// /// lambda表达式条件,仅支持实体基础成员(不包含导航对象) /// 若想使用导航对象,请使用 ISelect.ToUpdate() 方法 diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index 2b1551db..dc43fde1 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -633,9 +633,9 @@ namespace FreeSql.Internal.CommonProvider _versionColumn = _ignoreVersion ? null : _table?.VersionColumn; return this; } - public IUpdate SetSourceIgnore(T1 source, Func ignore) + public IUpdate SetSourceIgnore(T1 source, Func ignore = null) { - if (ignore == null) throw new ArgumentNullException(nameof(ignore)); + if (ignore == null) ignore = val => val == null; var columns = _table.Columns.Values .Where(col => ignore(_orm.GetEntityValueWithPropertyName(_table.Type, source, col.CsName))) .Select(col => col.Attribute.Name).ToArray(); @@ -754,14 +754,17 @@ namespace FreeSql.Internal.CommonProvider return this; } - public IUpdate SetDto(object dto) - { + public IUpdate SetDto(object dto) => SetDtoIgnore(dto, val => false); + public IUpdate SetDtoIgnore(object dto, Func ignore = null) + { if (dto == null) return this; + if (ignore == null) ignore = val => val == null; if (dto is Dictionary) { var dic = dto as Dictionary; foreach (var kv in dic) { + if (ignore(kv.Value)) continue; if (_table.ColumnsByCs.TryGetValue(kv.Key, out var trycol) == false) continue; if (_ignore.ContainsKey(trycol.Attribute.Name)) continue; SetPriv(trycol, kv.Value); @@ -771,41 +774,11 @@ namespace FreeSql.Internal.CommonProvider var dtoProps = dto.GetType().GetProperties(); foreach (var dtoProp in dtoProps) { - if (_table.ColumnsByCs.TryGetValue(dtoProp.Name, out var trycol) == false) continue; + var val = dtoProp.GetValue(dto, null); + if (ignore(val)) continue; + if (_table.ColumnsByCs.TryGetValue(dtoProp.Name, out var trycol) == false) continue; if (_ignore.ContainsKey(trycol.Attribute.Name)) continue; - SetPriv(trycol, dtoProp.GetValue(dto, null)); - } - return this; - } - public IUpdate SetDtoIgnoreNull(object dto) - { - if (dto == null) return this; - if (dto is Dictionary) - { - var dic = dto as Dictionary; - foreach (var kv in dic) - { - if (kv.Value == null) - { - continue; - } - if (_table.ColumnsByCs.TryGetValue(kv.Key, out var trycol) == false) continue; - if (_ignore.ContainsKey(trycol.Attribute.Name)) continue; - SetPriv(trycol, kv.Value); - } - return this; - } - var dtoProps = dto.GetType().GetProperties(); - foreach (var dtoProp in dtoProps) - { - var v3 = dtoProp.GetValue(dto, null); - if (v3 == null) - { - continue; - } - if (_table.ColumnsByCs.TryGetValue(dtoProp.Name, out var trycol) == false) continue; - if (_ignore.ContainsKey(trycol.Attribute.Name)) continue; - SetPriv(trycol, v3); + SetPriv(trycol, val); } return this; }