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