Browse Source

Synchronization code

SqlSugar5
sunkaixuan 2 years ago
parent
commit
27536ab701
  1. 44
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs
  2. 1
      Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs

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

@ -426,6 +426,50 @@ namespace SqlSugar
AppendSets();
return this;
}
public IUpdateable<T> SetColumns(Expression<Func<T, T>> columns, bool appendColumnsByDataFilter)
{
ThrowUpdateByObject();
var expResult = UpdateBuilder.GetExpressionValue(columns, ResolveExpressType.Update);
var resultArray = expResult.GetResultArray();
Check.ArgumentNullException(resultArray, "UpdateColumns Parameter error, UpdateColumns(it=>new T{ it.id=1}) is valid, UpdateColumns(it=>T) is error");
if (resultArray.HasValue())
{
foreach (var item in resultArray)
{
string key = SqlBuilder.GetNoTranslationColumnName(item);
var value = item;
if (value.Contains("= \"SYSDATE\""))
{
value = value.Replace("= \"SYSDATE\"", "= SYSDATE");
}
UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(SqlBuilder.GetTranslationColumnName(key), value));
}
}
if (appendColumnsByDataFilter)
{
var data = ((UpdateableProvider<T>)this.Context.Updateable(new T() { })).UpdateObjs.First();
foreach (var item in this.EntityInfo.Columns.Where(it => !it.IsPrimarykey && !it.IsIgnore && !it.IsOnlyIgnoreUpdate))
{
var value = item.PropertyInfo.GetValue(data);
if (value != null)
{
if (!value.Equals(UtilMethods.GetDefaultValue(item.UnderType)))
{
var pName = this.SqlBuilder.SqlParameterKeyWord + item.PropertyName + 1000;
var p = new SugarParameter(pName, value);
this.UpdateBuilder.Parameters.Add(p);
UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(SqlBuilder.GetTranslationColumnName(item.DbColumnName), SqlBuilder.GetTranslationColumnName(item.DbColumnName)+"="+pName));
}
}
}
}
this.UpdateBuilder.DbColumnInfoList = UpdateBuilder.DbColumnInfoList.Where(it => (UpdateParameterIsNull == false && IsPrimaryKey(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();
CheckTranscodeing(false);
AppendSets();
return this;
}
public IUpdateable<T> SetColumns(Expression<Func<T, bool>> columns)
{
ThrowUpdateByObject();

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

@ -68,6 +68,7 @@ namespace SqlSugar
/// <param name="columns"></param>
/// <returns></returns>
IUpdateable<T> SetColumns(Expression<Func<T, T>> columns);
IUpdateable<T> SetColumns(Expression<Func<T, T>> columns,bool appendColumnsByDataFilter);
IUpdateable<T> SetColumns(string fieldName,object fieldValue);
IUpdateable<T> SetColumns(Expression<Func<T,object>> filedNameExpression, object fieldValue);

Loading…
Cancel
Save