Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  trimspace before check single line comment
  • Loading branch information
martianzhang committed Nov 23, 2018
1 parent a4a641a commit d23dfdb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ast/testdata/TestSplitStatement.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions ast/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions ast/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func TestSplitStatement(t *testing.T) {
[]byte(`--`),
[]byte(`-- comment`),
[]byte(`# comment`),
// https://github.com/XiaoMi/soar/issues/116
[]byte(`select
*
-- comment
Expand All @@ -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"),
Expand Down

0 comments on commit d23dfdb

Please sign in to comment.