Ticket #1904: postgresql_psycopg2.2.diff

File postgresql_psycopg2.2.diff, 4.9 KB (added by germish@…, 18 years ago)

delete the old .diff file. i forogt to remove that import psycopg2.extensions line at the top.

  • trunk/django/db/backends/postgresql_psycopg2/base.py

     
    104104# Register these custom typecasts, because Django expects dates/times to be
    105105# in Python's native (standard-library) datetime/time format, whereas psycopg
    106106# use mx.DateTime by default.
    107 Database.register_type(Database.new_type((1082,), "DATE", util.typecast_date))
    108 Database.register_type(Database.new_type((1083,1266), "TIME", util.typecast_time))
    109 Database.register_type(Database.new_type((1114,1184), "TIMESTAMP", util.typecast_timestamp))
    110 Database.register_type(Database.new_type((16,), "BOOLEAN", util.typecast_boolean))
     107Database._2psycopg.register_type(Database._2psycopg.new_type((1082,), "DATE", util.typecast_date))
     108Database._2psycopg.register_type(Database._2psycopg.new_type((1083,1266), "TIME", util.typecast_time))
     109Database._2psycopg.register_type(Database._2psycopg.new_type((1114,1184), "TIMESTAMP", util.typecast_timestamp))
     110Database._2psycopg.register_type(Database._2psycopg.new_type((16,), "BOOLEAN", util.typecast_boolean))
    111111
    112112OPERATOR_MAPPING = {
    113113    'exact': '= %s',
  • trunk/django/db/backends/postgresql_psycopg2/introspection.py

     
    1 from django.db.backends.postgresql.introspection import *
     1from django.db import transaction
     2from django.db.backends.postgresql_psycopg2.base import quote_name
     3
     4def get_table_list(cursor):
     5    "Returns a list of table names in the current database."
     6    cursor.execute("""
     7        SELECT c.relname
     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()]
     14
     15def 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
     19
     20def get_relations(cursor, table_name):
     21    """
     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.
     24    """
     25    cursor.execute("""
     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
     30            AND c1.relname = %s
     31            AND con.contype = 'f'""", [table_name])
     32    relations = {}
     33    for row in cursor.fetchall():
     34        try:
     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])
     37        except ValueError:
     38            continue
     39    return relations
     40
     41def get_indexes(cursor, table_name):
     42    """
     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}
     47    """
     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.
     52    cursor.execute("""
     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])
     59    indexes = {}
     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.
     65        if ' ' in row[0]:
     66            continue
     67        col_name = desc[int(row[0])-1][0]
     68        indexes[col_name] = {'primary_key': row[2], 'unique': row[1]}
     69    return indexes
     70
     71# Maps type codes to Django Field types.
     72DATA_TYPES_REVERSE = {
     73    16: 'BooleanField',
     74    21: 'SmallIntegerField',
     75    23: 'IntegerField',
     76    25: 'TextField',
     77    869: 'IPAddressField',
     78    1043: 'CharField',
     79    1082: 'DateField',
     80    1083: 'TimeField',
     81    1114: 'DateTimeField',
     82    1184: 'DateTimeField',
     83    1266: 'TimeField',
     84    1700: 'FloatField',
     85}
Back to Top