mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-08 10:24:55 +08:00
Optimized update single row
This commit is contained in:
@@ -24,7 +24,7 @@ namespace OrmTest.Demo
|
|||||||
|
|
||||||
//Only update Name
|
//Only update Name
|
||||||
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();
|
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();
|
||||||
var t3_1 = db.Updateable(updateObj).UpdateColumns(it => it=="Name").ExecuteCommand();
|
var t3_1 = db.Updateable(updateObj).UpdateColumns(it => it == "Name").ExecuteCommand();
|
||||||
|
|
||||||
|
|
||||||
//Ignore Name and TestId
|
//Ignore Name and TestId
|
||||||
@@ -60,8 +60,11 @@ namespace OrmTest.Demo
|
|||||||
db.Updateable(updateObj).Where(true).ExecuteCommand();
|
db.Updateable(updateObj).Where(true).ExecuteCommand();
|
||||||
|
|
||||||
|
|
||||||
var t12= db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommandAsync();
|
var t12 = db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommandAsync();
|
||||||
t12.Wait();
|
t12.Wait();
|
||||||
|
|
||||||
|
//update one columns
|
||||||
|
var count= db.Updateable<Student>().UpdateColumns(it => it.SchoolId == 1).Where(it => it.Id == 1).ExecuteCommand();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ namespace SqlSugar
|
|||||||
Task<bool> result = new Task<bool>(() =>
|
Task<bool> result = new Task<bool>(() =>
|
||||||
{
|
{
|
||||||
IUpdateable<T> asyncUpdateable = CopyUpdateable();
|
IUpdateable<T> asyncUpdateable = CopyUpdateable();
|
||||||
return asyncUpdateable.ExecuteCommand()>0;
|
return asyncUpdateable.ExecuteCommand() > 0;
|
||||||
});
|
});
|
||||||
result.Start();
|
result.Start();
|
||||||
return result;
|
return result;
|
||||||
@@ -125,7 +125,19 @@ namespace SqlSugar
|
|||||||
item.IsPrimarykey = true;
|
item.IsPrimarykey = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => updateColumns.Any(uc=>uc.Equals(it.PropertyName,StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey || it.IsIdentity).ToList();
|
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => updateColumns.Any(uc => uc.Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey || it.IsIdentity).ToList();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUpdateable<T> UpdateColumns(Expression<Func<T, bool>> columns) {
|
||||||
|
var binaryExp = columns.Body as BinaryExpression;
|
||||||
|
Check.Exception(!binaryExp.NodeType.IsIn(ExpressionType.Equal), "No support {0}", columns.ToString());
|
||||||
|
Check.Exception(!(binaryExp.Left is MemberExpression), "No support {0}", columns.ToString());
|
||||||
|
Check.Exception(ExpressionTool.IsConstExpression(binaryExp.Left as MemberExpression), "No support {0}", columns.ToString());
|
||||||
|
var expResult = UpdateBuilder.GetExpressionValue(columns, ResolveExpressType.WhereSingle).GetResultString().Trim().TrimStart('(').TrimEnd(')');
|
||||||
|
string key = SqlBuilder.GetNoTranslationColumnName(expResult);
|
||||||
|
UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(SqlBuilder.GetTranslationColumnName(key), expResult));
|
||||||
|
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ namespace SqlSugar
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IUpdateable<T> WhereColumns(Expression<Func<T, object>> columns);
|
IUpdateable<T> WhereColumns(Expression<Func<T, object>> columns);
|
||||||
IUpdateable<T> UpdateColumns(Expression<Func<T, object>> columns);
|
IUpdateable<T> UpdateColumns(Expression<Func<T, object>> columns);
|
||||||
|
IUpdateable<T> UpdateColumns(Expression<Func<T, bool>> columns);
|
||||||
IUpdateable<T> UpdateColumns(Func<string, bool> updateColumMethod);
|
IUpdateable<T> UpdateColumns(Func<string, bool> updateColumMethod);
|
||||||
IUpdateable<T> UpdateColumns(Expression<Func<T, T>> columns);
|
IUpdateable<T> UpdateColumns(Expression<Func<T, T>> columns);
|
||||||
IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns);
|
IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns);
|
||||||
|
|||||||
Reference in New Issue
Block a user