Browse Source

Support logic delete

pull/10/head
sunkaixuna 3 years ago
parent
commit
e65b2e014a
  1. 11
      Src/Asp.Net/SqlServerTest/Demo/Demo4_Deleteable.cs
  2. 9
      Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs
  3. 73
      Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/LogicDeleteProvider.cs
  4. 15
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs
  5. 1
      Src/Asp.Net/SqlSugar/Interface/IDeleteable.cs
  6. 2
      Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs
  7. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

11
Src/Asp.Net/SqlServerTest/Demo/Demo4_Deleteable.cs

@ -41,8 +41,19 @@ namespace OrmTest
//by expression
db.Deleteable<Order>().Where(it => it.Id == 11111).ExecuteCommand();
//logic delete
db.CodeFirst.InitTables<LogicTest>();
;
db.Deleteable<LogicTest>().Where(it=>it.Id==1).IsLogic().ExecuteCommand();
Console.WriteLine("#### Deleteable End ####");
}
}
public class LogicTest
{
[SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
public int Id { get; set; }
public bool isdeleted { get; set; }
}
}

9
Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs

@ -21,7 +21,7 @@ namespace SqlSugar
public bool IsEnableDiffLogEvent { get; set; }
public DiffLogModel diffModel { get; set; }
public List<string> tempPrimaryKeys { get; set; }
private Action RemoveCacheFunc { get; set; }
internal Action RemoveCacheFunc { get; set; }
public EntityInfo EntityInfo
{
get
@ -272,6 +272,13 @@ namespace SqlSugar
result.deleteobj = this;
return result;
}
public LogicDeleteProvider<T> IsLogic()
{
LogicDeleteProvider<T> result = new LogicDeleteProvider<T>();
result.DeleteBuilder = this.DeleteBuilder;
result.Deleteable = this;
return result;
}
public IDeleteable<T> RemoveDataCache(string likeString)
{
this.RemoveCacheFunc = () =>

73
Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/LogicDeleteProvider.cs

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class LogicDeleteProvider<T> where T : class, new()
{
public DeleteableProvider<T> Deleteable { get; set; }
public DeleteBuilder DeleteBuilder { get; set; }
public int ExecuteCommand(string LogicFieldName = null)
{
ISqlSugarClient db;
List<SugarParameter> pars;
string where;
LogicFieldName = _ExecuteCommand(LogicFieldName, out db, out where, out pars);
var updateable = db.Updateable<T>().SetColumns(LogicFieldName, "@IsDeleted");
if (pars != null)
updateable.UpdateBuilder.Parameters.AddRange(pars);
Convert(updateable as UpdateableProvider<T>);
var result = updateable.Where(where, new { IsDeleted = true }).ExecuteCommand();
return result;
}
public async Task<int> ExecuteCommandAsync(string LogicFieldName = null)
{
ISqlSugarClient db;
List<SugarParameter> pars;
string where;
LogicFieldName = _ExecuteCommand(LogicFieldName, out db, out where, out pars);
var updateable = db.Updateable<T>().SetColumns(LogicFieldName, "@IsDeleted");
if (pars != null)
updateable.UpdateBuilder.Parameters.AddRange(pars);
Convert(updateable as UpdateableProvider<T>);
var result =await updateable.Where(where, new { IsDeleted = true }).ExecuteCommandAsync();
return result;
}
private void Convert(UpdateableProvider<T> updateable)
{
updateable.IsEnableDiffLogEvent = Deleteable.IsEnableDiffLogEvent;
updateable.diffModel = Deleteable.diffModel;
updateable.UpdateBuilder.TableWithString = DeleteBuilder.TableWithString;
updateable.RemoveCacheFunc = Deleteable.RemoveCacheFunc;
}
private string _ExecuteCommand(string LogicFieldName, out ISqlSugarClient db, out string where, out List<SugarParameter> pars)
{
var entityInfo = Deleteable.EntityInfo;
db = Deleteable.Context;
where = DeleteBuilder.GetWhereString.Substring(5);
pars = DeleteBuilder.Parameters;
if (LogicFieldName.IsNullOrEmpty())
{
var column = entityInfo.Columns.FirstOrDefault(it =>
it.PropertyName.EqualCase("isdelete") ||
it.PropertyName.EqualCase("isdeleted") ||
it.DbColumnName.EqualCase("isdelete") ||
it.DbColumnName.EqualCase("isdeleted"));
if (column != null)
{
LogicFieldName = column.DbColumnName;
}
}
Check.Exception(LogicFieldName == null, ErrorMessage.GetThrowMessage(
$"{entityInfo.EntityName} is not isdelete or isdeleted"
, $"{entityInfo.EntityName} 没有IsDelete或者IsDeleted 的属性, 你也可以用 IsLogic().ExecuteCommand(\"\")"));
return LogicFieldName;
}
}
}

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

@ -32,7 +32,7 @@ namespace SqlSugar
public bool IsAs { get; set; }
public bool IsEnableDiffLogEvent { get; set; }
public DiffLogModel diffModel { get; set; }
private Action RemoveCacheFunc { get; set; }
internal Action RemoveCacheFunc { get; set; }
private int SetColumnsIndex { get; set; }
private List<DbColumnInfo> columns { get; set; }
#endregion
@ -291,6 +291,19 @@ namespace SqlSugar
#endregion
#region Update by expression
public IUpdateable<T> SetColumns(string fieldName, object fieldValue)
{
ThrowUpdateByObject();
var columnInfo = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName.EqualCase(fieldName));
if (columnInfo != null)
{
fieldName = columnInfo.DbColumnName;
}
UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(fieldName, fieldValue+""));
this.UpdateBuilder.DbColumnInfoList = this.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();
AppendSets();
return this;
}
public IUpdateable<T> SetColumns(Expression<Func<T, T>> columns)
{
ThrowUpdateByObject();

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

@ -37,6 +37,7 @@ namespace SqlSugar
KeyValuePair<string, List<SugarParameter>> ToSql();
IDeleteable<T> EnableQueryFilter();
SplitTableDeleteProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc);
LogicDeleteProvider<T> IsLogic();
void AddQueue();
}
}

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

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

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -84,6 +84,7 @@
<Compile Include="Abstract\DbMaintenanceProvider\Methods.cs" />
<Compile Include="Abstract\DbMaintenanceProvider\Properties.cs" />
<Compile Include="Abstract\AdoProvider\AdoProvider.cs" />
<Compile Include="Abstract\DeleteProvider\LogicDeleteProvider.cs" />
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />

Loading…
Cancel
Save