go-gorm / sharding

High performance table sharding plugin for Gorm.
MIT License
276 stars 60 forks source link

mysql使用了for update报expected semicolon or EOF, found 'FOR'错误 #91

Open hhhax opened 1 year ago

hhhax commented 1 year ago

我的sql加上了 for update 提示这个报错

image image
fly-studio commented 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
}