Changeset 786 for trunk/pgdb.py


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

Implement the cursor description as a property

It's better to create the description only on demand.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pgdb.py

    r784 r786  
    235235        self._src = self._cnx.source()
    236236        # the official attribute for describing the result columns
    237         self.description = None
    238         # unofficial attributes for convenience and performance
    239         self.colnames = self.coltypes = None
     237        self._description = None
    240238        if self.row_factory is Cursor.row_factory:
    241239            # the row factory needs to be determined dynamically
     
    347345            None, size, precision, scale, None)
    348346
     347    @property
     348    def description(self):
     349        """Read-only attribute describing the result columns."""
     350        descr = self._description
     351        if self._description is True:
     352            make = self._make_description
     353            descr = [make(info) for info in self._src.listinfo()]
     354            self._description = descr
     355        return descr
     356
     357    @property
     358    def colnames(self):
     359        """Unofficial convenience method for getting the column names."""
     360        return [d[0] for d in self.description]
     361
     362    @property
     363    def coltypes(self):
     364        """Unofficial convenience method for getting the column types."""
     365        return [d[1] for d in self.description]
     366
    349367    def close(self):
    350368        """Close the cursor object."""
    351369        self._src.close()
    352         self.description = None
    353         self.colnames = self.coltypes = None
     370        self._description = None
    354371        self.rowcount = -1
    355372        self.lastrowid = None
     
    375392            # don't do anything without parameters
    376393            return
    377         self.description = None
    378         self.colnames = self.coltypes = None
     394        self._description = None
    379395        self.rowcount = -1
    380396        # first try to execute all queries
     
    408424        # then initialize result raw count and description
    409425        if self._src.resulttype == RESULT_DQL:
     426            self._description = True  # fetch on demand
    410427            self.rowcount = self._src.ntuples
    411             description = self._make_description
    412             description = [description(info) for info in self._src.listinfo()]
    413             self.colnames = [d[0] for d in description]
    414             self.coltypes = [d[1] for d in description]
    415             self.description = description
    416428            self.lastrowid = None
    417429            if self.build_row_factory:
Note: See TracChangeset for help on using the changeset viewer.