mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 12:28:15 +08:00
- 移除 FreeSql.Repository 扩展方法 FromRepository;
- 调整 ISelect.AsTable 规则,每一次使用将增加 UNION ALL 查询; - 优化 AsTable UseSyncStructureToLower/ToUpper 设置,兼容 AsTable((t,o) => "(select * from tb)"); #89
This commit is contained in:
@ -810,13 +810,12 @@
|
||||
<member name="M:FreeSql.ISelect0`2.AsTable(System.Func{System.Type,System.String,System.String})">
|
||||
<summary>
|
||||
设置表名规则,可用于分库/分表,参数1:实体类型;参数2:默认表名;返回值:新表名; <para></para>
|
||||
设置后可查询分表后的多个子表记录,以 UNION ALL 形式执行。 <para></para>
|
||||
设置多次,可查询分表后的多个子表记录,以 UNION ALL 形式执行。 <para></para>
|
||||
如:select.AsTable((type, oldname) => "table_1").AsTable((type, oldname) => "table_2").AsTable((type, oldname) => "table_3").ToSql(a => a.Id); <para></para>
|
||||
select * from (SELECT a."Id" as1 FROM "table_1" a) ftb <para></para>
|
||||
UNION ALL<para></para>
|
||||
select * from (SELECT a."Id" as1 FROM "table_2" a) ftb <para></para>
|
||||
UNION ALL<para></para>
|
||||
select * from (SELECT a."Id" as1 FROM "table_3" a) ftb
|
||||
UNION ALL select * from (SELECT a."Id" as1 FROM "table_2" a) ftb <para></para>
|
||||
UNION ALL select * from (SELECT a."Id" as1 FROM "table_3" a) ftb <para></para>
|
||||
还可以这样:select.AsTable((a, b) => "(select * from tb_topic where clicks > 10)").Page(1, 10).ToList()
|
||||
</summary>
|
||||
<param name="tableRule"></param>
|
||||
<returns></returns>
|
||||
|
@ -69,13 +69,12 @@ namespace FreeSql
|
||||
|
||||
/// <summary>
|
||||
/// 设置表名规则,可用于分库/分表,参数1:实体类型;参数2:默认表名;返回值:新表名; <para></para>
|
||||
/// 设置后可查询分表后的多个子表记录,以 UNION ALL 形式执行。 <para></para>
|
||||
/// 设置多次,可查询分表后的多个子表记录,以 UNION ALL 形式执行。 <para></para>
|
||||
/// 如:select.AsTable((type, oldname) => "table_1").AsTable((type, oldname) => "table_2").AsTable((type, oldname) => "table_3").ToSql(a => a.Id); <para></para>
|
||||
/// select * from (SELECT a."Id" as1 FROM "table_1" a) ftb <para></para>
|
||||
/// UNION ALL<para></para>
|
||||
/// select * from (SELECT a."Id" as1 FROM "table_2" a) ftb <para></para>
|
||||
/// UNION ALL<para></para>
|
||||
/// select * from (SELECT a."Id" as1 FROM "table_3" a) ftb
|
||||
/// UNION ALL select * from (SELECT a."Id" as1 FROM "table_2" a) ftb <para></para>
|
||||
/// UNION ALL select * from (SELECT a."Id" as1 FROM "table_3" a) ftb <para></para>
|
||||
/// 还可以这样:select.AsTable((a, b) => "(select * from tb_topic where clicks > 10)").Page(1, 10).ToList()
|
||||
/// </summary>
|
||||
/// <param name="tableRule"></param>
|
||||
/// <returns></returns>
|
||||
|
@ -130,13 +130,10 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
if (_tableRule == null) return _table.DbName;
|
||||
var newname = _tableRule(_table.DbName);
|
||||
if (!string.IsNullOrEmpty(newname))
|
||||
{
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) return newname.ToLower();
|
||||
if (_orm.CodeFirst.IsSyncStructureToUpper) return newname.ToUpper();
|
||||
return newname;
|
||||
}
|
||||
return _table.DbName;
|
||||
if (string.IsNullOrEmpty(newname)) return _table.DbName;
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
|
||||
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
|
||||
return newname;
|
||||
}
|
||||
public IDelete<T1> AsTable(Func<string, string> tableRule)
|
||||
{
|
||||
|
@ -552,13 +552,10 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
if (_tableRule == null) return _table.DbName;
|
||||
var newname = _tableRule(_table.DbName);
|
||||
if (!string.IsNullOrEmpty(newname))
|
||||
{
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) return newname.ToLower();
|
||||
if (_orm.CodeFirst.IsSyncStructureToUpper) return newname.ToUpper();
|
||||
return newname;
|
||||
}
|
||||
return _table.DbName;
|
||||
if (string.IsNullOrEmpty(newname)) return _table.DbName;
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
|
||||
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
|
||||
return newname;
|
||||
}
|
||||
public IInsert<T1> AsTable(Func<string, string> tableRule)
|
||||
{
|
||||
|
@ -906,23 +906,33 @@ namespace FreeSql.Internal.CommonProvider
|
||||
return (map, field.ToString());
|
||||
}
|
||||
|
||||
protected string[] TableRuleInvoke(Type type, string oldname)
|
||||
protected List<Dictionary<Type, string>> GetTableRuleUnions()
|
||||
{
|
||||
List<string> newnames = new List<string>();
|
||||
foreach (var tr in _tableRules)
|
||||
var unions = new List<Dictionary<Type, string>>();
|
||||
var trs = _tableRules.Any() ? _tableRules : new List<Func<Type, string, string>>(new[] { new Func<Type, string, string>((type, oldname) => oldname) });
|
||||
foreach (var tr in trs)
|
||||
{
|
||||
var newname = tr?.Invoke(type, oldname);
|
||||
if (!string.IsNullOrEmpty(newname))
|
||||
var dict = new Dictionary<Type, string>();
|
||||
foreach (var tb in _tables)
|
||||
{
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) newnames.Add(newname.ToLower());
|
||||
else if (_orm.CodeFirst.IsSyncStructureToUpper) newnames.Add(newname.ToUpper());
|
||||
else newnames.Add(newname);
|
||||
if (tb.Type == SelectTableInfoType.Parent) continue;
|
||||
if (dict.ContainsKey(tb.Table.Type)) continue;
|
||||
var name = tr?.Invoke(tb.Table.Type, tb.Table.DbName);
|
||||
if (string.IsNullOrEmpty(name)) name = tb.Table.DbName;
|
||||
else
|
||||
{
|
||||
if (name.IndexOf(' ') == -1)
|
||||
{
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) name = name.ToLower();
|
||||
if (_orm.CodeFirst.IsSyncStructureToUpper) name = name.ToUpper();
|
||||
}
|
||||
}
|
||||
dict.Add(tb.Table.Type, name);
|
||||
}
|
||||
unions.Add(dict);
|
||||
}
|
||||
if (newnames.Any() == false) return new[] { oldname };
|
||||
return newnames.Distinct().ToArray();
|
||||
return unions;
|
||||
}
|
||||
|
||||
public TSelect AsTable(Func<Type, string, string> tableRule)
|
||||
{
|
||||
if (tableRule != null) _tableRules.Add(tableRule);
|
||||
|
@ -604,13 +604,10 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
if (_tableRule == null) return _table.DbName;
|
||||
var newname = _tableRule(_table.DbName);
|
||||
if (!string.IsNullOrEmpty(newname))
|
||||
{
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) return newname.ToLower();
|
||||
if (_orm.CodeFirst.IsSyncStructureToUpper) return newname.ToUpper();
|
||||
return newname;
|
||||
}
|
||||
return _table.DbName;
|
||||
if (string.IsNullOrEmpty(newname)) return _table.DbName;
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
|
||||
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
|
||||
return newname;
|
||||
}
|
||||
public IUpdate<T1> AsTable(Func<string, string> tableRule)
|
||||
{
|
||||
|
@ -363,36 +363,5 @@ namespace FreeSql.Internal
|
||||
}
|
||||
while (initConns.TryTake(out var conn)) pool.Return(conn);
|
||||
}
|
||||
|
||||
public static List<Dictionary<Type, string>> GetAllTableRule(List<SelectTableInfo> _tables, Func<Type, string, string[]> tableRuleInvoke)
|
||||
{
|
||||
var tableRuleSorted = new List<(Type, string[])>();
|
||||
var tableRuleDict = new Dictionary<Type, bool>();
|
||||
foreach(var tb in _tables)
|
||||
{
|
||||
if (tb.Type == SelectTableInfoType.Parent) continue;
|
||||
if (tableRuleDict.ContainsKey(tb.Table.Type)) continue;
|
||||
var names = tableRuleInvoke(tb.Table.Type, tb.Table.DbName);
|
||||
tableRuleSorted.Add((tb.Table.Type, names));
|
||||
tableRuleDict.Add(tb.Table.Type, true);
|
||||
}
|
||||
var tableRules = new List<Dictionary<Type, string>>();
|
||||
tableRules.Add(tableRuleSorted.Select(a => (a.Item1, a.Item2.First())).ToDictionary(a => a.Item1, a => a.Item2));
|
||||
for (var z = tableRuleSorted.Count - 1; z >=0; z--)
|
||||
{
|
||||
var tbrd = tableRuleSorted[z];
|
||||
var curpos = tableRules.Count;
|
||||
for (var a = 1; a < tbrd.Item2.Length; a++)
|
||||
{
|
||||
for (var b = 0; b < curpos; b++)
|
||||
{
|
||||
var tr = new Dictionary<Type, string>();
|
||||
foreach (var oldtd in tableRules[b]) tr.Add(oldtd.Key, tbrd.Item1 == oldtd.Key ? tbrd.Item2[a] : oldtd.Value);
|
||||
tableRules.Add(tr);
|
||||
}
|
||||
}
|
||||
}
|
||||
return tableRules;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user