Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #47 from john-bodley/john-bodley-cherry-pick-5086
Browse files Browse the repository at this point in the history
[get_df] Adding support for multi-statement SQL (apache#5086)
  • Loading branch information
john-bodley authored May 30, 2018
2 parents 5dbdcfe + 59cb96d commit 4d89180
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from sqlalchemy.sql import text
from sqlalchemy.sql.expression import TextAsFrom
from sqlalchemy_utils import EncryptedType
import sqlparse

from superset import app, db, db_engine_specs, security_manager, utils
from superset.connectors.connector_registry import ConnectorRegistry
Expand Down Expand Up @@ -691,8 +692,9 @@ def get_quoter(self):
return self.get_dialect().identifier_preparer.quote

def get_df(self, sql, schema):
sqls = [x.strip() for x in sql.strip().strip(';').split(';')]
sqls = [str(s).strip().strip(';') for s in sqlparse.parse(sql)]
eng = self.get_sqla_engine(schema=schema)

for i in range(len(sqls) - 1):
eng.execute(sqls[i])

Expand Down
6 changes: 6 additions & 0 deletions tests/model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ def test_single_statement(self):
df = main_db.get_df('SELECT 1', None)
self.assertEquals(df.iat[0, 0], 1)

df = main_db.get_df('SELECT 1;', None)
self.assertEquals(df.iat[0, 0], 1)

def test_multi_statement(self):
main_db = self.get_main_database(db.session)

if main_db.backend == 'mysql':
df = main_db.get_df('USE superset; SELECT 1', None)
self.assertEquals(df.iat[0, 0], 1)

df = main_db.get_df("USE superset; SELECT ';';", None)
self.assertEquals(df.iat[0, 0], ';')

0 comments on commit 4d89180

Please sign in to comment.