Skip to content

Commit

Permalink
add column comment to get_table_description()
Browse files Browse the repository at this point in the history
  • Loading branch information
mShan0 committed May 23, 2023
1 parent 068c7fc commit e938360
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions mssql/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from django import VERSION
from django.db.backends.base.introspection import BaseDatabaseIntrospection
from django.db.backends.base.introspection import FieldInfo
from django.db.backends.base.introspection import FieldInfo as BaseFieldInfo
from django.db.backends.base.introspection import TableInfo as BaseTableInfo
from django.db.models.indexes import Index
from django.conf import settings
Expand All @@ -17,6 +17,7 @@
SQL_BIGAUTOFIELD = -777444
SQL_TIMESTAMP_WITH_TIMEZONE = -155

FieldInfo = namedtuple("FieldInfo", BaseFieldInfo._fields + ("comment",))
TableInfo = namedtuple("TableInfo", BaseTableInfo._fields + ("comment",))

def get_schema_name():
Expand Down Expand Up @@ -144,15 +145,27 @@ def get_table_description(self, cursor, table_name, identity_check=True):
column.append(collation_name[0] if collation_name else '')
else:
column.append('')

if VERSION >= (4, 2):
sql = """select CAST(ep.value AS VARCHAR) AS COMMENT
FROM sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id
INNER JOIN sys.extended_properties ep ON c.object_id=ep.major_id AND ep.minor_id = c.column_id
WHERE t.name = '%s' AND c.name = '%s' AND ep.name = 'MS_Description'
""" % (table_name, column[0])
cursor.execute(sql)
comment = cursor.fetchone()
column.append(comment)
if identity_check and self._is_auto_field(cursor, table_name, column[0]):
if column[1] == Database.SQL_BIGINT:
column[1] = SQL_BIGAUTOFIELD
else:
column[1] = SQL_AUTOFIELD
if column[1] == Database.SQL_WVARCHAR and column[3] < 4000:
column[1] = Database.SQL_WCHAR
items.append(FieldInfo(*column))
if VERSION >= (4, 2):
items.append(FieldInfo(*column))
else:
items.append(BaseFieldInfo(*column))
return items

def get_sequences(self, cursor, table_name, table_fields=()):
Expand Down

0 comments on commit e938360

Please sign in to comment.