mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
- 修复 子查询使用基类+AsType 可能产生的 bug;#1215
This commit is contained in:
parent
064cb82cf0
commit
f49c5f2827
@ -317,8 +317,20 @@ namespace base_entity
|
|||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public string[] Tags { get; init; } = Array.Empty<string>();
|
public string[] Tags { get; init; } = Array.Empty<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class BaseModel<T>
|
||||||
|
{
|
||||||
|
public static int fsql;
|
||||||
|
}
|
||||||
|
|
||||||
static void Main(string[] args)
|
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
|
#region 初始化 IFreeSql
|
||||||
var fsql = new FreeSql.FreeSqlBuilder()
|
var fsql = new FreeSql.FreeSqlBuilder()
|
||||||
.UseAutoSyncStructure(true)
|
.UseAutoSyncStructure(true)
|
||||||
|
@ -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>
|
||||||
|
@ -7,6 +7,78 @@ namespace FreeSql.Tests.SqlServer
|
|||||||
{
|
{
|
||||||
public class SqlServerSelectWithTempQueryTest
|
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]
|
[Fact]
|
||||||
public void SingleTablePartitionBy()
|
public void SingleTablePartitionBy()
|
||||||
{
|
{
|
||||||
|
@ -1765,6 +1765,10 @@ namespace FreeSql.Internal
|
|||||||
finds = tsc._tables.Where(a2 => (isa && a2.Parameter != null || isa && a2.Parameter == null) &&
|
finds = tsc._tables.Where(a2 => (isa && a2.Parameter != null || isa && a2.Parameter == null) &&
|
||||||
a2.Table.Type == tbtmp.Type && a2.Alias == alias).ToArray();
|
a2.Table.Type == tbtmp.Type && a2.Alias == alias).ToArray();
|
||||||
if (finds.Length != 1)
|
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) &&
|
finds = tsc._tables.Where(a2 => (isa && a2.Parameter != null || isa && a2.Parameter == null) &&
|
||||||
a2.Table.Type == tbtmp.Type).ToArray();
|
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(); //外部表,内部表一起查
|
//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) {
|
//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(); //查询外部表
|
// finds = tsc._tables.Where((a2, c2) => (isa || a2.Parameter == null) && a2.Table.CsName == tbtmp.CsName && a2.Type == SelectTableInfoType.Parent && a2.Alias == alias).ToArray(); //查询外部表
|
||||||
|
Loading…
x
Reference in New Issue
Block a user