summaryrefslogtreecommitdiff
path: root/examples/extract_table_names.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/extract_table_names.py')
-rw-r--r--examples/extract_table_names.py5
1 files changed, 5 insertions, 0 deletions
diff --git a/examples/extract_table_names.py b/examples/extract_table_names.py
index 891ff6f..d8d1698 100644
--- a/examples/extract_table_names.py
+++ b/examples/extract_table_names.py
@@ -13,6 +13,7 @@ import sqlparse
from sqlparse.sql import IdentifierList, Identifier
from sqlparse.tokens import Keyword, DML
+
def is_subselect(parsed):
if not parsed.is_group():
return False
@@ -21,6 +22,7 @@ def is_subselect(parsed):
return True
return False
+
def extract_from_part(parsed):
from_seen = False
for item in parsed.tokens:
@@ -35,6 +37,7 @@ def extract_from_part(parsed):
elif item.ttype is Keyword and item.value.upper() == 'FROM':
from_seen = True
+
def extract_table_identifiers(token_stream):
for item in token_stream:
if isinstance(item, IdentifierList):
@@ -47,9 +50,11 @@ def extract_table_identifiers(token_stream):
elif item.ttype is Keyword:
yield item.value
+
def extract_tables():
stream = extract_from_part(sqlparse.parse(sql)[0])
return list(extract_table_identifiers(stream))
+
if __name__ == '__main__':
print 'Tables: %s' % ', '.join(extract_tables())