Skip to content

Commit 14f1b9c

Browse files
sfc-gh-stakedasfc-gh-abhatnagar
authored andcommitted
SNOW-140347 get tabe comment views
1 parent c308e6a commit 14f1b9c

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

snowdialect.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,18 +572,43 @@ def get_schema_names(self, connection, **kw):
572572

573573
return [self.normalize_name(row[1]) for row in cursor]
574574

575-
def get_table_comment(self, connection, table_name, schema=None, **kw):
575+
def _get_table_comment(self, connection, table_name, schema=None, **kw):
576576
"""
577-
Returns comment of table in a dictionary as described by SQLAlchemy spec
577+
Returns comment of table in a dictionary as described by SQLAlchemy spec.
578578
"""
579-
sql_command = "SHOW /* sqlalchemy:get_table_comment */ " \
580-
"TABLES LIKE '{0}'{1}".format(
579+
sql_command = "SHOW /* sqlalchemy:_get_table_comment */ " \
580+
"TABLES LIKE '{}'{}".format(
581581
table_name,
582-
(' IN SCHEMA {0}'.format(self.normalize_name(schema))) if schema else ''
582+
(' IN SCHEMA {}'.format(self.normalize_name(schema))) if schema else ''
583583
)
584584
cursor = connection.execute(sql_command)
585-
ans = cursor.fetchone()
586-
return {'text': ans['comment'] if ans['comment'] else None}
585+
return cursor.fetchone()
586+
587+
def _get_view_comment(self, connection, table_name, schema=None, **kw):
588+
"""
589+
Returns comment of view in a dictionary as described by SQLAlchemy spec.
590+
"""
591+
sql_command = "SHOW /* sqlalchemy:_get_view_comment */ " \
592+
"VIEWS LIKE '{}'{}".format(
593+
table_name,
594+
(' IN SCHEMA {}'.format(self.normalize_name(schema))) if schema else ''
595+
)
596+
cursor = connection.execute(sql_command)
597+
return cursor.fetchone()
598+
599+
def get_table_comment(self, connection, table_name, schema=None, **kw):
600+
"""
601+
Returns comment associated with a table (or view) in a dictionary as
602+
SQLAlchemy expects. Note that since SQLAlchemy may not (in fact,
603+
typically does not) know if this is a table or a view, we have to
604+
handle both cases here.
605+
"""
606+
result = self._get_table_comment(connection, table_name, schema)
607+
if result is None:
608+
# the "table" being reflected is actually a view
609+
result = self._get_view_comment(connection, table_name, schema)
610+
611+
return {'text': result['comment'] if result and result['comment'] else None}
587612

588613

589614
@sa_vnt.listens_for(Table, 'before_create')

test/test_core.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,47 @@ def test_view_definition(engine_testaccount, db_parameters):
546546
sql.strip()
547547
assert inspector.get_view_names() == [test_view_name]
548548
finally:
549-
engine_testaccount.execute(
550-
"DROP TABLE IF EXISTS {0}".format(test_table_name))
551-
engine_testaccount.execute(
552-
"DROP VIEW IF EXISTS {0}".format(test_view_name))
549+
engine_testaccount.execute(text(
550+
"DROP TABLE IF EXISTS {0}".format(test_table_name)))
551+
engine_testaccount.execute(text(
552+
"DROP VIEW IF EXISTS {0}".format(test_view_name)))
553+
554+
555+
def test_view_comment_reading(engine_testaccount, db_parameters):
556+
"""
557+
Tests reading a comment from a view once it's defined
558+
"""
559+
test_table_name = "test_table_sqlalchemy"
560+
test_view_name = "testview_sqlalchemy"
561+
engine_testaccount.execute("""
562+
CREATE OR REPLACE TABLE {} (
563+
id INTEGER,
564+
name STRING
565+
)
566+
""".format(test_table_name))
567+
sql = """
568+
CREATE OR REPLACE VIEW {} AS
569+
SELECT * FROM {} WHERE id > 10""".format(
570+
test_view_name, test_table_name)
571+
engine_testaccount.execute(text(sql).execution_options(
572+
autocommit=True))
573+
comment_text = "hello my viewing friends"
574+
sql = "COMMENT ON VIEW {} IS '{}';".format(
575+
test_view_name, comment_text)
576+
engine_testaccount.execute(text(sql).execution_options(
577+
autocommit=True))
578+
try:
579+
inspector = inspect(engine_testaccount)
580+
# NOTE: sqlalchemy doesn't have a way to get view comments specifically,
581+
# but the code to get table comments should work for views too
582+
assert inspector.get_table_comment(test_view_name) == {'text': comment_text}
583+
assert inspector.get_table_comment(test_table_name) == {'text': None}
584+
assert str(inspector.get_columns(test_table_name)) == str(inspector.get_columns(test_view_name))
585+
finally:
586+
engine_testaccount.execute(text(
587+
"DROP TABLE IF EXISTS {0}".format(test_table_name)))
588+
engine_testaccount.execute(text(
589+
"DROP VIEW IF EXISTS {0}".format(test_view_name)))
553590

554591

555592
@pytest.mark.skip("Temp table cannot be viewed for some reason")

0 commit comments

Comments
 (0)