- fix: AsTable for Select

This commit is contained in:
2881099 2022-04-09 20:54:48 +08:00
parent 932b8fde58
commit b9a21f6c11
2 changed files with 16 additions and 6 deletions

View File

@ -75,9 +75,6 @@ namespace FreeSql.DataAnnotations
public interface IAsTable public interface IAsTable
{ {
/// <summary>
/// 所有分表名
/// </summary>
string[] AllTables { get; } string[] AllTables { get; }
string GetTableNameByColumnValue(object columnValue, bool autoExpand = false); string GetTableNameByColumnValue(object columnValue, bool autoExpand = false);
string[] GetTableNamesByColumnValueRange(object columnValue1, object columnValue2); string[] GetTableNamesByColumnValueRange(object columnValue1, object columnValue2);

View File

@ -460,13 +460,14 @@ namespace FreeSql.Internal.CommonProvider
protected List<Dictionary<Type, string>> GetTableRuleUnions() protected List<Dictionary<Type, string>> GetTableRuleUnions()
{ {
var unions = new List<Dictionary<Type, string>>(); var unions = new List<Dictionary<Type, string>>();
var trs = _tableRules.Any() ? _tableRules : new List<Func<Type, string, string>>(); var trs = _tableRules.Any() ? _tableRules : new List<Func<Type, string, string>>(new [] { new Func<Type, string, string>((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) 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; string[] aret = null;
if (_where.Length == 0) aret = tb.Table.AsTableImpl.AllTables; if (_where.Length == 0) aret = tb.Table.AsTableImpl.AllTables;
@ -481,6 +482,18 @@ namespace FreeSql.Internal.CommonProvider
} }
return aret; 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 }; 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(); 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();