mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Add table filter
This commit is contained in:
parent
fd463376aa
commit
e5d0b6a6a6
@ -4,6 +4,7 @@ using System.Data.SqlClient;
|
|||||||
using System.Dynamic;
|
using System.Dynamic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
@ -296,17 +297,42 @@ namespace SqlSugar
|
|||||||
var gobalFilterList = this.Context.QueryFilter.GeFilterList.Where(it => it.FilterName.IsNullOrEmpty()).ToList();
|
var gobalFilterList = this.Context.QueryFilter.GeFilterList.Where(it => it.FilterName.IsNullOrEmpty()).ToList();
|
||||||
foreach (var item in gobalFilterList.Where(it => it.IsJoinQuery == !IsSingle()))
|
foreach (var item in gobalFilterList.Where(it => it.IsJoinQuery == !IsSingle()))
|
||||||
{
|
{
|
||||||
var filterResult = item.FilterValue(this.Context);
|
if (item.GetType().Name.StartsWith("TableFilterItem"))
|
||||||
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);
|
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)
|
public virtual string GetExternalOrderBy(string externalOrderBy)
|
||||||
{
|
{
|
||||||
return Regex.Replace(externalOrderBy, @"\[\w+\]\.", "");
|
return Regex.Replace(externalOrderBy, @"\[\w+\]\.", "");
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
@ -18,6 +19,25 @@ namespace SqlSugar
|
|||||||
public bool IsJoinQuery { get; set; }
|
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 class SqlFilterResult
|
||||||
{
|
{
|
||||||
public string Sql { get; set; }
|
public string Sql { get; set; }
|
||||||
|
Loading…
Reference in New Issue
Block a user