Skip to content

Commit b3700f4

Browse files
committed
Previous fix for period failed when another token (non-groupable) followed.
1 parent 24f0d3d commit b3700f4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

sqlparse/engine/grouping.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,17 @@ def valid_prev(token):
9797
return imt(token, i=sqlcls, t=ttypes)
9898

9999
def valid_next(token):
100+
# issue261, allow invalid next token
101+
return True
102+
103+
def post(tlist, pidx, tidx, nidx):
104+
# next_ validation is being performed here. issue261
100105
sqlcls = sql.SquareBrackets, sql.Function
101106
ttypes = T.Name, T.String.Symbol, T.Wildcard
102-
return token is None or imt(token, i=sqlcls, t=ttypes)
107+
next_ = tlist[nidx] if nidx is not None else None
108+
valid_next = imt(next_, i=sqlcls, t=ttypes)
103109

104-
def post(tlist, pidx, tidx, nidx):
105-
return (pidx, nidx) if nidx is not None else (pidx, tidx)
110+
return (pidx, nidx) if valid_next else (pidx, tidx)
106111

107112
_group(tlist, sql.Identifier, match, valid_prev, valid_next, post)
108113

tests/test_grouping.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ def test_identifier_invalid(self):
9393
self.assertEqual(p.tokens[0].get_real_name(), None)
9494
self.assertEqual(p.tokens[0].get_parent_name(), 'a')
9595

96+
def test_identifier_invalid_in_middle(self):
97+
# issue261
98+
s = 'SELECT foo. FROM foo'
99+
p = sqlparse.parse(s)[0]
100+
assert isinstance(p[2], sql.Identifier)
101+
assert p[2][1].ttype == T.Punctuation
102+
assert p[3].ttype == T.Whitespace
103+
assert str(p[2]) == 'foo.'
104+
96105
def test_identifier_as_invalid(self): # issue8
97106
p = sqlparse.parse('foo as select *')[0]
98107
self.assert_(len(p.tokens), 5)

0 commit comments

Comments
 (0)