Skip to content

Commit d1bd317

Browse files
2881028810
authored andcommitted
- 优化 IUpdate.Set 支持 Set(a => new { Clicks = a.Clicks + 1, Time = DateTime.Now }) 指定多个字段更新的用法;
1 parent 6feb340 commit d1bd317

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

FreeSql.Tests/UnitTest1.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ public static string FormatDateTime() {
269269
[Fact]
270270
public void Test1() {
271271

272+
var tbid = g.sqlite.Select<TaskBuild>().First().Id;
273+
var tbidsql = g.sqlite.Update<TaskBuild>().Where(a => a.Id == tbid)
274+
.Set(a => new TaskBuild {
275+
FileName = "111",
276+
TaskName = a.TaskName + "333"
277+
}).ToSql();
278+
279+
272280
var dkdkdkd = g.oracle.Select<Templates>().ToList();
273281

274282

FreeSql/FreeSql.xml

Lines changed: 3 additions & 1 deletion
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ public interface IUpdate<T1> where T1 : class {
7474
IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value);
7575
/// <summary>
7676
/// 设置列的的新值为基础上增加,格式:Set(a => a.Clicks + 1) 相当于 clicks=clicks+1
77+
/// <para></para>
78+
/// 指定更新,格式:Set(a => new { Clicks = a.Clicks + 1, Time = DateTime.Now }) 相当于 set clicks=clicks+1,time='2019-06-19....'
7779
/// </summary>
7880
/// <typeparam name="TMember"></typeparam>
79-
/// <param name="binaryExpression"></param>
81+
/// <param name="exp"></param>
8082
/// <returns></returns>
81-
IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> binaryExpression);
83+
IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> exp);
8284
/// <summary>
8385
/// 设置值,自定义SQL语法,SetRaw("title = ?title", new { title = "newtitle" })
8486
/// </summary>

FreeSql/Internal/CommonProvider/UpdateProvider.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Data.Common;
77
using System.Linq;
88
using System.Linq.Expressions;
9+
using System.Reflection;
910
using System.Text;
1011
using System.Threading.Tasks;
1112

@@ -340,15 +341,33 @@ public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember va
340341
//foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value);
341342
return this;
342343
}
343-
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> binaryExpression) {
344-
if (binaryExpression?.Body.NodeType == ExpressionType.Equal) {
345-
_set.Append(", ").Append(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, binaryExpression, null));
344+
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> exp) {
345+
var body = exp?.Body;
346+
var nodeType = body?.NodeType;
347+
if (nodeType == ExpressionType.Equal) {
348+
_set.Append(", ").Append(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp, null));
346349
return this;
347350
}
348-
if (binaryExpression?.Body is BinaryExpression == false &&
349-
binaryExpression?.Body.NodeType != ExpressionType.Call) return this;
351+
352+
if (nodeType == ExpressionType.MemberInit) {
353+
var initExp = body as MemberInitExpression;
354+
if (initExp.Bindings?.Count > 0) {
355+
for (var a = 0; a < initExp.Bindings.Count; a++) {
356+
var initAssignExp = (initExp.Bindings[a] as MemberAssignment);
357+
if (initAssignExp == null) continue;
358+
var memberName = initExp.Bindings[a].Member.Name;
359+
if (_table.ColumnsByCsIgnore.ContainsKey(memberName)) continue;
360+
if (_table.ColumnsByCs.TryGetValue(memberName, out var col) == false) throw new Exception($"找不到属性:{memberName}");
361+
var memberValue = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, initAssignExp.Expression, null);
362+
_setIncr.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(memberValue);
363+
}
364+
}
365+
return this;
366+
}
367+
if (body is BinaryExpression == false &&
368+
nodeType != ExpressionType.Call) return this;
350369
var cols = new List<SelectColumnInfo>();
351-
var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, cols, binaryExpression, null);
370+
var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, cols, exp, null);
352371
if (cols.Any() == false) return this;
353372
foreach (var col in cols) {
354373
if (col.Column.Attribute.IsNullable == true && col.Column.Attribute.MapType.IsNullableType()) {

0 commit comments

Comments
 (0)