diff --git a/ast/testdata/TestSplitStatement.golden b/ast/testdata/TestSplitStatement.golden index e4d6516323cb82f31ad7c0118570059fe6b10ea9..8d0870839ae367041e80f730c181caa446f392d8 100644 --- a/ast/testdata/TestSplitStatement.golden +++ b/ast/testdata/TestSplitStatement.golden @@ -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 diff --git a/ast/token.go b/ast/token.go index 8502b6349e3a5a83b95fae96255d329a6b480781..c326127d8c0be2da7da2681dfca00f3992c6fed9 100644 --- a/ast/token.go +++ b/ast/token.go @@ -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 } } diff --git a/ast/token_test.go b/ast/token_test.go index 8bb79dd27ead29ed772af977e518a8e83f0e9d26..a62b3faa7e3b25127785923b04b4aed8132f2a87 100644 --- a/ast/token_test.go +++ b/ast/token_test.go @@ -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{