Skip to content

Commit 068c7fc

Browse files
committed
add table comment to get_table_list query
1 parent a3e7f72 commit 068c7fc

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

mssql/introspection.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
from django.db import DatabaseError
55
import pyodbc as Database
66

7+
from collections import namedtuple
8+
79
from django import VERSION
8-
from django.db.backends.base.introspection import (
9-
BaseDatabaseIntrospection, FieldInfo, TableInfo,
10-
)
10+
from django.db.backends.base.introspection import BaseDatabaseIntrospection
11+
from django.db.backends.base.introspection import FieldInfo
12+
from django.db.backends.base.introspection import TableInfo as BaseTableInfo
1113
from django.db.models.indexes import Index
1214
from django.conf import settings
1315

1416
SQL_AUTOFIELD = -777555
1517
SQL_BIGAUTOFIELD = -777444
1618
SQL_TIMESTAMP_WITH_TIMEZONE = -155
1719

20+
TableInfo = namedtuple("TableInfo", BaseTableInfo._fields + ("comment",))
1821

1922
def get_schema_name():
2023
return getattr(settings, 'SCHEMA_TO_INSPECT', 'SCHEMA_NAME()')
@@ -71,13 +74,28 @@ def get_table_list(self, cursor):
7174
"""
7275
Returns a list of table and view names in the current database.
7376
"""
74-
sql = 'SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s' % (
77+
sql = """SELECT
78+
TABLE_NAME,
79+
TABLE_TYPE,
80+
CAST(ep.value AS VARCHAR) AS COMMENT
81+
FROM INFORMATION_SCHEMA.TABLES i
82+
INNER JOIN sys.tables t ON t.name = i.TABLE_NAME
83+
LEFT JOIN sys.extended_properties ep ON t.object_id = ep.major_id
84+
WHERE
85+
((ep.name = 'MS_DESCRIPTION' AND ep.minor_id = 0) OR ep.value IS NULL)
86+
AND
87+
i.TABLE_SCHEMA = %s""" % (
7588
get_schema_name())
7689
cursor.execute(sql)
7790
types = {'BASE TABLE': 't', 'VIEW': 'v'}
78-
return [TableInfo(row[0], types.get(row[1]))
79-
for row in cursor.fetchall()
80-
if row[0] not in self.ignored_tables]
91+
if VERSION >= (4, 2):
92+
return [TableInfo(row[0], types.get(row[1]), row[2])
93+
for row in cursor.fetchall()
94+
if row[0] not in self.ignored_tables]
95+
else:
96+
return [BaseTableInfo(row[0], types.get(row[1]))
97+
for row in cursor.fetchall()
98+
if row[0] not in self.ignored_tables]
8199

82100
def _is_auto_field(self, cursor, table_name, column_name):
83101
"""
@@ -111,7 +129,7 @@ def get_table_description(self, cursor, table_name, identity_check=True):
111129

112130
if not columns:
113131
raise DatabaseError(f"Table {table_name} does not exist.")
114-
132+
115133
items = []
116134
for column in columns:
117135
if VERSION >= (3, 2):

0 commit comments

Comments
 (0)