sunkaixuan
6 years ago
4 changed files with 83 additions and 2 deletions
@ -0,0 +1,74 @@ |
|||
using SqlSugar; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
|
|||
namespace OrmTest |
|||
{ |
|||
public class Democ_GobalFilter |
|||
{ |
|||
public static void Init() |
|||
{ |
|||
var db = GetInstance(); |
|||
|
|||
|
|||
var sql = db.Queryable<Order>().ToSql(); |
|||
//SELECT [Id],[Name],[Price],[CreateTime] FROM [Order] WHERE isDelete=0
|
|||
Console.WriteLine(sql); |
|||
|
|||
|
|||
var sql2 = db.Queryable<Order,OrderItem>((main,ot)=> main.Id==ot.OrderId).ToSql(); |
|||
//SELECT [Id],[Name],[Price],[CreateTime] FROM [Order] main ,[OrderDetail] ot WHERE ( [main].[Id] = [ot].[OrderId] ) AND main.isDelete=0
|
|||
Console.WriteLine(sql2); |
|||
|
|||
|
|||
var sql3 = db.Queryable<Order>().Filter("Myfilter").ToSql();// Myfilter+Gobal
|
|||
//SELECT [Id],[Name],[Price],[CreateTime] FROM [Order] WHERE Name='jack' AND isDelete=0
|
|||
Console.WriteLine(sql3); |
|||
|
|||
var sql4 = db.Queryable<Order>().Filter("Myfilter",isDisabledGobalFilter:true).ToSql();//only Myfilter
|
|||
//SELECT [Id],[Name],[Price],[CreateTime] FROM [Order] WHERE Name='jack'
|
|||
Console.WriteLine(sql4); |
|||
} |
|||
|
|||
|
|||
public static SqlSugarClient GetInstance() |
|||
{ |
|||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.SqlServer, ConnectionString = Config.ConnectionString, IsAutoCloseConnection = true }); |
|||
|
|||
//single table query gobal filter
|
|||
db.QueryFilter.Add(new SqlFilterItem() |
|||
{ |
|||
FilterValue = filterDb => |
|||
{ |
|||
//Writable logic
|
|||
return new SqlFilterResult() { Sql = " isDelete=0" };//Global string perform best
|
|||
} |
|||
}); |
|||
|
|||
//Multi-table query gobal filter
|
|||
db.QueryFilter.Add(new SqlFilterItem() |
|||
{ |
|||
FilterValue = filterDb => |
|||
{ |
|||
//Writable logic
|
|||
return new SqlFilterResult() { Sql = " main.isDelete=0" }; |
|||
}, |
|||
IsJoinQuery=true |
|||
}); |
|||
|
|||
//Specific filters
|
|||
db.QueryFilter.Add(new SqlFilterItem() |
|||
{ |
|||
FilterName= "Myfilter", |
|||
FilterValue = filterDb => |
|||
{ |
|||
//Writable logic
|
|||
return new SqlFilterResult() { Sql = "Name='jack'" }; |
|||
} |
|||
}); |
|||
return db; |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue