Skip to content
果糖网 edited this page Jul 2, 2024 · 9 revisions

1、Introduce

The purpose of the global filter is to set a query condition, when you query the operation to meet this condition, then your query will be attached to the condition you set

Application scenario: Filtering fake deleted data For example, isdelete=false is added to the end of each query

2、Table Filters

2.1 Manually Adding Filters

Add filters by entity, and all Queryable operations for that entity take effect

/*** Filter write position ***/
SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig(){
ConnectionString = "Connector string ",
DbType = DbType.SqlServer,
IsAutoCloseConnection = true},
db=>{

// Just write the filter here
db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted==false);
// If the vstore is multi-tenant
//db.GetConnection(XXX).AddTableFilter
// Whether you use GetConnection or GetConnectionScope is consistent with your code
});



/*** Specific use cases ***/

db.QueryFilter.AddTableFilter<Order>(it => it.Name.Contains("a"));
db.QueryFilter.AddTableFilterIF<Order>( isAdmin == false ,it => it.Name.Contains("a"));


// Interface filter (valid for classes that inherit interfaces) Please upgrade 5.1.3.47
db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDelete==false);


/**** Use case explanation ***/
db.QueryFilter.AddTableFilter<Order>(it => it.Name.Contains("a")); // Add a filter to the Order type

// A single table is valid
db.Queryable<Order>().ToList();
// SELECT [Id],[Name],[Price],[CreateTime],[CustomId] FROM [Order]  WHERE  ([Name] like '%'+@MethodConst0+'%')

// Multi-table queries are also valid
db.Queryable<OrderItem>().LeftJoin<Order>((i, o) => i.OrderId == o.Id)
.Select((i,o)=>i).ToList();
//SELECT i.* FROM [OrderDetail] i Left Join [Order]  o
//ON ( [i].[OrderId] = [o].[Id] )  AND  ([o].[Name] like '%'+@MethodConst1+'%')

2.2 Disabling, Clearing, backing up, and Restoring

Please upgrade 5.1.3.47 or later

db.QueryFilter
AddTableFilter<IDeletedFilter>(it => it.IsDeleted==false)//IDeletedFilter is a user-defined interface, and the entity that inherits this interface is valid
AddTableFilterIF<ITenantFilter>(isAdmint==false,it=>it.OrgId== user OrgId); //ITenantFilter Indicates a user-defined interface

// Use Case 1: A single statement is cleared and only the current statement is affected
db.Queryable<Order>().ClearFilter().ToList(); // All filters are invalid
db.Queryable<Order>().ClearFilter<IDeletedFilter>().ToList(); // Only the IDeletedFilter filter is invalid
db.Queryable<Order>().ClearFilter<IDeletedFilter,ITenantFilter>().ToList(); //IDeletedFilter+ITenantFilter is invalid

// Use Case 2: The current context clears, does not affect other requests, only the current request clears
db.QueryFilter.Clear();
db.QueryFilter.Clear<IDeletedFilter>();

// Use Case 3: Clear and restore, does not affect other requests, only the current request clear
db.QueryFilter.ClearAndBackup(); // Multiple overloads ClearAndBackup<T,T2>();
db.Queryable<Order>().ToList();
db.QueryFilter.Restore(); // Restore filter (suitable for the following code also requires filters)