mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
Merge branch 'master' of https://github.com/dotnetcore/FreeSql
This commit is contained in:
commit
243eb4ae91
2
.github/workflows/docfx.yml
vendored
2
.github/workflows/docfx.yml
vendored
@ -14,7 +14,7 @@ jobs:
|
||||
- name: Setup .NET Core
|
||||
uses: actions/setup-dotnet@v1
|
||||
with:
|
||||
dotnet-version: 6.0.x
|
||||
dotnet-version: 7.0.x
|
||||
- name: Remove Examples
|
||||
run: dotnet sln FreeSql.sln remove (ls -r Examples/**/*.csproj)
|
||||
- name: Remove FreeSql.Tests
|
||||
|
@ -491,6 +491,17 @@ namespace base_entity
|
||||
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
|
||||
#endregion
|
||||
|
||||
var sqlv01 = fsql.Select<BaseDataEntity>().AsType(typeof(GoodsData))
|
||||
.ToSql(v => new GoodsDataDTO()
|
||||
{
|
||||
Id = v.Id,
|
||||
GoodsNo = v.Code,
|
||||
GoodsName = v.Name,
|
||||
});
|
||||
// 解析会连带导出 CategoryId ,但是如果查询别名不是 a 时就会重置到基类表
|
||||
// SELECT a.`CategoryId` as1, v.`Id` as2, v.`Code` as3, v.`Name` as4
|
||||
// FROM `FreeSqlTest`.`bdd_1` a, `BaseDataEntity` v
|
||||
|
||||
var groupsql01 = fsql.Select<User1>()
|
||||
.GroupBy(a => new
|
||||
{
|
||||
@ -1043,7 +1054,8 @@ namespace base_entity
|
||||
.Where((a, b) => a.IsDeleted == false)
|
||||
.ToSql((a, b) => new
|
||||
{
|
||||
user = a, group = b
|
||||
user = a,
|
||||
group = b
|
||||
});
|
||||
sql1 = sql1.Replace("INNER JOIN ", "FULL JOIN ");
|
||||
|
||||
@ -1100,7 +1112,8 @@ namespace base_entity
|
||||
groups11 = fsql.Select<UserGroup>().Where(c => c.Id == b.GroupId).ToList(),
|
||||
groups22 = fsql.Select<UserGroup>().Where(c => c.Id == b.GroupId).ToList(c => new
|
||||
{
|
||||
c.Id, c.GroupName,
|
||||
c.Id,
|
||||
c.GroupName,
|
||||
username = b.Username,
|
||||
})
|
||||
}),
|
||||
@ -1805,4 +1818,26 @@ namespace base_entity
|
||||
[Column(Name = "更新时间", CanInsert = false, CanUpdate = true, ServerTime = DateTimeKind.Local)]
|
||||
public DateTime 更新时间 { get; set; }
|
||||
}
|
||||
|
||||
abstract class BaseDataEntity
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public virtual int CategoryId { get; set; }
|
||||
public virtual string Code { get; set; }
|
||||
public virtual string Name { get; set; }
|
||||
}
|
||||
[Table(Name = "`FreeSqlTest`.`bdd_1`")]
|
||||
class GoodsData : BaseDataEntity
|
||||
{
|
||||
public override Int32 CategoryId { get; set; }
|
||||
public override string Code { get; set; }
|
||||
public override string Name { get; set; }
|
||||
}
|
||||
class GoodsDataDTO
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public int CategoryId { get; set; }
|
||||
public string GoodsNo { get; set; }
|
||||
public string GoodsName { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -53,6 +53,9 @@ public static class FreeSqlJsonMapCoreExtensions
|
||||
var isJsonMap = e.Property.GetCustomAttributes(typeof(JsonMapAttribute), false).Any() || _dicJsonMapFluentApi.TryGetValue(e.EntityType, out var tryjmfu) && tryjmfu.ContainsKey(e.Property.Name);
|
||||
if (isJsonMap)
|
||||
{
|
||||
if (FreeSql.Internal.Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(e.Property.PropertyType))
|
||||
return; //基础类型使用 JsonMap 无效
|
||||
|
||||
e.ModifyResult.MapType = typeof(string);
|
||||
e.ModifyResult.StringLength = -2;
|
||||
if (_dicTypes.TryAdd(e.Property.PropertyType, true))
|
||||
|
@ -733,6 +733,15 @@
|
||||
<param name="modelBuilder"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlDbContextExtensions.ApplyConfigurationsFromAssembly(FreeSql.ICodeFirst,System.Reflection.Assembly,System.Func{System.Type,System.Boolean})">
|
||||
<summary>
|
||||
根据Assembly扫描所有继承IEntityTypeConfiguration<T>的配置类
|
||||
</summary>
|
||||
<param name="codeFirst"></param>
|
||||
<param name="assembly"></param>
|
||||
<param name="predicate"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlDbContextExtensions.CreateDbContext(IFreeSql)">
|
||||
<summary>
|
||||
创建普通数据上下文档对象
|
||||
@ -791,5 +800,14 @@
|
||||
<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>
|
||||
|
@ -54,16 +54,16 @@ namespace FreeSql.Tests.PostgreSQL
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
var sql = insert.AppendData(items.First()).ToSql();
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert\"(\"clicks\", \"title\", \"createtime\") VALUES(@Clicks_0, @Title_0, @CreateTime_0)", sql);
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert2\"(\"clicks\", \"title\", \"createtime\") VALUES(@Clicks_0, @Title_0, @CreateTime_0)", sql);
|
||||
|
||||
sql = insert.AppendData(items).ToSql();
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert\"(\"clicks\", \"title\", \"createtime\") VALUES(@Clicks_0, @Title_0, @CreateTime_0), (@Clicks_1, @Title_1, @CreateTime_1), (@Clicks_2, @Title_2, @CreateTime_2), (@Clicks_3, @Title_3, @CreateTime_3), (@Clicks_4, @Title_4, @CreateTime_4), (@Clicks_5, @Title_5, @CreateTime_5), (@Clicks_6, @Title_6, @CreateTime_6), (@Clicks_7, @Title_7, @CreateTime_7), (@Clicks_8, @Title_8, @CreateTime_8), (@Clicks_9, @Title_9, @CreateTime_9)", sql);
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert2\"(\"clicks\", \"title\", \"createtime\") VALUES(@Clicks_0, @Title_0, @CreateTime_0), (@Clicks_1, @Title_1, @CreateTime_1), (@Clicks_2, @Title_2, @CreateTime_2), (@Clicks_3, @Title_3, @CreateTime_3), (@Clicks_4, @Title_4, @CreateTime_4), (@Clicks_5, @Title_5, @CreateTime_5), (@Clicks_6, @Title_6, @CreateTime_6), (@Clicks_7, @Title_7, @CreateTime_7), (@Clicks_8, @Title_8, @CreateTime_8), (@Clicks_9, @Title_9, @CreateTime_9)", sql);
|
||||
|
||||
sql = insert.AppendData(items).InsertColumns(a => a.Title).ToSql();
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert\"(\"title\") VALUES(@Title_0), (@Title_1), (@Title_2), (@Title_3), (@Title_4), (@Title_5), (@Title_6), (@Title_7), (@Title_8), (@Title_9)", sql);
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert2\"(\"title\") VALUES(@Title_0), (@Title_1), (@Title_2), (@Title_3), (@Title_4), (@Title_5), (@Title_6), (@Title_7), (@Title_8), (@Title_9)", sql);
|
||||
|
||||
sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).ToSql();
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert\"(\"clicks\", \"title\") VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert2\"(\"clicks\", \"title\") VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -73,10 +73,10 @@ namespace FreeSql.Tests.PostgreSQL
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
var sql = insert.AppendData(items).InsertColumns(a => a.Title).ToSql();
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert\"(\"title\") VALUES(@Title_0), (@Title_1), (@Title_2), (@Title_3), (@Title_4), (@Title_5), (@Title_6), (@Title_7), (@Title_8), (@Title_9)", sql);
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert2\"(\"title\") VALUES(@Title_0), (@Title_1), (@Title_2), (@Title_3), (@Title_4), (@Title_5), (@Title_6), (@Title_7), (@Title_8), (@Title_9)", sql);
|
||||
|
||||
sql = insert.AppendData(items).InsertColumns(a => new { a.Title, a.Clicks }).ToSql();
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert\"(\"clicks\", \"title\") VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert2\"(\"clicks\", \"title\") VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
|
||||
}
|
||||
[Fact]
|
||||
public void IgnoreColumns()
|
||||
@ -85,10 +85,10 @@ namespace FreeSql.Tests.PostgreSQL
|
||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
||||
|
||||
var sql = insert.AppendData(items).IgnoreColumns(a => a.CreateTime).ToSql();
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert\"(\"clicks\", \"title\") VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert2\"(\"clicks\", \"title\") VALUES(@Clicks_0, @Title_0), (@Clicks_1, @Title_1), (@Clicks_2, @Title_2), (@Clicks_3, @Title_3), (@Clicks_4, @Title_4), (@Clicks_5, @Title_5), (@Clicks_6, @Title_6), (@Clicks_7, @Title_7), (@Clicks_8, @Title_8), (@Clicks_9, @Title_9)", sql);
|
||||
|
||||
sql = insert.AppendData(items).IgnoreColumns(a => new { a.Title, a.CreateTime }).ToSql();
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert\"(\"clicks\") VALUES(@Clicks_0), (@Clicks_1), (@Clicks_2), (@Clicks_3), (@Clicks_4), (@Clicks_5), (@Clicks_6), (@Clicks_7), (@Clicks_8), (@Clicks_9)", sql);
|
||||
Assert.Equal("INSERT INTO \"tb_topic_insert2\"(\"clicks\") VALUES(@Clicks_0), (@Clicks_1), (@Clicks_2), (@Clicks_3), (@Clicks_4), (@Clicks_5), (@Clicks_6), (@Clicks_7), (@Clicks_8), (@Clicks_9)", sql);
|
||||
|
||||
g.pgsql.Delete<TopicIgnore>().Where("1=1").ExecuteAffrows();
|
||||
var itemsIgnore = new List<TopicIgnore>();
|
||||
|
@ -40,7 +40,7 @@ FROM (
|
||||
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]
|
||||
SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId], bi.[RefItemComId]
|
||||
FROM [bie_1] bi
|
||||
WHERE (bi.[IsDeleted] = 0)) htb ON a.[Id] = htb.[HeadId]
|
||||
WHERE (a.[IsDeleted] = 0) ) a
|
||||
@ -74,7 +74,7 @@ WHERE (((a.[Id]) in (SELECT DISTINCT v.[Id]
|
||||
WHERE (ti2.[RefHeadId] = ht2.[HeadId] AND ti2.[RefItemId] = ht2.[Id])), 0) [RefQuantity]
|
||||
FROM [bhe_1] bh
|
||||
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], bi.[RefItemComId]
|
||||
FROM [bie_1] bi
|
||||
WHERE (bi.[IsDeleted] = 0)) ht2 ON bh.[Id] = ht2.[HeadId]
|
||||
WHERE (bh.[IsDeleted] = 0) ) v
|
||||
@ -111,7 +111,7 @@ RIGHT JOIN (
|
||||
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]
|
||||
SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId], bi.[RefItemComId]
|
||||
FROM [bie_1] bi
|
||||
WHERE (bi.[IsDeleted] = 0)) htb ON a.[Id] = htb.[HeadId]
|
||||
WHERE (a.[IsDeleted] = 0) ) a
|
||||
@ -302,7 +302,7 @@ ORDER BY a.[Date] DESC", sql3);
|
||||
.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]
|
||||
Assert.Equal(@"SELECT bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId], bi.[RefItemComId]
|
||||
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
|
||||
@ -324,7 +324,7 @@ WHERE (bi.[HeadId] = '62f83a6d-eb53-0608-0097-d177142cadcb' AND bi.[IsDeleted] =
|
||||
.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])
|
||||
SELECT bi.[Id], bi.[IsDeleted], bi.[Id], bi.[HeadId], bi.[GoodsId], bi.[Quantity], bi.[RefHeadId], bi.[RefItemId], bi.[RefItemComId], 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
|
||||
@ -375,6 +375,7 @@ WHERE (a.[RefQuantity] < a.[Quantity])", sql2);
|
||||
public decimal Quantity { get; set; }
|
||||
public Guid? RefHeadId { get; set; }
|
||||
public Guid? RefItemId { get; set; }
|
||||
public Guid? RefItemComId { get; set; }
|
||||
}
|
||||
[Table(Name = "bie_1")]
|
||||
class BiEntity1 : BaseItemEntity { }
|
||||
@ -382,6 +383,116 @@ WHERE (a.[RefQuantity] < a.[Quantity])", sql2);
|
||||
class BiEntity2 : BaseItemEntity { }
|
||||
#endregion
|
||||
|
||||
#region 2022/12/30
|
||||
abstract class BaseItemComEntity : SoftDelete
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid HeadId { get; set; }
|
||||
public Guid ItemId { get; set; }
|
||||
public int GoodsId { get; set; }
|
||||
public int ComponentId { get; set; }
|
||||
public decimal Quantity { get; set; }
|
||||
public int Level { get; set; }
|
||||
}
|
||||
[Table(Name = "bic_1")]
|
||||
class BicEntity1 : BaseItemComEntity { }
|
||||
[Fact]
|
||||
public void VicDemo20221230()
|
||||
{
|
||||
var fsql = g.mysql;
|
||||
// 逻辑相同 sql1 能够生成 sql2 就会报类型转换错误
|
||||
var sql1 = fsql.Select<BaseItemComEntity>().AsType(typeof(BicEntity1)).As("bic")
|
||||
.Where(bic => bic.Level == 0)
|
||||
.FromQuery(fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity1)).As("bi"))
|
||||
.InnerJoin((bic, bi) => bic.ItemId == bi.Id)
|
||||
.Where((bic, bi) => bi.IsDeleted == false)
|
||||
.WithTempQuery((bic, _) => bic)
|
||||
.FromQuery(fsql.Select<BaseHeadEntity>().AsType(typeof(BhEntity1)).As("bh"))
|
||||
.InnerJoin((bic, bh) => bic.HeadId == bh.Id)
|
||||
.Where((bic, bh) => bh.IsDeleted == false)
|
||||
.WithTempQuery((bic, _) => new
|
||||
{
|
||||
BillItemCom = bic,
|
||||
RefQuantity = fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity1)).As("ti")
|
||||
.Where(ti => ti.RefHeadId == bic.HeadId)
|
||||
.Where(ti => ti.RefItemId == bic.ItemId)
|
||||
.Where(ti => ti.RefItemComId == bic.Id)
|
||||
.Where(ti => ti.IsDeleted == false)
|
||||
.FromQuery(fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity1)))
|
||||
.InnerJoin(ti => ti.t1.HeadId == ti.t2.Id)
|
||||
.Where(ti => ti.t2.IsDeleted == false)
|
||||
.Sum(ti => ti.t1.Quantity),
|
||||
})
|
||||
.Where(v => v.RefQuantity < v.BillItemCom.Quantity)
|
||||
.ToSql();
|
||||
Assert.Equal(@"SELECT *
|
||||
FROM (
|
||||
SELECT a.`IsDeleted`, a.`Id`, a.`HeadId`, a.`ItemId`, a.`GoodsId`, a.`ComponentId`, a.`Quantity`, a.`Level`, ifnull((SELECT sum(ti.`Quantity`)
|
||||
FROM `bie_1` ti
|
||||
INNER JOIN (
|
||||
SELECT a.`IsDeleted`, a.`Id`, a.`HeadId`, a.`GoodsId`, a.`Quantity`, a.`RefHeadId`, a.`RefItemId`, a.`RefItemComId`
|
||||
FROM `bie_1` a) ht2 ON ti.`HeadId` = ht2.`Id`
|
||||
WHERE (ti.`RefHeadId` = a.`HeadId`) AND (ti.`RefItemId` = a.`ItemId`) AND (ti.`RefItemComId` = a.`Id`) AND (ti.`IsDeleted` = 0) AND (ht2.`IsDeleted` = 0)), 0) `RefQuantity`
|
||||
FROM (
|
||||
SELECT bic.`IsDeleted`, bic.`Id`, bic.`HeadId`, bic.`ItemId`, bic.`GoodsId`, bic.`ComponentId`, bic.`Quantity`, bic.`Level`
|
||||
FROM `bic_1` bic
|
||||
INNER JOIN (
|
||||
SELECT bi.`IsDeleted`, bi.`Id`, bi.`HeadId`, bi.`GoodsId`, bi.`Quantity`, bi.`RefHeadId`, bi.`RefItemId`, bi.`RefItemComId`
|
||||
FROM `bie_1` bi) bi ON bic.`ItemId` = bi.`Id`
|
||||
WHERE (bic.`Level` = 0) AND (bi.`IsDeleted` = 0) ) a
|
||||
INNER JOIN (
|
||||
SELECT bh.`IsDeleted`, bh.`Id`, bh.`No`, bh.`Date`
|
||||
FROM `bhe_1` bh) bh ON a.`HeadId` = bh.`Id`
|
||||
WHERE (bh.`IsDeleted` = 0) ) a
|
||||
WHERE (a.`RefQuantity` < a.`Quantity`)", sql1);
|
||||
|
||||
var sql2 = fsql.Select<BaseItemComEntity>().AsType(typeof(BicEntity1))
|
||||
.Where(v => v.Level == 0)
|
||||
.FromQuery(fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity1)))
|
||||
.InnerJoin(v => v.t1.ItemId == v.t2.Id)
|
||||
.Where(v => v.t2.IsDeleted == false)
|
||||
.WithTempQuery(v => v.t1)
|
||||
.FromQuery(fsql.Select<BaseHeadEntity>().AsType(typeof(BhEntity1)))
|
||||
.InnerJoin(v => v.t1.HeadId == v.t2.Id)
|
||||
.Where(v => v.t2.IsDeleted == false)
|
||||
.WithTempQuery(v => new
|
||||
{
|
||||
BillItemCom = v.t1,
|
||||
RefQuantity = fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity1)).As("ti")
|
||||
.Where(ti => ti.RefHeadId == v.t1.HeadId)
|
||||
.Where(ti => ti.RefItemId == v.t1.ItemId)
|
||||
.Where(ti => ti.RefItemComId == v.t1.Id)
|
||||
.Where(ti => ti.IsDeleted == false)
|
||||
.FromQuery(fsql.Select<BaseItemEntity>().AsType(typeof(BiEntity1)))
|
||||
.InnerJoin(ti => ti.t1.HeadId == ti.t2.Id)
|
||||
.Where(ti => ti.t2.IsDeleted == false)
|
||||
.Sum(ti => ti.t1.Quantity),
|
||||
})
|
||||
.Where(v => v.RefQuantity < v.BillItemCom.Quantity)
|
||||
.ToSql();
|
||||
Assert.Equal(@"SELECT *
|
||||
FROM (
|
||||
SELECT a.`IsDeleted`, a.`Id`, a.`HeadId`, a.`ItemId`, a.`GoodsId`, a.`ComponentId`, a.`Quantity`, a.`Level`, ifnull((SELECT sum(ti.`Quantity`)
|
||||
FROM `bie_1` ti
|
||||
INNER JOIN (
|
||||
SELECT a.`IsDeleted`, a.`Id`, a.`HeadId`, a.`GoodsId`, a.`Quantity`, a.`RefHeadId`, a.`RefItemId`, a.`RefItemComId`
|
||||
FROM `bie_1` a) ht2 ON ti.`HeadId` = ht2.`Id`
|
||||
WHERE (ti.`RefHeadId` = a.`HeadId`) AND (ti.`RefItemId` = a.`ItemId`) AND (ti.`RefItemComId` = a.`Id`) AND (ti.`IsDeleted` = 0) AND (ht2.`IsDeleted` = 0)), 0) `RefQuantity`
|
||||
FROM (
|
||||
SELECT a.`IsDeleted`, a.`Id`, a.`HeadId`, a.`ItemId`, a.`GoodsId`, a.`ComponentId`, a.`Quantity`, a.`Level`
|
||||
FROM `bic_1` a
|
||||
INNER JOIN (
|
||||
SELECT a.`IsDeleted`, a.`Id`, a.`HeadId`, a.`GoodsId`, a.`Quantity`, a.`RefHeadId`, a.`RefItemId`, a.`RefItemComId`
|
||||
FROM `bie_1` a) htb ON a.`ItemId` = htb.`Id`
|
||||
WHERE (a.`Level` = 0) AND (htb.`IsDeleted` = 0) ) a
|
||||
INNER JOIN (
|
||||
SELECT a.`IsDeleted`, a.`Id`, a.`No`, a.`Date`
|
||||
FROM `bhe_1` a) htb ON a.`HeadId` = htb.`Id`
|
||||
WHERE (htb.`IsDeleted` = 0) ) a
|
||||
WHERE (a.`RefQuantity` < a.`Quantity`)", sql2);
|
||||
}
|
||||
#endregion
|
||||
|
||||
[Fact]
|
||||
public void SingleTablePartitionBy()
|
||||
{
|
||||
|
@ -1715,7 +1715,12 @@ namespace FreeSql.Internal
|
||||
if (oper2.NodeType == ExpressionType.Parameter)
|
||||
{
|
||||
var oper2Parm = oper2 as ParameterExpression;
|
||||
expStack.Push(exp2.Type.IsAbstract || exp2.Type.IsInterface || exp2.Type.IsAssignableFrom(oper2Parm.Type) ? oper2Parm : Expression.Parameter(exp2.Type, oper2Parm.Name));
|
||||
if (exp2.Type.IsAbstract || exp2.Type.IsInterface || exp2.Type.IsAssignableFrom(oper2Parm.Type))
|
||||
expStack.Push(oper2Parm);
|
||||
else if (oper2Parm.Type != typeof(object) && oper2Parm.Type.IsAssignableFrom(exp2.Type))
|
||||
expStack.Push(oper2Parm);
|
||||
else
|
||||
expStack.Push(Expression.Parameter(exp2.Type, oper2Parm.Name));
|
||||
}
|
||||
else
|
||||
expStack.Push(oper2);
|
||||
@ -2315,12 +2320,14 @@ namespace FreeSql.Internal
|
||||
{
|
||||
private List<SelectTableInfo> tables;
|
||||
private ParameterExpression[] parameters;
|
||||
private ParameterExpression lambdaHzyParameter;
|
||||
public LambdaExpression Modify(LambdaExpression lambda, List<SelectTableInfo> tables)
|
||||
{
|
||||
this.tables = tables.Where(a => a.Type != SelectTableInfoType.Parent).ToList();
|
||||
parameters = this.tables.Select(a => a.Parameter ??
|
||||
Expression.Parameter(a.Table.Type,
|
||||
a.Alias.StartsWith("SP10") ? a.Alias.Replace("SP10", "ht") : a.Alias)).ToArray();
|
||||
lambdaHzyParameter = lambda.Parameters.FirstOrDefault();
|
||||
var exp = Visit(lambda.Body);
|
||||
return Expression.Lambda(exp, parameters);
|
||||
}
|
||||
@ -2333,6 +2340,7 @@ namespace FreeSql.Internal
|
||||
var parent = node.Expression as MemberExpression;
|
||||
if (parent.Expression?.NodeType == ExpressionType.Parameter &&
|
||||
parent.Expression.Type.Name.StartsWith("HzyTuple`") == true &&
|
||||
parent.Expression == lambdaHzyParameter &&
|
||||
int.TryParse(parent.Member.Name.Replace("t", ""), out widx) && widx > 0 && widx <= tables.Count)
|
||||
{
|
||||
if (parameters[widx - 1].Type != parent.Type) //解决 BaseEntity + AsTable 时报错
|
||||
@ -2343,6 +2351,7 @@ namespace FreeSql.Internal
|
||||
|
||||
if (node.Expression?.NodeType == ExpressionType.Parameter &&
|
||||
node.Expression.Type.Name.StartsWith("HzyTuple`") == true &&
|
||||
node.Expression == lambdaHzyParameter &&
|
||||
int.TryParse(node.Member.Name.Replace("t", ""), out widx) && widx > 0 && widx <= tables.Count)
|
||||
{
|
||||
if (parameters[widx - 1].Type != node.Type) //解决 BaseEntity + AsTable 时报错
|
||||
|
@ -317,16 +317,9 @@ namespace FreeSql.ClickHouse
|
||||
case "Contains":
|
||||
var args0Value = getExp(exp.Arguments[0]);
|
||||
if (args0Value == "NULL") return $"({left}) IS NULL";
|
||||
if (args0Value.Contains("%"))
|
||||
{
|
||||
if (exp.Method.Name == "StartsWith") return $"locate({args0Value}, {left}) = 1";
|
||||
if (exp.Method.Name == "EndsWith") return $"locate({args0Value}, {left}) = char_length({args0Value})";
|
||||
return $"locate({args0Value}, {left}) > 0";
|
||||
}
|
||||
if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"concat({args0Value}, '%')")}";
|
||||
if (exp.Method.Name == "StartsWith") return $"positionCaseInsensitive({left}, {args0Value}) = 1";
|
||||
if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"concat('%', {args0Value})")}";
|
||||
if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) LIKE {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}";
|
||||
return $"({left}) LIKE concat('%', {args0Value}, '%')";
|
||||
return $"positionCaseInsensitive({left}, {args0Value}) > 0";
|
||||
case "ToLower": return $"lower({left})";
|
||||
case "ToUpper": return $"upper({left})";
|
||||
case "Substring":
|
||||
|
Loading…
x
Reference in New Issue
Block a user