introspection.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import re
  2. from .base import FIELD_TYPE
  3. from django.utils.datastructures import OrderedSet
  4. from django.db.backends import BaseDatabaseIntrospection, FieldInfo
  5. from django.utils.encoding import force_text
  6. foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")
  7. class DatabaseIntrospection(BaseDatabaseIntrospection):
  8. data_types_reverse = {
  9. FIELD_TYPE.BLOB: 'TextField',
  10. FIELD_TYPE.CHAR: 'CharField',
  11. FIELD_TYPE.DECIMAL: 'DecimalField',
  12. FIELD_TYPE.NEWDECIMAL: 'DecimalField',
  13. FIELD_TYPE.DATE: 'DateField',
  14. FIELD_TYPE.DATETIME: 'DateTimeField',
  15. FIELD_TYPE.DOUBLE: 'FloatField',
  16. FIELD_TYPE.FLOAT: 'FloatField',
  17. FIELD_TYPE.INT24: 'IntegerField',
  18. FIELD_TYPE.LONG: 'IntegerField',
  19. FIELD_TYPE.LONGLONG: 'BigIntegerField',
  20. FIELD_TYPE.SHORT: 'IntegerField',
  21. FIELD_TYPE.STRING: 'CharField',
  22. FIELD_TYPE.TIME: 'TimeField',
  23. FIELD_TYPE.TIMESTAMP: 'DateTimeField',
  24. FIELD_TYPE.TINY: 'IntegerField',
  25. FIELD_TYPE.TINY_BLOB: 'TextField',
  26. FIELD_TYPE.MEDIUM_BLOB: 'TextField',
  27. FIELD_TYPE.LONG_BLOB: 'TextField',
  28. FIELD_TYPE.VAR_STRING: 'CharField',
  29. }
  30. def get_table_list(self, cursor):
  31. "Returns a list of table names in the current database."
  32. cursor.execute("SHOW TABLES")
  33. return [row[0] for row in cursor.fetchall()]
  34. def get_table_description(self, cursor, table_name):
  35. """
  36. Returns a description of the table, with the DB-API cursor.description interface."
  37. """
  38. # varchar length returned by cursor.description is an internal length,
  39. # not visible length (#5725), use information_schema database to fix this
  40. cursor.execute("""
  41. SELECT column_name, character_maximum_length FROM information_schema.columns
  42. WHERE table_name = %s AND table_schema = DATABASE()
  43. AND character_maximum_length IS NOT NULL""", [table_name])
  44. length_map = dict(cursor.fetchall())
  45. # Also getting precision and scale from information_schema (see #5014)
  46. cursor.execute("""
  47. SELECT column_name, numeric_precision, numeric_scale FROM information_schema.columns
  48. WHERE table_name = %s AND table_schema = DATABASE()
  49. AND data_type='decimal'""", [table_name])
  50. numeric_map = dict((line[0], tuple(int(n) for n in line[1:])) for line in cursor.fetchall())
  51. cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
  52. return [FieldInfo(*((force_text(line[0]),)
  53. + line[1:3]
  54. + (length_map.get(line[0], line[3]),)
  55. + numeric_map.get(line[0], line[4:6])
  56. + (line[6],)))
  57. for line in cursor.description]
  58. def _name_to_index(self, cursor, table_name):
  59. """
  60. Returns a dictionary of {field_name: field_index} for the given table.
  61. Indexes are 0-based.
  62. """
  63. return dict((d[0], i) for i, d in enumerate(self.get_table_description(cursor, table_name)))
  64. def get_relations(self, cursor, table_name):
  65. """
  66. Returns a dictionary of {field_index: (field_index_other_table, other_table)}
  67. representing all relationships to the given table. Indexes are 0-based.
  68. """
  69. my_field_dict = self._name_to_index(cursor, table_name)
  70. constraints = self.get_key_columns(cursor, table_name)
  71. relations = {}
  72. for my_fieldname, other_table, other_field in constraints:
  73. other_field_index = self._name_to_index(cursor, other_table)[other_field]
  74. my_field_index = my_field_dict[my_fieldname]
  75. relations[my_field_index] = (other_field_index, other_table)
  76. return relations
  77. def get_key_columns(self, cursor, table_name):
  78. """
  79. Returns a list of (column_name, referenced_table_name, referenced_column_name) for all
  80. key columns in given table.
  81. """
  82. key_columns = []
  83. cursor.execute("""
  84. SELECT column_name, referenced_table_name, referenced_column_name
  85. FROM information_schema.key_column_usage
  86. WHERE table_name = %s
  87. AND table_schema = DATABASE()
  88. AND referenced_table_name IS NOT NULL
  89. AND referenced_column_name IS NOT NULL""", [table_name])
  90. key_columns.extend(cursor.fetchall())
  91. return key_columns
  92. def get_indexes(self, cursor, table_name):
  93. cursor.execute("SHOW INDEX FROM %s" % self.connection.ops.quote_name(table_name))
  94. # Do a two-pass search for indexes: on first pass check which indexes
  95. # are multicolumn, on second pass check which single-column indexes
  96. # are present.
  97. rows = list(cursor.fetchall())
  98. multicol_indexes = set()
  99. for row in rows:
  100. if row[3] > 1:
  101. multicol_indexes.add(row[2])
  102. indexes = {}
  103. for row in rows:
  104. if row[2] in multicol_indexes:
  105. continue
  106. if row[4] not in indexes:
  107. indexes[row[4]] = {'primary_key': False, 'unique': False}
  108. # It's possible to have the unique and PK constraints in separate indexes.
  109. if row[2] == 'PRIMARY':
  110. indexes[row[4]]['primary_key'] = True
  111. if not row[1]:
  112. indexes[row[4]]['unique'] = True
  113. return indexes
  114. def get_constraints(self, cursor, table_name):
  115. """
  116. Retrieves any constraints or keys (unique, pk, fk, check, index) across one or more columns.
  117. """
  118. constraints = {}
  119. # Get the actual constraint names and columns
  120. name_query = """
  121. SELECT kc.`constraint_name`, kc.`column_name`,
  122. kc.`referenced_table_name`, kc.`referenced_column_name`
  123. FROM information_schema.key_column_usage AS kc
  124. WHERE
  125. kc.table_schema = %s AND
  126. kc.table_name = %s
  127. """
  128. cursor.execute(name_query, [self.connection.settings_dict['NAME'], table_name])
  129. for constraint, column, ref_table, ref_column in cursor.fetchall():
  130. if constraint not in constraints:
  131. constraints[constraint] = {
  132. 'columns': OrderedSet(),
  133. 'primary_key': False,
  134. 'unique': False,
  135. 'index': False,
  136. 'check': False,
  137. 'foreign_key': (ref_table, ref_column) if ref_column else None,
  138. }
  139. constraints[constraint]['columns'].add(column)
  140. # Now get the constraint types
  141. type_query = """
  142. SELECT c.constraint_name, c.constraint_type
  143. FROM information_schema.table_constraints AS c
  144. WHERE
  145. c.table_schema = %s AND
  146. c.table_name = %s
  147. """
  148. cursor.execute(type_query, [self.connection.settings_dict['NAME'], table_name])
  149. for constraint, kind in cursor.fetchall():
  150. if kind.lower() == "primary key":
  151. constraints[constraint]['primary_key'] = True
  152. constraints[constraint]['unique'] = True
  153. elif kind.lower() == "unique":
  154. constraints[constraint]['unique'] = True
  155. # Now add in the indexes
  156. cursor.execute("SHOW INDEX FROM %s" % self.connection.ops.quote_name(table_name))
  157. for table, non_unique, index, colseq, column in [x[:5] for x in cursor.fetchall()]:
  158. if index not in constraints:
  159. constraints[index] = {
  160. 'columns': OrderedSet(),
  161. 'primary_key': False,
  162. 'unique': False,
  163. 'index': True,
  164. 'check': False,
  165. 'foreign_key': None,
  166. }
  167. constraints[index]['index'] = True
  168. constraints[index]['columns'].add(column)
  169. # Convert the sorted sets to lists
  170. for constraint in constraints.values():
  171. constraint['columns'] = list(constraint['columns'])
  172. return constraints