From b9a21f6c112fb3df20cbf1c25301126bf999931e Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Sat, 9 Apr 2022 20:54:48 +0800 Subject: [PATCH] - fix: AsTable for Select --- FreeSql/DataAnnotations/TableAttribute.cs | 3 --- .../SelectProvider/Select0Provider.cs | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/FreeSql/DataAnnotations/TableAttribute.cs b/FreeSql/DataAnnotations/TableAttribute.cs index e6dbe57c..2c27feca 100644 --- a/FreeSql/DataAnnotations/TableAttribute.cs +++ b/FreeSql/DataAnnotations/TableAttribute.cs @@ -75,9 +75,6 @@ namespace FreeSql.DataAnnotations public interface IAsTable { - /// - /// 所有分表名 - /// string[] AllTables { get; } string GetTableNameByColumnValue(object columnValue, bool autoExpand = false); string[] GetTableNamesByColumnValueRange(object columnValue1, object columnValue2); diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs index 60e7e4c9..d3d13dea 100644 --- a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs +++ b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs @@ -460,13 +460,14 @@ namespace FreeSql.Internal.CommonProvider protected List> GetTableRuleUnions() { var unions = new List>(); - var trs = _tableRules.Any() ? _tableRules : new List>(); + var trs = _tableRules.Any() ? _tableRules : new List>(new [] { new Func((type, oldname) => null) }); - if (trs.Any() == false) + if (trs.Count == 1 && _tables.Any(a => a.Table.AsTableImpl != null && string.IsNullOrWhiteSpace(trs[0](a.Table.Type, a.Table.DbName)) == true)) { string[] LocalGetTableNames(SelectTableInfo tb) { - if (tb.Table.AsTableImpl != null) + var trname = trs[0](tb.Table.Type, tb.Table.DbName); + if (tb.Table.AsTableImpl != null && string.IsNullOrWhiteSpace(trname) == true) { string[] aret = null; if (_where.Length == 0) aret = tb.Table.AsTableImpl.AllTables; @@ -481,6 +482,18 @@ namespace FreeSql.Internal.CommonProvider } return aret; } + if (string.IsNullOrWhiteSpace(trname) == false) + { + if (trname.IndexOf(' ') == -1) //还可以这样:select.AsTable((a, b) => "(select * from tb_topic where clicks > 10)").Page(1, 10).ToList() + { + if (_orm.CodeFirst.IsSyncStructureToLower) trname = trname.ToLower(); + if (_orm.CodeFirst.IsSyncStructureToUpper) trname = trname.ToUpper(); + if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(tb.Table.Type, trname); + } + else + trname = trname.Replace(" \r\n", " \r\n "); + return new string[] { trname }; + } return new string[] { tb.Table.DbName }; } var tbnames = _tables.GroupBy(a => a.Table.Type).Select(g => _tables.Where(a => a.Table.Type == g.Key).FirstOrDefault()).Select(a => new { Tb = a, Names = LocalGetTableNames(a) }).ToList();