Skip to content

Commit a17db7a

Browse files
committed
Improve parsing of inline comments for identifiers (fixes andialbrecht#163).
1 parent c8f2feb commit a17db7a

4 files changed

Lines changed: 13 additions & 1 deletion

File tree

CHANGES

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Bug Fixes
55
* Fix a regression for identifiers with square bracktes notation (issue153).
66

77
Enhancements
8-
* Improved formatting of HAVING statements.
8+
* Improve formatting of HAVING statements.
9+
* Improve parsing of inline comments (issue163).
910

1011

1112
Release 0.1.14 (Nov 30, 2014)

sqlparse/engine/grouping.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def _consume_cycle(tl, i):
172172
if next(x)(t):
173173
yield t
174174
else:
175+
if isinstance(t, sql.Comment) and t.is_multiline():
176+
yield t
175177
raise StopIteration
176178

177179
def _next_token(tl, i):

sqlparse/sql.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ class Comment(TokenList):
559559
"""A comment."""
560560
__slots__ = ('value', 'ttype', 'tokens')
561561

562+
def is_multiline(self):
563+
return self.tokens and self.tokens[0].ttype == T.Comment.Multiline
564+
562565

563566
class Where(TokenList):
564567
"""A WHERE clause."""

tests/test_grouping.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ def test_identifier_list_other(self): # issue2
131131
l = p.tokens[2]
132132
self.assertEqual(len(l.tokens), 13)
133133

134+
def test_identifier_list_with_inline_comments(self): # issue163
135+
p = sqlparse.parse('foo /* a comment */, bar')[0]
136+
self.assert_(isinstance(p.tokens[0], sql.IdentifierList))
137+
self.assert_(isinstance(p.tokens[0].tokens[0], sql.Identifier))
138+
self.assert_(isinstance(p.tokens[0].tokens[3], sql.Identifier))
139+
134140
def test_where(self):
135141
s = 'select * from foo where bar = 1 order by id desc'
136142
p = sqlparse.parse(s)[0]

0 commit comments

Comments
 (0)