コード例 #1
0
def _table_info(cursor, name):
    cursor.execute('PRAGMA table_info(%s)' % quote_name(name))
    # cid, name, type, notnull, dflt_value, pk
    return [{'name': field[1],
             'type': field[2],
             'null_ok': not field[3],
             'pk': field[5]     # undocumented
             } for field in cursor.fetchall()]
コード例 #2
0
def get_indexes(cursor, table_name):
    """
    Returns a dictionary of fieldname -> infodict for the given table,
    where each infodict is in the format:
        {'primary_key': boolean representing whether it's the primary key,
         'unique': boolean representing whether it's a unique index}
    """
    indexes = {}
    for info in _table_info(cursor, table_name):
        indexes[info['name']] = {'primary_key': info['pk'] != 0,
                                 'unique': False}
    cursor.execute('PRAGMA index_list(%s)' % quote_name(table_name))
    # seq, name, unique
    for index, unique in [(field[1], field[2]) for field in cursor.fetchall()]:
        if not unique:
            continue
        cursor.execute('PRAGMA index_info(%s)' % quote_name(index))
        info = cursor.fetchall()
        # Skip indexes across multiple fields
        if len(info) != 1:
            continue
        name = info[0][2] # seqno, cid, name
        indexes[name]['unique'] = True
    return indexes