From 195a649eb7a48c93920bd9d0d8d3454eaa284215 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 9 Jul 2022 15:20:42 +0800 Subject: [PATCH] Update .net core project --- .../ExecuteNavProvider/DeleteNavManyToMany.cs | 52 +++++++++++++++++++ .../ExecuteNavProvider/DeleteNavOneToOne.cs | 22 +++++++- .../ExecuteNavProvider/DeleteNavProvider.cs | 1 + .../ExecuteNavProvider/DeleteNavTask.cs | 18 +++++++ .../SqlSugar/Entities/DeleteNavOptions.cs | 14 +++++ .../DbMethods/DefaultDbMethod.cs | 8 +++ .../ExpressionsToSql/DbMethods/IDbMethods.cs | 1 + .../ExpressionsToSql/DbMethods/SqlFunc.cs | 6 +++ .../MethodCallExpressionResolve.cs | 2 + 9 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 Src/Asp.NetCore2/SqlSugar/Entities/DeleteNavOptions.cs diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavManyToMany.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavManyToMany.cs index ad30f01ae..96b86ff56 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavManyToMany.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavManyToMany.cs @@ -10,8 +10,60 @@ namespace SqlSugar { private void DeleteManyToMany(string name, EntityColumnInfo nav) where TChild : class, new() { + var parentEntity = _ParentEntity; + var parentList = _ParentList.Cast().ToList(); + var parentPkColumn = parentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true); + var parentNavigateProperty = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == name); + var thisEntity = this._Context.EntityMaintenance.GetEntityInfo(); + var thisPkColumn = thisEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true); + Check.Exception(thisPkColumn == null, $"{thisPkColumn.EntityName} need primary key", $"{thisPkColumn.EntityName}需要主键"); + Check.Exception(parentPkColumn == null, $"{parentPkColumn.EntityName} need primary key", $"{parentPkColumn.EntityName}需要主键"); + var mappingType = parentNavigateProperty.Navigat.MappingType; + var mappingEntity = this._Context.EntityMaintenance.GetEntityInfo(mappingType); + var mappingA = mappingEntity.Columns.FirstOrDefault(x => x.PropertyName == parentNavigateProperty.Navigat.MappingAId); + var mappingB = mappingEntity.Columns.FirstOrDefault(x => x.PropertyName == parentNavigateProperty.Navigat.MappingBId); + var mappingPk = mappingEntity.Columns + .Where(it => it.PropertyName != mappingA.PropertyName) + .Where(it => it.PropertyName != mappingB.PropertyName) + .Where(it => it.IsPrimarykey && !it.IsIdentity && it.OracleSequenceName.IsNullOrEmpty()).FirstOrDefault(); + + if (IsDeleteA()) + { + if (!_IsDeletedParant) + SetContext(() => this._Context.Deleteable(parentList).ExecuteCommand()); + } + + var aids = _ParentList.Select(it => parentPkColumn.PropertyInfo.GetValue(it)).ToList(); + var bids = _Context.Queryable().AS(mappingEntity.DbTableName).In(mappingA.DbColumnName, aids) + .Select(mappingB.DbColumnName).ToDataTable() + .Rows.Cast().Select(it => it[0]).ToList(); + + + var childList = this._Context.Queryable().In(thisPkColumn.DbColumnName, bids).ToList(); + + + if (IsDeleteB()) + { + SetContext(() => _Context.Deleteable(childList).ExecuteCommand()); + } + + this._ParentList = childList.Cast().ToList(); + this._ParentPkColumn = thisPkColumn; + this._IsDeletedParant = true; + + SetContext(() => _Context.Deleteable().AS(mappingEntity.DbTableName).In( + mappingA.DbColumnName, aids + ).ExecuteCommand()); } + private bool IsDeleteA() + { + return deleteNavOptions != null && deleteNavOptions.ManyToMayIsDeleteA; + } + private bool IsDeleteB() + { + return deleteNavOptions != null && deleteNavOptions.ManyToMayIsDeleteB; + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToOne.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToOne.cs index 6ac67dc67..6ed8868af 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToOne.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavOneToOne.cs @@ -10,7 +10,27 @@ namespace SqlSugar { private void DeleteOneToOne(string name, EntityColumnInfo nav) where TChild : class, new() { - + var parentEntity = _ParentEntity; + var parentList = _ParentList.Cast().ToList(); + var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name); + var parentPkColumn = parentEntity.Columns.FirstOrDefault(it => it.IsPrimarykey); + var thisEntity = this._Context.EntityMaintenance.GetEntityInfo(); + EntityColumnInfo thisPkColumn = GetPkColumnByNav(thisEntity, nav); + Check.Exception(thisPkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误"); + + + if (!_IsDeletedParant) + SetContext(() => this._Context.Deleteable(parentList).ExecuteCommand()); + + + var ids = _ParentList.Select(it => parentColumn.PropertyInfo.GetValue(it)).ToList(); + List childList = this._Context.Queryable().In(thisPkColumn.DbColumnName, ids).ToList(); + + this._ParentList = childList.Cast().ToList(); + this._ParentPkColumn = thisPkColumn; + this._IsDeletedParant = true; + + SetContext(() => this._Context.Deleteable(childList).ExecuteCommand()); } } diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs index f38d73254..d7edca7f0 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavProvider.cs @@ -9,6 +9,7 @@ namespace SqlSugar { public partial class DeleteNavProvider where T : class, new() where Root : class, new() { + internal DeleteNavOptions deleteNavOptions; public List _Roots { get; set; } public List _ParentList { get; set; } public List _RootList { get; set; } diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs index d3433384c..ee4137f9d 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/DeleteNavTask.cs @@ -31,6 +31,12 @@ namespace SqlSugar result.Context = this.Context; return result; } + public DeleteNavTask Include(Expression>> expression,DeleteNavOptions deleteNavOptions) where TChild : class, new() + { + var result= Include(expression); + deleteNavProvider.deleteNavOptions = deleteNavOptions; + return result; + } } public class DeleteNavTask where T : class, new() where Root : class, new() { @@ -52,6 +58,18 @@ namespace SqlSugar result.Context = this.Context; return result; } + public DeleteNavTask ThenInclude(Expression>> expression,DeleteNavOptions deleteNavOptions) where TChild : class, new() + { + DeleteNavTask result = new DeleteNavTask(); + Func> func = () => { + var dev = PreFunc(); + dev.deleteNavOptions = deleteNavOptions; + return dev.ThenInclude(expression); + }; + result.PreFunc = func; + result.Context = this.Context; + return result; + } public DeleteNavTask Include(Expression> expression) where TChild : class, new() { return AsNav().ThenInclude(expression); diff --git a/Src/Asp.NetCore2/SqlSugar/Entities/DeleteNavOptions.cs b/Src/Asp.NetCore2/SqlSugar/Entities/DeleteNavOptions.cs new file mode 100644 index 000000000..e4772cdd2 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Entities/DeleteNavOptions.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class DeleteNavOptions + { + public bool ManyToMayIsDeleteA { get; set; } + public bool ManyToMayIsDeleteB { get; set; } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs index 124b6a790..5b0c0c577 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs @@ -544,5 +544,13 @@ namespace SqlSugar { return model.Args[0].MemberName + " DESC "; } + public virtual string Stuff(MethodCallExpressionModel model) + { + var parameter1 = model.Args[0]; + var parameter2 = model.Args[1]; + var parameter3 = model.Args[2]; + var parameter4 = model.Args[3]; + return $" STUFF ({parameter1.MemberName}, {parameter2.MemberName}, {parameter3.MemberName}, {parameter4.MemberName}) "; + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs index 746af359c..d81e8af07 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs @@ -85,5 +85,6 @@ namespace SqlSugar string LessThanOrEqual(MethodCallExpressionModel model); string Asc(MethodCallExpressionModel model); string Desc(MethodCallExpressionModel model); + string Stuff(MethodCallExpressionModel model); } } diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs index 23f80679f..c39192d82 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs @@ -25,6 +25,12 @@ namespace SqlSugar { throw new NotSupportedException("Can only be used in expressions"); } + + public static string Stuff(string sourceString, int start, int length, string AddString) + { + throw new NotSupportedException("Can only be used in expressions"); + } + public static bool GreaterThanOrEqual(object thisValue, object gtValue) { throw new NotSupportedException("Can only be used in expressions"); diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index 9c93febc4..3ffa6fb7f 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -998,6 +998,8 @@ namespace SqlSugar return this.Context.DbMehtods.Asc(model); case "Desc": return this.Context.DbMehtods.Desc(model); + case "Stuff": + return this.Context.DbMehtods.Stuff(model); default: break; }