-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the query runner to enable async mode.
- Loading branch information
Bogdan Kyryliuk
committed
Aug 15, 2016
1 parent
5688246
commit b78253b
Showing
11 changed files
with
496 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1. [] implement the polling of the query results | ||
2. [] implement the retrieving of the CTA results | ||
3. [] implement parsing of the query to retrieve the table names | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com | ||
# | ||
# This example is part of python-sqlparse and is released under | ||
# the BSD License: http://www.opensource.org/licenses/bsd-license.php | ||
# | ||
# This example illustrates how to extract table names from nested | ||
# SELECT statements. | ||
# | ||
# See: | ||
# http://groups.google.com/group/sqlparse/browse_thread/thread/b0bd9a022e9d4895 | ||
|
||
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 | ||
for item in parsed.tokens: | ||
if item.ttype is DML and item.value.upper() == 'SELECT': | ||
return True | ||
return False | ||
|
||
|
||
def extract_from_part(parsed): | ||
from_seen = False | ||
for item in parsed.tokens: | ||
if from_seen: | ||
if is_subselect(item): | ||
for x in extract_from_part(item): | ||
yield x | ||
elif item.ttype is Keyword: | ||
raise StopIteration | ||
else: | ||
yield item | ||
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): | ||
for identifier in item.get_identifiers(): | ||
yield identifier.get_name() | ||
elif isinstance(item, Identifier): | ||
yield item.get_name() | ||
# It's a bug to check for Keyword here, but in the example | ||
# above some tables names are identified as keywords... | ||
elif item.ttype is Keyword: | ||
yield item.value | ||
|
||
|
||
# TODO(bkyryliuk): add logic to support joins and unions. | ||
def extract_tables(sql): | ||
stream = extract_from_part(sqlparse.parse(sql)[0]) | ||
return list(extract_table_identifiers(stream)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.