Author: cito
Date: Tue Jan 26 13:16:29 2016
New Revision: 786
Log:
Implement the cursor description as a property
It's better to create the description only on demand.
Modified:
trunk/docs/contents/pgdb/cursor.rst
trunk/pgdb.py
Modified: trunk/docs/contents/pgdb/cursor.rst
==============================================================================
--- trunk/docs/contents/pgdb/cursor.rst Tue Jan 26 12:50:41 2016 (r785)
+++ trunk/docs/contents/pgdb/cursor.rst Tue Jan 26 13:16:29 2016 (r786)
@@ -36,8 +36,8 @@
- *scale*
- *null_ok*
- Note that *display_size*, *precision*, *scale* and *null_ok*
- are not implemented.
+ The values for *precision* and *scale* are only set for numeric types.
+ The values for *display_size* and *null_ok* are always ``None``.
This attribute will be ``None`` for operations that do not return rows
or if the cursor has not had an operation invoked via the
Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py Tue Jan 26 12:50:41 2016 (r785)
+++ trunk/pgdb.py Tue Jan 26 13:16:29 2016 (r786)
@@ -234,9 +234,7 @@
self._type_cache = dbcnx._type_cache
self._src = self._cnx.source()
# the official attribute for describing the result columns
- self.description = None
- # unofficial attributes for convenience and performance
- self.colnames = self.coltypes = None
+ self._description = None
if self.row_factory is Cursor.row_factory:
# the row factory needs to be determined dynamically
self.row_factory = None
@@ -346,11 +344,30 @@
return CursorDescription(name, type_code,
None, size, precision, scale, None)
+ @property
+ def description(self):
+ """Read-only attribute describing the result columns."""
+ descr = self._description
+ if self._description is True:
+ make = self._make_description
+ descr = [make(info) for info in self._src.listinfo()]
+ self._description = descr
+ return descr
+
+ @property
+ def colnames(self):
+ """Unofficial convenience method for getting the column names."""
+ return [d[0] for d in self.description]
+
+ @property
+ def coltypes(self):
+ """Unofficial convenience method for getting the column types."""
+ return [d[1] for d in self.description]
+
def close(self):
"""Close the cursor object."""
self._src.close()
- self.description = None
- self.colnames = self.coltypes = None
+ self._description = None
self.rowcount = -1
self.lastrowid = None
@@ -374,8 +391,7 @@
if not seq_of_parameters:
# don't do anything without parameters
return
- self.description = None
- self.colnames = self.coltypes = None
+ self._description = None
self.rowcount = -1
# first try to execute all queries
rowcount = 0
@@ -407,12 +423,8 @@
raise _op_error("internal error in '%s': %s" % (sql, err))
# then initialize result raw count and description
if self._src.resulttype == RESULT_DQL:
+ self._description = True # fetch on demand
self.rowcount = self._src.ntuples
- description = self._make_description
- description = [description(info) for info in self._src.listinfo()]
- self.colnames = [d[0] for d in description]
- self.coltypes = [d[1] for d in description]
- self.description = description
self.lastrowid = None
if self.build_row_factory:
self.row_factory = self.build_row_factory()
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql