Open hhhax opened 1 year ago
sharing遇到for update时,无法正常分表
问题出现在:Sharding.resolve
中的expr, err := sqlparser.NewParser(strings.NewReader(query)).ParseStatement()
sqlparser不识别for update
语句,以至于if后直接return,导致无法进行分表操作
需要修复
`github.com/longbridgeapp/sqlparser`
临时的解决方法是用RAW来写语句,table的分表名用下面的算法得出,分表的计算方法如下:
func ShardingAlgorithm(numberOfShards int, value any) (suffix string, err error) {
var tableFormat string
if numberOfShards == 0 {
return "", errors.New("specify NumberOfShards or ShardingAlgorithm")
}
if numberOfShards < 10 {
tableFormat = "_%01d"
} else if numberOfShards < 100 {
tableFormat = "_%02d"
} else if numberOfShards < 1000 {
tableFormat = "_%03d"
} else if numberOfShards < 10000 {
tableFormat = "_%04d"
}
id := 0
switch value := value.(type) {
case int:
id = value
case int64:
id = int(value)
case string:
id, err = strconv.Atoi(value)
if err != nil {
id = int(crc32.ChecksumIEEE([]byte(value)))
}
default:
return "", errors.Errorf("default algorithm only support integer and string column," +
"if you use other type, specify you own ShardingAlgorithm")
}
return fmt.Sprintf(tableFormat, id%numberOfShards), nil
}
我的sql加上了 for update 提示这个报错