config重构
This commit is contained in:
1 parent
0810383afc
commit
5f873914e0
14 files changed
+134
-208
No files matched your search
@@ -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) {
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user