forked from go-gorm/sharding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharding.go
314 lines (277 loc) · 7.38 KB
/
sharding.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package gormsharding
import (
"errors"
"strconv"
"strings"
"sync"
"github.com/longbridgeapp/sqlparser"
"gorm.io/gorm"
)
var (
ErrMissingShardingKey = errors.New("sharding key or id required, and use operator =")
ErrInvalidID = errors.New("invalid id format")
)
type Sharding struct {
*gorm.DB
ConnPool *ConnPool
Resolvers map[string]Resolver
querys sync.Map
}
type Resolver struct {
// EnableFullTable whether to enable a full table
EnableFullTable bool
// ShardingColumn column name of the sharding column, for example: user_id
ShardingColumn string
// ShardingAlgorithm implement the sharding algorithm for generate table name suffix
ShardingAlgorithm func(columnValue interface{}) (suffix string, err error)
// ShardingAlgorithm sharding algorithm by primary key, if it posible.
ShardingAlgorithmByPrimaryKey func(id int64) (suffix string)
// PrimaryKeyGenerate for generate primary key
PrimaryKeyGenerate func(tableIdx int64) int64
}
func Register(resolvers map[string]Resolver) Sharding {
return Sharding{Resolvers: resolvers}
}
// Name plugin name for Gorm plugin interface
func (s *Sharding) Name() string {
return "gorm:sharding"
}
// LastQuery get last SQL query
func (s *Sharding) LastQuery() string {
if query, ok := s.querys.Load("last_query"); ok {
return query.(string)
}
return ""
}
// Initialize implement for Gorm plugin interface
func (s *Sharding) Initialize(db *gorm.DB) error {
s.DB = db
s.registerConnPool(db)
return nil
}
// resolve split the old query to full table query and sharding table query
func (s *Sharding) resolve(query string, args ...interface{}) (ftQuery, stQuery, tableName string, err error) {
ftQuery = query
stQuery = query
if len(s.Resolvers) == 0 {
return
}
expr, err := sqlparser.NewParser(strings.NewReader(query)).ParseStatement()
if err != nil {
return ftQuery, stQuery, tableName, nil
}
var table *sqlparser.TableName
var condition sqlparser.Expr
var isInsert bool
var insertNames []*sqlparser.Ident
var insertValues []sqlparser.Expr
switch stmt := expr.(type) {
case *sqlparser.SelectStatement:
tbl, ok := stmt.FromItems.(*sqlparser.TableName)
if !ok {
return
}
if stmt.Hint != nil && stmt.Hint.Value == "nosharding" {
return
}
table = tbl
condition = stmt.Condition
case *sqlparser.InsertStatement:
table = stmt.TableName
isInsert = true
insertNames = stmt.ColumnNames
insertValues = stmt.Expressions[0].Exprs
case *sqlparser.UpdateStatement:
condition = stmt.Condition
table = stmt.TableName
case *sqlparser.DeleteStatement:
condition = stmt.Condition
table = stmt.TableName
default:
return ftQuery, stQuery, "", sqlparser.ErrNotImplemented
}
tableName = table.Name.Name
r, ok := s.Resolvers[tableName]
if !ok {
return
}
var value interface{}
var isID bool
if isInsert {
value, isID, err = s.insertValue(r.ShardingColumn, insertNames, insertValues, args...)
if err != nil {
return
}
} else {
value, isID, err = s.nonInsertValue(r.ShardingColumn, condition, args...)
if err != nil {
return
}
}
var suffix string
if isID {
if id, ok := value.(int64); ok {
suffix = r.ShardingAlgorithmByPrimaryKey(id)
} else if idUint, ok := value.(uint64); ok {
suffix = r.ShardingAlgorithmByPrimaryKey(int64(idUint))
} else if idStr, ok := value.(string); ok {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return ftQuery, stQuery, tableName, ErrInvalidID
}
suffix = r.ShardingAlgorithmByPrimaryKey(id)
} else {
return ftQuery, stQuery, tableName, ErrInvalidID
}
} else {
suffix, err = r.ShardingAlgorithm(value)
if err != nil {
return
}
}
newTable := &sqlparser.TableName{Name: &sqlparser.Ident{Name: tableName + suffix}}
fillID := true
if isInsert {
for _, name := range insertNames {
if name.Name == "id" {
fillID = false
break
}
}
if fillID {
tblIdx, err := strconv.Atoi(strings.Replace(suffix, "_", "", 1))
if err != nil {
return ftQuery, stQuery, tableName, err
}
id := r.PrimaryKeyGenerate(int64(tblIdx))
insertNames = append(insertNames, &sqlparser.Ident{Name: "id"})
insertValues = append(insertValues, &sqlparser.NumberLit{Value: strconv.FormatInt(id, 10)})
}
}
switch stmt := expr.(type) {
case *sqlparser.InsertStatement:
if fillID {
stmt.ColumnNames = insertNames
stmt.Expressions[0].Exprs = insertValues
}
ftQuery = stmt.String()
stmt.TableName = newTable
stQuery = stmt.String()
case *sqlparser.SelectStatement:
ftQuery = stmt.String()
stmt.FromItems = newTable
stmt.OrderBy = replaceOrderByTableName(stmt.OrderBy, tableName, newTable.Name.Name)
stQuery = stmt.String()
case *sqlparser.UpdateStatement:
ftQuery = stmt.String()
stmt.TableName = newTable
stQuery = stmt.String()
case *sqlparser.DeleteStatement:
ftQuery = stmt.String()
stmt.TableName = newTable
stQuery = stmt.String()
}
return
}
func (s *Sharding) insertValue(key string, names []*sqlparser.Ident, exprs []sqlparser.Expr, args ...interface{}) (value interface{}, isID bool, err error) {
bind := false
find := false
if len(names) != len(exprs) {
return nil, false, errors.New("column names and expressions mismatch")
}
for i, name := range names {
if name.Name == key {
switch expr := exprs[i].(type) {
case *sqlparser.BindExpr:
bind = true
value = expr.Name
case *sqlparser.StringLit:
value = expr.Value
case *sqlparser.NumberLit:
value = expr.Value
default:
return nil, false, sqlparser.ErrNotImplemented
}
find = true
break
}
}
if !find {
return nil, false, ErrMissingShardingKey
}
if bind {
value, err = getBindValue(value, args)
}
return
}
func (s *Sharding) nonInsertValue(key string, condition sqlparser.Expr, args ...interface{}) (value interface{}, isID bool, err error) {
bind := false
find := false
err = sqlparser.Walk(sqlparser.VisitFunc(func(node sqlparser.Node) error {
if n, ok := node.(*sqlparser.BinaryExpr); ok {
if x, ok := n.X.(*sqlparser.Ident); ok {
if x.Name == key && n.Op == sqlparser.EQ {
find = true
isID = false
bind = false
switch expr := n.Y.(type) {
case *sqlparser.BindExpr:
bind = true
value = expr.Name
case *sqlparser.StringLit:
value = expr.Value
case *sqlparser.NumberLit:
value = expr.Value
default:
return sqlparser.ErrNotImplemented
}
return nil
} else if x.Name == "id" && n.Op == sqlparser.EQ {
find = true
isID = true
bind = false
switch expr := n.Y.(type) {
case *sqlparser.BindExpr:
bind = true
value = expr.Name
case *sqlparser.NumberLit:
value = expr.Value
default:
return ErrInvalidID
}
return nil
}
}
}
return nil
}), condition)
if err != nil {
return
}
if !find {
return nil, false, ErrMissingShardingKey
}
if bind {
value, err = getBindValue(value, args)
}
return
}
func replaceOrderByTableName(orderBy []*sqlparser.OrderingTerm, oldName, newName string) []*sqlparser.OrderingTerm {
for i, term := range orderBy {
if x, ok := term.X.(*sqlparser.QualifiedRef); ok {
if x.Table.Name == oldName {
x.Table.Name = newName
orderBy[i].X = x
}
}
}
return orderBy
}
func getBindValue(value interface{}, args []interface{}) (interface{}, error) {
bindPos := strings.Replace(value.(string), "$", "", 1)
pos, err := strconv.Atoi(bindPos)
if err != nil {
return nil, err
}
return args[pos-1], nil
}