mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- 优化 全局过滤器禁用时子查询传播问题;#1208
This commit is contained in:
parent
081953a4c1
commit
f042ea46bb
@ -800,5 +800,14 @@
|
|||||||
<param name="that"></param>
|
<param name="that"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
|
||||||
|
<summary>
|
||||||
|
批量注入 Repository,可以参考代码自行调整
|
||||||
|
</summary>
|
||||||
|
<param name="services"></param>
|
||||||
|
<param name="globalDataFilter"></param>
|
||||||
|
<param name="assemblies"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
</members>
|
</members>
|
||||||
</doc>
|
</doc>
|
||||||
|
@ -149,6 +149,11 @@
|
|||||||
用户
|
用户
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="P:FreeSql.Tests.Issues._1208.DoorDeviceEntity02.Users">
|
||||||
|
<summary>
|
||||||
|
用户
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="P:FreeSql.Tests.Issues._467.PayOrder.Money">
|
<member name="P:FreeSql.Tests.Issues._467.PayOrder.Money">
|
||||||
<summary>
|
<summary>
|
||||||
收款金额
|
收款金额
|
||||||
|
@ -8,7 +8,7 @@ namespace FreeSql.Tests.Issues
|
|||||||
public class _1208
|
public class _1208
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void GlobalFilter()
|
public void GlobalFilter01()
|
||||||
{
|
{
|
||||||
using (var fsql = new FreeSqlBuilder()
|
using (var fsql = new FreeSqlBuilder()
|
||||||
.UseConnectionString(DataType.Sqlite, "data source=:memory:")
|
.UseConnectionString(DataType.Sqlite, "data source=:memory:")
|
||||||
@ -171,6 +171,174 @@ ORDER BY a.""Id"" DESC", sql);
|
|||||||
public ICollection<UserEntity> Users { get; set; }
|
public ICollection<UserEntity> Users { get; set; }
|
||||||
public int TenantId { get; set; }
|
public int TenantId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GlobalFilter02()
|
||||||
|
{
|
||||||
|
using (var fsql = new FreeSqlBuilder()
|
||||||
|
.UseConnectionString(DataType.Sqlite, "data source=:memory:")
|
||||||
|
.UseAutoSyncStructure(true)
|
||||||
|
.Build())
|
||||||
|
{
|
||||||
|
fsql.GlobalFilter.Apply<UserEntity02>("TenantQuery", a => a.TenantId == 100);
|
||||||
|
fsql.GlobalFilter.Apply<UserEntity02>("SoftDeleted", a => a.SoftDeleted == false);
|
||||||
|
|
||||||
|
var userRepository = fsql.GetRepository<UserEntity02>();
|
||||||
|
|
||||||
|
var deviceId = 100;
|
||||||
|
var sql = userRepository.Select
|
||||||
|
.Where(i => i.DoorDevices.AsSelect().Any(x => x.Id == deviceId))
|
||||||
|
.OrderByDescending(true, a => a.Id)
|
||||||
|
.ToSql();
|
||||||
|
Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted""
|
||||||
|
FROM ""issues1208_User02"" a
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDeviceUser02"" Mx_Mi
|
||||||
|
WHERE (Mx_Mi.""UserId"" = a.""Id"") AND (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDevice02"" x
|
||||||
|
WHERE (x.""Id"" = 100) AND (x.""Id"" = Mx_Mi.""DoorDeviceId"") AND (x.""TenantId"" = 100) AND (x.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (Mx_Mi.""TenantId"" = 100) AND (Mx_Mi.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (a.""TenantId"" = 100) AND (a.""SoftDeleted"" = 0)
|
||||||
|
ORDER BY a.""Id"" DESC", sql);
|
||||||
|
|
||||||
|
sql = userRepository.Select
|
||||||
|
.Where(i => i.DoorDevices.AsSelect().DisableGlobalFilter("TenantQuery").Any(x => x.Id == deviceId))
|
||||||
|
.OrderByDescending(true, a => a.Id)
|
||||||
|
.ToSql();
|
||||||
|
Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted""
|
||||||
|
FROM ""issues1208_User02"" a
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDeviceUser02"" Mx_Mi
|
||||||
|
WHERE (Mx_Mi.""UserId"" = a.""Id"") AND (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDevice02"" x
|
||||||
|
WHERE (x.""Id"" = 100) AND (x.""Id"" = Mx_Mi.""DoorDeviceId"") AND (x.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (Mx_Mi.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (a.""TenantId"" = 100) AND (a.""SoftDeleted"" = 0)
|
||||||
|
ORDER BY a.""Id"" DESC", sql);
|
||||||
|
|
||||||
|
sql = userRepository.Select.DisableGlobalFilter("TenantQuery")
|
||||||
|
.Where(i => i.DoorDevices.AsSelect().Any(x => x.Id == deviceId))
|
||||||
|
.OrderByDescending(true, a => a.Id)
|
||||||
|
.ToSql();
|
||||||
|
Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted""
|
||||||
|
FROM ""issues1208_User02"" a
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDeviceUser02"" Mx_Mi
|
||||||
|
WHERE (Mx_Mi.""UserId"" = a.""Id"") AND (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDevice02"" x
|
||||||
|
WHERE (x.""Id"" = 100) AND (x.""Id"" = Mx_Mi.""DoorDeviceId"") AND (x.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (Mx_Mi.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (a.""SoftDeleted"" = 0)
|
||||||
|
ORDER BY a.""Id"" DESC", sql);
|
||||||
|
|
||||||
|
using (userRepository.DataFilter.Disable("TenantQuery"))
|
||||||
|
{
|
||||||
|
sql = userRepository.Select
|
||||||
|
.Where(i => i.DoorDevices.AsSelect().Any(x => x.Id == deviceId))
|
||||||
|
.OrderByDescending(true, a => a.Id)
|
||||||
|
.ToSql();
|
||||||
|
Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted""
|
||||||
|
FROM ""issues1208_User02"" a
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDeviceUser02"" Mx_Mi
|
||||||
|
WHERE (Mx_Mi.""UserId"" = a.""Id"") AND (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDevice02"" x
|
||||||
|
WHERE (x.""Id"" = 100) AND (x.""Id"" = Mx_Mi.""DoorDeviceId"") AND (x.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (Mx_Mi.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (a.""SoftDeleted"" = 0)
|
||||||
|
ORDER BY a.""Id"" DESC", sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
sql = userRepository.Select
|
||||||
|
.Where(i => i.DoorDevices.Any(x => x.Id == deviceId))
|
||||||
|
.OrderByDescending(true, a => a.Id)
|
||||||
|
.ToSql();
|
||||||
|
Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted""
|
||||||
|
FROM ""issues1208_User02"" a
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDevice02"" x
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDeviceUser02"" Mx_Ma
|
||||||
|
WHERE (Mx_Ma.""DoorDeviceId"" = x.""Id"") AND (Mx_Ma.""UserId"" = a.""Id"") AND (Mx_Ma.""TenantId"" = 100) AND (Mx_Ma.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (x.""Id"" = 100) AND (x.""TenantId"" = 100) AND (x.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (a.""TenantId"" = 100) AND (a.""SoftDeleted"" = 0)
|
||||||
|
ORDER BY a.""Id"" DESC", sql);
|
||||||
|
|
||||||
|
sql = userRepository.Select.DisableGlobalFilter("TenantQuery")
|
||||||
|
.Where(i => i.DoorDevices.Any(x => x.Id == deviceId))
|
||||||
|
.OrderByDescending(true, a => a.Id)
|
||||||
|
.ToSql();
|
||||||
|
Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted""
|
||||||
|
FROM ""issues1208_User02"" a
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDevice02"" x
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDeviceUser02"" Mx_Ma
|
||||||
|
WHERE (Mx_Ma.""DoorDeviceId"" = x.""Id"") AND (Mx_Ma.""UserId"" = a.""Id"") AND (Mx_Ma.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (x.""Id"" = 100) AND (x.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (a.""SoftDeleted"" = 0)
|
||||||
|
ORDER BY a.""Id"" DESC", sql);
|
||||||
|
|
||||||
|
using (userRepository.DataFilter.Disable("TenantQuery"))
|
||||||
|
{
|
||||||
|
sql = userRepository.Select
|
||||||
|
.Where(i => i.DoorDevices.Any(x => x.Id == deviceId))
|
||||||
|
.OrderByDescending(true, a => a.Id)
|
||||||
|
.ToSql();
|
||||||
|
Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted""
|
||||||
|
FROM ""issues1208_User02"" a
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDevice02"" x
|
||||||
|
WHERE (exists(SELECT 1
|
||||||
|
FROM ""issues1208_DoorDeviceUser02"" Mx_Ma
|
||||||
|
WHERE (Mx_Ma.""DoorDeviceId"" = x.""Id"") AND (Mx_Ma.""UserId"" = a.""Id"") AND (Mx_Ma.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (x.""Id"" = 100) AND (x.""SoftDeleted"" = 0)
|
||||||
|
limit 0,1)) AND (a.""SoftDeleted"" = 0)
|
||||||
|
ORDER BY a.""Id"" DESC", sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table(Name = "issues1208_User02")]
|
||||||
|
class UserEntity02
|
||||||
|
{
|
||||||
|
[Column(IsIdentity = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Navigate(ManyToMany = typeof(DoorDeviceUserEntity02))]
|
||||||
|
public ICollection<DoorDeviceEntity02> DoorDevices { get; set; }
|
||||||
|
|
||||||
|
public int TenantId { get; set; }
|
||||||
|
public bool SoftDeleted { get; set; }
|
||||||
|
}
|
||||||
|
[Table(Name = "issues1208_DoorDeviceUser02")]
|
||||||
|
class DoorDeviceUserEntity02
|
||||||
|
{
|
||||||
|
public int UserId { get; set; }
|
||||||
|
|
||||||
|
[Navigate(nameof(UserId))]
|
||||||
|
public UserEntity02 User { get; set; }
|
||||||
|
|
||||||
|
public int DoorDeviceId { get; set; }
|
||||||
|
|
||||||
|
[Navigate(nameof(DoorDeviceId))]
|
||||||
|
public DoorDeviceEntity02 DoorDevice { get; set; }
|
||||||
|
public int TenantId { get; set; }
|
||||||
|
public bool SoftDeleted { get; set; }
|
||||||
|
}
|
||||||
|
[Table(Name = "issues1208_DoorDevice02")]
|
||||||
|
class DoorDeviceEntity02
|
||||||
|
{
|
||||||
|
[Column(IsIdentity = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户
|
||||||
|
/// </summary>
|
||||||
|
[Navigate(ManyToMany = typeof(DoorDeviceUserEntity02))]
|
||||||
|
public ICollection<UserEntity02> Users { get; set; }
|
||||||
|
public int TenantId { get; set; }
|
||||||
|
public bool SoftDeleted { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1241,14 +1241,8 @@ namespace FreeSql.Internal
|
|||||||
}
|
}
|
||||||
if (tsc.whereGlobalFilter != null)
|
if (tsc.whereGlobalFilter != null)
|
||||||
{
|
{
|
||||||
if (tsc.whereGlobalFilter.Any() == false)
|
fsqlSelect0._whereGlobalFilter.Clear();
|
||||||
fsqlSelect0._whereGlobalFilter.Clear();
|
if (tsc.whereGlobalFilter.Any()) fsqlSelect0._whereGlobalFilter.AddRange(tsc.whereGlobalFilter);
|
||||||
else
|
|
||||||
{
|
|
||||||
var fsqlGlobalFilter = fsqlSelect0._whereGlobalFilter;
|
|
||||||
if (fsqlGlobalFilter != tsc.whereGlobalFilter)
|
|
||||||
fsqlGlobalFilter.AddRange(tsc.whereGlobalFilter.Where(b => !fsqlGlobalFilter.Any(a => a.Name == b.Name)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (fsqlType != null)
|
else if (fsqlType != null)
|
||||||
@ -2375,14 +2369,8 @@ namespace FreeSql.Internal
|
|||||||
if (tableRule != null) midSelect._tableRules.Add(tableRule);
|
if (tableRule != null) midSelect._tableRules.Add(tableRule);
|
||||||
if (whereGlobalFilter != null)
|
if (whereGlobalFilter != null)
|
||||||
{
|
{
|
||||||
if (whereGlobalFilter.Any() == false)
|
midSelect._whereGlobalFilter.Clear();
|
||||||
midSelect._whereGlobalFilter.Clear();
|
if (whereGlobalFilter.Any()) midSelect._whereGlobalFilter.AddRange(whereGlobalFilter);
|
||||||
else
|
|
||||||
{
|
|
||||||
var fsqlGlobalFilter = midSelect._whereGlobalFilter;
|
|
||||||
if (fsqlGlobalFilter != whereGlobalFilter)
|
|
||||||
fsqlGlobalFilter.AddRange(whereGlobalFilter.Where(b => !fsqlGlobalFilter.Any(a => a.Name == b.Name)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
switch (commonExp._ado.DataType)
|
switch (commonExp._ado.DataType)
|
||||||
{
|
{
|
||||||
@ -2443,14 +2431,8 @@ namespace FreeSql.Internal
|
|||||||
if (tableRule != null) select._tableRules.Add(tableRule);
|
if (tableRule != null) select._tableRules.Add(tableRule);
|
||||||
if (whereGlobalFilter != null)
|
if (whereGlobalFilter != null)
|
||||||
{
|
{
|
||||||
if (whereGlobalFilter.Any() == false)
|
select._whereGlobalFilter.Clear();
|
||||||
select._whereGlobalFilter.Clear();
|
if (whereGlobalFilter.Any()) select._whereGlobalFilter.AddRange(whereGlobalFilter);
|
||||||
else
|
|
||||||
{
|
|
||||||
var fsqlGlobalFilter = select._whereGlobalFilter;
|
|
||||||
if (fsqlGlobalFilter != whereGlobalFilter)
|
|
||||||
fsqlGlobalFilter.AddRange(whereGlobalFilter.Where(b => !fsqlGlobalFilter.Any(a => a.Name == b.Name)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (true)
|
while (true)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user