From f042ea46bb15cff628b5e75bf63313967baa2258 Mon Sep 17 00:00:00 2001
From: 2881099 <2881099@qq.com>
Date: Tue, 9 Aug 2022 19:11:24 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20=E5=85=A8=E5=B1=80?=
=?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=99=A8=E7=A6=81=E7=94=A8=E6=97=B6=E5=AD=90?=
=?UTF-8?q?=E6=9F=A5=E8=AF=A2=E4=BC=A0=E6=92=AD=E9=97=AE=E9=A2=98=EF=BC=9B?=
=?UTF-8?q?#1208?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql.DbContext/FreeSql.DbContext.xml | 9 +
FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml | 5 +
FreeSql.Tests/FreeSql.Tests/Issues/1208.cs | 172 +++++++++++++++++-
FreeSql/Internal/CommonExpression.cs | 30 +--
4 files changed, 190 insertions(+), 26 deletions(-)
diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml
index 537315e2..26522f10 100644
--- a/FreeSql.DbContext/FreeSql.DbContext.xml
+++ b/FreeSql.DbContext/FreeSql.DbContext.xml
@@ -800,5 +800,14 @@
+
+
+ 批量注入 Repository,可以参考代码自行调整
+
+
+
+
+
+
diff --git a/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml b/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml
index a0d7a412..4ee3e866 100644
--- a/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml
+++ b/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml
@@ -149,6 +149,11 @@
用户
+
+
+ 用户
+
+
收款金额
diff --git a/FreeSql.Tests/FreeSql.Tests/Issues/1208.cs b/FreeSql.Tests/FreeSql.Tests/Issues/1208.cs
index 22f81bce..5358365d 100644
--- a/FreeSql.Tests/FreeSql.Tests/Issues/1208.cs
+++ b/FreeSql.Tests/FreeSql.Tests/Issues/1208.cs
@@ -8,7 +8,7 @@ namespace FreeSql.Tests.Issues
public class _1208
{
[Fact]
- public void GlobalFilter()
+ public void GlobalFilter01()
{
using (var fsql = new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, "data source=:memory:")
@@ -171,6 +171,174 @@ ORDER BY a.""Id"" DESC", sql);
public ICollection Users { 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("TenantQuery", a => a.TenantId == 100);
+ fsql.GlobalFilter.Apply("SoftDeleted", a => a.SoftDeleted == false);
+
+ var userRepository = fsql.GetRepository();
+
+ 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 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; }
+
+ ///
+ /// 用户
+ ///
+ [Navigate(ManyToMany = typeof(DoorDeviceUserEntity02))]
+ public ICollection Users { get; set; }
+ public int TenantId { get; set; }
+ public bool SoftDeleted { get; set; }
+ }
+ }
}
diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs
index 9c0bf50d..645876d0 100644
--- a/FreeSql/Internal/CommonExpression.cs
+++ b/FreeSql/Internal/CommonExpression.cs
@@ -1241,14 +1241,8 @@ namespace FreeSql.Internal
}
if (tsc.whereGlobalFilter != null)
{
- if (tsc.whereGlobalFilter.Any() == false)
- fsqlSelect0._whereGlobalFilter.Clear();
- else
- {
- var fsqlGlobalFilter = fsqlSelect0._whereGlobalFilter;
- if (fsqlGlobalFilter != tsc.whereGlobalFilter)
- fsqlGlobalFilter.AddRange(tsc.whereGlobalFilter.Where(b => !fsqlGlobalFilter.Any(a => a.Name == b.Name)));
- }
+ fsqlSelect0._whereGlobalFilter.Clear();
+ if (tsc.whereGlobalFilter.Any()) fsqlSelect0._whereGlobalFilter.AddRange(tsc.whereGlobalFilter);
}
}
else if (fsqlType != null)
@@ -2375,14 +2369,8 @@ namespace FreeSql.Internal
if (tableRule != null) midSelect._tableRules.Add(tableRule);
if (whereGlobalFilter != null)
{
- if (whereGlobalFilter.Any() == false)
- midSelect._whereGlobalFilter.Clear();
- else
- {
- var fsqlGlobalFilter = midSelect._whereGlobalFilter;
- if (fsqlGlobalFilter != whereGlobalFilter)
- fsqlGlobalFilter.AddRange(whereGlobalFilter.Where(b => !fsqlGlobalFilter.Any(a => a.Name == b.Name)));
- }
+ midSelect._whereGlobalFilter.Clear();
+ if (whereGlobalFilter.Any()) midSelect._whereGlobalFilter.AddRange(whereGlobalFilter);
}
switch (commonExp._ado.DataType)
{
@@ -2443,14 +2431,8 @@ namespace FreeSql.Internal
if (tableRule != null) select._tableRules.Add(tableRule);
if (whereGlobalFilter != null)
{
- if (whereGlobalFilter.Any() == false)
- select._whereGlobalFilter.Clear();
- else
- {
- var fsqlGlobalFilter = select._whereGlobalFilter;
- if (fsqlGlobalFilter != whereGlobalFilter)
- fsqlGlobalFilter.AddRange(whereGlobalFilter.Where(b => !fsqlGlobalFilter.Any(a => a.Name == b.Name)));
- }
+ select._whereGlobalFilter.Clear();
+ if (whereGlobalFilter.Any()) select._whereGlobalFilter.AddRange(whereGlobalFilter);
}
}
while (true)