diff options
| -rw-r--r-- | sqlparse/filters/others.py | 7 | ||||
| -rw-r--r-- | tests/test_format.py | 13 |
2 files changed, 20 insertions, 0 deletions
diff --git a/sqlparse/filters/others.py b/sqlparse/filters/others.py index eeb91f1..408f5d2 100644 --- a/sqlparse/filters/others.py +++ b/sqlparse/filters/others.py @@ -26,6 +26,13 @@ class StripCommentsFilter(object): if (prev_ is None or next_ is None or prev_.is_whitespace or prev_.match(T.Punctuation, '(') or next_.is_whitespace or next_.match(T.Punctuation, ')')): + # Insert a whitespace to ensure the following SQL produces + # a valid SQL (see #425). For example: + # + # Before: select a--comment\nfrom foo + # After: select a from foo + if prev_ is not None and next_ is None: + tlist.tokens.insert(tidx, sql.Token(T.Whitespace, ' ')) tlist.tokens.remove(token) else: tlist.tokens[tidx] = sql.Token(T.Whitespace, ' ') diff --git a/tests/test_format.py b/tests/test_format.py index e87e041..acee9ac 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -50,6 +50,19 @@ class TestFormat(object): sql = 'select-- foo\nfrom -- bar\nwhere' res = sqlparse.format(sql, strip_comments=True) assert res == 'select from where' + sql = 'select *-- statement starts here\n\nfrom foo' + res = sqlparse.format(sql, strip_comments=True) + assert res == 'select * from foo' + sql = 'select * from foo-- statement starts here\nwhere' + res = sqlparse.format(sql, strip_comments=True) + assert res == 'select * from foo where' + sql = 'select a-- statement starts here\nfrom foo' + res = sqlparse.format(sql, strip_comments=True) + assert res == 'select a from foo' + sql = '--comment\nselect a-- statement starts here\n' \ + 'from foo--comment\nf' + res = sqlparse.format(sql, strip_comments=True) + assert res == 'select a from foo f' def test_strip_comments_invalid_option(self): sql = 'select-- foo\nfrom -- bar\nwhere' |
