Browse Source

Add Updateable(data).SetColumns(Expression<Func<T, T>> columns, bool appendColumnsByDataFilter)

SqlSugar5
sunkaixuan 2 years ago
parent
commit
5d9e9fca88
  1. 44
      Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs
  2. 1
      Src/Asp.NetCore2/SqlSugar/Interface/IUpdateable.cs
  3. 2
      Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec

44
Src/Asp.NetCore2/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.NetCore2/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);

2
Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec

@ -2,7 +2,7 @@
<package >
<metadata>
<id>SqlSugarCore</id>
<version>5.1.3.2-preview06</version>
<version>5.1.3.2-preview07</version>
<authors>sunkaixuan</authors>
<owners>果糖大数据科技</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>

Loading…
Cancel
Save