MCPcopy
hub / github.com/dbcli/pgcli / extract_from_part

Function extract_from_part

pgcli/packages/parseutils/tables.py:36–72  ·  view source on GitHub ↗
(parsed, stop_at_punctuation=True)

Source from the content-addressed store, hash-verified

34
35
36def extract_from_part(parsed, stop_at_punctuation=True):
37 tbl_prefix_seen = False
38 for item in parsed.tokens:
39 if tbl_prefix_seen:
40 if is_subselect(item):
41 yield from extract_from_part(item, stop_at_punctuation)
42 elif stop_at_punctuation and item.ttype is Punctuation:
43 return
44 # An incomplete nested select won't be recognized correctly as a
45 # sub-select. eg: 'SELECT * FROM (SELECT id FROM user'. This causes
46 # the second FROM to trigger this elif condition resulting in a
47 # `return`. So we need to ignore the keyword if the keyword
48 # FROM.
49 # Also 'SELECT * FROM abc JOIN def' will trigger this elif
50 # condition. So we need to ignore the keyword JOIN and its variants
51 # INNER JOIN, FULL OUTER JOIN, etc.
52 elif item.ttype is Keyword and (not item.value.upper() == "FROM") and (not item.value.upper().endswith("JOIN")):
53 tbl_prefix_seen = False
54 else:
55 yield item
56 elif item.ttype is Keyword or item.ttype is Keyword.DML:
57 item_val = item.value.upper()
58 if item_val in (
59 "COPY",
60 "FROM",
61 "INTO",
62 "UPDATE",
63 "TABLE",
64 ) or item_val.endswith("JOIN"):
65 tbl_prefix_seen = True
66 # 'SELECT a, FROM abc' will detect FROM as part of the column list.
67 # So this check here is necessary.
68 elif isinstance(item, IdentifierList):
69 for identifier in item.get_identifiers():
70 if identifier.ttype is Keyword and identifier.value.upper() == "FROM":
71 tbl_prefix_seen = True
72 break
73
74
75def extract_table_identifiers(token_stream, allow_functions=True):

Callers 1

extract_tablesFunction · 0.85

Calls 1

is_subselectFunction · 0.85

Tested by

no test coverage detected