Changeset 739 for trunk/pg.py


Ignore:
Timestamp:
Jan 13, 2016, 7:30:50 PM (4 years ago)
Author:
cito
Message:

Return ordered dict for attributes is possible

Sometimes it's important to know the order of the columns in a table.
By returning an OrderedDict? instead of a dict in get_attnames, we can
deliver that information en passant, while staying backward compatible.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pg.py

    r738 r739  
    3939from collections import namedtuple
    4040from functools import partial
     41
     42try:
     43    from collections import OrderedDict
     44except ImportError:  # Python 2.6 or 3.0
     45    OrderedDict = dict
    4146
    4247try:
     
    554559        the values are the names of the attributes' types).
    555560
    556         If the optional newattnames exists, it must be a dictionary and
    557         will become the new attribute names dictionary.
     561        If your Python version supports this, the dictionary will be an
     562        OrderedDictionary with the column names in the right order.
     563
     564        If flush is set then the internal cache for attribute names will
     565        be flushed. This may be necessary after the database schema or
     566        the search path has been changed.
    558567
    559568        By default, only a limited number of simple types will be returned.
     
    573582                " WHERE a.attrelid = %s::regclass"
    574583                " AND (a.attnum > 0 OR a.attname = 'oid')"
    575                 " AND NOT a.attisdropped") % (
     584                " AND NOT a.attisdropped ORDER BY a.attnum") % (
    576585                    '::regtype' if self._regtypes else '',
    577586                    self._prepare_qualified_param(table, 1))
     
    579588            if not names:
    580589                raise KeyError('Table %s does not exist' % table)
    581             if self._regtypes:
    582                 names = dict(names)
    583             else:
    584                 names = dict((name, _simpletype(typ)) for name, typ in names)
     590            if not self._regtypes:
     591                names = ((name, _simpletype(typ)) for name, typ in names)
     592            names = OrderedDict(names)
    585593            attnames[table] = names  # cache it
    586594        return names
Note: See TracChangeset for help on using the changeset viewer.