From 5b19e44d7f7289c9c47869687355494c78578dce Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 27 Mar 2022 22:27:06 +0800 Subject: [PATCH] Update core --- .../Abstract/AdoProvider/AdoProvider.cs | 14 +++++ .../QueryableProvider/QueryableProvider.cs | 58 +++++++++++++++++++ .../SugarProvider/SqlSugarCoreProvider.cs | 1 + .../UpdateProvider/UpdateableProvider.cs | 5 ++ .../SqlSugar/Entities/ConnectionConfig.cs | 18 ++++++ .../SqlSugar/Interface/IQueryable.cs | 3 +- .../SqlSugar/Interface/IUpdateable.cs | 2 + 7 files changed, 100 insertions(+), 1 deletion(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 2087ec38a..b3c791444 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -339,6 +339,8 @@ namespace SqlSugar InitParameters(ref sql, parameters); if (FormatSql != null) sql = FormatSql(sql); + if (this.Context.CurrentConnectionConfig?.SqlMiddle?.IsSqlMiddle==true) + return this.Context.CurrentConnectionConfig.SqlMiddle.ExecuteCommand(sql, parameters); SetConnectionStart(sql); if (this.ProcessingEventStartingSQL != null) ExecuteProcessingSQL(ref sql,ref parameters); @@ -371,6 +373,8 @@ namespace SqlSugar InitParameters(ref sql, parameters); if (FormatSql != null) sql = FormatSql(sql); + if (this.Context.CurrentConnectionConfig?.SqlMiddle?.IsSqlMiddle == true) + return this.Context.CurrentConnectionConfig.SqlMiddle.GetDataReader(sql, parameters); SetConnectionStart(sql); var isSp = this.CommandType == CommandType.StoredProcedure; if (this.ProcessingEventStartingSQL != null) @@ -403,6 +407,8 @@ namespace SqlSugar InitParameters(ref sql, parameters); if (FormatSql != null) sql = FormatSql(sql); + if (this.Context.CurrentConnectionConfig?.SqlMiddle?.IsSqlMiddle == true) + return this.Context.CurrentConnectionConfig.SqlMiddle.GetDataSetAll(sql, parameters); SetConnectionStart(sql); if (this.ProcessingEventStartingSQL != null) ExecuteProcessingSQL(ref sql,ref parameters); @@ -438,6 +444,8 @@ namespace SqlSugar InitParameters(ref sql, parameters); if (FormatSql != null) sql = FormatSql(sql); + if (this.Context.CurrentConnectionConfig?.SqlMiddle?.IsSqlMiddle == true) + return this.Context.CurrentConnectionConfig.SqlMiddle.GetScalar(sql, parameters); SetConnectionStart(sql); if (this.ProcessingEventStartingSQL != null) ExecuteProcessingSQL(ref sql,ref parameters); @@ -473,6 +481,8 @@ namespace SqlSugar InitParameters(ref sql, parameters); if (FormatSql != null) sql = FormatSql(sql); + if (this.Context.CurrentConnectionConfig?.SqlMiddle?.IsSqlMiddle == true) + return await this.Context.CurrentConnectionConfig.SqlMiddle.ExecuteCommandAsync(sql, parameters); SetConnectionStart(sql); if (this.ProcessingEventStartingSQL != null) ExecuteProcessingSQL(ref sql,ref parameters); @@ -510,6 +520,8 @@ namespace SqlSugar InitParameters(ref sql, parameters); if (FormatSql != null) sql = FormatSql(sql); + if (this.Context.CurrentConnectionConfig?.SqlMiddle?.IsSqlMiddle == true) + return await this.Context.CurrentConnectionConfig.SqlMiddle.GetDataReaderAsync(sql, parameters); SetConnectionStart(sql); var isSp = this.CommandType == CommandType.StoredProcedure; if (this.ProcessingEventStartingSQL != null) @@ -547,6 +559,8 @@ namespace SqlSugar InitParameters(ref sql, parameters); if (FormatSql != null) sql = FormatSql(sql); + if (this.Context.CurrentConnectionConfig?.SqlMiddle?.IsSqlMiddle == true) + return await this.Context.CurrentConnectionConfig.SqlMiddle.GetScalarAsync(sql, parameters); SetConnectionStart(sql); if (this.ProcessingEventStartingSQL != null) ExecuteProcessingSQL(ref sql,ref parameters); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 9d17fd3ea..afbe48904 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1276,6 +1276,64 @@ namespace SqlSugar InitMapping(); return _ToList(); } + public virtual void ForEach(Action action, int singleMaxReads = 300,System.Threading.CancellationTokenSource cancellationTokenSource = null) + { + Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0, ErrorMessage.GetThrowMessage("no support Skip take, use PageForEach", "不支持Skip Take,请使用 Queryale.PageForEach")); + var totalNumber = 0; + var totalPage = 1; + for (int i = 1; i <= totalPage; i++) + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + var queryable = this.Clone(); + var page = + totalPage==1? + queryable.ToPageList(i, singleMaxReads, ref totalNumber, ref totalPage): + queryable.ToPageList(i, singleMaxReads); + foreach (var item in page) + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + action.Invoke(item); + } + } + } + public virtual void ForEachByPage(Action action, int pageIndex, int pageSize, ref int totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null) + { + int count = this.Clone().Count(); + if (count > 0) + { + if (pageSize > singleMaxReads && count - ((pageIndex - 1) * pageSize) > singleMaxReads) + { + Int32 Skip = (pageIndex - 1) * pageSize; + Int32 NowCount = count - Skip; + Int32 number = 0; + if (NowCount > pageSize) NowCount = pageSize; + while (NowCount > 0) + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + if (number + singleMaxReads > pageSize) singleMaxReads = NowCount; + foreach (var item in this.Clone().Skip(Skip).Take(singleMaxReads).ToList()) + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + action.Invoke(item); + } + NowCount -= singleMaxReads; + Skip += singleMaxReads; + number += singleMaxReads; + } + } + else + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + foreach (var item in this.Clone().ToPageList(pageIndex, pageSize)) + { + if (cancellationTokenSource?.IsCancellationRequested == true) return; + action.Invoke(item); + } + } + } + totalNumber = count; + } + public List ToOffsetPage(int pageIndex, int pageSize) { if (this.Context.CurrentConnectionConfig.DbType != DbType.SqlServer) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarCoreProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarCoreProvider.cs index c2c42afa8..aab4c2da2 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarCoreProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarCoreProvider.cs @@ -86,6 +86,7 @@ namespace SqlSugar IsWithNoLockQuery = it.MoreSettings.IsWithNoLockQuery, TableEnumIsString=it.MoreSettings.TableEnumIsString }, + SqlMiddle=it.SqlMiddle, SlaveConnectionConfigs = it.SlaveConnectionConfigs }).ToList(); } diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 3eaec21ec..ad964d1cc 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -169,6 +169,11 @@ namespace SqlSugar this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => !ignoreColumns.Contains(it.DbColumnName.ToLower())).ToList(); return this; } + public IUpdateable IgnoreColumnsIF(bool IsIgnore, Expression> columns) + { + if (IsIgnore) this.IgnoreColumns(columns); + return this; + } public IUpdateable IgnoreColumns(string[] columns) { if (columns.HasValue()) diff --git a/Src/Asp.NetCore2/SqlSugar/Entities/ConnectionConfig.cs b/Src/Asp.NetCore2/SqlSugar/Entities/ConnectionConfig.cs index a6d088004..044e165cc 100644 --- a/Src/Asp.NetCore2/SqlSugar/Entities/ConnectionConfig.cs +++ b/Src/Asp.NetCore2/SqlSugar/Entities/ConnectionConfig.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Reflection; using System.Text; @@ -61,6 +62,23 @@ namespace SqlSugar [JsonIgnore] public AopEvents AopEvents { get;set; } + /// + /// + /// + public SqlMiddle SqlMiddle { get; set; } + } + public class SqlMiddle + { + public bool? IsSqlMiddle { get; set; } + public Func GetScalar { get; set; } = (s,p) => throw new NotSupportedException("SqlMiddle.GetScalar"); + public Func ExecuteCommand { get; set; } = (s, p) => throw new NotSupportedException("SqlMiddle.ExecuteCommand"); + public Func GetDataReader { get; set; } = (s, p) => throw new NotSupportedException("SqlMiddle.GetDataReader"); + public Func GetDataSetAll { get; set; } = (s, p) => throw new NotSupportedException("SqlMiddle.GetDataSetAll"); + public Func> GetScalarAsync { get; set; } = (s, p) => throw new NotSupportedException("SqlMiddle.GetScalarAsync"); + public Func> ExecuteCommandAsync { get; set; } = (s, p) => throw new NotSupportedException("SqlMiddle.ExecuteCommandAsync"); + public Func> GetDataReaderAsync { get; set; } = (s, p) => throw new NotSupportedException("SqlMiddle.GetDataReaderAsync"); + public Func> GetDataSetAllAsync { get; set; } = (s, p) => throw new NotSupportedException("SqlMiddle.GetDataSetAllAsync"); + } public class AopEvents { diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs index edb8e0e70..42495d22b 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs @@ -108,7 +108,8 @@ namespace SqlSugar ISugarQueryable Select(string select); ISugarQueryable Select(string select); ISugarQueryable MergeTable(); - + void ForEach(Action action, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null); + void ForEachByPage(Action action, int pageIndex, int pageSize, ref int totalNumber, int singleMaxReads = 300, System.Threading.CancellationTokenSource cancellationTokenSource = null); int Count(); Task CountAsync(); int Count(Expression> expression); diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IUpdateable.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IUpdateable.cs index e259690ba..7b076d92f 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IUpdateable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IUpdateable.cs @@ -80,6 +80,8 @@ namespace SqlSugar IUpdateable IgnoreColumns(bool ignoreAllNullColumns, bool isOffIdentity = false, bool ignoreAllDefaultValue = false); IUpdateable IgnoreColumns(Expression> columns); + IUpdateable IgnoreColumnsIF(bool isIgnore, Expression> columns); + IUpdateable IgnoreColumns(params string[] columns);