- 修复 子查询内使用嵌套查询 bug;#1215

This commit is contained in:
2881099 2022-08-15 18:29:32 +08:00
parent cad0d30f51
commit e6ba6feff1
5 changed files with 42 additions and 26 deletions

View File

@ -800,14 +800,5 @@
<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>

View File

@ -34,14 +34,14 @@ namespace FreeSql.Tests.SqlServer
FROM [bhe_1] a FROM [bhe_1] a
WHERE (a.Id IN (SELECT a.[Id] as1 WHERE (a.Id IN (SELECT a.[Id] as1
FROM ( FROM (
SELECT a.[IsDeleted], a.[Id], a.[No], a.[Date], SP10b.[Quantity], isnull((SELECT sum(ti2.[Quantity]) SELECT a.[IsDeleted], a.[Id], a.[No], a.[Date], htb.[Quantity], isnull((SELECT sum(ti2.[Quantity])
FROM [bie_2] ti2 FROM [bie_2] ti2
WHERE (ti2.[RefHeadId] = SP10b.[HeadId] AND ti2.[RefItemId] = SP10b.[Id])), 0) [RefQuantity] WHERE (ti2.[RefHeadId] = htb.[HeadId] AND ti2.[RefItemId] = htb.[Id])), 0) [RefQuantity]
FROM [bhe_1] a FROM [bhe_1] a
INNER JOIN ( INNER JOIN (
SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId] SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId]
FROM [bie_1] bi FROM [bie_1] bi
WHERE (bi.[IsDeleted] = 0)) SP10b ON a.[Id] = SP10b.[HeadId] WHERE (bi.[IsDeleted] = 0)) htb ON a.[Id] = htb.[HeadId]
WHERE (a.[IsDeleted] = 0) ) a WHERE (a.[IsDeleted] = 0) ) a
WHERE (a.[RefQuantity] < a.[Quantity]) WHERE (a.[RefQuantity] < a.[Quantity])
GROUP BY a.[Id])) GROUP BY a.[Id]))
@ -68,14 +68,14 @@ ORDER BY a.[Date] DESC", sql1);
FROM [bhe_1] a FROM [bhe_1] a
WHERE (((a.[Id]) in (SELECT DISTINCT v.[Id] WHERE (((a.[Id]) in (SELECT DISTINCT v.[Id]
FROM ( FROM (
SELECT ht1.[IsDeleted], ht1.[Id], ht1.[No], ht1.[Date], ht2.[Quantity], isnull((SELECT sum(ti2.[Quantity]) SELECT bh.[IsDeleted], bh.[Id], bh.[No], bh.[Date], ht2.[Quantity], isnull((SELECT sum(ti2.[Quantity])
FROM [bie_2] ti2 FROM [bie_2] ti2
WHERE (ti2.[RefHeadId] = ht2.[HeadId] AND ti2.[RefItemId] = ht2.[Id])), 0) [RefQuantity] WHERE (ti2.[RefHeadId] = ht2.[HeadId] AND ti2.[RefItemId] = ht2.[Id])), 0) [RefQuantity]
FROM [bhe_1] ht1 FROM [bhe_1] bh
INNER JOIN ( INNER JOIN (
SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId] SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId]
FROM [bie_1] bi FROM [bie_1] bi
WHERE (bi.[IsDeleted] = 0)) ht2 ON ht1.[Id] = ht2.[HeadId] WHERE (bi.[IsDeleted] = 0)) ht2 ON bh.[Id] = ht2.[HeadId]
WHERE (bh.[IsDeleted] = 0) ) v WHERE (bh.[IsDeleted] = 0) ) v
WHERE (v.[RefQuantity] < v.[Quantity])))) WHERE (v.[RefQuantity] < v.[Quantity]))))
ORDER BY a.[Date] DESC", sql2); ORDER BY a.[Date] DESC", sql2);
@ -99,8 +99,8 @@ ORDER BY a.[Date] DESC", sql2);
) )
.RightJoin(v => v.t1.Id == v.t2.Id) .RightJoin(v => v.t1.Id == v.t2.Id)
.OrderByDescending(v => v.t1.Date) .OrderByDescending(v => v.t1.Date)
.ToSql(); .ToSql(v => v.t1);
Assert.Equal(@"SELECT * Assert.Equal(@"SELECT a.[IsDeleted] as1, a.[Id] as2, a.[No] as3, a.[Date] as4
FROM [bhe_1] a FROM [bhe_1] a
RIGHT JOIN ( RIGHT JOIN (
SELECT DISTINCT a.[Id] SELECT DISTINCT a.[Id]
@ -115,7 +115,28 @@ RIGHT JOIN (
WHERE (bi.[IsDeleted] = 0)) htb ON a.[Id] = htb.[HeadId] WHERE (bi.[IsDeleted] = 0)) htb ON a.[Id] = htb.[HeadId]
WHERE (a.[IsDeleted] = 0) ) a WHERE (a.[IsDeleted] = 0) ) a
WHERE (a.[RefQuantity] < a.[Quantity]) ) htb ON a.[Id] = htb.[Id] WHERE (a.[RefQuantity] < a.[Quantity]) ) htb ON a.[Id] = htb.[Id]
ORDER BY a.[Date] DESC;", sql3); ORDER BY a.[Date] DESC", sql3);
fsql.Select<BaseHeadEntity>().AsType(typeof(BhEntity1))
.FromQuery(
fsql.Select<BaseHeadEntity>().AsType(typeof(BhEntity1)).Where(bh => bh.IsDeleted == false)
.FromQuery(fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity1)).As("bi").Where(bi => bi.IsDeleted == false))
.InnerJoin(v => v.t1.Id == v.t2.HeadId)
.WithTempQuery(v => new
{
BillHead = v.t1,
Quantity = v.t2.Quantity,
RefQuantity = fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity2)).As("bi2")
.Where(ti2 => ti2.RefHeadId == v.t2.HeadId && ti2.RefItemId == v.t2.Id)
.Sum(ti2 => ti2.Quantity),
})
.Where(v => v.RefQuantity < v.Quantity)
.Distinct()
.WithTempQuery(v => new { v.BillHead.Id })
)
.RightJoin(v => v.t1.Id == v.t2.Id)
.OrderByDescending(v => v.t1.Date)
.ToList();
} }
[Fact] [Fact]

View File

@ -1119,6 +1119,7 @@ namespace FreeSql.Internal
Select0Provider fsqlSelect0 = null; Select0Provider fsqlSelect0 = null;
List<SelectTableInfo> fsqltables = null; List<SelectTableInfo> fsqltables = null;
var fsqltable1SetAlias = false; var fsqltable1SetAlias = false;
var fsqltable1SetAliasGai = 0;
Type fsqlType = null; Type fsqlType = null;
Stack<Expression> asSelectBefores = new Stack<Expression>(); Stack<Expression> asSelectBefores = new Stack<Expression>();
var asSelectSql = ""; var asSelectSql = "";
@ -1279,16 +1280,16 @@ namespace FreeSql.Internal
if (argExpLambda.Parameters.Count == 1 && argExpLambda.Parameters[0].Type.FullName.StartsWith("FreeSql.Internal.Model.HzyTuple`")) if (argExpLambda.Parameters.Count == 1 && argExpLambda.Parameters[0].Type.FullName.StartsWith("FreeSql.Internal.Model.HzyTuple`"))
{ {
for (var gai = 0; gai < fsqlTypeGenericArgs.Length; gai++) for (; fsqltable1SetAliasGai < fsqlTypeGenericArgs.Length; fsqltable1SetAliasGai++)
fsqltables[gai].Alias = "ht" + (gai + 1); fsqltables[fsqltable1SetAliasGai].Alias = "ht" + (fsqltable1SetAliasGai + 1);
} }
else else
{ {
for (var gai = 0; gai < fsqlTypeGenericArgs.Length && gai < argExpLambda.Parameters.Count; gai++) for (; fsqltable1SetAliasGai < fsqlTypeGenericArgs.Length && fsqltable1SetAliasGai < argExpLambda.Parameters.Count; fsqltable1SetAliasGai++)
{ {
var alias = argExpLambda.Parameters[gai].Name; var alias = argExpLambda.Parameters[fsqltable1SetAliasGai].Name;
if (fsqltables.Any(x => x.Type == SelectTableInfoType.Parent && x.Alias == alias)) alias = $"sub_{alias}"; if (fsqltables.Any(x => x.Type == SelectTableInfoType.Parent && x.Alias == alias)) alias = $"sub_{alias}";
fsqltables[gai].Alias = alias; fsqltables[fsqltable1SetAliasGai].Alias = alias;
} }
} }
} }
@ -1317,10 +1318,11 @@ namespace FreeSql.Internal
case nameof(ISelect<object>.WithTempQuery): case nameof(ISelect<object>.WithTempQuery):
fsql = method.Invoke(fsql, args); fsql = method.Invoke(fsql, args);
fsqlType = fsql.GetType(); fsqlType = fsql.GetType();
fsqlSelect0 = fsql as Select0Provider; fsqlSelect0 = fsql as Select0Provider;
if (tsc.dbParams != null) fsqlSelect0._params = tsc.dbParams; if (tsc.dbParams != null) fsqlSelect0._params = tsc.dbParams;
fsqltables = fsqlSelect0._tables; fsqltables = fsqlSelect0._tables;
fsqltable1SetAlias = false; fsqltable1SetAlias = false;
if (method.Name == nameof(ISelect<object>.WithTempQuery)) fsqltable1SetAliasGai = 0;
break; break;
default: default:
method.Invoke(fsql, args); method.Invoke(fsql, args);

View File

@ -207,7 +207,8 @@ And other friends who made important suggestions for this project, they include:
[feijie999](https://github.com/feijie999), [feijie999](https://github.com/feijie999),
constantine, constantine,
[JohnZhou2020](https://github.com/JohnZhou2020), [JohnZhou2020](https://github.com/JohnZhou2020),
[mafeng8](https://github.com/mafeng8), etc. [mafeng8](https://github.com/mafeng8),
[VicBilibily](https://github.com/VicBilibily), etc.
## 💕 Donation ## 💕 Donation

View File

@ -210,7 +210,8 @@ Elapsed: 00:00:00.6495301; Query Entity Counts: 131072; ORM: Dapper
[feijie999](https://github.com/feijie999), [feijie999](https://github.com/feijie999),
constantine, constantine,
[JohnZhou2020](https://github.com/JohnZhou2020), [JohnZhou2020](https://github.com/JohnZhou2020),
[mafeng8](https://github.com/mafeng8) 等。 [mafeng8](https://github.com/mafeng8),
[VicBilibily](https://github.com/VicBilibily) 等。
## 💕 Donation (捐赠) ## 💕 Donation (捐赠)