From 6474f63029281ede51e3c8f73ae51946dcd97feb Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Wed, 15 Jan 2020 11:06:41 +0800 Subject: [PATCH] fix SplitStatement cause endless loop bug --- ast/token.go | 2 +- ast/token_test.go | 8 +++++--- cmd/soar/soar.go | 9 ++++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ast/token.go b/ast/token.go index e322fcc..d1e4356 100644 --- a/ast/token.go +++ b/ast/token.go @@ -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 diff --git a/ast/token_test.go b/ast/token_test.go index c85f1ea..c0c66ab 100644 --- a/ast/token_test.go +++ b/ast/token_test.go @@ -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(";")) diff --git a/cmd/soar/soar.go b/cmd/soar/soar.go index 94d634f..d543f5f 100644 --- a/cmd/soar/soar.go +++ b/cmd/soar/soar.go @@ -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) -- GitLab