Skip to content

Commit 82b3b6b

Browse files
committed
Refactor grouping
Alternate form to transverse the tlists and clean up matching.
1 parent 90ef13b commit 82b3b6b

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

sqlparse/engine/grouping.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ def _group_left_right(tlist, m, cls,
2323
valid_right=lambda t: t is not None,
2424
semicolon=False):
2525
"""Groups together tokens that are joined by a middle token. ie. x < y"""
26-
[_group_left_right(sgroup, m, cls, valid_left, valid_right, semicolon)
27-
for sgroup in tlist.get_sublists() if not isinstance(sgroup, cls)]
2826

29-
token = tlist.token_next_by(m=m)
30-
while token:
27+
for token in list(tlist):
28+
if token.is_group() and not isinstance(token, cls):
29+
_group_left_right(token, m, cls, valid_left, valid_right,
30+
semicolon)
31+
32+
if not token.match(*m):
33+
continue
34+
3135
left, right = tlist.token_prev(token), tlist.token_next(token)
3236

3337
if valid_left(left) and valid_right(right):
@@ -36,8 +40,7 @@ def _group_left_right(tlist, m, cls,
3640
sright = tlist.token_next_by(m=M_SEMICOLON, idx=right)
3741
right = sright or right
3842
tokens = tlist.tokens_between(left, right)
39-
token = tlist.group_tokens(cls, tokens, extend=True)
40-
token = tlist.token_next_by(m=m, idx=token)
43+
tlist.group_tokens(cls, tokens, extend=True)
4144

4245

4346
def _group_matching(tlist, cls):
@@ -89,8 +92,8 @@ def group_comparison(tlist):
8992
sql.Operation)
9093
T_COMPERABLE = T_NUMERICAL + T_STRING + T_NAME
9194

92-
func = lambda tk: imt(tk, t=T_COMPERABLE, i=I_COMPERABLE) or (
93-
imt(tk, t=T.Keyword) and tk.value.upper() == 'NULL')
95+
func = lambda tk: (imt(tk, t=T_COMPERABLE, i=I_COMPERABLE) or
96+
(tk and tk.is_keyword and tk.normalized == 'NULL'))
9497

9598
_group_left_right(tlist, (T.Operator.Comparison, None), sql.Comparison,
9699
valid_left=func, valid_right=func)

0 commit comments

Comments
 (0)