Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hhhax opened this issue Feb 10, 2023 · 1 comment
Open
Assignees

Comments

@hhhax
Copy link

hhhax commented Feb 10, 2023

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

@hhhax hhhax changed the title mysql使用了for update报 mysql使用了for update报expected semicolon or EOF, found 'FOR'错误 Feb 10, 2023
@fly-studio
Copy link

fly-studio commented May 4, 2023

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants