diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index 4efbf7d9a..073951257 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -600,6 +600,21 @@ static void Main(string[] args) BaseEntity.Initialization(fsql, () => _asyncUow.Value); #endregion + var x01sql01 = fsql.Select() + .Include(a => a.Test1) + .Include(a => a.Test2) + .Include(a => a.Test3) + .ToSql(); + + var txxx01 = fsql.Select() + .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() .Join(fsql.Select().Where(a => a.GroupName == "xxx"), (a, b) => a.GroupId == b.Id) .AsTable("t1", null) @@ -2950,4 +2965,30 @@ public class B11 public int Id { get; set; } public string Name { 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; } } \ No newline at end of file diff --git a/FreeSql/DataAnnotations/ColumnAttribute.cs b/FreeSql/DataAnnotations/ColumnAttribute.cs index 8ca277665..549b2b03f 100644 --- a/FreeSql/DataAnnotations/ColumnAttribute.cs +++ b/FreeSql/DataAnnotations/ColumnAttribute.cs @@ -126,6 +126,7 @@ public class ColumnAttribute : Attribute /// /// 重读功能 /// 比如:[Column(RereadSql = "{0}.STAsText()")] + /// 或者:[Column(RereadSql = "{geo}.STAsText()")] /// 查询:SELECT a.[id], a.[geo].STAsText() FROM [table] a /// public string RereadSql { get; set; } diff --git a/FreeSql/DataAnnotations/ColumnFluent.cs b/FreeSql/DataAnnotations/ColumnFluent.cs index 47de1150c..fcc415459 100644 --- a/FreeSql/DataAnnotations/ColumnFluent.cs +++ b/FreeSql/DataAnnotations/ColumnFluent.cs @@ -206,6 +206,7 @@ public ColumnFluent RewriteSql(string value) /// /// 重读功能 /// 比如:[Column(RereadSql = "{0}.STAsText()")] + /// 或者:[Column(RereadSql = "{geo}.STAsText()")] /// 查询:SELECT a.[id], a.[geo].STAsText() FROM [table] a /// /// diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 7e96ab0aa..ea78dcc5a 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -127,6 +127,7 @@ 重读功能 比如:[Column(RereadSql = "{0}.STAsText()")] + 或者:[Column(RereadSql = "{geo}.STAsText()")] 查询:SELECT a.[id], a.[geo].STAsText() FROM [table] a @@ -260,6 +261,7 @@ 重读功能 比如:[Column(RereadSql = "{0}.STAsText()")] + 或者:[Column(RereadSql = "{geo}.STAsText()")] 查询:SELECT a.[id], a.[geo].STAsText() FROM [table] a diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index eb39d5cb3..89fb22b25 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -75,7 +75,18 @@ public string RereadColumn(ColumnInfo col, string columnName) { 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}";