Add table filter

This commit is contained in:
skx 2021-01-29 23:14:26 +08:00
parent fd463376aa
commit e5d0b6a6a6
2 changed files with 51 additions and 5 deletions

View File

@ -4,6 +4,7 @@ using System.Data.SqlClient;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
@ -296,17 +297,42 @@ namespace SqlSugar
var gobalFilterList = this.Context.QueryFilter.GeFilterList.Where(it => it.FilterName.IsNullOrEmpty()).ToList();
foreach (var item in gobalFilterList.Where(it => it.IsJoinQuery == !IsSingle()))
{
var filterResult = item.FilterValue(this.Context);
WhereInfos.Add(this.Builder.AppendWhereOrAnd(this.WhereInfos.IsNullOrEmpty(), filterResult.Sql + UtilConstants.Space));
var filterParamters = this.Context.Ado.GetParameters(filterResult.Parameters);
if (filterParamters.HasValue())
if (item.GetType().Name.StartsWith("TableFilterItem"))
{
this.Parameters.AddRange(filterParamters);
AppendTableFilter(item);
}
else
{
var filterResult = item.FilterValue(this.Context);
WhereInfos.Add(this.Builder.AppendWhereOrAnd(this.WhereInfos.IsNullOrEmpty(), filterResult.Sql + UtilConstants.Space));
var filterParamters = this.Context.Ado.GetParameters(filterResult.Parameters);
if (filterParamters.HasValue())
{
this.Parameters.AddRange(filterParamters);
}
}
}
}
}
private void AppendTableFilter(SqlFilterItem item)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = item.GetType();
PropertyInfo field = type.GetProperty("exp", flag);
Type ChildType = type.GetProperty("type", flag).GetValue(item,null) as Type;
var exp=field.GetValue(item,null) as Expression;
var isMain = ChildType == this.EntityType;
var isSingle = IsSingle();
var expValue = GetExpressionValue(exp, isSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple);
var sql = expValue.GetResultString();
if (!isSingle)
{
}
WhereInfos.Add(sql);
}
public virtual string GetExternalOrderBy(string externalOrderBy)
{
return Regex.Replace(externalOrderBy, @"\[\w+\]\.", "");

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
@ -18,6 +19,25 @@ namespace SqlSugar
public bool IsJoinQuery { get; set; }
}
public class TableFilterItem<T>: SqlFilterItem where T :class,new()
{
private TableFilterItem()
{
}
private Expression exp { get; set; }
private Type type { get; set; }
public TableFilterItem(Expression<Func<T,bool>> expression)
{
exp = expression;
type = typeof(T);
}
private new string FilterName { get; set; }
private new Func<ISqlSugarClient, SqlFilterResult> FilterValue { get; set; }
private new bool IsJoinQuery { get; set; }
}
public class SqlFilterResult
{
public string Sql { get; set; }