Skip to content

Commit

Permalink
fix #116
Browse files Browse the repository at this point in the history
  SplitStatement check if single comment line is in multi-line sql.

  pure comment line should break quickly, single comment in multi-line
  sql should wait for line delimiter
  • Loading branch information
martianzhang committed Nov 21, 2018
1 parent eeeab8e commit 147ee2d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions ast/testdata/TestSplitStatement.golden
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
13 select
*
-- comment
from tb
where col = 1
14 select
* --
from tb
where col = 1
15 select
* #
from tb
where col = 1
16 select
*
--
from tb
where col = 1
0 select * from test\Ghello
1 select 'hello\Gworld', col from test\Ghello
2 -- select * from test\Ghello
Expand Down
7 changes: 6 additions & 1 deletion ast/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,12 @@ func SplitStatement(buf []byte, delimiter []byte) (string, string, []byte) {
if singleLineComment {
if b == '\r' || b == '\n' {
sql = string(buf[:i])
break
if strings.HasPrefix(sql, "--") || strings.HasPrefix(sql, "#") {
// just comment, query start with '--', '#'
break
}
// comment in multi-line sql
continue
}
}

Expand Down
13 changes: 13 additions & 0 deletions ast/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ func TestSplitStatement(t *testing.T) {
*
-- comment
from tb
where col = 1`),
[]byte(`select
* --
from tb
where col = 1`),
[]byte(`select
* #
from tb
where col = 1`),
[]byte(`select
*
--
from tb
where col = 1`),
}
buf2s := [][]byte{
Expand Down

0 comments on commit 147ee2d

Please sign in to comment.