Django

Code

Changeset 6165

Show
Ignore:
Timestamp:
09/14/07 00:33:39 (1 year ago)
Author:
gwilson
Message:

Refs #4299 -- Removed some code duplication in the postgresql introspection modules by letting the postgresql_psycopg2 backend use identical functions from the postgresql backend.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/backends/postgresql_psycopg2/introspection.py

    r5967 r6165  
    11from django.db.backends.postgresql_psycopg2.base import DatabaseOperations 
     2from django.db.backends.postgresql.introspection import get_table_list, get_table_description, get_indexes, DATA_TYPES_REVERSE 
    23 
    34quote_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 
    205 
    216def get_relations(cursor, table_name): 
     
    3621        relations[row[0][0] - 1] = (row[1][0] - 1, row[2]) 
    3722    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 }