Browse Source

Optimized update single row

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
7b015209c0
  1. 7
      Src/Asp.Net/SqlServerTest/Demos/2_Update.cs
  2. 16
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs
  3. 1
      Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs

7
Src/Asp.Net/SqlServerTest/Demos/2_Update.cs

@ -24,7 +24,7 @@ namespace OrmTest.Demo
//Only update Name
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
@ -60,8 +60,11 @@ namespace OrmTest.Demo
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();
//update one columns
var count= db.Updateable<Student>().UpdateColumns(it => it.SchoolId == 1).Where(it => it.Id == 1).ExecuteCommand();
}
}
}

16
Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs

@ -52,7 +52,7 @@ namespace SqlSugar
Task<bool> result = new Task<bool>(() =>
{
IUpdateable<T> asyncUpdateable = CopyUpdateable();
return asyncUpdateable.ExecuteCommand()>0;
return asyncUpdateable.ExecuteCommand() > 0;
});
result.Start();
return result;
@ -125,7 +125,19 @@ namespace SqlSugar
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;
}

1
Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs

@ -25,6 +25,7 @@ namespace SqlSugar
/// <returns></returns>
IUpdateable<T> WhereColumns(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(Expression<Func<T, T>> columns);
IUpdateable<T> IgnoreColumns(Expression<Func<T, object>> columns);

Loading…
Cancel
Save