Browse Source

Delete nav

pull/20/head
sunkaixuan 2 years ago
parent
commit
512e3ef5b4
  1. 82
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToMany.cs
  2. 69
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs
  3. 31
      Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs
  4. 5
      Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs

82
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToMany.cs

@ -4,16 +4,92 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace SqlSugar namespace SqlSugar
{ {
public partial class DeleteNavProvider<Root, T> where T : class, new() where Root : class, new() public partial class DeleteNavProvider<Root, T> where T : class, new() where Root : class, new()
{ {
private void DeleteOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new() private void DeleteOneToMany<TChild>(string name, EntityColumnInfo nav) where TChild : class, new()
{ {
throw new NotImplementedException(); var parentEntity = _ParentEntity;
var prentList = _ParentList.Cast<T>().ToList();
var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name);
var thisEntity = this._Context.EntityMaintenance.GetEntityInfo<TChild>();
var thisPkColumn = GetPkColumnByNav(thisEntity, nav);
var thisFkColumn = GetFKColumnByNav(thisEntity, nav);
EntityColumnInfo parentPkColumn = GetParentPkColumn();
EntityColumnInfo parentNavColumn = GetParentPkNavColumn(nav);
if (parentNavColumn != null)
{
parentPkColumn = parentNavColumn;
}
if (!_IsDeletedParant)
SetContext(() => this._Context.Deleteable(prentList).ExecuteCommand());
var ids = _ParentList.Select(it => parentPkColumn.PropertyInfo.GetValue(it)).ToList();
var childList = this._Context.Queryable<TChild>().In(thisFkColumn.DbColumnName, ids).ToList();
this._ParentList = childList.Cast<object>().ToList();
this._ParentPkColumn = thisPkColumn;
this._IsDeletedParant = true;
SetContext(() => this._Context.Deleteable(childList).ExecuteCommand());
}
private void SetContext(Action action)
{
var key = "_DeleteNavTask";
if (this._Context.TempItems == null)
{
this._Context.TempItems = new Dictionary<string, object>();
}
if (!this._Context.TempItems.ContainsKey(key))
{
this._Context.TempItems.Add(key, null);
}
var oldTask = this._Context.TempItems[key];
var newTask = new List<Action>();
if (oldTask != null)
{
newTask = (List<Action>)oldTask;
}
newTask.Add(action);
this._Context.TempItems[key] = newTask;
}
private EntityColumnInfo GetParentPkColumn()
{
EntityColumnInfo parentPkColumn = _ParentPkColumn;
if (_ParentPkColumn == null)
{
parentPkColumn = _ParentPkColumn = this._ParentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey);
}
return parentPkColumn;
}
private EntityColumnInfo GetParentPkNavColumn(EntityColumnInfo nav)
{
EntityColumnInfo result = null;
if (nav.Navigat.Name2.HasValue())
{
result = _ParentPkColumn = this._ParentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name2);
}
return result;
} }
private EntityColumnInfo GetPkColumnByNav(EntityInfo entity, EntityColumnInfo nav)
{
var pkColumn = entity.Columns.FirstOrDefault(it => it.IsPrimarykey == true);
if (nav.Navigat.Name2.HasValue())
{
pkColumn = entity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name2);
}
return pkColumn;
}
private EntityColumnInfo GetFKColumnByNav(EntityInfo entity, EntityColumnInfo nav)
{
var fkColumn = entity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
return fkColumn;
}
} }
} }

69
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs

@ -15,10 +15,12 @@ namespace SqlSugar
public EntityInfo _ParentEntity { get; set; } public EntityInfo _ParentEntity { get; set; }
public EntityColumnInfo _ParentPkColumn { get; set; } public EntityColumnInfo _ParentPkColumn { get; set; }
public SqlSugarProvider _Context { get; set; } public SqlSugarProvider _Context { get; set; }
public bool _IsDeletedParant { get; set; }
public DeleteNavTask<Root, TChild> ThenInclude< TChild>(Expression<Func<Root, TChild>> expression) public DeleteNavProvider<Root, TChild> ThenInclude< TChild>(Expression<Func<T, TChild>> expression)
where TChild : class, new() where TChild : class, new()
{ {
InitParentList();
var name = ExpressionTool.GetMemberName(expression); var name = ExpressionTool.GetMemberName(expression);
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
if (nav.Navigat == null) if (nav.Navigat == null)
@ -37,13 +39,72 @@ namespace SqlSugar
{ {
DeleteManyToMany<TChild>(name, nav); DeleteManyToMany<TChild>(name, nav);
} }
return null; return GetResult<TChild>();
} }
public DeleteNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<Root, List<TChild>>> expression) public DeleteNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression)
where TChild : class, new() where TChild : class, new()
{ {
throw new NotImplementedException(); InitParentList();
var name = ExpressionTool.GetMemberName(expression);
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
if (nav.Navigat == null)
{
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
}
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
{
DeleteOneToOne<TChild>(name, nav);
}
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
{
DeleteOneToMany<TChild>(name, nav);
}
else
{
DeleteManyToMany<TChild>(name, nav);
}
return GetResult<TChild>();
}
private DeleteNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
{
return new DeleteNavProvider<Root, TChild>()
{
_Context = this._Context,
_ParentEntity = this._ParentEntity,
_ParentList = this._ParentList,
_Roots = this._Roots,
_ParentPkColumn = this._ParentPkColumn,
_RootList = this._RootList,
_IsDeletedParant=this._IsDeletedParant
};
}
public DeleteNavProvider<Root, Root> AsNav()
{
return new DeleteNavProvider<Root, Root>
{
_Context = _Context,
_ParentEntity = null,
_ParentList = null,
_Roots = _Roots,
_IsDeletedParant = this._IsDeletedParant,
_ParentPkColumn = this._Context.EntityMaintenance.GetEntityInfo<Root>().Columns.First(it => it.IsPrimarykey)
};
}
private void InitParentList()
{
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<T>();
if (_RootList == null)
{
_RootList = _ParentList = _Roots.Cast<object>().ToList();
}
else if (_ParentList == null)
{
_ParentList = _RootList;
var pkColumn = this._Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it => it.IsPrimarykey);
this._ParentPkColumn = pkColumn;
}
} }
} }
} }

31
Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs

@ -9,7 +9,7 @@ namespace SqlSugar
{ {
public class DeleteNavTaskInit<Root,T> where T : class, new() where Root : class, new() public class DeleteNavTaskInit<Root,T> where T : class, new() where Root : class, new()
{ {
public List<T> Roots { get; set; } internal List<T> Roots { get; set; }
internal SqlSugarProvider Context { get; set; } internal SqlSugarProvider Context { get; set; }
internal DeleteNavProvider<Root, Root> deleteNavProvider { get; set; } internal DeleteNavProvider<Root, Root> deleteNavProvider { get; set; }
@ -17,7 +17,7 @@ namespace SqlSugar
{ {
this.Context = deleteNavProvider._Context; this.Context = deleteNavProvider._Context;
DeleteNavTask<Root, TChild> result = new DeleteNavTask<Root, TChild>(); DeleteNavTask<Root, TChild> result = new DeleteNavTask<Root, TChild>();
Func<DeleteNavTask<Root, TChild>> func = () => deleteNavProvider.ThenInclude(expression); Func<DeleteNavProvider<Root, TChild>> func = () => deleteNavProvider.ThenInclude(expression);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
return result; return result;
@ -26,7 +26,7 @@ namespace SqlSugar
{ {
this.Context = deleteNavProvider._Context; this.Context = deleteNavProvider._Context;
DeleteNavTask<Root, TChild> result = new DeleteNavTask<Root, TChild>(); DeleteNavTask<Root, TChild> result = new DeleteNavTask<Root, TChild>();
Func<DeleteNavTask<Root, TChild>> func = () => deleteNavProvider.ThenInclude(expression); Func<DeleteNavProvider<Root, TChild>> func = () => deleteNavProvider.ThenInclude(expression);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
return result; return result;
@ -35,11 +35,11 @@ namespace SqlSugar
public class DeleteNavTask<Root, T> where T : class, new() where Root : class, new() public class DeleteNavTask<Root, T> where T : class, new() where Root : class, new()
{ {
public SqlSugarProvider Context { get; set; } public SqlSugarProvider Context { get; set; }
public Func<DeleteNavTask<Root, T>> PreFunc { get; set; } public Func<DeleteNavProvider<Root, T>> PreFunc { get; set; }
public DeleteNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new() public DeleteNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
{ {
DeleteNavTask<Root, TChild> result = new DeleteNavTask<Root, TChild>(); DeleteNavTask<Root, TChild> result = new DeleteNavTask<Root, TChild>();
Func<DeleteNavTask<Root, TChild>> func = () => PreFunc().ThenInclude(expression); Func<DeleteNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
return result; return result;
@ -47,7 +47,7 @@ namespace SqlSugar
public DeleteNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new() public DeleteNavTask<Root, TChild> ThenInclude<TChild>(Expression<Func<T, List<TChild>>> expression) where TChild : class, new()
{ {
DeleteNavTask<Root, TChild> result = new DeleteNavTask<Root, TChild>(); DeleteNavTask<Root, TChild> result = new DeleteNavTask<Root, TChild>();
Func<DeleteNavTask<Root, TChild>> func = () => PreFunc().ThenInclude(expression); Func<DeleteNavProvider<Root, TChild>> func = () => PreFunc().ThenInclude(expression);
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
return result; return result;
@ -62,16 +62,19 @@ namespace SqlSugar
} }
public bool ExecuteCommand() public bool ExecuteCommand()
{ {
PreFunc();
var hasTran = this.Context.Ado.Transaction != null; var hasTran = this.Context.Ado.Transaction != null;
if (hasTran) if (hasTran)
{ {
PreFunc(); ExecTasks();
} }
else else
{ {
this.Context.Ado.UseTran(() => this.Context.Ado.UseTran(() =>
{ {
PreFunc(); ExecTasks();
}, ex => throw ex); }, ex => throw ex);
} }
return true; return true;
@ -89,10 +92,20 @@ namespace SqlSugar
private DeleteNavTask<Root, Root> AsNav() private DeleteNavTask<Root, Root> AsNav()
{ {
DeleteNavTask<Root, Root> result = new DeleteNavTask<Root, Root>(); DeleteNavTask<Root, Root> result = new DeleteNavTask<Root, Root>();
Func<DeleteNavTask<Root, Root>> func = () => PreFunc().AsNav(); Func<DeleteNavProvider<Root, Root>> func = () => PreFunc().AsNav();
result.PreFunc = func; result.PreFunc = func;
result.Context = this.Context; result.Context = this.Context;
return result; return result;
} }
private void ExecTasks()
{
var tasks=(List<Action>)this.Context.TempItems["_DeleteNavTask"];
tasks.Reverse();
foreach (var task in tasks)
{
task();
}
this.Context.TempItems.Remove("_DeleteNavTask");
}
} }
} }

5
Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs

@ -910,8 +910,9 @@ namespace SqlSugar
public DeleteNavTaskInit<T, T> DeleteNav<T>(List<T> datas) where T : class, new() public DeleteNavTaskInit<T, T> DeleteNav<T>(List<T> datas) where T : class, new()
{ {
var result = new DeleteNavTaskInit<T, T>(); var result = new DeleteNavTaskInit<T, T>();
result.Roots = datas; result.deleteNavProvider = new DeleteNavProvider<T, T>();
result.Context = this; result.deleteNavProvider._Roots = datas;
result.deleteNavProvider._Context = this;
return result; return result;
} }
public DeleteNavTaskInit<T, T> DeleteNav<T>(Expression<Func<T, bool>> whereExpression) where T : class, new() public DeleteNavTaskInit<T, T> DeleteNav<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()

Loading…
Cancel
Save