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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. return field_type
  37. def get_table_list(self, cursor):
  38. """Return a list of table and view names in the current database."""
  39. cursor.execute("""
  40. SELECT c.relname,
  41. CASE WHEN {} THEN 'p' WHEN c.relkind IN ('m', 'v') THEN 'v' ELSE 't' END
  42. FROM pg_catalog.pg_class c
  43. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  44. WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
  45. AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
  46. AND pg_catalog.pg_table_is_visible(c.oid)
  47. """.format('c.relispartition' if self.connection.features.supports_table_partitions else 'FALSE'))
  48. return [TableInfo(*row) for row in cursor.fetchall() if row[0] not in self.ignored_tables]
  49. def get_table_description(self, cursor, table_name):
  50. """
  51. Return a description of the table with the DB-API cursor.description
  52. interface.
  53. """
  54. # Query the pg_catalog tables as cursor.description does not reliably
  55. # return the nullable property and information_schema.columns does not
  56. # contain details of materialized views.
  57. cursor.execute("""
  58. SELECT
  59. a.attname AS column_name,
  60. NOT (a.attnotnull OR (t.typtype = 'd' AND t.typnotnull)) AS is_nullable,
  61. pg_get_expr(ad.adbin, ad.adrelid) AS column_default
  62. FROM pg_attribute a
  63. LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
  64. JOIN pg_type t ON a.atttypid = t.oid
  65. JOIN pg_class c ON a.attrelid = c.oid
  66. JOIN pg_namespace n ON c.relnamespace = n.oid
  67. WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
  68. AND c.relname = %s
  69. AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
  70. AND pg_catalog.pg_table_is_visible(c.oid)
  71. """, [table_name])
  72. field_map = {line[0]: line[1:] for line in cursor.fetchall()}
  73. cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
  74. return [
  75. FieldInfo(
  76. line.name,
  77. line.type_code,
  78. line.display_size,
  79. line.internal_size,
  80. line.precision,
  81. line.scale,
  82. *field_map[line.name],
  83. )
  84. for line in cursor.description
  85. ]
  86. def get_sequences(self, cursor, table_name, table_fields=()):
  87. cursor.execute("""
  88. SELECT s.relname as sequence_name, col.attname
  89. FROM pg_class s
  90. JOIN pg_namespace sn ON sn.oid = s.relnamespace
  91. JOIN pg_depend d ON d.refobjid = s.oid AND d.refclassid = 'pg_class'::regclass
  92. JOIN pg_attrdef ad ON ad.oid = d.objid AND d.classid = 'pg_attrdef'::regclass
  93. JOIN pg_attribute col ON col.attrelid = ad.adrelid AND col.attnum = ad.adnum
  94. JOIN pg_class tbl ON tbl.oid = ad.adrelid
  95. JOIN pg_namespace n ON n.oid = tbl.relnamespace
  96. WHERE s.relkind = 'S'
  97. AND d.deptype in ('a', 'n')
  98. AND n.nspname = 'public'
  99. AND tbl.relname = %s
  100. """, [table_name])
  101. return [
  102. {'name': row[0], 'table': table_name, 'column': row[1]}
  103. for row in cursor.fetchall()
  104. ]
  105. def get_relations(self, cursor, table_name):
  106. """
  107. Return a dictionary of {field_name: (field_name_other_table, other_table)}
  108. representing all relationships to the given table.
  109. """
  110. cursor.execute("""
  111. SELECT c2.relname, a1.attname, a2.attname
  112. FROM pg_constraint con
  113. LEFT JOIN pg_class c1 ON con.conrelid = c1.oid
  114. LEFT JOIN pg_class c2 ON con.confrelid = c2.oid
  115. LEFT JOIN pg_attribute a1 ON c1.oid = a1.attrelid AND a1.attnum = con.conkey[1]
  116. LEFT JOIN pg_attribute a2 ON c2.oid = a2.attrelid AND a2.attnum = con.confkey[1]
  117. WHERE c1.relname = %s AND con.contype = 'f'
  118. """, [table_name])
  119. return {row[1]: (row[2], row[0]) for row in cursor.fetchall()}
  120. def get_key_columns(self, cursor, table_name):
  121. cursor.execute("""
  122. SELECT kcu.column_name, ccu.table_name AS referenced_table, ccu.column_name AS referenced_column
  123. FROM information_schema.constraint_column_usage ccu
  124. LEFT JOIN information_schema.key_column_usage kcu
  125. ON ccu.constraint_catalog = kcu.constraint_catalog
  126. AND ccu.constraint_schema = kcu.constraint_schema
  127. AND ccu.constraint_name = kcu.constraint_name
  128. LEFT JOIN information_schema.table_constraints tc
  129. ON ccu.constraint_catalog = tc.constraint_catalog
  130. AND ccu.constraint_schema = tc.constraint_schema
  131. AND ccu.constraint_name = tc.constraint_name
  132. WHERE kcu.table_name = %s AND tc.constraint_type = 'FOREIGN KEY'
  133. """, [table_name])
  134. return cursor.fetchall()
  135. def get_constraints(self, cursor, table_name):
  136. """
  137. Retrieve any constraints or keys (unique, pk, fk, check, index) across
  138. one or more columns. Also retrieve the definition of expression-based
  139. indexes.
  140. """
  141. constraints = {}
  142. # Loop over the key table, collecting things as constraints. The column
  143. # array must return column names in the same order in which they were
  144. # created.
  145. cursor.execute("""
  146. SELECT
  147. c.conname,
  148. array(
  149. SELECT attname
  150. FROM unnest(c.conkey) WITH ORDINALITY cols(colid, arridx)
  151. JOIN pg_attribute AS ca ON cols.colid = ca.attnum
  152. WHERE ca.attrelid = c.conrelid
  153. ORDER BY cols.arridx
  154. ),
  155. c.contype,
  156. (SELECT fkc.relname || '.' || fka.attname
  157. FROM pg_attribute AS fka
  158. JOIN pg_class AS fkc ON fka.attrelid = fkc.oid
  159. WHERE fka.attrelid = c.confrelid AND fka.attnum = c.confkey[1]),
  160. cl.reloptions
  161. FROM pg_constraint AS c
  162. JOIN pg_class AS cl ON c.conrelid = cl.oid
  163. JOIN pg_namespace AS ns ON cl.relnamespace = ns.oid
  164. WHERE ns.nspname = %s AND cl.relname = %s
  165. """, ["public", table_name])
  166. for constraint, columns, kind, used_cols, options in cursor.fetchall():
  167. constraints[constraint] = {
  168. "columns": columns,
  169. "primary_key": kind == "p",
  170. "unique": kind in ["p", "u"],
  171. "foreign_key": tuple(used_cols.split(".", 1)) if kind == "f" else None,
  172. "check": kind == "c",
  173. "index": False,
  174. "definition": None,
  175. "options": options,
  176. }
  177. # Now get indexes
  178. cursor.execute("""
  179. SELECT
  180. indexname, array_agg(attname ORDER BY arridx), indisunique, indisprimary,
  181. array_agg(ordering ORDER BY arridx), amname, exprdef, s2.attoptions
  182. FROM (
  183. SELECT
  184. c2.relname as indexname, idx.*, attr.attname, am.amname,
  185. CASE
  186. WHEN idx.indexprs IS NOT NULL THEN
  187. pg_get_indexdef(idx.indexrelid)
  188. END AS exprdef,
  189. CASE am.amname
  190. WHEN 'btree' THEN
  191. CASE (option & 1)
  192. WHEN 1 THEN 'DESC' ELSE 'ASC'
  193. END
  194. END as ordering,
  195. c2.reloptions as attoptions
  196. FROM (
  197. SELECT *
  198. FROM pg_index i, unnest(i.indkey, i.indoption) WITH ORDINALITY koi(key, option, arridx)
  199. ) idx
  200. LEFT JOIN pg_class c ON idx.indrelid = c.oid
  201. LEFT JOIN pg_class c2 ON idx.indexrelid = c2.oid
  202. LEFT JOIN pg_am am ON c2.relam = am.oid
  203. LEFT JOIN pg_attribute attr ON attr.attrelid = c.oid AND attr.attnum = idx.key
  204. WHERE c.relname = %s
  205. ) s2
  206. GROUP BY indexname, indisunique, indisprimary, amname, exprdef, attoptions;
  207. """, [table_name])
  208. for index, columns, unique, primary, orders, type_, definition, options in cursor.fetchall():
  209. if index not in constraints:
  210. basic_index = type_ == 'btree' and not index.endswith('_btree') and options is None
  211. constraints[index] = {
  212. "columns": columns if columns != [None] else [],
  213. "orders": orders if orders != [None] else [],
  214. "primary_key": primary,
  215. "unique": unique,
  216. "foreign_key": None,
  217. "check": False,
  218. "index": True,
  219. "type": Index.suffix if basic_index else type_,
  220. "definition": definition,
  221. "options": options,
  222. }
  223. return constraints