app/django/db/backends/postgresql/introspection.py
changeset 54 03e267d67478
child 323 ff1a9aa48cfd
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 from django.db.backends.postgresql.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         try:
       
    36             # row[0] and row[1] are like "{2}", so strip the curly braces.
       
    37             relations[int(row[0][1:-1]) - 1] = (int(row[1][1:-1]) - 1, row[2])
       
    38         except ValueError:
       
    39             continue
       
    40     return relations
       
    41 
       
    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 }