- 优化 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

@ -600,6 +600,21 @@ namespace base_entity
BaseEntity.Initialization(fsql, () => _asyncUow.Value); BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion #endregion
var x01sql01 = fsql.Select<Main1>()
.Include(a => a.Test1)
.Include(a => a.Test2)
.Include(a => a.Test3)
.ToSql();
var txxx01 = fsql.Select<User1>()
.WhereDynamicFilter(new DynamicFilterInfo
{
Field = nameof(User1.CreateTime),
Operator = DynamicFilterOperator.DateRange,
Value = $"{DateTime.MinValue.ToString("yyyy-MM-dd")},{DateTime.MinValue.ToString("yyyy-MM-dd")}"
})
.ToSql();
var updatejoin031sql = fsql.Update<User1>() var updatejoin031sql = fsql.Update<User1>()
.Join<UserGroup>(fsql.Select<UserGroup>().Where(a => a.GroupName == "xxx"), (a, b) => a.GroupId == b.Id) .Join<UserGroup>(fsql.Select<UserGroup>().Where(a => a.GroupName == "xxx"), (a, b) => a.GroupId == b.Id)
.AsTable("t1", null) .AsTable("t1", null)
@ -2950,4 +2965,30 @@ public class B11
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public A11 a { get; set; } public A11 a { get; set; }
}
public class Main1
{
public long Id { get; set; }
public long Test1Id { get; set; }
public long Test2Id { get; set; }
public long Test3Id { get; set; }
public virtual Test2 Test1 { get; set; }
public virtual Test2 Test2 { get; set; }
public virtual Test2 Test3 { get; set; }
}
public class Test2
{
public long Id { get; set; }
[Column(RereadSql = "IIF({IsEnabled} = 1, {0}, {0} + '-已停用')")]
public string ItemName { get; set; }
public bool IsEnabled { get; set; }
} }

View File

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

View File

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

View File

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

View File

@ -75,7 +75,18 @@ namespace FreeSql.Internal
{ {
var result = QuoteReadColumnAdapter(col.CsType, col.Attribute.MapType, columnName); var result = QuoteReadColumnAdapter(col.CsType, col.Attribute.MapType, columnName);
if (string.IsNullOrWhiteSpace(col?.Attribute.RereadSql) == false) 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; return result;
} }
public virtual string FieldAsAlias(string alias) => $" {alias}"; public virtual string FieldAsAlias(string alias) => $" {alias}";