Skip to content

Commit 7115b68

Browse files
committed
- 优化 RereadSql 支持表的其他字段使用;#1655
1 parent 448b25c commit 7115b68

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

Examples/base_entity/Program.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,21 @@ static void Main(string[] args)
600600
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
601601
#endregion
602602

603+
var x01sql01 = fsql.Select<Main1>()
604+
.Include(a => a.Test1)
605+
.Include(a => a.Test2)
606+
.Include(a => a.Test3)
607+
.ToSql();
608+
609+
var txxx01 = fsql.Select<User1>()
610+
.WhereDynamicFilter(new DynamicFilterInfo
611+
{
612+
Field = nameof(User1.CreateTime),
613+
Operator = DynamicFilterOperator.DateRange,
614+
Value = $"{DateTime.MinValue.ToString("yyyy-MM-dd")},{DateTime.MinValue.ToString("yyyy-MM-dd")}"
615+
})
616+
.ToSql();
617+
603618
var updatejoin031sql = fsql.Update<User1>()
604619
.Join<UserGroup>(fsql.Select<UserGroup>().Where(a => a.GroupName == "xxx"), (a, b) => a.GroupId == b.Id)
605620
.AsTable("t1", null)
@@ -2950,4 +2965,30 @@ public class B11
29502965
public int Id { get; set; }
29512966
public string Name { get; set; }
29522967
public A11 a { get; set; }
2968+
}
2969+
public class Main1
2970+
{
2971+
public long Id { get; set; }
2972+
2973+
public long Test1Id { get; set; }
2974+
2975+
public long Test2Id { get; set; }
2976+
2977+
public long Test3Id { get; set; }
2978+
2979+
public virtual Test2 Test1 { get; set; }
2980+
2981+
public virtual Test2 Test2 { get; set; }
2982+
2983+
public virtual Test2 Test3 { get; set; }
2984+
}
2985+
2986+
public class Test2
2987+
{
2988+
public long Id { get; set; }
2989+
2990+
[Column(RereadSql = "IIF({IsEnabled} = 1, {0}, {0} + '-已停用')")]
2991+
public string ItemName { get; set; }
2992+
2993+
public bool IsEnabled { get; set; }
29532994
}

FreeSql/DataAnnotations/ColumnAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public class ColumnAttribute : Attribute
126126
/// <summary>
127127
/// 重读功能<para></para>
128128
/// 比如:[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
129+
/// 或者:[Column(RereadSql = &quot;{geo}.STAsText()&quot;)]<para></para>
129130
/// 查询:SELECT a.[id], a.[geo].STAsText() FROM [table] a
130131
/// </summary>
131132
public string RereadSql { get; set; }

FreeSql/DataAnnotations/ColumnFluent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ public ColumnFluent RewriteSql(string value)
206206
/// <summary>
207207
/// 重读功能<para></para>
208208
/// 比如:[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
209+
/// 或者:[Column(RereadSql = &quot;{geo}.STAsText()&quot;)]<para></para>
209210
/// 查询:SELECT a.[id], a.[geo].STAsText() FROM [table] a
210211
/// </summary>
211212
/// <param name="value"></param>

FreeSql/FreeSql.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Internal/CommonUtils.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,18 @@ public string RereadColumn(ColumnInfo col, string columnName)
7575
{
7676
var result = QuoteReadColumnAdapter(col.CsType, col.Attribute.MapType, columnName);
7777
if (string.IsNullOrWhiteSpace(col?.Attribute.RereadSql) == false)
78-
return string.Format(col.Attribute.RereadSql, result);
78+
{
79+
var tableAlias = columnName.Replace(QuoteSqlName(col.Attribute.Name), "");
80+
var rereadSql = Regex.Replace(col.Attribute.RereadSql, @"\{([_\w][_\w\d]+)\}", rm =>
81+
{
82+
if (col.Table.ColumnsByCs.TryGetValue(rm.Groups[1].Value, out var trycol))
83+
return $"{tableAlias}{QuoteSqlName(trycol.Attribute.Name)}";
84+
else if (col.Table.Columns.TryGetValue(rm.Groups[1].Value, out trycol))
85+
return $"{tableAlias}{QuoteSqlName(trycol.Attribute.Name)}";
86+
return rm.Groups[0].Value;
87+
});
88+
return string.Format(rereadSql, result);
89+
}
7990
return result;
8091
}
8192
public virtual string FieldAsAlias(string alias) => $" {alias}";

0 commit comments

Comments
 (0)