- 修复 子查询使用基类+AsType 可能产生的 bug;#1215

This commit is contained in:
2881099 2022-08-14 00:04:35 +08:00
parent 064cb82cf0
commit f49c5f2827
4 changed files with 91 additions and 11 deletions

View File

@ -317,8 +317,20 @@ namespace base_entity
public Guid Id { get; set; }
public string[] Tags { get; init; } = Array.Empty<string>();
}
class BaseModel<T>
{
public static int fsql;
}
static void Main(string[] args)
{
BaseModel<User1>.fsql = 1;
BaseModel<UserGroup>.fsql = 2;
Console.WriteLine(BaseModel<User1>.fsql);
Console.WriteLine(BaseModel<UserGroup>.fsql);
#region IFreeSql
var fsql = new FreeSql.FreeSqlBuilder()
.UseAutoSyncStructure(true)

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

@ -7,6 +7,78 @@ namespace FreeSql.Tests.SqlServer
{
public class SqlServerSelectWithTempQueryTest
{
#region issues #1215
[Fact]
public void VicDemo20220813()
{
var fsql = g.sqlserver;
var id = Guid.Parse("62f83a6d-eb53-0608-0097-d177142cadcb");
var sql1 = fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity1)).As("bi")
.Where(bi => bi.HeadId == id && bi.IsDeleted == false)
.Where(bi => fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity2)).As("ti")
.Where(ti => ti.RefHeadId == bi.HeadId && ti.RefItemId == bi.Id)
.Sum(ti => ti.Quantity) <= bi.Quantity)
.ToSql();
Assert.Equal(@"SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId]
FROM [bie_1] bi
WHERE (bi.[HeadId] = '62f83a6d-eb53-0608-0097-d177142cadcb' AND bi.[IsDeleted] = 0) AND (isnull((SELECT sum(ti.[Quantity])
FROM [bie_2] ti
WHERE (ti.[RefHeadId] = bi.[HeadId] AND ti.[RefItemId] = bi.[Id])), 0) <= bi.[Quantity])", sql1);
var sql2 = fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity1)).As("bi")
.Where(bi => bi.HeadId == id && bi.IsDeleted == false)
.Where(bi => bi.HeadId == id && bi.IsDeleted == false)
.WithTempQuery(bi => new
{
bi.Id,
BillItem = bi,
bi.Quantity,
RefQuantity = fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity2)).As("ti")
.Where(ti => ti.RefHeadId == bi.HeadId && ti.RefItemId == bi.Id)
.Sum(ti => ti.Quantity),
})
.Where(v => v.RefQuantity < v.Quantity)
.ToSql();
Assert.Equal(@"SELECT *
FROM (
SELECT bi.[Id], bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId], bi.[Quantity], isnull((SELECT sum(ti.[Quantity])
FROM [bie_2] ti
WHERE (ti.[RefHeadId] = bi.[HeadId] AND ti.[RefItemId] = bi.[Id])), 0) [RefQuantity]
FROM [bie_1] bi
WHERE (bi.[HeadId] = '62f83a6d-eb53-0608-0097-d177142cadcb' AND bi.[IsDeleted] = 0) AND (bi.[HeadId] = '62f83a6d-eb53-0608-0097-d177142cadcb' AND bi.[IsDeleted] = 0) ) a
WHERE (a.[RefQuantity] < a.[Quantity])", sql2);
}
abstract class SoftDelete
{
public bool IsDeleted { get; set; }
}
abstract class BaseHeadEntity : SoftDelete
{
public Guid Id { get; set; }
public string No { get; set; }
public DateTime Date { get; set; }
}
[Table(Name = "bhe_1")]
class BhEntity1 : BaseHeadEntity { }
[Table(Name = "bhe_2")]
class BhEntity2 : BaseHeadEntity { }
abstract class BaseItemEntity : SoftDelete
{
public Guid Id { get; set; }
public Guid HeadId { get; set; }
public int GoodsId { get; set; }
public decimal Quantity { get; set; }
public Guid? RefHeadId { get; set; }
public Guid? RefItemId { get; set; }
}
[Table(Name = "bie_1")]
class BiEntity1 : BaseItemEntity { }
[Table(Name = "bie_2")]
class BiEntity2 : BaseItemEntity { }
#endregion
[Fact]
public void SingleTablePartitionBy()
{

View File

@ -1765,6 +1765,10 @@ namespace FreeSql.Internal
finds = tsc._tables.Where(a2 => (isa && a2.Parameter != null || isa && a2.Parameter == null) &&
a2.Table.Type == tbtmp.Type && a2.Alias == alias).ToArray();
if (finds.Length != 1)
{
finds = tsc._tables.Where(a2 => (isa && a2.Parameter != null || isa && a2.Parameter == null) &&
tbtmp.Type.IsAssignableFrom(a2.Table.Type) && a2.Alias == alias).ToArray();
if (finds.Length != 1)
{
finds = tsc._tables.Where(a2 => (isa && a2.Parameter != null || isa && a2.Parameter == null) &&
a2.Table.Type == tbtmp.Type).ToArray();
@ -1777,6 +1781,7 @@ namespace FreeSql.Internal
}
}
}
}
//finds = tsc._tables.Where((a2, c2) => (isa || a2.Parameter == null) && a2.Table.CsName == tbtmp.CsName && (isthis && a2.Type != SelectTableInfoType.Parent || !isthis)).ToArray(); //外部表,内部表一起查
//if (finds.Length > 1) {
// finds = tsc._tables.Where((a2, c2) => (isa || a2.Parameter == null) && a2.Table.CsName == tbtmp.CsName && a2.Type == SelectTableInfoType.Parent && a2.Alias == alias).ToArray(); //查询外部表