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