Skip to content

Commit

Permalink
- 优化 RereadSql 支持表的其他字段使用;#1655
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Nov 2, 2023
1 parent 448b25c commit 7115b68
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
41 changes: 41 additions & 0 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,21 @@ static void Main(string[] args)
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#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>()
.Join<UserGroup>(fsql.Select<UserGroup>().Where(a => a.GroupName == "xxx"), (a, b) => a.GroupId == b.Id)
.AsTable("t1", null)
Expand Down Expand Up @@ -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; }
}
1 change: 1 addition & 0 deletions FreeSql/DataAnnotations/ColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public class ColumnAttribute : Attribute
/// <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; }
Expand Down
1 change: 1 addition & 0 deletions FreeSql/DataAnnotations/ColumnFluent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public ColumnFluent RewriteSql(string value)
/// <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>
Expand Down
2 changes: 2 additions & 0 deletions FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion FreeSql/Internal/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down

0 comments on commit 7115b68

Please sign in to comment.