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