MCPcopy
hub / github.com/andialbrecht/sqlparse / _group

Function _group

sqlparse/engine/grouping.py:478–525  ·  view source on GitHub ↗

Groups together tokens that are joined by a middle token. i.e. x < y

(tlist, cls, match,
           valid_prev=lambda t: True,
           valid_next=lambda t: True,
           post=None,
           extend=True,
           recurse=True,
           depth=0
           )

Source from the content-addressed store, hash-verified

476
477
478def _group(tlist, cls, match,
479 valid_prev=lambda t: True,
480 valid_next=lambda t: True,
481 post=None,
482 extend=True,
483 recurse=True,
484 depth=0
485 ):
486 """Groups together tokens that are joined by a middle token. i.e. x < y"""
487 if MAX_GROUPING_DEPTH is not None and depth > MAX_GROUPING_DEPTH:
488 raise SQLParseError(
489 f"Maximum grouping depth exceeded ({MAX_GROUPING_DEPTH})."
490 )
491
492 # Limit the number of tokens to prevent DoS attacks
493 if MAX_GROUPING_TOKENS is not None \
494 and len(tlist.tokens) > MAX_GROUPING_TOKENS:
495 raise SQLParseError(
496 f"Maximum number of tokens exceeded ({MAX_GROUPING_TOKENS})."
497 )
498
499 tidx_offset = 0
500 pidx, prev_ = None, None
501 token_list = list(tlist)
502
503 for idx, token in enumerate(token_list):
504 tidx = idx - tidx_offset
505 if tidx < 0: # tidx shouldn't get negative
506 continue
507
508 if token.is_whitespace:
509 continue
510
511 if recurse and token.is_group and not isinstance(token, cls):
512 _group(token, cls, match, valid_prev, valid_next,
513 post, extend, True, depth + 1)
514
515 if match(token):
516 nidx, next_ = tlist.token_next(tidx)
517 if prev_ and valid_prev(prev_) and valid_next(next_):
518 from_idx, to_idx = post(tlist, pidx, tidx, nidx)
519 grp = tlist.group_tokens(cls, from_idx, to_idx, extend=extend)
520
521 tidx_offset += to_idx - from_idx
522 pidx, prev_ = from_idx, grp
523 continue
524
525 pidx, prev_ = tidx, token

Callers 10

group_typecastsFunction · 0.85
group_tzcastsFunction · 0.85
group_typed_literalFunction · 0.85
group_periodFunction · 0.85
group_asFunction · 0.85
group_assignmentFunction · 0.85
group_comparisonFunction · 0.85
group_arraysFunction · 0.85
group_operatorFunction · 0.85
group_identifier_listFunction · 0.85

Calls 7

SQLParseErrorClass · 0.90
matchFunction · 0.85
valid_prevFunction · 0.85
valid_nextFunction · 0.85
postFunction · 0.85
token_nextMethod · 0.80
group_tokensMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…