introspection.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. from collections import namedtuple
  2. import sqlparse
  3. from MySQLdb.constants import FIELD_TYPE
  4. from django.db.backends.base.introspection import (
  5. BaseDatabaseIntrospection, FieldInfo as BaseFieldInfo, TableInfo,
  6. )
  7. from django.db.models.indexes import Index
  8. from django.utils.datastructures import OrderedSet
  9. FieldInfo = namedtuple('FieldInfo', BaseFieldInfo._fields + ('extra', 'is_unsigned'))
  10. InfoLine = namedtuple('InfoLine', 'col_name data_type max_len num_prec num_scale extra column_default is_unsigned')
  11. class DatabaseIntrospection(BaseDatabaseIntrospection):
  12. data_types_reverse = {
  13. FIELD_TYPE.BLOB: 'TextField',
  14. FIELD_TYPE.CHAR: 'CharField',
  15. FIELD_TYPE.DECIMAL: 'DecimalField',
  16. FIELD_TYPE.NEWDECIMAL: 'DecimalField',
  17. FIELD_TYPE.DATE: 'DateField',
  18. FIELD_TYPE.DATETIME: 'DateTimeField',
  19. FIELD_TYPE.DOUBLE: 'FloatField',
  20. FIELD_TYPE.FLOAT: 'FloatField',
  21. FIELD_TYPE.INT24: 'IntegerField',
  22. FIELD_TYPE.LONG: 'IntegerField',
  23. FIELD_TYPE.LONGLONG: 'BigIntegerField',
  24. FIELD_TYPE.SHORT: 'SmallIntegerField',
  25. FIELD_TYPE.STRING: 'CharField',
  26. FIELD_TYPE.TIME: 'TimeField',
  27. FIELD_TYPE.TIMESTAMP: 'DateTimeField',
  28. FIELD_TYPE.TINY: 'IntegerField',
  29. FIELD_TYPE.TINY_BLOB: 'TextField',
  30. FIELD_TYPE.MEDIUM_BLOB: 'TextField',
  31. FIELD_TYPE.LONG_BLOB: 'TextField',
  32. FIELD_TYPE.VAR_STRING: 'CharField',
  33. }
  34. def get_field_type(self, data_type, description):
  35. field_type = super().get_field_type(data_type, description)
  36. if 'auto_increment' in description.extra:
  37. if field_type == 'IntegerField':
  38. return 'AutoField'
  39. elif field_type == 'BigIntegerField':
  40. return 'BigAutoField'
  41. elif field_type == 'SmallIntegerField':
  42. return 'SmallAutoField'
  43. if description.is_unsigned:
  44. if field_type == 'IntegerField':
  45. return 'PositiveIntegerField'
  46. elif field_type == 'SmallIntegerField':
  47. return 'PositiveSmallIntegerField'
  48. return field_type
  49. def get_table_list(self, cursor):
  50. """Return a list of table and view names in the current database."""
  51. cursor.execute("SHOW FULL TABLES")
  52. return [TableInfo(row[0], {'BASE TABLE': 't', 'VIEW': 'v'}.get(row[1]))
  53. for row in cursor.fetchall()]
  54. def get_table_description(self, cursor, table_name):
  55. """
  56. Return a description of the table with the DB-API cursor.description
  57. interface."
  58. """
  59. # information_schema database gives more accurate results for some figures:
  60. # - varchar length returned by cursor.description is an internal length,
  61. # not visible length (#5725)
  62. # - precision and scale (for decimal fields) (#5014)
  63. # - auto_increment is not available in cursor.description
  64. cursor.execute("""
  65. SELECT
  66. column_name, data_type, character_maximum_length,
  67. numeric_precision, numeric_scale, extra, column_default,
  68. CASE
  69. WHEN column_type LIKE '%% unsigned' THEN 1
  70. ELSE 0
  71. END AS is_unsigned
  72. FROM information_schema.columns
  73. WHERE table_name = %s AND table_schema = DATABASE()""", [table_name])
  74. field_info = {line[0]: InfoLine(*line) for line in cursor.fetchall()}
  75. cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
  76. def to_int(i):
  77. return int(i) if i is not None else i
  78. fields = []
  79. for line in cursor.description:
  80. info = field_info[line[0]]
  81. fields.append(FieldInfo(
  82. *line[:3],
  83. to_int(info.max_len) or line[3],
  84. to_int(info.num_prec) or line[4],
  85. to_int(info.num_scale) or line[5],
  86. line[6],
  87. info.column_default,
  88. info.extra,
  89. info.is_unsigned,
  90. ))
  91. return fields
  92. def get_sequences(self, cursor, table_name, table_fields=()):
  93. for field_info in self.get_table_description(cursor, table_name):
  94. if 'auto_increment' in field_info.extra:
  95. # MySQL allows only one auto-increment column per table.
  96. return [{'table': table_name, 'column': field_info.name}]
  97. return []
  98. def get_relations(self, cursor, table_name):
  99. """
  100. Return a dictionary of {field_name: (field_name_other_table, other_table)}
  101. representing all relationships to the given table.
  102. """
  103. constraints = self.get_key_columns(cursor, table_name)
  104. relations = {}
  105. for my_fieldname, other_table, other_field in constraints:
  106. relations[my_fieldname] = (other_field, other_table)
  107. return relations
  108. def get_key_columns(self, cursor, table_name):
  109. """
  110. Return a list of (column_name, referenced_table_name, referenced_column_name)
  111. for all key columns in the given table.
  112. """
  113. key_columns = []
  114. cursor.execute("""
  115. SELECT column_name, referenced_table_name, referenced_column_name
  116. FROM information_schema.key_column_usage
  117. WHERE table_name = %s
  118. AND table_schema = DATABASE()
  119. AND referenced_table_name IS NOT NULL
  120. AND referenced_column_name IS NOT NULL""", [table_name])
  121. key_columns.extend(cursor.fetchall())
  122. return key_columns
  123. def get_storage_engine(self, cursor, table_name):
  124. """
  125. Retrieve the storage engine for a given table. Return the default
  126. storage engine if the table doesn't exist.
  127. """
  128. cursor.execute(
  129. "SELECT engine "
  130. "FROM information_schema.tables "
  131. "WHERE table_name = %s", [table_name])
  132. result = cursor.fetchone()
  133. if not result:
  134. return self.connection.features._mysql_storage_engine
  135. return result[0]
  136. def _parse_constraint_columns(self, check_clause, columns):
  137. check_columns = OrderedSet()
  138. statement = sqlparse.parse(check_clause)[0]
  139. tokens = (token for token in statement.flatten() if not token.is_whitespace)
  140. for token in tokens:
  141. if (
  142. token.ttype == sqlparse.tokens.Name and
  143. self.connection.ops.quote_name(token.value) == token.value and
  144. token.value[1:-1] in columns
  145. ):
  146. check_columns.add(token.value[1:-1])
  147. return check_columns
  148. def get_constraints(self, cursor, table_name):
  149. """
  150. Retrieve any constraints or keys (unique, pk, fk, check, index) across
  151. one or more columns.
  152. """
  153. constraints = {}
  154. # Get the actual constraint names and columns
  155. name_query = """
  156. SELECT kc.`constraint_name`, kc.`column_name`,
  157. kc.`referenced_table_name`, kc.`referenced_column_name`
  158. FROM information_schema.key_column_usage AS kc
  159. WHERE
  160. kc.table_schema = DATABASE() AND
  161. kc.table_name = %s
  162. ORDER BY kc.`ordinal_position`
  163. """
  164. cursor.execute(name_query, [table_name])
  165. for constraint, column, ref_table, ref_column in cursor.fetchall():
  166. if constraint not in constraints:
  167. constraints[constraint] = {
  168. 'columns': OrderedSet(),
  169. 'primary_key': False,
  170. 'unique': False,
  171. 'index': False,
  172. 'check': False,
  173. 'foreign_key': (ref_table, ref_column) if ref_column else None,
  174. }
  175. constraints[constraint]['columns'].add(column)
  176. # Now get the constraint types
  177. type_query = """
  178. SELECT c.constraint_name, c.constraint_type
  179. FROM information_schema.table_constraints AS c
  180. WHERE
  181. c.table_schema = DATABASE() AND
  182. c.table_name = %s
  183. """
  184. cursor.execute(type_query, [table_name])
  185. for constraint, kind in cursor.fetchall():
  186. if kind.lower() == "primary key":
  187. constraints[constraint]['primary_key'] = True
  188. constraints[constraint]['unique'] = True
  189. elif kind.lower() == "unique":
  190. constraints[constraint]['unique'] = True
  191. # Add check constraints.
  192. if self.connection.features.can_introspect_check_constraints:
  193. unnamed_constraints_index = 0
  194. columns = {info.name for info in self.get_table_description(cursor, table_name)}
  195. if self.connection.mysql_is_mariadb:
  196. type_query = """
  197. SELECT c.constraint_name, c.check_clause
  198. FROM information_schema.check_constraints AS c
  199. WHERE
  200. c.constraint_schema = DATABASE() AND
  201. c.table_name = %s
  202. """
  203. else:
  204. type_query = """
  205. SELECT cc.constraint_name, cc.check_clause
  206. FROM
  207. information_schema.check_constraints AS cc,
  208. information_schema.table_constraints AS tc
  209. WHERE
  210. cc.constraint_schema = DATABASE() AND
  211. tc.table_schema = cc.constraint_schema AND
  212. cc.constraint_name = tc.constraint_name AND
  213. tc.constraint_type = 'CHECK' AND
  214. tc.table_name = %s
  215. """
  216. cursor.execute(type_query, [table_name])
  217. for constraint, check_clause in cursor.fetchall():
  218. constraint_columns = self._parse_constraint_columns(check_clause, columns)
  219. # Ensure uniqueness of unnamed constraints. Unnamed unique
  220. # and check columns constraints have the same name as
  221. # a column.
  222. if set(constraint_columns) == {constraint}:
  223. unnamed_constraints_index += 1
  224. constraint = '__unnamed_constraint_%s__' % unnamed_constraints_index
  225. constraints[constraint] = {
  226. 'columns': constraint_columns,
  227. 'primary_key': False,
  228. 'unique': False,
  229. 'index': False,
  230. 'check': True,
  231. 'foreign_key': None,
  232. }
  233. # Now add in the indexes
  234. cursor.execute("SHOW INDEX FROM %s" % self.connection.ops.quote_name(table_name))
  235. for table, non_unique, index, colseq, column, type_ in [x[:5] + (x[10],) for x in cursor.fetchall()]:
  236. if index not in constraints:
  237. constraints[index] = {
  238. 'columns': OrderedSet(),
  239. 'primary_key': False,
  240. 'unique': False,
  241. 'check': False,
  242. 'foreign_key': None,
  243. }
  244. constraints[index]['index'] = True
  245. constraints[index]['type'] = Index.suffix if type_ == 'BTREE' else type_.lower()
  246. constraints[index]['columns'].add(column)
  247. # Convert the sorted sets to lists
  248. for constraint in constraints.values():
  249. constraint['columns'] = list(constraint['columns'])
  250. return constraints