Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column.RereadSql使用其他字段判断动态获取时, 无法在多表关联中指定表别名 #1655

Closed
misgw opened this issue Nov 1, 2023 · 7 comments

Comments

@misgw
Copy link

misgw commented Nov 1, 2023

问题描述及重现代码:

预期: Test作为字典表,通过IsEnabled字段实现非启用状态下查询时ItemName 字段附加"-已停用"后缀

问题: 单表查询可以实现, 多表关联时, 获取不到Test的表别名, 此时多次关联Test表时, 生成sql语句执行时提示"IsEnabled"列不明确

期望: [Column(RereadSql = "IIF(IsEnabled = 1, {0}, {0} + '-已停用')")] 中通过占位符指定当前的表别名
例如: [Column(RereadSql = "IIF({tableAlias}.IsEnabled = 1, {tableAlias}.{0}, {tableAlias}.{0} + '-已停用')")]

//实体类
public class Main
{
    public long Id { get; set; }

    public long Test1Id { get; set; }

    public long Test2Id { get; set; }

    public long Test3Id { get; set; }
    
    public virtual Test Test1 { get; set; }

    public virtual Test Test2 { get; set; }

    public virtual Test Test3 { get; set; }
}

public class Test
{
    public long Id { get; set; }

    [Column(RereadSql = "IIF(IsEnabled = 1, {0}, {0} + '-已停用')")]
    public string ItemName { get; set; }

    public bool IsEnabled { get; set; }
}

生成的sql

SELECT 
	IIF(IsEnabled = 1, a__Test1.ItemName, a__Test1.ItemName + '-已停用') as1,
	IIF(IsEnabled = 1, a__Test2.ItemName, a__Test2.ItemName + '-已停用') as2,
	IIF(IsEnabled = 1, a__Test3.ItemName, a__Test3.ItemName + '-已停用') as3
FROM Main a
LEFT JOIN Test a__Test1 ON a__Test1.Id = a.Test1Id
LEFT JOIN Test a__Test2 ON a__Test2.Id = a.Test2Id
LEFT JOIN Test a__Test3 ON a__Test3.Id = a.Test3Id

数据库版本

SQL Server 2019 EE

安装的Nuget包

  • FreeSql(3.2.800)
  • FreeSql.Provider.SqlServer(3.2.800)
  • FreeSql.Repository(3.2.800)
  • FreeSql.Extensions.LazyLoading(3.2.800)

.net framework/. net core? 及具体版本

net6.0

@2881099
Copy link
Collaborator

2881099 commented Nov 1, 2023

应该不会有其他表的访问情况?

解决方向,是否只考虑本表即可

@2881099
Copy link
Collaborator

2881099 commented Nov 1, 2023

[Column(RereadSql = "IIF([IsEnabled] = 1, {0}, {0} + '-已停用')")]
public string ItemName { get; set; }

public bool IsEnabled { get; set; }

暂定,使用方括号代表替换为目标 alias.column

方括号内容,可以是属性名,或者字段名。

@misgw
Copy link
Author

misgw commented Nov 1, 2023

应该不会有其他表的访问情况?

解决方向,是否只考虑本表即可

  1. 目前场景只会存在使用当前表用来判断,还没有遇到需要访问其他表的情况
  2. 只考虑本表即可

@misgw
Copy link
Author

misgw commented Nov 2, 2023

[Column(RereadSql = "IIF([IsEnabled] = 1, {0}, {0} + '-已停用')")] public string ItemName { get; set; }

public bool IsEnabled { get; set; }

暂定,使用方括号代表替换为目标 alias.column

方括号内容,可以是属性名,或者字段名。

  1. 暂定方案中,能否使用多个方括号标记?
  2. 期望一种明确的形式,类似于目前的{}占位符

@misgw misgw closed this as completed Nov 2, 2023
@misgw misgw reopened this Nov 2, 2023
@2881099
Copy link
Collaborator

2881099 commented Nov 2, 2023

[Column(RereadSql = "IIF({IsEnabled} = 1, {0}, {0} + '-已停用')")]
public string ItemName { get; set; }

public bool IsEnabled { get; set; }

使用花括号吧!

@misgw
Copy link
Author

misgw commented Nov 2, 2023

[Column(RereadSql = "IIF({IsEnabled} = 1, {0}, {0} + '-已停用')")] public string ItemName { get; set; }

public bool IsEnabled { get; set; }

使用花括号吧!

可以考虑多个字段的情况

@2881099
Copy link
Collaborator

2881099 commented Nov 2, 2023

SELECT a.id, a.test1id, a.test2id, a.test3id, a__Test1.id as5, IIF(a__Test1.isenabled = 1, a__Test1.itemname, a__Test1.itemname + '-已停用') itemname, a__Test1.isenabled, a__Test2.id as8, IIF(a__Test2.isenabled = 1, a__Test2.itemname, a__Test2.itemname + '-已停用') as9, a__Test2.isenabled as10, a__Test3.id as11, IIF(a__Test3.isenabled = 1, a__Test3.itemname, a__Test3.itemname + '-已停用') as12, a__Test3.isenabled as13
FROM main1 a
LEFT JOIN test2 a__Test1 ON a__Test1.id = a.test1id
LEFT JOIN test2 a__Test2 ON a__Test2.id = a.test2id
LEFT JOIN test2 a__Test3 ON a__Test3.id = a.test3id

待发布

2881099 added a commit that referenced this issue Nov 2, 2023
@misgw misgw closed this as completed Nov 2, 2023
2881099 added a commit that referenced this issue Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants