- 修复 子查询内使用嵌套查询 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>
<returns></returns>
</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>
</doc>

View File

@ -34,14 +34,14 @@ namespace FreeSql.Tests.SqlServer
FROM [bhe_1] a
WHERE (a.Id IN (SELECT a.[Id] as1
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
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
INNER JOIN (
SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId]
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.[RefQuantity] < a.[Quantity])
GROUP BY a.[Id]))
@ -68,14 +68,14 @@ ORDER BY a.[Date] DESC", sql1);
FROM [bhe_1] a
WHERE (((a.[Id]) in (SELECT DISTINCT v.[Id]
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
WHERE (ti2.[RefHeadId] = ht2.[HeadId] AND ti2.[RefItemId] = ht2.[Id])), 0) [RefQuantity]
FROM [bhe_1] ht1
FROM [bhe_1] bh
INNER JOIN (
SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId]
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 (v.[RefQuantity] < v.[Quantity]))))
ORDER BY a.[Date] DESC", sql2);
@ -99,8 +99,8 @@ ORDER BY a.[Date] DESC", sql2);
)
.RightJoin(v => v.t1.Id == v.t2.Id)
.OrderByDescending(v => v.t1.Date)
.ToSql();
Assert.Equal(@"SELECT *
.ToSql(v => v.t1);
Assert.Equal(@"SELECT a.[IsDeleted] as1, a.[Id] as2, a.[No] as3, a.[Date] as4
FROM [bhe_1] a
RIGHT JOIN (
SELECT DISTINCT a.[Id]
@ -115,7 +115,28 @@ RIGHT JOIN (
WHERE (bi.[IsDeleted] = 0)) htb ON a.[Id] = htb.[HeadId]
WHERE (a.[IsDeleted] = 0) ) a
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]

View File

@ -1119,6 +1119,7 @@ namespace FreeSql.Internal
Select0Provider fsqlSelect0 = null;
List<SelectTableInfo> fsqltables = null;
var fsqltable1SetAlias = false;
var fsqltable1SetAliasGai = 0;
Type fsqlType = null;
Stack<Expression> asSelectBefores = new Stack<Expression>();
var asSelectSql = "";
@ -1279,16 +1280,16 @@ namespace FreeSql.Internal
if (argExpLambda.Parameters.Count == 1 && argExpLambda.Parameters[0].Type.FullName.StartsWith("FreeSql.Internal.Model.HzyTuple`"))
{
for (var gai = 0; gai < fsqlTypeGenericArgs.Length; gai++)
fsqltables[gai].Alias = "ht" + (gai + 1);
for (; fsqltable1SetAliasGai < fsqlTypeGenericArgs.Length; fsqltable1SetAliasGai++)
fsqltables[fsqltable1SetAliasGai].Alias = "ht" + (fsqltable1SetAliasGai + 1);
}
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}";
fsqltables[gai].Alias = alias;
fsqltables[fsqltable1SetAliasGai].Alias = alias;
}
}
}
@ -1321,6 +1322,7 @@ namespace FreeSql.Internal
if (tsc.dbParams != null) fsqlSelect0._params = tsc.dbParams;
fsqltables = fsqlSelect0._tables;
fsqltable1SetAlias = false;
if (method.Name == nameof(ISelect<object>.WithTempQuery)) fsqltable1SetAliasGai = 0;
break;
default:
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),
constantine,
[JohnZhou2020](https://github.com/JohnZhou2020),
[mafeng8](https://github.com/mafeng8), etc.
[mafeng8](https://github.com/mafeng8),
[VicBilibily](https://github.com/VicBilibily), etc.
## 💕 Donation

View File

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