Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

introspection.py 8.5KB

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