From 66a8d5f6dc37dd49dfae3e43ced6aa38f7f28af1 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 20 Jan 2023 15:05:49 +0800 Subject: [PATCH] Update gobal filter --- .../Abstract/QueryableProvider/QueryableHelper.cs | 1 + .../QueryableProvider/QueryableProvider.cs | 6 ++++++ .../Abstract/SqlBuilderProvider/QueryBuilder.cs | 14 +++++++++----- Src/Asp.NetCore2/SqlSugar/Entities/SqlFilter.cs | 2 +- Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs | 1 + 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs index 9f5b1810a..c6c8b0b44 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs @@ -1556,6 +1556,7 @@ namespace SqlSugar asyncQueryableBuilder.SubToListParameters= this.Context.Utilities.TranslateCopy(this.QueryBuilder.SubToListParameters); asyncQueryableBuilder.AppendColumns = this.Context.Utilities.TranslateCopy(this.QueryBuilder.AppendColumns); asyncQueryableBuilder.AppendValues = this.Context.Utilities.TranslateCopy(this.QueryBuilder.AppendValues); + asyncQueryableBuilder.RemoveFilters = this.QueryBuilder.RemoveFilters?.ToArray(); } protected int SetCacheTime(int cacheDurationInSeconds) { diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index b685585c3..d0ecc157c 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -266,6 +266,12 @@ namespace SqlSugar this.Filter(null, true); return this; } + public ISugarQueryable ClearFilter(params Type[] types) + { + this.QueryBuilder.RemoveFilters = types; + this.Filter(null, true); + return this; + } public ISugarQueryable Filter(Type type) { this.Context.InitMappingInfo(type); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs index 921c2190c..6916e5071 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs @@ -34,6 +34,7 @@ namespace SqlSugar #endregion #region Splicing basic + public Type[] RemoveFilters { get; set; } public Dictionary SubToListParameters { get; set; } internal List AppendColumns { get; set; } internal List> AppendValues { get; set; } @@ -297,7 +298,7 @@ namespace SqlSugar if (this.Context != null) { var db = Context; - BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic; + BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic| BindingFlags.Public; var index = 0; if (db.QueryFilter.GeFilterList != null) { @@ -306,7 +307,7 @@ namespace SqlSugar PropertyInfo field = item.GetType().GetProperty("exp", flag); if (field != null) { - Type ChildType = item.GetType().GetProperty("type", flag).GetValue(item, null) as Type; + Type ChildType = item.type; var isInterface = ChildType.IsInterface && type.GetInterfaces().Any(it => it == ChildType); if (ChildType == type|| isInterface) { @@ -366,7 +367,10 @@ namespace SqlSugar if (!IsDisabledGobalFilter && this.Context.QueryFilter.GeFilterList.HasValue()) { var gobalFilterList = this.Context.QueryFilter.GeFilterList.Where(it => it.FilterName.IsNullOrEmpty()).ToList(); - + if (this.RemoveFilters != null && this.RemoveFilters.Length > 0) + { + gobalFilterList = gobalFilterList.Where(it => !this.RemoveFilters.Contains(it.type)).ToList(); + } foreach (var item in gobalFilterList) { if (item.GetType().Name.StartsWith("TableFilterItem")) @@ -389,10 +393,10 @@ namespace SqlSugar private void AppendTableFilter(SqlFilterItem item) { - BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic; + BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic |BindingFlags.Public; Type type = item.GetType(); PropertyInfo field = type.GetProperty("exp", flag); - Type ChildType = type.GetProperty("type", flag).GetValue(item, null) as Type; + Type ChildType = item.type; var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(ChildType); var exp = field.GetValue(item, null) as Expression; var isMain = ChildType == this.EntityType||(ChildType.IsInterface&& this.EntityType.GetInterfaces().Any(it => it == ChildType)); diff --git a/Src/Asp.NetCore2/SqlSugar/Entities/SqlFilter.cs b/Src/Asp.NetCore2/SqlSugar/Entities/SqlFilter.cs index caefac234..c70823e53 100644 --- a/Src/Asp.NetCore2/SqlSugar/Entities/SqlFilter.cs +++ b/Src/Asp.NetCore2/SqlSugar/Entities/SqlFilter.cs @@ -17,6 +17,7 @@ namespace SqlSugar /// Is it a multiple table query? /// public bool IsJoinQuery { get; set; } + internal Type type { get; set; } } public class TableFilterItem: SqlFilterItem @@ -26,7 +27,6 @@ namespace SqlSugar } private Expression exp { get; set; } - private Type type { get; set; } public TableFilterItem(Expression> expression,bool isJoinOn=false) { exp = expression; diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs index 328697c07..24be431e9 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IQueryable.cs @@ -33,6 +33,7 @@ namespace SqlSugar ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); ISugarQueryable Filter(string FilterName, bool isDisabledGobalFilter = false); + ISugarQueryable ClearFilter(params Type[] types); ISugarQueryable ClearFilter(); ISugarQueryable Filter(Type type); ISugarQueryable Mapper(Action mapperAction);