- 优化 RereadSql 支持表的其他字段使用;#1655

This commit is contained in:
2881099
2023-11-02 10:30:13 +08:00
parent 448b25c479
commit 7115b68871
5 changed files with 57 additions and 1 deletions

View File

@ -126,6 +126,7 @@ namespace FreeSql.DataAnnotations
/// <summary>
/// 重读功能<para></para>
/// 比如:[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
/// 或者:[Column(RereadSql = &quot;{geo}.STAsText()&quot;)]<para></para>
/// 查询SELECT a.[id], a.[geo].STAsText() FROM [table] a
/// </summary>
public string RereadSql { get; set; }

View File

@ -206,6 +206,7 @@ namespace FreeSql.DataAnnotations
/// <summary>
/// 重读功能<para></para>
/// 比如:[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
/// 或者:[Column(RereadSql = &quot;{geo}.STAsText()&quot;)]<para></para>
/// 查询SELECT a.[id], a.[geo].STAsText() FROM [table] a
/// </summary>
/// <param name="value"></param>

View File

@ -127,6 +127,7 @@
<summary>
重读功能<para></para>
比如:[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
或者:[Column(RereadSql = &quot;{geo}.STAsText()&quot;)]<para></para>
查询SELECT a.[id], a.[geo].STAsText() FROM [table] a
</summary>
</member>
@ -260,6 +261,7 @@
<summary>
重读功能<para></para>
比如:[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
或者:[Column(RereadSql = &quot;{geo}.STAsText()&quot;)]<para></para>
查询SELECT a.[id], a.[geo].STAsText() FROM [table] a
</summary>
<param name="value"></param>

View File

@ -75,7 +75,18 @@ namespace FreeSql.Internal
{
var result = QuoteReadColumnAdapter(col.CsType, col.Attribute.MapType, columnName);
if (string.IsNullOrWhiteSpace(col?.Attribute.RereadSql) == false)
return string.Format(col.Attribute.RereadSql, result);
{
var tableAlias = columnName.Replace(QuoteSqlName(col.Attribute.Name), "");
var rereadSql = Regex.Replace(col.Attribute.RereadSql, @"\{([_\w][_\w\d]+)\}", rm =>
{
if (col.Table.ColumnsByCs.TryGetValue(rm.Groups[1].Value, out var trycol))
return $"{tableAlias}{QuoteSqlName(trycol.Attribute.Name)}";
else if (col.Table.Columns.TryGetValue(rm.Groups[1].Value, out trycol))
return $"{tableAlias}{QuoteSqlName(trycol.Attribute.Name)}";
return rm.Groups[0].Value;
});
return string.Format(rereadSql, result);
}
return result;
}
public virtual string FieldAsAlias(string alias) => $" {alias}";