- 优化 IUpdate.Set 支持 Set(a => new { Clicks = a.Clicks + 1, Time = DateTime.Now }) 指定多个字段更新的用法;

This commit is contained in:
28810 2019-06-19 12:07:06 +08:00
parent 6feb340b85
commit d1bd3170a5
4 changed files with 40 additions and 9 deletions

View File

@ -269,6 +269,14 @@ namespace FreeSql.Tests {
[Fact] [Fact]
public void Test1() { public void Test1() {
var tbid = g.sqlite.Select<TaskBuild>().First().Id;
var tbidsql = g.sqlite.Update<TaskBuild>().Where(a => a.Id == tbid)
.Set(a => new TaskBuild {
FileName = "111",
TaskName = a.TaskName + "333"
}).ToSql();
var dkdkdkd = g.oracle.Select<Templates>().ToList(); var dkdkdkd = g.oracle.Select<Templates>().ToList();

View File

@ -1534,9 +1534,11 @@
<member name="M:FreeSql.IUpdate`1.Set``1(System.Linq.Expressions.Expression{System.Func{`0,``0}})"> <member name="M:FreeSql.IUpdate`1.Set``1(System.Linq.Expressions.Expression{System.Func{`0,``0}})">
<summary> <summary>
设置列的的新值为基础上增加格式Set(a => a.Clicks + 1) 相当于 clicks=clicks+1 设置列的的新值为基础上增加格式Set(a => a.Clicks + 1) 相当于 clicks=clicks+1
<para></para>
指定更新格式Set(a => new { Clicks = a.Clicks + 1, Time = DateTime.Now }) 相当于 set clicks=clicks+1,time='2019-06-19....'
</summary> </summary>
<typeparam name="TMember"></typeparam> <typeparam name="TMember"></typeparam>
<param name="binaryExpression"></param> <param name="exp"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.IUpdate`1.SetRaw(System.String,System.Object)"> <member name="M:FreeSql.IUpdate`1.SetRaw(System.String,System.Object)">

View File

@ -74,11 +74,13 @@ namespace FreeSql {
IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value); IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value);
/// <summary> /// <summary>
/// 设置列的的新值为基础上增加格式Set(a => a.Clicks + 1) 相当于 clicks=clicks+1 /// 设置列的的新值为基础上增加格式Set(a => a.Clicks + 1) 相当于 clicks=clicks+1
/// <para></para>
/// 指定更新格式Set(a => new { Clicks = a.Clicks + 1, Time = DateTime.Now }) 相当于 set clicks=clicks+1,time='2019-06-19....'
/// </summary> /// </summary>
/// <typeparam name="TMember"></typeparam> /// <typeparam name="TMember"></typeparam>
/// <param name="binaryExpression"></param> /// <param name="exp"></param>
/// <returns></returns> /// <returns></returns>
IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> binaryExpression); IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> exp);
/// <summary> /// <summary>
/// 设置值自定义SQL语法SetRaw("title = ?title", new { title = "newtitle" }) /// 设置值自定义SQL语法SetRaw("title = ?title", new { title = "newtitle" })
/// </summary> /// </summary>

View File

@ -6,6 +6,7 @@ using System.Data;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -340,15 +341,33 @@ namespace FreeSql.Internal.CommonProvider {
//foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value); //foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value);
return this; return this;
} }
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> binaryExpression) { public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> exp) {
if (binaryExpression?.Body.NodeType == ExpressionType.Equal) { var body = exp?.Body;
_set.Append(", ").Append(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, binaryExpression, null)); var nodeType = body?.NodeType;
if (nodeType == ExpressionType.Equal) {
_set.Append(", ").Append(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp, null));
return this; return this;
} }
if (binaryExpression?.Body is BinaryExpression == false &&
binaryExpression?.Body.NodeType != ExpressionType.Call) return this; if (nodeType == ExpressionType.MemberInit) {
var initExp = body as MemberInitExpression;
if (initExp.Bindings?.Count > 0) {
for (var a = 0; a < initExp.Bindings.Count; a++) {
var initAssignExp = (initExp.Bindings[a] as MemberAssignment);
if (initAssignExp == null) continue;
var memberName = initExp.Bindings[a].Member.Name;
if (_table.ColumnsByCsIgnore.ContainsKey(memberName)) continue;
if (_table.ColumnsByCs.TryGetValue(memberName, out var col) == false) throw new Exception($"找不到属性:{memberName}");
var memberValue = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, initAssignExp.Expression, null);
_setIncr.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(memberValue);
}
}
return this;
}
if (body is BinaryExpression == false &&
nodeType != ExpressionType.Call) return this;
var cols = new List<SelectColumnInfo>(); var cols = new List<SelectColumnInfo>();
var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, cols, binaryExpression, null); var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, cols, exp, null);
if (cols.Any() == false) return this; if (cols.Any() == false) return this;
foreach (var col in cols) { foreach (var col in cols) {
if (col.Column.Attribute.IsNullable == true && col.Column.Attribute.MapType.IsNullableType()) { if (col.Column.Attribute.IsNullable == true && col.Column.Attribute.MapType.IsNullableType()) {