Changeset 681


Ignore:
Timestamp:
Dec 30, 2015, 5:39:53 PM (4 years ago)
Author:
cito
Message:

Let cursor.description return named tuples

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/changelog.rst

    r679 r681  
    1212  Since the API provides objects of these types only by the use of
    1313  constructor functions, this should not cause any incompatibilities.
     14- The tty parameter and attribute of database connections has been
     15  removed since it is not supported any more since PostgreSQL 7.4.
     16- The 7-tuples returned by the description attribute of a pgdb cursor
     17  are now named tuples, i.e. their elements can be also accessed by name.
    1418
    1519Version 4.2
  • trunk/docs/pgdb.rst

    r680 r681  
    225225.. attribute:: Cursor.description
    226226
    227     This read-only attribute is a sequence of 7-item sequences.
    228 
    229     Each of these sequences contains information describing one result column:
    230 
    231     - *name*
    232     - *type_code*
    233     - *display_size*
    234     - *internal_size*
    235     - *precision*
    236     - *scale*
    237     - *null_ok*
    238 
    239     Note that *precision*, *scale* and *null_ok* are not implemented.
     227    This read-only attribute is a sequence of 7-item named tuples.
     228
     229    Each of these named tuples contains information describing
     230    one result column:
     231
     232        - *name*
     233        - *type_code*
     234        - *display_size*
     235        - *internal_size*
     236        - *precision*
     237        - *scale*
     238        - *null_ok*
     239
     240    Note that *display_size*, *precision*, *scale* and *null_ok*
     241    are not implemented.
    240242
    241243    This attribute will be ``None`` for operations that do not return rows
  • trunk/module/pgdb.py

    r680 r681  
    4949    #   [(column_name, type_name, display_size,
    5050    #           internal_size, precision, scale, null_ok), ...]
    51     # Note that precision, scale and null_ok are not implemented.
     51    # Note that display_size, precision, scale and null_ok
     52    # are not implemented.
    5253
    5354    cursor.rowcount # number of rows available in the result set
     
    7273from decimal import Decimal
    7374from math import isnan, isinf
     75from collections import namedtuple
    7476
    7577try:
     
    197199            # The column name is omitted from the return value.
    198200            # It will have to be prepended by the caller.
    199             res = (res[0], None, int(res[1]),
    200                 None, None, None)
     201            res = (res[0], None, int(res[1]), None, None, None)
    201202            self[oid] = res
    202203            return res
     
    365366            getdescr = self._type_cache.getdescr
    366367            coltypes = self._src.listinfo()
    367             self.description = [
    368                 typ[1:2] + getdescr(typ[2]) for typ in coltypes]
     368            self.description = [CursorDescription(
     369                typ[1], *getdescr(typ[2])) for typ in coltypes]
    369370            self.lastrowid = None
    370371        else:
     
    438439        """Not supported."""
    439440        pass  # unsupported, but silently passed
     441
     442
     443CursorDescription = namedtuple('CursorDescription',
     444    ['name', 'type_code', 'display_size', 'internal_size',
     445     'precision', 'scale', 'null_ok'])
    440446
    441447
  • trunk/module/tests/test_dbapi20.py

    r679 r681  
    7575        self.assertTrue(ret is cur, 'execute() should return cursor')
    7676        self.assertEqual(cur.fetchone(), {'a': 1, 'b': 2})
     77
     78    def test_description_named(self):
     79        con = self._connect()
     80        cur = con.cursor()
     81        cur.execute("select 123456789::int8 as col")
     82        desc = cur.description
     83        self.assertIsInstance(desc, list)
     84        self.assertEqual(len(desc), 1)
     85        desc = desc[0]
     86        self.assertIsInstance(desc, tuple)
     87        self.assertEqual(desc.name, 'col')
     88        self.assertEqual(desc.type_code, 'int8')
     89        self.assertIsNone(desc.display_size)
     90        self.assertIsInstance(desc.internal_size, int)
     91        self.assertEqual(desc.internal_size, 8)
     92        self.assertIsNone(desc.precision)
     93        self.assertIsNone(desc.scale)
     94        self.assertIsNone(desc.null_ok)
    7795
    7896    def test_cursor_iteration(self):
Note: See TracChangeset for help on using the changeset viewer.