From d23dfdb47a3023eb697b411612a3bc43e2c0a74a Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Fri, 23 Nov 2018 12:07:59 +0800 Subject: [PATCH] fix #120 trimspace before check single line comment --- ast/testdata/TestSplitStatement.golden | 2 ++ ast/token.go | 8 +++++--- ast/token_test.go | 7 +++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ast/testdata/TestSplitStatement.golden b/ast/testdata/TestSplitStatement.golden index 8d08708..2bec028 100644 --- a/ast/testdata/TestSplitStatement.golden +++ b/ast/testdata/TestSplitStatement.golden @@ -31,6 +31,8 @@ where col = 1 -- from tb where col = 1 +17 +-- comment 0 select * from test\Ghello 1 select 'hello\Gworld', col from test\Ghello 2 -- select * from test\Ghello diff --git a/ast/token.go b/ast/token.go index c326127..ac0add0 100644 --- a/ast/token.go +++ b/ast/token.go @@ -925,16 +925,18 @@ func SplitStatement(buf []byte, delimiter []byte) (string, string, []byte) { } // new line end single line comment - if singleLineComment { - if b == '\r' || b == '\n' { + if b == '\r' || b == '\n' { + if singleLineComment { sql = string(buf[:i]) - if strings.HasPrefix(sql, "--") || strings.HasPrefix(sql, "#") { + if strings.HasPrefix(strings.TrimSpace(sql), "--") || + strings.HasPrefix(strings.TrimSpace(sql), "#") { // just comment, query start with '--', '#' break } // comment in multi-line sql continue } + continue } // multi line comment diff --git a/ast/token_test.go b/ast/token_test.go index a62b3fa..5acce53 100644 --- a/ast/token_test.go +++ b/ast/token_test.go @@ -127,6 +127,7 @@ func TestSplitStatement(t *testing.T) { []byte(`--`), []byte(`-- comment`), []byte(`# comment`), + // https://github.com/XiaoMi/soar/issues/116 []byte(`select * -- comment @@ -145,6 +146,12 @@ where col = 1`), -- from tb where col = 1`), + // https://github.com/XiaoMi/soar/issues/120 + []byte(` +-- comment +select col from tb; +select col from tb; +`), } buf2s := [][]byte{ []byte("select * from test\\Ghello"), -- GitLab