config重构

This commit is contained in:
linwenxiang committed 2020-12-08 20:28:26 +08:00
1 parent 0810383afc
commit 5f873914e0
14 files changed
+134 -208

No files matched your search

+15
View File
@@ -1,8 +1,11 @@
package table
import (
"fmt"
"hash/crc32"
"strconv"
"gorm.io/gorm"
)
// Crc32Hash 用于32张分表
@@ -22,3 +25,15 @@ func Crc8Hash(src string) string {
sum := crc32.ChecksumIEEE([]byte(src)) % 8
return strconv.Itoa(int(sum))
}
// e.g. DB.Scopes(DynamicTable(Crc32Hash, "test", "小圈圈")).Find(&tests)
// DynamicTable 设置动态表名scope params: f 分表计算函数 baseTable 基础表名 fieldValue 参与分表字段
func DynamicTable(f func(string) string, baseTable, fieldValue string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Table(fmt.Sprintf("%s_%s", baseTable, f(fieldValue)))
}
}
func CreateSubTable(f func(string) string) {
}