提交 6474f630 编写于 作者: martianzhang's avatar martianzhang

fix SplitStatement cause endless loop bug

上级 4aa713f2
......@@ -851,7 +851,7 @@ func Compress(sql string) string {
}
// SplitStatement SQL切分
// return original sql, remove comment sql, left over buf
// return 1. original sql, 2. remove comment sql, 3. left over buf
func SplitStatement(buf []byte, delimiter []byte) (string, string, []byte) {
var singleLineComment bool
var multiLineComment bool
......
......@@ -172,6 +172,7 @@ select col from tb;
[]byte(`select /*!50000 1,*/ 1;`), // 20
[]byte(`UPDATE xxx SET c1=' LOGGER.error(""); }' WHERE id = 2 ;`), // 21
[]byte("UPDATE `xxx` SET aaa='a;' WHERE `id` = 15;"), // 22
// []byte(`/* comment here */ SET MAX_JOIN_SIZE=#`), // 23
}
// \G 分隔符
buf2s := [][]byte{
......@@ -185,9 +186,10 @@ select col from tb;
\\G*/
from test\\Ghello`), // 6
}
// single sql test
// SplitStatement(bufs[22], []byte(";"))
// return
//// single sql test
//b, t1, t2 := SplitStatement(bufs[23], []byte(";"))
//fmt.Println("buf: ", b, "sql: ", t1, "left: ", string(t2))
//return
err := common.GoldenDiff(func() {
for i, buf := range bufs {
sql, _, _ := SplitStatement(buf, []byte(";"))
......
......@@ -119,7 +119,14 @@ func main() {
// leftLineCounter
llc := ast.LeftNewLines([]byte(orgSQL))
lineCounter += llc
buf = string(bufBytes)
if len(buf) == len(bufBytes) {
// 防止切分死循环,当剩余的内容和原 SQL 相同时直接清空 buf
buf = ""
orgSQL = string(bufBytes)
sql = orgSQL
} else {
buf = string(bufBytes)
}
// 去除无用的备注和空格
sql = database.RemoveSQLComments(sql)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册