Ticket #3702: adodbintrospection.patch

File adodbintrospection.patch, 2.9 KB (added by Moof, 17 years ago)

patch for django.db.backends.ado_mssql.introspection

  • introspection.py

     
     1from django.db.backends import util
     2import adodbapi.adodbapi
     3
    14def get_table_list(cursor):
    2     raise NotImplementedError
     5    "Returns a list of table names in the current database."
     6    cursor.execute("sp_tables @table_type=\"'TABLE'\"")
     7    rows = cursor.fetchall()
     8    return [row[2] for row in rows]
     9   
    310
    411def get_table_description(cursor, table_name):
    5     raise NotImplementedError
     12    "Returns a description of the table, with the DB-API cursor.description interface."
     13    cursor.execute("SELECT TOP 1 * FROM %s" % table_name)
     14    return cursor.description
     15   
    616
     17def _name_to_index(cursor, table_name):
     18    """
     19    Returns a dictionary of {field_name: field_index} for the given table.
     20    Indexes are 0-based.
     21    """
     22    return dict([(d[0], i) for i, d in enumerate(get_table_description(cursor, table_name))])
     23
    724def get_relations(cursor, table_name):
    8     raise NotImplementedError
     25    """
     26    Returns a dictionary of {field_index: (field_index_other_table, other_table)}
     27    representing all relationships to the given table. Indexes are 0-based.
     28    """
     29    my_field_dict = _name_to_index(cursor, table_name)
     30    cursor.execute("sp_fkeys @fktable_name='%s'" % u"ForeginTable1")
     31    their_field_dicts = {}
     32    result = {}
     33    for row in util.dictfetchall(cursor):
     34        if row[u'FKTABLE_NAME'] != table_name:
     35            continue
     36        if not their_field_dicts.get(row[u'PKTABLE_NAME']):
     37            their_field_dicts[row[u'PKTABLE_NAME']
     38                ] = _name_to_index(cursor, row[u'PKTABLE_NAME'])
     39        their_fields = their_field_dicts[row[u'PKTABLE_NAME']]
     40        result[my_field_dict[row[u'FKCOLUMN_NAME']]] = (
     41            their_fields[row[u'PKCOLUMN_NAME']],
     42            row[u'PKTABLE_NAME'])
     43    return result
     44   
    945
    1046def get_indexes(cursor, table_name):
    11     raise NotImplementedError
     47    """
     48    Returns a dictionary of fieldname -> infodict for the given table,
     49    where each infodict is in the format:
     50        {'primary_key': boolean representing whether it's the primary key,
     51         'unique': boolean representing whether it's a unique index}
     52    """
     53    cursor.execute("sp_helpindex @objname='%s'" % table_name)
     54    indexes = {}
     55    try:
     56        rows = cursor.fetchall()
     57    except adodbapi.adodbapi.Error, e:
     58        if e.args == (None,):
     59            rows = [] # this is a workaround because I don't understand
     60                      # why this error comes up. Will report to adodbapi
     61        else:
     62            raise
     63       
     64    for row in rows:
     65        flags = row[1]
     66        flagdict = {'primary_key': 'primary key' in flags,
     67                    'unique': 'unique' in flags}
     68        indexes[row[2]] = flagdict
     69    return indexes
     70   
    1271
    1372DATA_TYPES_REVERSE = {}
Back to Top