Skip to content

Commit dcf8eb2

Browse files
Andreas AlbrechtAndreas Albrecht
authored andcommitted
Merge branch 'issue_425' of https://github.com/fredyw/sqlparse into fredyw-issue_425
2 parents 6a7eb24 + 6c7b89c commit dcf8eb2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

sqlparse/filters/others.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def get_next_comment():
2626
if (prev_ is None or next_ is None
2727
or prev_.is_whitespace or prev_.match(T.Punctuation, '(')
2828
or next_.is_whitespace or next_.match(T.Punctuation, ')')):
29+
# Insert a whitespace to ensure the following SQL produces
30+
# a valid SQL (see #425). For example:
31+
#
32+
# Before: select a--comment\nfrom foo
33+
# After: select a from foo
34+
if prev_ is not None and next_ is None:
35+
tlist.tokens.insert(tidx, sql.Token(T.Whitespace, ' '))
2936
tlist.tokens.remove(token)
3037
else:
3138
tlist.tokens[tidx] = sql.Token(T.Whitespace, ' ')

tests/test_format.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ def test_strip_comments_single(self):
5050
sql = 'select-- foo\nfrom -- bar\nwhere'
5151
res = sqlparse.format(sql, strip_comments=True)
5252
assert res == 'select from where'
53+
sql = 'select *-- statement starts here\n\nfrom foo'
54+
res = sqlparse.format(sql, strip_comments=True)
55+
assert res == 'select * from foo'
56+
sql = 'select * from foo-- statement starts here\nwhere'
57+
res = sqlparse.format(sql, strip_comments=True)
58+
assert res == 'select * from foo where'
59+
sql = 'select a-- statement starts here\nfrom foo'
60+
res = sqlparse.format(sql, strip_comments=True)
61+
assert res == 'select a from foo'
62+
sql = '--comment\nselect a-- statement starts here\n' \
63+
'from foo--comment\nf'
64+
res = sqlparse.format(sql, strip_comments=True)
65+
assert res == 'select a from foo f'
5366

5467
def test_strip_comments_invalid_option(self):
5568
sql = 'select-- foo\nfrom -- bar\nwhere'

0 commit comments

Comments
 (0)