|
| 1 | +# This example illustrates how to extract table names from nested |
| 2 | +# SELECT statements. |
| 3 | + |
| 4 | +# See: |
| 5 | +# http://groups.google.com/group/sqlparse/browse_thread/thread/b0bd9a022e9d4895 |
| 6 | + |
| 7 | +sql = """ |
| 8 | +select K.a from (select H.b from (select G.c from (select F.d from |
| 9 | +(select E.e from A, B, C, D, E), F), G), H), I, J, K; |
| 10 | +""" |
| 11 | + |
| 12 | +import sqlparse |
| 13 | +from sqlparse.sql import IdentifierList, Identifier |
| 14 | +from sqlparse.tokens import Keyword, DML |
| 15 | + |
| 16 | +def is_subselect(parsed): |
| 17 | + if not parsed.is_group(): |
| 18 | + return False |
| 19 | + for item in parsed.tokens: |
| 20 | + if item.ttype is DML and item.value.upper() == 'SELECT': |
| 21 | + return True |
| 22 | + return False |
| 23 | + |
| 24 | +def extract_from_part(parsed): |
| 25 | + from_seen = False |
| 26 | + for item in parsed.tokens: |
| 27 | + if from_seen: |
| 28 | + if is_subselect(item): |
| 29 | + for x in extract_from_part(item): |
| 30 | + yield x |
| 31 | + else: |
| 32 | + yield item |
| 33 | + elif item.ttype is Keyword and item.value.upper() == 'FROM': |
| 34 | + from_seen = True |
| 35 | + |
| 36 | +def extract_table_identifiers(token_stream): |
| 37 | + for item in token_stream: |
| 38 | + if isinstance(item, IdentifierList): |
| 39 | + for identifier in item.get_identifiers(): |
| 40 | + yield identifier.get_name() |
| 41 | + elif isinstance(item, Identifier): |
| 42 | + yield item.get_name() |
| 43 | + # It's a bug to check for Keyword here, but in the example |
| 44 | + # above some tables names are identified as keywords... |
| 45 | + elif item.ttype is Keyword: |
| 46 | + yield item.value |
| 47 | + |
| 48 | +def extract_tables(): |
| 49 | + stream = extract_from_part(sqlparse.parse(sql)[0]) |
| 50 | + return list(extract_table_identifiers(stream)) |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + print 'Tables: %s' % ', '.join(extract_tables()) |
0 commit comments