mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Support logic delete
This commit is contained in:
parent
338ce461f3
commit
e65b2e014a
@ -41,8 +41,19 @@ namespace OrmTest
|
|||||||
//by expression
|
//by expression
|
||||||
db.Deleteable<Order>().Where(it => it.Id == 11111).ExecuteCommand();
|
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 ####");
|
Console.WriteLine("#### Deleteable End ####");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public class LogicTest
|
||||||
|
{
|
||||||
|
[SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public bool isdeleted { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ namespace SqlSugar
|
|||||||
public bool IsEnableDiffLogEvent { get; set; }
|
public bool IsEnableDiffLogEvent { get; set; }
|
||||||
public DiffLogModel diffModel { get; set; }
|
public DiffLogModel diffModel { get; set; }
|
||||||
public List<string> tempPrimaryKeys { get; set; }
|
public List<string> tempPrimaryKeys { get; set; }
|
||||||
private Action RemoveCacheFunc { get; set; }
|
internal Action RemoveCacheFunc { get; set; }
|
||||||
public EntityInfo EntityInfo
|
public EntityInfo EntityInfo
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -272,6 +272,13 @@ namespace SqlSugar
|
|||||||
result.deleteobj = this;
|
result.deleteobj = this;
|
||||||
return result;
|
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)
|
public IDeleteable<T> RemoveDataCache(string likeString)
|
||||||
{
|
{
|
||||||
this.RemoveCacheFunc = () =>
|
this.RemoveCacheFunc = () =>
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -32,7 +32,7 @@ namespace SqlSugar
|
|||||||
public bool IsAs { get; set; }
|
public bool IsAs { get; set; }
|
||||||
public bool IsEnableDiffLogEvent { get; set; }
|
public bool IsEnableDiffLogEvent { get; set; }
|
||||||
public DiffLogModel diffModel { get; set; }
|
public DiffLogModel diffModel { get; set; }
|
||||||
private Action RemoveCacheFunc { get; set; }
|
internal Action RemoveCacheFunc { get; set; }
|
||||||
private int SetColumnsIndex { get; set; }
|
private int SetColumnsIndex { get; set; }
|
||||||
private List<DbColumnInfo> columns { get; set; }
|
private List<DbColumnInfo> columns { get; set; }
|
||||||
#endregion
|
#endregion
|
||||||
@ -291,6 +291,19 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Update by expression
|
#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)
|
public IUpdateable<T> SetColumns(Expression<Func<T, T>> columns)
|
||||||
{
|
{
|
||||||
ThrowUpdateByObject();
|
ThrowUpdateByObject();
|
||||||
|
@ -37,6 +37,7 @@ namespace SqlSugar
|
|||||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||||
IDeleteable<T> EnableQueryFilter();
|
IDeleteable<T> EnableQueryFilter();
|
||||||
SplitTableDeleteProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc);
|
SplitTableDeleteProvider<T> SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc);
|
||||||
|
LogicDeleteProvider<T> IsLogic();
|
||||||
void AddQueue();
|
void AddQueue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ namespace SqlSugar
|
|||||||
/// <param name="columns"></param>
|
/// <param name="columns"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IUpdateable<T> SetColumns(Expression<Func<T, T>> columns);
|
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);
|
IUpdateable<T> UpdateColumnsIF(bool isUpdateColumns,Expression<Func<T, object>> columns);
|
||||||
|
@ -84,6 +84,7 @@
|
|||||||
<Compile Include="Abstract\DbMaintenanceProvider\Methods.cs" />
|
<Compile Include="Abstract\DbMaintenanceProvider\Methods.cs" />
|
||||||
<Compile Include="Abstract\DbMaintenanceProvider\Properties.cs" />
|
<Compile Include="Abstract\DbMaintenanceProvider\Properties.cs" />
|
||||||
<Compile Include="Abstract\AdoProvider\AdoProvider.cs" />
|
<Compile Include="Abstract\AdoProvider\AdoProvider.cs" />
|
||||||
|
<Compile Include="Abstract\DeleteProvider\LogicDeleteProvider.cs" />
|
||||||
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
|
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
|
||||||
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
|
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
|
||||||
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />
|
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user