Django

Code

Changeset 3285

Show
Ignore:
Timestamp:
07/06/06 23:06:00 (3 years ago)
Author:
adrian
Message:

Fixed #2272 -- Improved SQLite database introspection. Thanks, dne@mayonnaise.net

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r3174 r3285  
    5555    Alex Dedul 
    5656    deric@monowerks.com 
     57    dne@mayonnaise.net 
    5758    Jeremy Dunck <http://dunck.us/> 
    5859    Clint Ecker 
  • django/trunk/django/db/backends/sqlite3/introspection.py

    r2809 r3285  
    33 
    44def get_table_list(cursor): 
    5     cursor.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name") 
     5    "Returns a list of table names in the current database." 
     6    # Skip the sqlite_sequence system table used for autoincrement key 
     7    # generation. 
     8    cursor.execute(""" 
     9        SELECT name FROM sqlite_master 
     10        WHERE type='table' AND NOT name='sqlite_sequence' 
     11        ORDER BY name""") 
    612    return [row[0] for row in cursor.fetchall()] 
    713 
    814def get_table_description(cursor, table_name): 
    9     cursor.execute("PRAGMA table_info(%s)" % quote_name(table_name)) 
    10     return [(row[1], row[2], None, None) for row in cursor.fetchall()] 
     15    "Returns a description of the table, with the DB-API cursor.description interface." 
     16    return [(info['name'], info['type'], None, None, None, None, 
     17             info['null_ok']) for info in _table_info(cursor, table_name)] 
    1118 
    1219def get_relations(cursor, table_name): 
     
    1421 
    1522def get_indexes(cursor, table_name): 
    16     raise NotImplementedError 
     23    """ 
     24    Returns a dictionary of fieldname -> infodict for the given table, 
     25    where each infodict is in the format: 
     26        {'primary_key': boolean representing whether it's the primary key, 
     27         'unique': boolean representing whether it's a unique index} 
     28    """ 
     29    indexes = {} 
     30    for info in _table_info(cursor, table_name): 
     31        indexes[info['name']] = {'primary_key': info['pk'] != 0, 
     32                                 'unique': False} 
     33    cursor.execute('PRAGMA index_list(%s)' % quote_name(table_name)) 
     34    # seq, name, unique 
     35    for index, unique in [(field[1], field[2]) for field in cursor.fetchall()]: 
     36        if not unique: 
     37            continue 
     38        cursor.execute('PRAGMA index_info(%s)' % quote_name(index)) 
     39        info = cursor.fetchall() 
     40        # Skip indexes across multiple fields 
     41        if len(info) != 1: 
     42            continue 
     43        name = info[0][2] # seqno, cid, name 
     44        indexes[name]['unique'] = True 
     45    return indexes 
     46 
     47def _table_info(cursor, name): 
     48    cursor.execute('PRAGMA table_info(%s)' % quote_name(name)) 
     49    # cid, name, type, notnull, dflt_value, pk 
     50    return [{'name': field[1], 
     51             'type': field[2], 
     52             'null_ok': not field[3], 
     53             'pk': field[5]     # undocumented 
     54             } for field in cursor.fetchall()] 
    1755 
    1856# Maps SQL types to Django Field types. Some of the SQL types have multiple 
  • django/trunk/docs/django-admin.txt

    r3062 r3285  
    127127models that refer to other models are ordered properly. 
    128128 
    129 Primary keys are automatically introspected for PostgreSQL and MySQL, in which 
    130 case Django puts in the ``primary_key=True`` where needed. 
     129Primary keys are automatically introspected for PostgreSQL, MySQL and 
     130SQLite, in which case Django puts in the ``primary_key=True`` where 
     131needed. 
    131132 
    132133``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection