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