From 911574069b8629f0b8d9847e1b6fe5bbfe9dd29f Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 9 Jul 2023 16:38:47 +0800 Subject: [PATCH] Navigation update optlock --- .../ExecuteNavProvider/UpdateNavProvider.cs | 4 +- .../QueryableProvider/QueryableExecuteSql.cs | 44 +++++++++---------- .../UpdateProvider/UpdateableProvider.cs | 12 ++++- .../SqlSugar/Entities/DeleteNavOptions.cs | 2 + Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs | 1 + 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs index 2781e31c2..4c4721526 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs @@ -168,7 +168,7 @@ namespace SqlSugar } else { - x.AsUpdateable.EnableDiffLogEventIF(_RootOptions.IsDiffLogEvent, _RootOptions.DiffLogBizData).ExecuteCommand(); + x.AsUpdateable.EnableDiffLogEventIF(_RootOptions.IsDiffLogEvent, _RootOptions.DiffLogBizData).ExecuteCommandWithOptLockIF(_RootOptions?.IsOptLock, _RootOptions?.IsOptLock); newRoots.Add(item); } } @@ -180,7 +180,7 @@ namespace SqlSugar .EnableDiffLogEventIF(_RootOptions.IsDiffLogEvent,_RootOptions.DiffLogBizData) .UpdateColumns(_RootOptions.UpdateColumns) .IgnoreColumns(_RootOptions.IgnoreColumns) - .ExecuteCommand(); + .ExecuteCommandWithOptLockIF(_RootOptions?.IsOptLock, _RootOptions?.IsOptLock); } } else if (_RootOptions != null && _RootOptions?.IsDiffLogEvent == true) diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSql.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSql.cs index 4699d4699..47f5b4727 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSql.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableExecuteSql.cs @@ -98,17 +98,17 @@ namespace SqlSugar public virtual List ToList(Expression> expression) { - //if (this.QueryBuilder.Includes != null && this.QueryBuilder.Includes.Count > 0) - //{ - // return NavSelectHelper.GetList(expression, this); - // // var list = this.ToList().Select(expression.Compile()).ToList(); - // // return list; - //} - //else - //{ + if (this.QueryBuilder.Includes != null && this.QueryBuilder.Includes.Count > 0) + { + return NavSelectHelper.GetList(expression, this); + // var list = this.ToList().Select(expression.Compile()).ToList(); + // return list; + } + else + { var list = this.Select(expression).ToList(); return list; - //} + } } public virtual int Count() @@ -668,21 +668,21 @@ namespace SqlSugar } public virtual List ToPageList(int pageIndex, int pageSize, ref int totalNumber, Expression> expression) { - //if (this.QueryBuilder.Includes != null && this.QueryBuilder.Includes.Count > 0) - //{ - // if (pageIndex == 0) - // pageIndex = 1; - // var list = this.Clone().Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(expression); - // var countQueryable = this.Clone(); - // countQueryable.QueryBuilder.Includes = null; - // totalNumber = countQueryable.Count(); - // return list; - //} - //else - //{ + if (this.QueryBuilder.Includes != null && this.QueryBuilder.Includes.Count > 0) + { + if (pageIndex == 0) + pageIndex = 1; + var list = this.Clone().Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(expression); + var countQueryable = this.Clone(); + countQueryable.QueryBuilder.Includes = null; + totalNumber = countQueryable.Count(); + return list; + } + else + { var list = this.Select(expression).ToPageList(pageIndex, pageSize, ref totalNumber).ToList(); return list; - //} + } } public virtual List ToPageList(int pageIndex, int pageSize, ref int totalNumber) { diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index d1058d5c5..80b829bd8 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -64,7 +64,17 @@ namespace SqlSugar var sqlObj = this.ToSql(); this.Context.Queues.Add(sqlObj.Key, sqlObj.Value); } - + public virtual int ExecuteCommandWithOptLockIF(bool? IsVersionValidation ,bool? IsOptLock=null) + { + if (IsOptLock==true) + { + return ExecuteCommandWithOptLock(IsVersionValidation??false); + } + else + { + return this.ExecuteCommand(); + } + } public virtual int ExecuteCommandWithOptLock(bool IsVersionValidation=false) { Check.ExceptionEasy(this.UpdateBuilder.IsListUpdate==true, " OptLock can only be used on a single object, and the argument cannot be List", "乐观锁只能用于单个对象,参数不能是List,如果是一对多操作请更新主表统一用主表验证"); diff --git a/Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs b/Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs index 5fc8afbf7..331864007 100644 --- a/Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs +++ b/Src/Asp.Net/SqlSugar/Entities/DeleteNavOptions.cs @@ -38,6 +38,8 @@ namespace SqlSugar public bool IsDiffLogEvent { get; set; } public object DiffLogBizData { get; set; } public string[] IgnoreInsertColumns { get; set; } + public bool IsOptLock { get; set; } + } public class UpdateNavOptions { diff --git a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs index ad309215a..ca247f9de 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs @@ -15,6 +15,7 @@ namespace SqlSugar bool UpdateParameterIsNull { get; set; } int ExecuteCommandWithOptLock(bool isThrowError = false); + int ExecuteCommandWithOptLockIF(bool? IsVersionValidation, bool? IsOptLock = null); Task ExecuteCommandWithOptLockAsync(bool isThrowError = false); int ExecuteCommand(); bool ExecuteCommandHasChange();