|
1 from django.db.backends import BaseDatabaseIntrospection |
|
2 |
|
3 class DatabaseIntrospection(BaseDatabaseIntrospection): |
|
4 # Maps type codes to Django Field types. |
|
5 data_types_reverse = { |
|
6 16: 'BooleanField', |
|
7 20: 'BigIntegerField', |
|
8 21: 'SmallIntegerField', |
|
9 23: 'IntegerField', |
|
10 25: 'TextField', |
|
11 700: 'FloatField', |
|
12 701: 'FloatField', |
|
13 869: 'IPAddressField', |
|
14 1043: 'CharField', |
|
15 1082: 'DateField', |
|
16 1083: 'TimeField', |
|
17 1114: 'DateTimeField', |
|
18 1184: 'DateTimeField', |
|
19 1266: 'TimeField', |
|
20 1700: 'DecimalField', |
|
21 } |
|
22 |
|
23 def get_table_list(self, cursor): |
|
24 "Returns a list of table names in the current database." |
|
25 cursor.execute(""" |
|
26 SELECT c.relname |
|
27 FROM pg_catalog.pg_class c |
|
28 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
29 WHERE c.relkind IN ('r', 'v', '') |
|
30 AND n.nspname NOT IN ('pg_catalog', 'pg_toast') |
|
31 AND pg_catalog.pg_table_is_visible(c.oid)""") |
|
32 return [row[0] for row in cursor.fetchall()] |
|
33 |
|
34 def get_table_description(self, cursor, table_name): |
|
35 "Returns a description of the table, with the DB-API cursor.description interface." |
|
36 cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name)) |
|
37 return cursor.description |
|
38 |
|
39 def get_relations(self, cursor, table_name): |
|
40 """ |
|
41 Returns a dictionary of {field_index: (field_index_other_table, other_table)} |
|
42 representing all relationships to the given table. Indexes are 0-based. |
|
43 """ |
|
44 cursor.execute(""" |
|
45 SELECT con.conkey, con.confkey, c2.relname |
|
46 FROM pg_constraint con, pg_class c1, pg_class c2 |
|
47 WHERE c1.oid = con.conrelid |
|
48 AND c2.oid = con.confrelid |
|
49 AND c1.relname = %s |
|
50 AND con.contype = 'f'""", [table_name]) |
|
51 relations = {} |
|
52 for row in cursor.fetchall(): |
|
53 try: |
|
54 # row[0] and row[1] are like "{2}", so strip the curly braces. |
|
55 relations[int(row[0][1:-1]) - 1] = (int(row[1][1:-1]) - 1, row[2]) |
|
56 except ValueError: |
|
57 continue |
|
58 return relations |
|
59 |
|
60 def get_indexes(self, cursor, table_name): |
|
61 """ |
|
62 Returns a dictionary of fieldname -> infodict for the given table, |
|
63 where each infodict is in the format: |
|
64 {'primary_key': boolean representing whether it's the primary key, |
|
65 'unique': boolean representing whether it's a unique index} |
|
66 """ |
|
67 # This query retrieves each index on the given table, including the |
|
68 # first associated field name |
|
69 cursor.execute(""" |
|
70 SELECT attr.attname, idx.indkey, idx.indisunique, idx.indisprimary |
|
71 FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, |
|
72 pg_catalog.pg_index idx, pg_catalog.pg_attribute attr |
|
73 WHERE c.oid = idx.indrelid |
|
74 AND idx.indexrelid = c2.oid |
|
75 AND attr.attrelid = c.oid |
|
76 AND attr.attnum = idx.indkey[0] |
|
77 AND c.relname = %s""", [table_name]) |
|
78 indexes = {} |
|
79 for row in cursor.fetchall(): |
|
80 # row[1] (idx.indkey) is stored in the DB as an array. It comes out as |
|
81 # a string of space-separated integers. This designates the field |
|
82 # indexes (1-based) of the fields that have indexes on the table. |
|
83 # Here, we skip any indexes across multiple fields. |
|
84 if ' ' in row[1]: |
|
85 continue |
|
86 indexes[row[0]] = {'primary_key': row[3], 'unique': row[2]} |
|
87 return indexes |
|
88 |