2006-05-18 11:36:58 +08:00
|
|
|
from django.db.backends.postgresql_psycopg2.base import quote_name
|
|
|
|
|
|
|
|
def get_table_list(cursor):
|
|
|
|
"Returns a list of table names in the current database."
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT c.relname
|
|
|
|
FROM pg_catalog.pg_class c
|
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
|
|
|
WHERE c.relkind IN ('r', 'v', '')
|
|
|
|
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
|
|
|
|
AND pg_catalog.pg_table_is_visible(c.oid)""")
|
|
|
|
return [row[0] for row in cursor.fetchall()]
|
|
|
|
|
|
|
|
def get_table_description(cursor, table_name):
|
|
|
|
"Returns a description of the table, with the DB-API cursor.description interface."
|
|
|
|
cursor.execute("SELECT * FROM %s LIMIT 1" % quote_name(table_name))
|
|
|
|
return cursor.description
|
|
|
|
|
|
|
|
def get_relations(cursor, table_name):
|
|
|
|
"""
|
|
|
|
Returns a dictionary of {field_index: (field_index_other_table, other_table)}
|
|
|
|
representing all relationships to the given table. Indexes are 0-based.
|
|
|
|
"""
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT con.conkey, con.confkey, c2.relname
|
|
|
|
FROM pg_constraint con, pg_class c1, pg_class c2
|
|
|
|
WHERE c1.oid = con.conrelid
|
|
|
|
AND c2.oid = con.confrelid
|
|
|
|
AND c1.relname = %s
|
|
|
|
AND con.contype = 'f'""", [table_name])
|
|
|
|
relations = {}
|
|
|
|
for row in cursor.fetchall():
|
|
|
|
try:
|
2007-07-13 16:52:07 +08:00
|
|
|
# row[0] and row[1] are single-item lists, so grab the single item.
|
|
|
|
relations[int(row[0][0]) - 1] = (int(row[1][0]) - 1, row[2])
|
2006-05-18 11:36:58 +08:00
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
return relations
|
|
|
|
|
|
|
|
def get_indexes(cursor, table_name):
|
|
|
|
"""
|
|
|
|
Returns a dictionary of fieldname -> infodict for the given table,
|
|
|
|
where each infodict is in the format:
|
|
|
|
{'primary_key': boolean representing whether it's the primary key,
|
|
|
|
'unique': boolean representing whether it's a unique index}
|
|
|
|
"""
|
2006-06-02 00:27:41 +08:00
|
|
|
# This query retrieves each index on the given table, including the
|
|
|
|
# first associated field name
|
2006-05-18 11:36:58 +08:00
|
|
|
cursor.execute("""
|
2006-06-02 00:27:41 +08:00
|
|
|
SELECT attr.attname, idx.indkey, idx.indisunique, idx.indisprimary
|
2006-05-18 11:36:58 +08:00
|
|
|
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,
|
2006-06-02 00:27:41 +08:00
|
|
|
pg_catalog.pg_index idx, pg_catalog.pg_attribute attr
|
2006-05-18 11:36:58 +08:00
|
|
|
WHERE c.oid = idx.indrelid
|
|
|
|
AND idx.indexrelid = c2.oid
|
2006-06-02 00:27:41 +08:00
|
|
|
AND attr.attrelid = c.oid
|
|
|
|
AND attr.attnum = idx.indkey[0]
|
2006-05-18 11:36:58 +08:00
|
|
|
AND c.relname = %s""", [table_name])
|
|
|
|
indexes = {}
|
|
|
|
for row in cursor.fetchall():
|
2006-06-02 00:27:41 +08:00
|
|
|
# row[1] (idx.indkey) is stored in the DB as an array. It comes out as
|
2006-05-18 11:36:58 +08:00
|
|
|
# a string of space-separated integers. This designates the field
|
|
|
|
# indexes (1-based) of the fields that have indexes on the table.
|
|
|
|
# Here, we skip any indexes across multiple fields.
|
2006-06-02 00:27:41 +08:00
|
|
|
if ' ' in row[1]:
|
2006-05-18 11:36:58 +08:00
|
|
|
continue
|
2006-06-02 00:27:41 +08:00
|
|
|
indexes[row[0]] = {'primary_key': row[3], 'unique': row[2]}
|
2006-05-18 11:36:58 +08:00
|
|
|
return indexes
|
|
|
|
|
|
|
|
# Maps type codes to Django Field types.
|
|
|
|
DATA_TYPES_REVERSE = {
|
|
|
|
16: 'BooleanField',
|
|
|
|
21: 'SmallIntegerField',
|
|
|
|
23: 'IntegerField',
|
|
|
|
25: 'TextField',
|
2007-05-21 09:29:58 +08:00
|
|
|
701: 'FloatField',
|
2006-05-18 11:36:58 +08:00
|
|
|
869: 'IPAddressField',
|
|
|
|
1043: 'CharField',
|
|
|
|
1082: 'DateField',
|
|
|
|
1083: 'TimeField',
|
|
|
|
1114: 'DateTimeField',
|
|
|
|
1184: 'DateTimeField',
|
|
|
|
1266: 'TimeField',
|
2007-05-21 09:29:58 +08:00
|
|
|
1700: 'DecimalField',
|
2006-05-18 11:36:58 +08:00
|
|
|
}
|