@@ -572,18 +572,43 @@ def get_schema_names(self, connection, **kw):
572
572
573
573
return [self .normalize_name (row [1 ]) for row in cursor ]
574
574
575
- def get_table_comment (self , connection , table_name , schema = None , ** kw ):
575
+ def _get_table_comment (self , connection , table_name , schema = None , ** kw ):
576
576
"""
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.
578
578
"""
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 (
581
581
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 ''
583
583
)
584
584
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 }
587
612
588
613
589
614
@sa_vnt .listens_for (Table , 'before_create' )
0 commit comments