forked from go-gorm/sharding
/
dialector.go
150 lines (125 loc) · 3.16 KB
/
dialector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package sharding
import (
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
)
type ShardingDialector struct {
gorm.Dialector
sharding *Sharding
}
type ShardingMigrator struct {
gorm.Migrator
sharding *Sharding
dialector gorm.Dialector
}
func (m ShardingMigrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{}) {
for _, opt := range opts {
str := stmt.Quote(opt.DBName)
if opt.Expression != "" {
str = opt.Expression
} else if opt.Length > 0 {
str += fmt.Sprintf("(%d)", opt.Length)
}
if opt.Collate != "" {
str += " COLLATE " + opt.Collate
}
if opt.Sort != "" {
str += " " + opt.Sort
}
results = append(results, clause.Expr{SQL: str})
}
return
}
func NewShardingDialector(d gorm.Dialector, s *Sharding) ShardingDialector {
return ShardingDialector{
Dialector: d,
sharding: s,
}
}
func (d ShardingDialector) Migrator(db *gorm.DB) gorm.Migrator {
m := d.Dialector.Migrator(db)
return ShardingMigrator{
Migrator: m,
sharding: d.sharding,
dialector: d.Dialector,
}
}
func (m ShardingMigrator) AutoMigrate(dst ...interface{}) error {
shardingDsts, noShardingDsts, err := m.splitShardingDsts(dst...)
if err != nil {
return err
}
stmt := &gorm.Statement{DB: m.sharding.DB}
for _, sd := range shardingDsts {
tx := stmt.DB.Session(&gorm.Session{}).Table(sd.table)
if err := m.dialector.Migrator(tx).AutoMigrate(sd.dst); err != nil {
return err
}
}
if len(noShardingDsts) > 0 {
tx := stmt.DB.Session(&gorm.Session{})
tx.Statement.Settings.Store(ShardingIgnoreStoreKey, nil)
defer tx.Statement.Settings.Delete(ShardingIgnoreStoreKey)
if err := m.dialector.Migrator(tx).AutoMigrate(noShardingDsts...); err != nil {
return err
}
}
return nil
}
func (m ShardingMigrator) DropTable(dst ...interface{}) error {
shardingDsts, noShardingDsts, err := m.splitShardingDsts(dst...)
if err != nil {
return err
}
for _, sd := range shardingDsts {
if err := m.Migrator.DropTable(sd.table); err != nil {
return err
}
}
if len(noShardingDsts) > 0 {
if err := m.Migrator.DropTable(noShardingDsts...); err != nil {
return err
}
}
return nil
}
type shardingDst struct {
table string
dst interface{}
}
// splite sharding or normal dsts
func (m ShardingMigrator) splitShardingDsts(dsts ...interface{}) (shardingDsts []shardingDst,
noShardingDsts []interface{}, err error) {
shardingDsts = make([]shardingDst, 0)
noShardingDsts = make([]interface{}, 0)
for _, model := range dsts {
stmt := &gorm.Statement{DB: m.sharding.DB}
err = stmt.Parse(model)
if err != nil {
return
}
if cfg, ok := m.sharding.configs[stmt.Table]; ok {
// support sharding table
suffixs := cfg.ShardingSuffixs()
if len(suffixs) == 0 {
err = fmt.Errorf("sharding table:%s suffixs is empty", stmt.Table)
return
}
for _, suffix := range suffixs {
shardingTable := stmt.Table + suffix
shardingDsts = append(shardingDsts, shardingDst{
table: shardingTable,
dst: model,
})
}
if cfg.DoubleWrite {
noShardingDsts = append(noShardingDsts, model)
}
} else {
noShardingDsts = append(noShardingDsts, model)
}
}
return
}