mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
- fix: AsTable SafeThread
This commit is contained in:
parent
980ee1e893
commit
932b8fde58
@ -51,6 +51,11 @@ namespace FreeSql.DataAnnotations
|
|||||||
|
|
||||||
tb.AsTableColumn = tb.Columns.TryGetValue(atm.Groups[1].Value, out var trycol) ? trycol :
|
tb.AsTableColumn = tb.Columns.TryGetValue(atm.Groups[1].Value, out var trycol) ? trycol :
|
||||||
tb.ColumnsByCs.TryGetValue(atm.Groups[1].Value, out trycol) ? trycol : throw new Exception($"[Table(AsTable = xx)] 设置的属性名 {atm.Groups[1].Value} 不存在");
|
tb.ColumnsByCs.TryGetValue(atm.Groups[1].Value, out trycol) ? trycol : throw new Exception($"[Table(AsTable = xx)] 设置的属性名 {atm.Groups[1].Value} 不存在");
|
||||||
|
if (tb.AsTableColumn.Attribute.MapType.NullableTypeOrThis() != typeof(DateTime))
|
||||||
|
{
|
||||||
|
tb.AsTableColumn = null;
|
||||||
|
throw new Exception($"[Table(AsTable = xx)] 设置的属性名 {atm.Groups[1].Value} 不是 DateTime 类型");
|
||||||
|
}
|
||||||
int.TryParse(atm.Groups[5].Value, out var atm5);
|
int.TryParse(atm.Groups[5].Value, out var atm5);
|
||||||
string atm6 = atm.Groups[6].Value.ToLower();
|
string atm6 = atm.Groups[6].Value.ToLower();
|
||||||
tb.AsTableImpl = new DateTimeAsTableImpl(Name, DateTime.Parse($"{atm.Groups[2].Value}-{atm.Groups[3].Value}-{atm.Groups[4].Value}"), dt =>
|
tb.AsTableImpl = new DateTimeAsTableImpl(Name, DateTime.Parse($"{atm.Groups[2].Value}-{atm.Groups[3].Value}-{atm.Groups[4].Value}"), dt =>
|
||||||
@ -73,13 +78,14 @@ namespace FreeSql.DataAnnotations
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 所有分表名
|
/// 所有分表名
|
||||||
/// </summary>
|
/// </summary>
|
||||||
ICollection<string> AllTables { get; }
|
string[] AllTables { get; }
|
||||||
string GetTableNameByColumnValue(object columnValue, bool autoExpand = false);
|
string GetTableNameByColumnValue(object columnValue, bool autoExpand = false);
|
||||||
ICollection<string> GetTableNamesByColumnValueRange(object columnValue1, object columnValue2);
|
string[] GetTableNamesByColumnValueRange(object columnValue1, object columnValue2);
|
||||||
ICollection<string> GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils);
|
string[] GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils);
|
||||||
}
|
}
|
||||||
class DateTimeAsTableImpl : IAsTable
|
class DateTimeAsTableImpl : IAsTable
|
||||||
{
|
{
|
||||||
|
readonly object _lock = new object();
|
||||||
readonly List<string> _allTables = new List<string>();
|
readonly List<string> _allTables = new List<string>();
|
||||||
readonly List<DateTime> _allTablesTime = new List<DateTime>();
|
readonly List<DateTime> _allTablesTime = new List<DateTime>();
|
||||||
readonly DateTime _beginTime;
|
readonly DateTime _beginTime;
|
||||||
@ -105,6 +111,8 @@ namespace FreeSql.DataAnnotations
|
|||||||
void ExpandTable(DateTime beginTime, DateTime endTime)
|
void ExpandTable(DateTime beginTime, DateTime endTime)
|
||||||
{
|
{
|
||||||
if (beginTime > endTime) endTime = _nextTimeFunc(beginTime);
|
if (beginTime > endTime) endTime = _nextTimeFunc(beginTime);
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
while (beginTime <= endTime)
|
while (beginTime <= endTime)
|
||||||
{
|
{
|
||||||
var dtstr = beginTime.ToString(_tableNameFormat.Groups[1].Value);
|
var dtstr = beginTime.ToString(_tableNameFormat.Groups[1].Value);
|
||||||
@ -116,6 +124,7 @@ namespace FreeSql.DataAnnotations
|
|||||||
beginTime = _nextTimeFunc(beginTime);
|
beginTime = _nextTimeFunc(beginTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
DateTime ParseColumnValue(object columnValue)
|
DateTime ParseColumnValue(object columnValue)
|
||||||
{
|
{
|
||||||
if (columnValue == null) throw new Exception($"分表字段值不能为 null");
|
if (columnValue == null) throw new Exception($"分表字段值不能为 null");
|
||||||
@ -137,29 +146,36 @@ namespace FreeSql.DataAnnotations
|
|||||||
public string GetTableNameByColumnValue(object columnValue, bool autoExpand = false)
|
public string GetTableNameByColumnValue(object columnValue, bool autoExpand = false)
|
||||||
{
|
{
|
||||||
var dt = ParseColumnValue(columnValue);
|
var dt = ParseColumnValue(columnValue);
|
||||||
if (dt < _allTablesTime.Last()) throw new Exception($"分表字段值 \"{dt.ToString("yyyy-MM-dd HH:mm:ss")}\" 不能小于 \"{_beginTime.ToString("yyyy-MM-dd HH:mm:ss")} \"");
|
if (dt < _beginTime) throw new Exception($"分表字段值 \"{dt.ToString("yyyy-MM-dd HH:mm:ss")}\" 不能小于 \"{_beginTime.ToString("yyyy-MM-dd HH:mm:ss")} \"");
|
||||||
var tmpTime = _nextTimeFunc(_lastTime);
|
var tmpTime = _nextTimeFunc(_lastTime);
|
||||||
if (dt >= tmpTime && autoExpand)
|
if (dt >= tmpTime && autoExpand)
|
||||||
{
|
{
|
||||||
// 自动建表
|
// 自动建表
|
||||||
ExpandTable(tmpTime, dt);
|
ExpandTable(tmpTime, dt);
|
||||||
}
|
}
|
||||||
for (var a = 0; a < _allTables.Count; a++)
|
lock (_lock)
|
||||||
|
{
|
||||||
|
var allTablesCount = _allTablesTime.Count;
|
||||||
|
for (var a = 0; a < allTablesCount; a++)
|
||||||
if (dt >= _allTablesTime[a])
|
if (dt >= _allTablesTime[a])
|
||||||
return _allTables[a];
|
return _allTables[a];
|
||||||
|
}
|
||||||
throw new Exception($"分表字段值 \"{dt.ToString("yyyy-MM-dd HH:mm:ss")}\" 未匹配到分表名");
|
throw new Exception($"分表字段值 \"{dt.ToString("yyyy-MM-dd HH:mm:ss")}\" 未匹配到分表名");
|
||||||
}
|
}
|
||||||
public ICollection<string> GetTableNamesByColumnValueRange(object columnValue1, object columnValue2)
|
public string[] GetTableNamesByColumnValueRange(object columnValue1, object columnValue2)
|
||||||
{
|
{
|
||||||
var dt1 = ParseColumnValue(columnValue1);
|
var dt1 = ParseColumnValue(columnValue1);
|
||||||
var dt2 = ParseColumnValue(columnValue2);
|
var dt2 = ParseColumnValue(columnValue2);
|
||||||
if (dt1 > dt2) return new string[0];
|
if (dt1 > dt2) return new string[0];
|
||||||
|
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
int dt1idx = 0, dt2idx = 0;
|
int dt1idx = 0, dt2idx = 0;
|
||||||
if (dt1 < _allTablesTime.Last()) dt1idx = _allTablesTime.Count - 1;
|
var allTablesCount = _allTablesTime.Count;
|
||||||
|
if (dt1 < _beginTime) dt1idx = allTablesCount - 1;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (var a = _allTablesTime.Count - 2; a > -1; a--)
|
for (var a = allTablesCount - 2; a > -1; a--)
|
||||||
{
|
{
|
||||||
if (dt1 < _allTablesTime[a])
|
if (dt1 < _allTablesTime[a])
|
||||||
{
|
{
|
||||||
@ -171,7 +187,7 @@ namespace FreeSql.DataAnnotations
|
|||||||
if (dt2 > _allTablesTime.First()) dt2idx = 0;
|
if (dt2 > _allTablesTime.First()) dt2idx = 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (var a = 0; a < _allTablesTime.Count; a++)
|
for (var a = 0; a < allTablesCount; a++)
|
||||||
{
|
{
|
||||||
if (dt2 >= _allTablesTime[a])
|
if (dt2 >= _allTablesTime[a])
|
||||||
{
|
{
|
||||||
@ -182,10 +198,11 @@ namespace FreeSql.DataAnnotations
|
|||||||
}
|
}
|
||||||
if (dt2idx == -1) return new string[0];
|
if (dt2idx == -1) return new string[0];
|
||||||
|
|
||||||
if (dt1idx == _allTables.Count - 1 && dt2idx == 0) return _allTables;
|
if (dt1idx == allTablesCount - 1 && dt2idx == 0) return _allTables.ToArray();
|
||||||
var names = _allTables.GetRange(dt2idx, dt1idx - dt2idx + 1);
|
var names = _allTables.GetRange(dt2idx, dt1idx - dt2idx + 1).ToArray();
|
||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static readonly ConcurrentDictionary<string, Regex[]> _dicRegSqlWhereDateTimes = new ConcurrentDictionary<string, Regex[]>();
|
static readonly ConcurrentDictionary<string, Regex[]> _dicRegSqlWhereDateTimes = new ConcurrentDictionary<string, Regex[]>();
|
||||||
static Regex[] GetRegSqlWhereDateTimes(string columnName, string quoteParameterName)
|
static Regex[] GetRegSqlWhereDateTimes(string columnName, string quoteParameterName)
|
||||||
@ -239,9 +256,9 @@ namespace FreeSql.DataAnnotations
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
/// <exception cref="Exception"></exception>
|
/// <exception cref="Exception"></exception>
|
||||||
public ICollection<string> GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils)
|
public string[] GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(sqlWhere)) return _allTables;
|
if (string.IsNullOrWhiteSpace(sqlWhere)) return AllTables;
|
||||||
var quoteParameterName = commonUtils.QuoteParamterName("");
|
var quoteParameterName = commonUtils.QuoteParamterName("");
|
||||||
var quoteParameterNameCharArray = quoteParameterName.ToCharArray();
|
var quoteParameterNameCharArray = quoteParameterName.ToCharArray();
|
||||||
var columnName = commonUtils.QuoteSqlName(tb.Table.AsTableColumn.Attribute.Name);
|
var columnName = commonUtils.QuoteSqlName(tb.Table.AsTableColumn.Attribute.Name);
|
||||||
@ -278,9 +295,9 @@ namespace FreeSql.DataAnnotations
|
|||||||
if (val1 == null) throw new Exception($"未能解析分表字段值 {sqlWhere}");
|
if (val1 == null) throw new Exception($"未能解析分表字段值 {sqlWhere}");
|
||||||
return LocalGetTables2(m.Groups[1].Value, ParseColumnValue(val1));
|
return LocalGetTables2(m.Groups[1].Value, ParseColumnValue(val1));
|
||||||
}
|
}
|
||||||
return _allTables;
|
return AllTables;
|
||||||
|
|
||||||
ICollection<string> LocalGetTables(string opt1, string opt2, DateTime val1, DateTime val2)
|
string[] LocalGetTables(string opt1, string opt2, DateTime val1, DateTime val2)
|
||||||
{
|
{
|
||||||
switch (opt1)
|
switch (opt1)
|
||||||
{
|
{
|
||||||
@ -319,9 +336,9 @@ namespace FreeSql.DataAnnotations
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return _allTables;
|
return AllTables;
|
||||||
}
|
}
|
||||||
ICollection<string> LocalGetTables2(string opt, DateTime val1)
|
string[] LocalGetTables2(string opt, DateTime val1)
|
||||||
{
|
{
|
||||||
switch (m.Groups[1].Value)
|
switch (m.Groups[1].Value)
|
||||||
{
|
{
|
||||||
@ -336,10 +353,19 @@ namespace FreeSql.DataAnnotations
|
|||||||
case ">=":
|
case ">=":
|
||||||
return GetTableNamesByColumnValueRange(val1, _lastTime);
|
return GetTableNamesByColumnValueRange(val1, _lastTime);
|
||||||
}
|
}
|
||||||
return _allTables;
|
return AllTables;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICollection<string> AllTables => _allTables;
|
public string[] AllTables
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
return _allTables.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -469,8 +469,8 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
if (tb.Table.AsTableImpl != null)
|
if (tb.Table.AsTableImpl != null)
|
||||||
{
|
{
|
||||||
string[] aret = null;
|
string[] aret = null;
|
||||||
if (_where.Length == 0) aret = tb.Table.AsTableImpl.AllTables.ToArray();
|
if (_where.Length == 0) aret = tb.Table.AsTableImpl.AllTables;
|
||||||
else aret = tb.Table.AsTableImpl.GetTableNamesBySqlWhere(_where.ToString(), _params, tb, _commonUtils).ToArray();
|
else aret = tb.Table.AsTableImpl.GetTableNamesBySqlWhere(_where.ToString(), _params, tb, _commonUtils);
|
||||||
if (aret.Any() == false) aret = tb.Table.AsTableImpl.AllTables.Take(1).ToArray();
|
if (aret.Any() == false) aret = tb.Table.AsTableImpl.AllTables.Take(1).ToArray();
|
||||||
|
|
||||||
for (var a = 0; a < aret.Length; a++)
|
for (var a = 0; a < aret.Length; a++)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user