extract_tables() issue

282 views
Skip to first unread message

Eric L

unread,
Dec 12, 2016, 4:57:39 AM12/12/16
to sqlparse
Hello, when I use the sample code of extract_tables() with the following SQL statement:

"select test from (select test2 from mytable where (a=b)) tt"

I get "tt", but I think I should get "tt" and "mytable"
I have looked at the code and it seems that the parenthesis are not properly handled in the code. Can you help with this?

Ahmad Zareef

unread,
Feb 22, 2017, 8:50:32 AM2/22/17
to sqlparse
You may replace the following function in the example:
extract_table_identifiers(token_stream)

with the below definition:


def extract_table_identifiers(token_stream):
for item in token_stream:
if isinstance(item, IdentifierList):
for identifier in item.get_identifiers():
yield identifier.value
elif isinstance(item, Identifier):
yield item.value
# It's not necessary (in general cases) to check for Keyword here, but in case
# some table names are identified as keywords...
elif item.ttype is Keyword:
yield item.value

Please let me know if it worked.

Thanks...
Reply all
Reply to author
Forward
0 new messages