|
@@ -61,4 +61,45 @@ func (s *MonthlyShardingStrategy) IsNewPeriod(lastTableTime, currentTime time.Ti
|
|
|
currentMonth := time.Date(currentTime.Year(), currentTime.Month(), 1, 0, 0, 0, 0, currentTime.Location())
|
|
|
|
|
|
return currentMonth.After(lastMonth)
|
|
|
+}
|
|
|
+
|
|
|
+// DailyShardingStrategy 按日分表策略 - 每天分表
|
|
|
+type DailyShardingStrategy struct{}
|
|
|
+
|
|
|
+func NewDailyShardingStrategy() ShardingStrategy {
|
|
|
+ return &DailyShardingStrategy{}
|
|
|
+}
|
|
|
+
|
|
|
+func (s *DailyShardingStrategy) GetTableName(baseTableName string, t time.Time) string {
|
|
|
+ return fmt.Sprintf("%s_%d%02d%02d", baseTableName, t.Year(), t.Month(), t.Day())
|
|
|
+}
|
|
|
+
|
|
|
+func (s *DailyShardingStrategy) GetCurrentTableName(baseTableName string) string {
|
|
|
+ return s.GetTableName(baseTableName, time.Now())
|
|
|
+}
|
|
|
+
|
|
|
+func (s *DailyShardingStrategy) GetTableNamesByRange(baseTableName string, start, end time.Time) []string {
|
|
|
+ var tableNames []string
|
|
|
+ current := time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, start.Location())
|
|
|
+ endDay := time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, end.Location())
|
|
|
+
|
|
|
+ for !current.After(endDay) {
|
|
|
+ tableNames = append(tableNames, s.GetTableName(baseTableName, current))
|
|
|
+ current = current.AddDate(0, 0, 1) // 每次增加一天
|
|
|
+ }
|
|
|
+
|
|
|
+ return tableNames
|
|
|
+}
|
|
|
+
|
|
|
+func (s *DailyShardingStrategy) GetNextTableName(currentTableName string) string {
|
|
|
+ // 日度分表的序号表名
|
|
|
+ return fmt.Sprintf("%s_01", currentTableName)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *DailyShardingStrategy) IsNewPeriod(lastTableTime, currentTime time.Time) bool {
|
|
|
+ // 检查是否到了新的一天
|
|
|
+ lastDay := time.Date(lastTableTime.Year(), lastTableTime.Month(), lastTableTime.Day(), 0, 0, 0, 0, lastTableTime.Location())
|
|
|
+ currentDay := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentTime.Location())
|
|
|
+
|
|
|
+ return currentDay.After(lastDay)
|
|
|
}
|