Skip to content

Commit f26719d

Browse files
committed
Introduce @recurse to refactor recursion
1 parent 5a18305 commit f26719d

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

‎sqlparse/engine/grouping.py‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from sqlparse import sql
66
from sqlparse import tokens as T
7+
from sqlparse.utils import recurse
78

89

910
def _group_left_right(tlist, ttype, value, cls,
@@ -229,9 +230,8 @@ def _next_token(tl, i):
229230
token = _next_token(tlist, idx)
230231

231232

233+
@recurse(sql.IdentifierList)
232234
def group_identifier_list(tlist):
233-
[group_identifier_list(sgroup) for sgroup in tlist.get_sublists()
234-
if not isinstance(sgroup, sql.IdentifierList)]
235235
# Allowed list items
236236
fend1_funcs = [lambda t: isinstance(t, (sql.Identifier, sql.Function,
237237
sql.Case)),
@@ -327,9 +327,8 @@ def group_brackets(tlist):
327327
token = tlist.token_next_match(idx, T.Punctuation, ['(', '['])
328328

329329

330+
@recurse(sql.Comment)
330331
def group_comments(tlist):
331-
[group_comments(sgroup) for sgroup in tlist.get_sublists()
332-
if not isinstance(sgroup, sql.Comment)]
333332
idx = 0
334333
token = tlist.token_next_by_type(idx, T.Comment)
335334
while token:
@@ -348,9 +347,8 @@ def group_comments(tlist):
348347
token = tlist.token_next_by_type(idx, T.Comment)
349348

350349

350+
@recurse(sql.Where)
351351
def group_where(tlist):
352-
[group_where(sgroup) for sgroup in tlist.get_sublists()
353-
if not isinstance(sgroup, sql.Where)]
354352
idx = 0
355353
token = tlist.token_next_match(idx, T.Keyword, 'WHERE')
356354
stopwords = ('ORDER', 'GROUP', 'LIMIT', 'UNION', 'EXCEPT', 'HAVING')
@@ -368,10 +366,9 @@ def group_where(tlist):
368366
token = tlist.token_next_match(idx, T.Keyword, 'WHERE')
369367

370368

369+
@recurse(sql.Identifier, sql.Function, sql.Case)
371370
def group_aliased(tlist):
372371
clss = (sql.Identifier, sql.Function, sql.Case)
373-
[group_aliased(sgroup) for sgroup in tlist.get_sublists()
374-
if not isinstance(sgroup, clss)]
375372
idx = 0
376373
token = tlist.token_next_by_instance(idx, clss)
377374
while token:
@@ -390,9 +387,8 @@ def group_typecasts(tlist):
390387
_group_left_right(tlist, T.Punctuation, '::', sql.Identifier)
391388

392389

390+
@recurse(sql.Function)
393391
def group_functions(tlist):
394-
[group_functions(sgroup) for sgroup in tlist.get_sublists()
395-
if not isinstance(sgroup, sql.Function)]
396392
idx = 0
397393
token = tlist.token_next_by_type(idx, T.Name)
398394
while token:
@@ -420,8 +416,8 @@ def group_order(tlist):
420416
token = tlist.token_next_by_type(idx, T.Keyword.Order)
421417

422418

419+
@recurse()
423420
def align_comments(tlist):
424-
[align_comments(sgroup) for sgroup in tlist.get_sublists()]
425421
token = tlist.token_next_by(i=sql.Comment)
426422
while token:
427423
before = tlist.token_prev(tlist.token_index(token))

‎sqlparse/utils.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ def remove_quotes(val):
122122

123123

124124
def recurse(*cls):
125+
"""Function decorator to help with recursion
126+
127+
:param cls: Classes to not recurse over
128+
:return: function
129+
"""
125130
def wrap(f):
126131
def wrapped_f(tlist):
127132
for sgroup in tlist.get_sublists():

0 commit comments

Comments
 (0)