1
from django.db import transaction
2
from django.db.backends.postgresql.base import quote_name
4
def get_table_list(cursor):
5
"Returns a list of table names in the current database."
8
FROM pg_catalog.pg_class c
9
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
10
WHERE c.relkind IN ('r', 'v', '')
11
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
12
AND pg_catalog.pg_table_is_visible(c.oid)""")
13
return [row[0] for row in cursor.fetchall()]
15
def get_table_description(cursor, table_name):
16
"Returns a description of the table, with the DB-API cursor.description interface."
17
cursor.execute("SELECT * FROM %s LIMIT 1" % quote_name(table_name))
18
return cursor.description
20
def get_relations(cursor, table_name):
22
Returns a dictionary of {field_index: (field_index_other_table, other_table)}
23
representing all relationships to the given table. Indexes are 0-based.
26
SELECT con.conkey, con.confkey, c2.relname
27
FROM pg_constraint con, pg_class c1, pg_class c2
28
WHERE c1.oid = con.conrelid
29
AND c2.oid = con.confrelid
31
AND con.contype = 'f'""", [table_name])
33
for row in cursor.fetchall():
35
# row[0] and row[1] are like "{2}", so strip the curly braces.
36
relations[int(row[0][1:-1]) - 1] = (int(row[1][1:-1]) - 1, row[2])
41
def get_indexes(cursor, table_name):
43
Returns a dictionary of fieldname -> infodict for the given table,
44
where each infodict is in the format:
45
{'primary_key': boolean representing whether it's the primary key,
46
'unique': boolean representing whether it's a unique index}
48
# Get the table description because we only have the column indexes, and we
49
# need the column names.
50
desc = get_table_description(cursor, table_name)
51
# This query retrieves each index on the given table.
53
SELECT idx.indkey, idx.indisunique, idx.indisprimary
54
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,
55
pg_catalog.pg_index idx
56
WHERE c.oid = idx.indrelid
57
AND idx.indexrelid = c2.oid
58
AND c.relname = %s""", [table_name])
60
for row in cursor.fetchall():
61
# row[0] (idx.indkey) is stored in the DB as an array. It comes out as
62
# a string of space-separated integers. This designates the field
63
# indexes (1-based) of the fields that have indexes on the table.
64
# Here, we skip any indexes across multiple fields.
67
col_name = desc[int(row[0])-1][0]
68
indexes[col_name] = {'primary_key': row[2], 'unique': row[1]}
71
# Maps type codes to Django Field types.
72
DATA_TYPES_REVERSE = {
74
21: 'SmallIntegerField',
77
869: 'IPAddressField',
81
1114: 'DateTimeField',
82
1184: 'DateTimeField',