Author: cito
Date: Tue Jan 26 13:59:18 2016
New Revision: 787
Log:
Make the type cache of pgdb available to users
Modified:
trunk/docs/contents/pgdb/connection.rst
trunk/pgdb.py
trunk/tests/test_dbapi20.py
Modified: trunk/docs/contents/pgdb/connection.rst
==============================================================================
--- trunk/docs/contents/pgdb/connection.rst Tue Jan 26 13:16:29 2016
(r786)
+++ trunk/docs/contents/pgdb/connection.rst Tue Jan 26 13:59:18 2016
(r787)
@@ -74,5 +74,28 @@
The default cursor type used by the connection
If you want to use your own custom subclass of the :class:`Cursor` class
-with he connection, set this attribute to you custom cursor class. You will
+with he connection, set this attribute to your custom cursor class. You will
then get your custom cursor whenever you call :meth:`Connection.cursor`.
+
+.. versionadded:: 5.0
+
+.. attribute:: type_cache
+
+ A dictionary with type information on the PostgreSQL types
+
+You can request the dictionary either via type names or type OIDs.
+
+The values are named tuples containing the following fields:
+
+ - *oid* -- the OID of the type
+ - *name* -- the type's name
+ - *len* -- the internal size
+ - *type* -- ``'b'`` = base, ``'c'`` = composite, ...
+ - *category* -- ``'A'`` = Array, ``'B'`` = Boolean, ...
+ - *delim* -- delimiter to be used when parsing arrays
+ - *relid* -- the table OID for composite types
+
+For details, see the PostgreSQL documentation on `pg_type
+<http://www.postgresql.org/docs/current/static/catalog-pg-type.html>`_.
+
+.. versionadded:: 5.0
Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py Tue Jan 26 13:16:29 2016 (r786)
+++ trunk/pgdb.py Tue Jan 26 13:59:18 2016 (r787)
@@ -163,8 +163,8 @@
class TypeCache(dict):
"""Cache for database types.
- This cache maps type OIDs to TypeInfo tuples containing the name
- and other important info for the database type with the given OID.
+ This cache maps type OIDs and names to TypeInfo tuples containing
+ important information on the associated database type.
"""
def __init__(self, cnx):
@@ -182,7 +182,10 @@
else:
q += "typname = '%s'" % self._escape_string(key)
self._src.execute(q)
- res = list(self._src.fetch(1)[0])
+ res = self._src.fetch(1)
+ if not res:
+ raise KeyError('Type %s could not be found' % key)
+ res = list(res[0])
res[0] = int(res[0])
res[2] = int(res[2])
res[6] = int(res[6])
@@ -231,7 +234,7 @@
"""Create a cursor object for the database connection."""
self.connection = self._dbcnx = dbcnx
self._cnx = dbcnx._cnx
- self._type_cache = dbcnx._type_cache
+ self.type_cache = dbcnx.type_cache
self._src = self._cnx.source()
# the official attribute for describing the result columns
self._description = None
@@ -328,7 +331,7 @@
def _make_description(self, info):
"""Make the description tuple for the given field info."""
name, typ, size, mod = info[1:]
- type_info = self._type_cache[typ]
+ type_info = self.type_cache[typ]
type_code = type_info.name
if mod > 0:
mod -= 4
@@ -465,7 +468,7 @@
raise
except Error as err:
raise _db_error(str(err))
- typecast = self._type_cache.typecast
+ typecast = self.type_cache.typecast
return [self.row_factory([typecast(typ, value)
for typ, value in zip(self.coltypes, row)]) for row in result]
@@ -815,7 +818,7 @@
"""Create a database connection object."""
self._cnx = cnx # connection
self._tnx = False # transaction state
- self._type_cache = TypeCache(cnx)
+ self.type_cache = TypeCache(cnx)
self.cursor_type = Cursor
try:
self._cnx.source()
Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Tue Jan 26 13:16:29 2016 (r786)
+++ trunk/tests/test_dbapi20.py Tue Jan 26 13:59:18 2016 (r787)
@@ -293,13 +293,13 @@
def test_type_cache(self):
con = self._connect()
cur = con.cursor()
- type_info = cur._type_cache['numeric']
+ type_info = cur.type_cache['numeric']
self.assertEqual(type_info.oid, 1700)
self.assertEqual(type_info.name, 'numeric')
self.assertEqual(type_info.type, 'b') # base
self.assertEqual(type_info.category, 'N') # numeric
self.assertEqual(type_info.delim, ',')
- self.assertIs(cur._type_cache[1700], type_info)
+ self.assertIs(cur.type_cache[1700], type_info)
def test_cursor_iteration(self):
con = self._connect()
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql