introspection.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. from django.db.backends.base.introspection import (
  2. BaseDatabaseIntrospection, FieldInfo, TableInfo,
  3. )
  4. from django.db.models.indexes import Index
  5. class DatabaseIntrospection(BaseDatabaseIntrospection):
  6. # Maps type codes to Django Field types.
  7. data_types_reverse = {
  8. 16: 'BooleanField',
  9. 17: 'BinaryField',
  10. 20: 'BigIntegerField',
  11. 21: 'SmallIntegerField',
  12. 23: 'IntegerField',
  13. 25: 'TextField',
  14. 700: 'FloatField',
  15. 701: 'FloatField',
  16. 869: 'GenericIPAddressField',
  17. 1042: 'CharField', # blank-padded
  18. 1043: 'CharField',
  19. 1082: 'DateField',
  20. 1083: 'TimeField',
  21. 1114: 'DateTimeField',
  22. 1184: 'DateTimeField',
  23. 1186: 'DurationField',
  24. 1266: 'TimeField',
  25. 1700: 'DecimalField',
  26. 2950: 'UUIDField',
  27. }
  28. ignored_tables = []
  29. def get_field_type(self, data_type, description):
  30. field_type = super().get_field_type(data_type, description)
  31. if description.default and 'nextval' in description.default:
  32. if field_type == 'IntegerField':
  33. return 'AutoField'
  34. elif field_type == 'BigIntegerField':
  35. return 'BigAutoField'
  36. elif field_type == 'SmallIntegerField':
  37. return 'SmallAutoField'
  38. return field_type
  39. def get_table_list(self, cursor):
  40. """Return a list of table and view names in the current database."""
  41. cursor.execute("""
  42. SELECT c.relname,
  43. CASE WHEN {} THEN 'p' WHEN c.relkind IN ('m', 'v') THEN 'v' ELSE 't' END
  44. FROM pg_catalog.pg_class c
  45. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  46. WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
  47. AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
  48. AND pg_catalog.pg_table_is_visible(c.oid)
  49. """.format('c.relispartition' if self.connection.features.supports_table_partitions else 'FALSE'))
  50. return [TableInfo(*row) for row in cursor.fetchall() if row[0] not in self.ignored_tables]
  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. # Query the pg_catalog tables as cursor.description does not reliably
  57. # return the nullable property and information_schema.columns does not
  58. # contain details of materialized views.
  59. cursor.execute("""
  60. SELECT
  61. a.attname AS column_name,
  62. NOT (a.attnotnull OR (t.typtype = 'd' AND t.typnotnull)) AS is_nullable,
  63. pg_get_expr(ad.adbin, ad.adrelid) AS column_default
  64. FROM pg_attribute a
  65. LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
  66. JOIN pg_type t ON a.atttypid = t.oid
  67. JOIN pg_class c ON a.attrelid = c.oid
  68. JOIN pg_namespace n ON c.relnamespace = n.oid
  69. WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
  70. AND c.relname = %s
  71. AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
  72. AND pg_catalog.pg_table_is_visible(c.oid)
  73. """, [table_name])
  74. field_map = {line[0]: line[1:] for line in cursor.fetchall()}
  75. cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
  76. return [
  77. FieldInfo(
  78. line.name,
  79. line.type_code,
  80. line.display_size,
  81. line.internal_size,
  82. line.precision,
  83. line.scale,
  84. *field_map[line.name],
  85. )
  86. for line in cursor.description
  87. ]
  88. def get_sequences(self, cursor, table_name, table_fields=()):
  89. cursor.execute("""
  90. SELECT s.relname as sequence_name, col.attname
  91. FROM pg_class s
  92. JOIN pg_namespace sn ON sn.oid = s.relnamespace
  93. JOIN pg_depend d ON d.refobjid = s.oid AND d.refclassid = 'pg_class'::regclass
  94. JOIN pg_attrdef ad ON ad.oid = d.objid AND d.classid = 'pg_attrdef'::regclass
  95. JOIN pg_attribute col ON col.attrelid = ad.adrelid AND col.attnum = ad.adnum
  96. JOIN pg_class tbl ON tbl.oid = ad.adrelid
  97. WHERE s.relkind = 'S'
  98. AND d.deptype in ('a', 'n')
  99. AND pg_catalog.pg_table_is_visible(tbl.oid)
  100. AND tbl.relname = %s
  101. """, [table_name])
  102. return [
  103. {'name': row[0], 'table': table_name, 'column': row[1]}
  104. for row in cursor.fetchall()
  105. ]
  106. def get_relations(self, cursor, table_name):
  107. """
  108. Return a dictionary of {field_name: (field_name_other_table, other_table)}
  109. representing all relationships to the given table.
  110. """
  111. return {row[0]: (row[2], row[1]) for row in self.get_key_columns(cursor, table_name)}
  112. def get_key_columns(self, cursor, table_name):
  113. cursor.execute("""
  114. SELECT a1.attname, c2.relname, a2.attname
  115. FROM pg_constraint con
  116. LEFT JOIN pg_class c1 ON con.conrelid = c1.oid
  117. LEFT JOIN pg_class c2 ON con.confrelid = c2.oid
  118. LEFT JOIN pg_attribute a1 ON c1.oid = a1.attrelid AND a1.attnum = con.conkey[1]
  119. LEFT JOIN pg_attribute a2 ON c2.oid = a2.attrelid AND a2.attnum = con.confkey[1]
  120. WHERE
  121. c1.relname = %s AND
  122. con.contype = 'f' AND
  123. c1.relnamespace = c2.relnamespace AND
  124. pg_catalog.pg_table_is_visible(c1.oid)
  125. """, [table_name])
  126. return cursor.fetchall()
  127. def get_constraints(self, cursor, table_name):
  128. """
  129. Retrieve any constraints or keys (unique, pk, fk, check, index) across
  130. one or more columns. Also retrieve the definition of expression-based
  131. indexes.
  132. """
  133. constraints = {}
  134. # Loop over the key table, collecting things as constraints. The column
  135. # array must return column names in the same order in which they were
  136. # created.
  137. cursor.execute("""
  138. SELECT
  139. c.conname,
  140. array(
  141. SELECT attname
  142. FROM unnest(c.conkey) WITH ORDINALITY cols(colid, arridx)
  143. JOIN pg_attribute AS ca ON cols.colid = ca.attnum
  144. WHERE ca.attrelid = c.conrelid
  145. ORDER BY cols.arridx
  146. ),
  147. c.contype,
  148. (SELECT fkc.relname || '.' || fka.attname
  149. FROM pg_attribute AS fka
  150. JOIN pg_class AS fkc ON fka.attrelid = fkc.oid
  151. WHERE fka.attrelid = c.confrelid AND fka.attnum = c.confkey[1]),
  152. cl.reloptions
  153. FROM pg_constraint AS c
  154. JOIN pg_class AS cl ON c.conrelid = cl.oid
  155. WHERE cl.relname = %s AND pg_catalog.pg_table_is_visible(cl.oid)
  156. """, [table_name])
  157. for constraint, columns, kind, used_cols, options in cursor.fetchall():
  158. constraints[constraint] = {
  159. "columns": columns,
  160. "primary_key": kind == "p",
  161. "unique": kind in ["p", "u"],
  162. "foreign_key": tuple(used_cols.split(".", 1)) if kind == "f" else None,
  163. "check": kind == "c",
  164. "index": False,
  165. "definition": None,
  166. "options": options,
  167. }
  168. # Now get indexes
  169. cursor.execute("""
  170. SELECT
  171. indexname, array_agg(attname ORDER BY arridx), indisunique, indisprimary,
  172. array_agg(ordering ORDER BY arridx), amname, exprdef, s2.attoptions
  173. FROM (
  174. SELECT
  175. c2.relname as indexname, idx.*, attr.attname, am.amname,
  176. CASE
  177. WHEN idx.indexprs IS NOT NULL THEN
  178. pg_get_indexdef(idx.indexrelid)
  179. END AS exprdef,
  180. CASE am.amname
  181. WHEN 'btree' THEN
  182. CASE (option & 1)
  183. WHEN 1 THEN 'DESC' ELSE 'ASC'
  184. END
  185. END as ordering,
  186. c2.reloptions as attoptions
  187. FROM (
  188. SELECT *
  189. FROM pg_index i, unnest(i.indkey, i.indoption) WITH ORDINALITY koi(key, option, arridx)
  190. ) idx
  191. LEFT JOIN pg_class c ON idx.indrelid = c.oid
  192. LEFT JOIN pg_class c2 ON idx.indexrelid = c2.oid
  193. LEFT JOIN pg_am am ON c2.relam = am.oid
  194. LEFT JOIN pg_attribute attr ON attr.attrelid = c.oid AND attr.attnum = idx.key
  195. WHERE c.relname = %s AND pg_catalog.pg_table_is_visible(c.oid)
  196. ) s2
  197. GROUP BY indexname, indisunique, indisprimary, amname, exprdef, attoptions;
  198. """, [table_name])
  199. for index, columns, unique, primary, orders, type_, definition, options in cursor.fetchall():
  200. if index not in constraints:
  201. basic_index = type_ == 'btree' and not index.endswith('_btree') and options is None
  202. constraints[index] = {
  203. "columns": columns if columns != [None] else [],
  204. "orders": orders if orders != [None] else [],
  205. "primary_key": primary,
  206. "unique": unique,
  207. "foreign_key": None,
  208. "check": False,
  209. "index": True,
  210. "type": Index.suffix if basic_index else type_,
  211. "definition": definition,
  212. "options": options,
  213. }
  214. return constraints