Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions sharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (s *Sharding) resolve(query string, args ...any) (ftQuery, stQuery, tableNa
var value any
var id int64
var keyFind bool
value, id, keyFind, err = s.nonInsertValue(r.ShardingKey, condition, args...)
value, id, keyFind, err = s.nonInsertValue(r, condition, args...)
if err != nil {
return
}
Expand Down Expand Up @@ -501,7 +501,7 @@ func (s *Sharding) insertValue(key string, names []*sqlparser.Ident, exprs []sql
return
}

func (s *Sharding) nonInsertValue(key string, condition sqlparser.Expr, args ...any) (value any, id int64, keyFind bool, err error) {
func (s *Sharding) nonInsertValue(r Config, condition sqlparser.Expr, args ...any) (value any, id int64, keyFind bool, err error) {
err = sqlparser.Walk(sqlparser.VisitFunc(func(node sqlparser.Node) error {
if n, ok := node.(*sqlparser.BinaryExpr); ok {
x, ok := n.X.(*sqlparser.Ident)
Expand All @@ -512,7 +512,7 @@ func (s *Sharding) nonInsertValue(key string, condition sqlparser.Expr, args ...
}
}
if ok {
if x.Name == key && n.Op == sqlparser.EQ {
if x.Name == r.ShardingKey && n.Op == sqlparser.EQ {
keyFind = true
switch expr := n.Y.(type) {
case *sqlparser.BindExpr:
Expand All @@ -529,12 +529,26 @@ func (s *Sharding) nonInsertValue(key string, condition sqlparser.Expr, args ...
switch expr := n.Y.(type) {
case *sqlparser.BindExpr:
v := args[expr.Pos]
if r.ShardingAlgorithm != nil {
keyFind = true
if value == nil {
value = v
}
return nil
}
var ok bool
if id, ok = v.(int64); !ok {
return fmt.Errorf("ID should be int64 type")
}
case *sqlparser.NumberLit:
id, err = strconv.ParseInt(expr.Value, 10, 64)
if r.ShardingAlgorithm != nil {
keyFind = true
if value == nil {
value = id
}
return nil
}
if err != nil {
return err
}
Expand Down
Loading