Skip to content

Commit 5cff594

Browse files
2881028810
authored andcommitted
- 增加 IUpdate.SetDto 根据 dto 更新的方法;#218
1 parent f22f65f commit 5cff594

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/FreeSql.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Interface/Curd/IUpdate.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public interface IUpdate<T1> where T1 : class
5656
/// <param name="source">实体集合</param>
5757
/// <returns></returns>
5858
IUpdate<T1> SetSource(IEnumerable<T1> source);
59+
5960
/// <summary>
6061
/// 忽略的列,IgnoreColumns(a => a.Name) | IgnoreColumns(a => new{a.Name,a.Time}) | IgnoreColumns(a => new[]{"name","time"})
6162
/// </summary>
@@ -107,6 +108,17 @@ public interface IUpdate<T1> where T1 : class
107108
/// <returns></returns>
108109
IUpdate<T1> SetRaw(string sql, object parms = null);
109110

111+
/// <summary>
112+
/// 设置更新的列
113+
/// <para></para>
114+
/// SetDto(new { title = "xxx", clicks = 2 })
115+
/// <para></para>
116+
/// SetDto(new Dictionary&lt;string, object&gt; { ["title"] = "xxx", ["clicks"] = 2 })
117+
/// </summary>
118+
/// <param name="dto">dto 或 Dictionary&lt;string, object&gt;</param>
119+
/// <returns></returns>
120+
IUpdate<T1> SetDto(object dto);
121+
110122
/// <summary>
111123
/// lambda表达式条件,仅支持实体基础成员(不包含导航对象)<para></para>
112124
/// 若想使用导航对象,请使用 ISelect.ToUpdate() 方法

FreeSql/Internal/CommonProvider/UpdateProvider.cs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -334,26 +334,31 @@ public IUpdate<T1> SetSource(IEnumerable<T1> source)
334334
return this;
335335
}
336336

337-
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value)
337+
protected void SetPriv(ColumnInfo col, object value)
338338
{
339-
var cols = new List<SelectColumnInfo>();
340-
_commonExpression.ExpressionSelectColumn_MemberAccess(null, cols, SelectTableInfoType.From, column?.Body, true, null);
341-
if (cols.Count != 1) return this;
342-
var col = cols.First();
343339
object paramVal = null;
344-
if (col.Column.Attribute.MapType == typeof(TMember)) paramVal = value;
345-
else paramVal = Utils.GetDataReaderValue(col.Column.Attribute.MapType, value);
346-
_set.Append(", ").Append(_commonUtils.QuoteSqlName(col.Column.Attribute.Name)).Append(" = ");
340+
if (value != null)
341+
{
342+
if (col.Attribute.MapType == value.GetType()) paramVal = value;
343+
else paramVal = Utils.GetDataReaderValue(col.Attribute.MapType, value);
344+
}
345+
_set.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
347346
if (_noneParameter)
348347
{
349-
_set.Append(_commonUtils.GetNoneParamaterSqlValue(_params, col.Column.Attribute.MapType, paramVal));
348+
_set.Append(_commonUtils.GetNoneParamaterSqlValue(_params, col.Attribute.MapType, paramVal));
350349
}
351350
else
352351
{
353-
_set.Append(_commonUtils.QuoteWriteParamter(col.Column.Attribute.MapType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}"));
354-
_commonUtils.AppendParamter(_params, null, col.Column, col.Column.Attribute.MapType, paramVal);
352+
_set.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}"));
353+
_commonUtils.AppendParamter(_params, null, col, col.Attribute.MapType, paramVal);
355354
}
356-
//foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value);
355+
}
356+
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value)
357+
{
358+
var cols = new List<SelectColumnInfo>();
359+
_commonExpression.ExpressionSelectColumn_MemberAccess(null, cols, SelectTableInfoType.From, column?.Body, true, null);
360+
if (cols.Count != 1) return this;
361+
SetPriv(cols.First().Column, value);
357362
return this;
358363
}
359364
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> exp)
@@ -424,6 +429,27 @@ public IUpdate<T1> SetRaw(string sql, object parms = null)
424429
return this;
425430
}
426431

432+
public IUpdate<T1> SetDto(object dto)
433+
{
434+
if (dto == null) return this;
435+
if (dto is Dictionary<string, object>)
436+
{
437+
var dic = dto as Dictionary<string, object>;
438+
foreach (var kv in dic)
439+
{
440+
if (_table.ColumnsByCs.TryGetValue(kv.Key, out var trycol) == false) continue;
441+
SetPriv(trycol, kv.Value);
442+
}
443+
}
444+
var dtoProps = dto.GetType().GetProperties();
445+
foreach (var dtoProp in dtoProps)
446+
{
447+
if (_table.ColumnsByCs.TryGetValue(dtoProp.Name, out var trycol) == false) continue;
448+
SetPriv(trycol, dtoProp.GetValue(dto, null));
449+
}
450+
return this;
451+
}
452+
427453
public IUpdate<T1> Where(Expression<Func<T1, bool>> expression) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, expression?.Body, null, _params));
428454
public IUpdate<T1> Where(string sql, object parms = null)
429455
{

0 commit comments

Comments
 (0)