Changeset 787 for trunk


Ignore:
Timestamp:
Jan 26, 2016, 1:59:18 PM (4 years ago)
Author:
cito
Message:

Make the type cache of pgdb available to users

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/pgdb/connection.rst

    r710 r787  
    7575
    7676If you want to use your own custom subclass of the :class:`Cursor` class
    77 with he connection, set this attribute to you custom cursor class. You will
     77with he connection, set this attribute to your custom cursor class. You will
    7878then get your custom cursor whenever you call :meth:`Connection.cursor`.
     79
     80.. versionadded:: 5.0
     81
     82.. attribute:: type_cache
     83
     84    A dictionary with type information on the PostgreSQL types
     85
     86You can request the dictionary either via type names or type OIDs.
     87
     88The values are named tuples containing the following fields:
     89
     90        - *oid* -- the OID of the type
     91        - *name*  -- the type's name
     92        - *len*  -- the internal size
     93        - *type*  -- ``'b'`` = base, ``'c'`` = composite, ...
     94        - *category*  -- ``'A'`` = Array, ``'B'`` = Boolean, ...
     95        - *delim*  -- delimiter to be used when parsing arrays
     96        - *relid*  -- the table OID for composite types
     97
     98For details, see the PostgreSQL documentation on `pg_type
     99<http://www.postgresql.org/docs/current/static/catalog-pg-type.html>`_.
     100
     101.. versionadded:: 5.0
  • trunk/pgdb.py

    r786 r787  
    164164    """Cache for database types.
    165165
    166     This cache maps type OIDs to TypeInfo tuples containing the name
    167     and other important info for the database type with the given OID.
     166    This cache maps type OIDs and names to TypeInfo tuples containing
     167    important information on the associated database type.
    168168    """
    169169
     
    183183            q += "typname = '%s'" % self._escape_string(key)
    184184        self._src.execute(q)
    185         res = list(self._src.fetch(1)[0])
     185        res = self._src.fetch(1)
     186        if not res:
     187            raise KeyError('Type %s could not be found' % key)
     188        res = list(res[0])
    186189        res[0] = int(res[0])
    187190        res[2] = int(res[2])
     
    232235        self.connection = self._dbcnx = dbcnx
    233236        self._cnx = dbcnx._cnx
    234         self._type_cache = dbcnx._type_cache
     237        self.type_cache = dbcnx.type_cache
    235238        self._src = self._cnx.source()
    236239        # the official attribute for describing the result columns
     
    329332        """Make the description tuple for the given field info."""
    330333        name, typ, size, mod = info[1:]
    331         type_info = self._type_cache[typ]
     334        type_info = self.type_cache[typ]
    332335        type_code = type_info.name
    333336        if mod > 0:
     
    466469        except Error as err:
    467470            raise _db_error(str(err))
    468         typecast = self._type_cache.typecast
     471        typecast = self.type_cache.typecast
    469472        return [self.row_factory([typecast(typ, value)
    470473            for typ, value in zip(self.coltypes, row)]) for row in result]
     
    816819        self._cnx = cnx  # connection
    817820        self._tnx = False  # transaction state
    818         self._type_cache = TypeCache(cnx)
     821        self.type_cache = TypeCache(cnx)
    819822        self.cursor_type = Cursor
    820823        try:
  • trunk/tests/test_dbapi20.py

    r784 r787  
    294294        con = self._connect()
    295295        cur = con.cursor()
    296         type_info = cur._type_cache['numeric']
     296        type_info = cur.type_cache['numeric']
    297297        self.assertEqual(type_info.oid, 1700)
    298298        self.assertEqual(type_info.name, 'numeric')
     
    300300        self.assertEqual(type_info.category, 'N')  # numeric
    301301        self.assertEqual(type_info.delim, ',')
    302         self.assertIs(cur._type_cache[1700], type_info)
     302        self.assertIs(cur.type_cache[1700], type_info)
    303303
    304304    def test_cursor_iteration(self):
Note: See TracChangeset for help on using the changeset viewer.