mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 12:28:15 +08:00
- 完成 [Table(AsTable = xx)] 分表特性查询;
This commit is contained in:
@ -76,7 +76,7 @@ namespace FreeSql.DataAnnotations
|
||||
ICollection<string> AllTables { get; }
|
||||
string GetTableNameByColumnValue(object columnValue, bool autoExpand = false);
|
||||
ICollection<string> GetTableNamesByColumnValueRange(object columnValue1, object columnValue2);
|
||||
ICollection<string> GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, TableInfo table, CommonUtils commonUtils);
|
||||
ICollection<string> GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils);
|
||||
}
|
||||
class DateTimeAsTableImpl : IAsTable
|
||||
{
|
||||
@ -192,7 +192,7 @@ namespace FreeSql.DataAnnotations
|
||||
{
|
||||
return _dicRegSqlWhereDateTimes.GetOrAdd($"{columnName},{quoteParameterName}", cn =>
|
||||
{
|
||||
cn = columnName.Replace(@"\[", @"\\[").Replace(@"\]", @"\\]");
|
||||
cn = columnName.Replace("[", "\\[").Replace("]", "\\]").Replace(".", "\\.");
|
||||
return new[]
|
||||
{
|
||||
new Regex($@"({cn}\s*(<|<=|>|>=|=|between)\s*)(datetime|cdate|to_date)\(('[^']+')\)", RegexOptions.IgnoreCase),
|
||||
@ -239,15 +239,22 @@ namespace FreeSql.DataAnnotations
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public ICollection<string> GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, TableInfo table, CommonUtils commonUtils)
|
||||
public ICollection<string> GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(sqlWhere)) return _allTables;
|
||||
var quoteParameterName = commonUtils.QuoteParamterName("");
|
||||
var quoteParameterNameCharArray = quoteParameterName.ToCharArray();
|
||||
var regs = GetRegSqlWhereDateTimes(commonUtils.QuoteSqlName(table.AsTableColumn.Attribute.Name), quoteParameterName);
|
||||
var columnName = commonUtils.QuoteSqlName(tb.Table.AsTableColumn.Attribute.Name);
|
||||
var regs = GetRegSqlWhereDateTimes($"{(string.IsNullOrWhiteSpace(tb.Alias) ? "" : $"{tb.Alias}.")}{commonUtils.QuoteSqlName(tb.Table.AsTableColumn.Attribute.Name)}", quoteParameterName);
|
||||
for (var a = 0; a < 16; a++) sqlWhere = regs[a].Replace(sqlWhere, "$1$4");
|
||||
|
||||
var m = regs[16].Match(sqlWhere);
|
||||
if (m.Success) return GetTableNamesByColumnValueRange(m.Groups[1].Value, m.Groups[2].Value);
|
||||
m = m = regs[18].Match(sqlWhere);
|
||||
if (m.Success) return LocalGetTables(m.Groups[1].Value, m.Groups[3].Value, ParseColumnValue(m.Groups[2].Value), ParseColumnValue(m.Groups[4].Value));
|
||||
m = regs[20].Match(sqlWhere);
|
||||
if (m.Success) return LocalGetTables2(m.Groups[1].Value, ParseColumnValue(m.Groups[2].Value));
|
||||
|
||||
m = m = regs[17].Match(sqlWhere);
|
||||
if (m.Success)
|
||||
{
|
||||
@ -256,8 +263,6 @@ namespace FreeSql.DataAnnotations
|
||||
if (val1 == null || val2 == null) throw new Exception($"未能解析分表字段值 {sqlWhere}");
|
||||
return GetTableNamesByColumnValueRange(val1, val2);
|
||||
}
|
||||
m = m = regs[18].Match(sqlWhere);
|
||||
if (m.Success) return LocalGetTables(m.Groups[1].Value, m.Groups[3].Value, ParseColumnValue(m.Groups[2].Value), ParseColumnValue(m.Groups[4].Value));
|
||||
m = regs[19].Match(sqlWhere);
|
||||
if (m.Success)
|
||||
{
|
||||
@ -266,8 +271,6 @@ namespace FreeSql.DataAnnotations
|
||||
if (val1 == null || val2 == null) throw new Exception($"未能解析分表字段值 {sqlWhere}");
|
||||
return LocalGetTables(m.Groups[1].Value, m.Groups[3].Value, ParseColumnValue(val1), ParseColumnValue(val2));
|
||||
}
|
||||
m = regs[20].Match(sqlWhere);
|
||||
if (m.Success) return LocalGetTables2(m.Groups[1].Value, ParseColumnValue(m.Groups[2].Value));
|
||||
m = regs[21].Match(sqlWhere);
|
||||
if (m.Success)
|
||||
{
|
||||
|
@ -411,7 +411,7 @@
|
||||
所有分表名
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:FreeSql.DataAnnotations.DateTimeAsTableImpl.GetTableNamesBySqlWhere(System.String,System.Collections.Generic.List{System.Data.Common.DbParameter},FreeSql.Internal.Model.TableInfo,FreeSql.Internal.CommonUtils)">
|
||||
<member name="M:FreeSql.DataAnnotations.DateTimeAsTableImpl.GetTableNamesBySqlWhere(System.String,System.Collections.Generic.List{System.Data.Common.DbParameter},FreeSql.Internal.Model.SelectTableInfo,FreeSql.Internal.CommonUtils)">
|
||||
<summary>
|
||||
可以匹配以下条件(支持参数化):<para></para>
|
||||
`field` BETWEEN '2022-01-01 00:00:00' AND '2022-03-01 00:00:00'<para></para>
|
||||
|
@ -1690,6 +1690,7 @@ namespace FreeSql.Internal
|
||||
public abstract string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, ExpTSC tsc);
|
||||
public abstract string ExpressionLambdaToSqlCallConvert(MethodCallExpression exp, ExpTSC tsc);
|
||||
public abstract string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc);
|
||||
public string ExpressionConstDateTime(Expression exp) => exp is ConstantExpression operandExpConst ? formatSql(Utils.GetDataReaderValue(typeof(DateTime), operandExpConst.Value), null, null, null) : null;
|
||||
|
||||
public enum ExpressionStyle
|
||||
{
|
||||
|
@ -195,7 +195,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
var sb = new StringBuilder();
|
||||
if (_table.AsTableImpl != null)
|
||||
{
|
||||
var names = _table.AsTableImpl.GetTableNamesBySqlWhere(newwhere.ToString(), _params, _table, _commonUtils);
|
||||
var names = _table.AsTableImpl.GetTableNamesBySqlWhere(newwhere.ToString(), _params, new SelectTableInfo { Table = _table }, _commonUtils);
|
||||
foreach (var name in names)
|
||||
{
|
||||
_tableRule = old => name;
|
||||
@ -228,7 +228,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
var sb = new StringBuilder();
|
||||
if (_table.AsTableImpl != null)
|
||||
{
|
||||
var names = _table.AsTableImpl.GetTableNamesBySqlWhere(newwhere.ToString(), _params, _table, _commonUtils);
|
||||
var names = _table.AsTableImpl.GetTableNamesBySqlWhere(newwhere.ToString(), _params, new SelectTableInfo { Table = _table }, _commonUtils);
|
||||
foreach (var name in names)
|
||||
{
|
||||
_tableRule = old => name;
|
||||
|
@ -460,7 +460,54 @@ namespace FreeSql.Internal.CommonProvider
|
||||
protected List<Dictionary<Type, string>> GetTableRuleUnions()
|
||||
{
|
||||
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) => null) });
|
||||
var trs = _tableRules.Any() ? _tableRules : new List<Func<Type, string, string>>();
|
||||
|
||||
if (trs.Any() == false)
|
||||
{
|
||||
string[] LocalGetTableNames(SelectTableInfo tb)
|
||||
{
|
||||
if (tb.Table.AsTableImpl != null)
|
||||
{
|
||||
string[] aret = null;
|
||||
if (_where.Length == 0) aret = tb.Table.AsTableImpl.AllTables.ToArray();
|
||||
else aret = tb.Table.AsTableImpl.GetTableNamesBySqlWhere(_where.ToString(), _params, tb, _commonUtils).ToArray();
|
||||
if (aret.Any() == false) aret = tb.Table.AsTableImpl.AllTables.Take(1).ToArray();
|
||||
|
||||
for (var a = 0; a < aret.Length; a++)
|
||||
{
|
||||
if (_orm.CodeFirst.IsSyncStructureToLower) aret[a] = aret[a].ToLower();
|
||||
if (_orm.CodeFirst.IsSyncStructureToUpper) aret[a] = aret[a].ToUpper();
|
||||
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(tb.Table.Type, aret[a]);
|
||||
}
|
||||
return aret;
|
||||
}
|
||||
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 dict = new Dictionary<Type, string>();
|
||||
tbnames.ForEach(a =>
|
||||
{
|
||||
dict.Add(a.Tb.Table.Type, a.Names[0]);
|
||||
});
|
||||
unions.Add(dict);
|
||||
for (var a = 0; a < tbnames.Count; a++)
|
||||
{
|
||||
if (tbnames[a].Names.Length <= 1) continue;
|
||||
var unionsCount = unions.Count;
|
||||
for (var b = 1; b < tbnames[a].Names.Length; b++)
|
||||
{
|
||||
for (var d = 0; d < unionsCount; d++)
|
||||
{
|
||||
dict = new Dictionary<Type, string>();
|
||||
foreach (var uit in unions[d])
|
||||
dict.Add(uit.Key, uit.Key == tbnames[a].Tb.Table.Type ? tbnames[a].Names[b] : uit.Value);
|
||||
unions.Add(dict);
|
||||
}
|
||||
}
|
||||
}
|
||||
return unions;
|
||||
}
|
||||
if (trs.Any() == false) trs.Add(new Func<Type, string, string>((type, oldname) => null));
|
||||
foreach (var tr in trs)
|
||||
{
|
||||
var dict = new Dictionary<Type, string>();
|
||||
|
@ -803,7 +803,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
var sb = new StringBuilder();
|
||||
if (_table.AsTableImpl != null)
|
||||
{
|
||||
var names = _table.AsTableImpl.GetTableNamesBySqlWhere(newwhere.ToString(), _params, _table, _commonUtils);
|
||||
var names = _table.AsTableImpl.GetTableNamesBySqlWhere(newwhere.ToString(), _params, new SelectTableInfo { Table = _table }, _commonUtils);
|
||||
foreach (var name in names)
|
||||
{
|
||||
_tableRule = old => name;
|
||||
@ -835,7 +835,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
var sb = new StringBuilder();
|
||||
if (_table.AsTableImpl != null)
|
||||
{
|
||||
var names = _table.AsTableImpl.GetTableNamesBySqlWhere(newwhere.ToString(), _params, _table, _commonUtils);
|
||||
var names = _table.AsTableImpl.GetTableNamesBySqlWhere(newwhere.ToString(), _params, new SelectTableInfo { Table = _table }, _commonUtils);
|
||||
foreach (var name in names)
|
||||
{
|
||||
_tableRule = old => name;
|
||||
|
Reference in New Issue
Block a user