Skip to content

Commit aa1b668

Browse files
committed
- 修复 IAsTable 自动分表不同间隔的 bug;#1698
1 parent 2904ada commit aa1b668

File tree

3 files changed

+317
-216
lines changed

3 files changed

+317
-216
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/DataAnnotations/TableAttribute.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ class DateTimeAsTableImpl : IAsTable
108108
{
109109
readonly object _lock = new object();
110110
readonly List<string> _allTables = new List<string>();
111-
readonly List<DateTime> _allTablesTime = new List<DateTime>();
112-
readonly DateTime _beginTime;
111+
readonly List<DateTime> _allTablesTime = new List<DateTime>();
112+
readonly DateTime _beginTime;
113113
DateTime _lastTime;
114114
Func<DateTime, int, DateTime> _nextTimeFunc;
115115
string _tableName;
@@ -131,20 +131,20 @@ public DateTimeAsTableImpl(string tableName, DateTime beginTime, Func<DateTime,
131131
int GetTimestamp(DateTime dt) => (int)dt.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
132132
void ExpandTable(DateTime beginTime, DateTime endTime)
133133
{
134-
var index = 0;
135134
if (beginTime > endTime) endTime = _nextTimeFunc(beginTime, -1);
136135
lock (_lock)
137-
{
138-
while (beginTime <= endTime)
136+
{
137+
var index = _allTables.Count;
138+
while (beginTime <= endTime)
139139
{
140140
var dtstr = beginTime.ToString(_tableNameFormat.Groups[1].Value);
141141
var name = _tableName.Replace(_tableNameFormat.Groups[0].Value, dtstr);
142142
if (_allTables.Contains(name)) throw new ArgumentException(CoreStrings.Generated_Same_SubTable(_tableName));
143143
_allTables.Insert(0, name);
144-
_allTablesTime.Insert(0, beginTime);
145-
_lastTime = beginTime;
146-
beginTime = _nextTimeFunc(beginTime, index++);
147-
}
144+
_allTablesTime.Insert(0, beginTime);
145+
_lastTime = _nextTimeFunc(beginTime, index++);
146+
beginTime = _lastTime;
147+
}
148148
}
149149
}
150150
public NativeTuple<DateTime, DateTime> GetRangeByTableName(string tableName)
@@ -153,10 +153,10 @@ public NativeTuple<DateTime, DateTime> GetRangeByTableName(string tableName)
153153
{
154154
var idx = _allTables.FindIndex(a => a == tableName);
155155
if (idx == -1) return null;
156-
if (idx == 0) return NativeTuple.Create(_allTablesTime[idx], DateTime.Now);
156+
if (idx == 0) return NativeTuple.Create(_allTablesTime[idx], DateTime.Now);
157157
if (idx == _allTables.Count - 1) return NativeTuple.Create(DateTime.MinValue, _allTablesTime[idx]);
158-
return NativeTuple.Create(_allTablesTime[idx], _allTablesTime[idx - 1]);
159-
}
158+
return NativeTuple.Create(_allTablesTime[idx], _allTablesTime[idx - 1]);
159+
}
160160
}
161161
DateTime ParseColumnValue(object columnValue)
162162
{
@@ -180,11 +180,10 @@ public string GetTableNameByColumnValue(object columnValue, bool autoExpand = fa
180180
{
181181
var dt = ParseColumnValue(columnValue);
182182
if (dt < _beginTime) throw new Exception(CoreStrings.SubTableFieldValue_CannotLessThen(dt.ToString("yyyy-MM-dd HH:mm:ss"), _beginTime.ToString("yyyy-MM-dd HH:mm:ss")));
183-
var tmpTime = _nextTimeFunc(_lastTime, -1);
184-
if (dt >= tmpTime && autoExpand)
183+
if (dt >= _lastTime && autoExpand)
185184
{
186-
// 自动建表
187-
ExpandTable(tmpTime, dt);
185+
// 扩容分表
186+
ExpandTable(_lastTime, dt);
188187
}
189188
lock (_lock)
190189
{
@@ -217,7 +216,7 @@ public string[] GetTableNamesByColumnValueRange(object columnValue1, object colu
217216
}
218217
}
219218
}
220-
if (dt2 > _allTablesTime.First()) dt2idx = 0;
219+
if (dt2 > _allTablesTime[0]) dt2idx = 0;
221220
else
222221
{
223222
for (var a = 0; a < allTablesCount; a++)

0 commit comments

Comments
 (0)