Skip site navigation (1) Skip section navigation (2)

PyGreSQL, suggestion for DB wrapper class

From: "Christoph Zwerschke" <zwerschke(at)zuv(dot)uni-heidelberg(dot)de>
To: <pgsql-patches(at)postgresql(dot)org>
Cc: <darcy(at)druid(dot)net>
Subject: PyGreSQL, suggestion for DB wrapper class
Date: 2002-12-09 21:33:29
Message-ID: 002e01c29fca$9ddde390$271f11ac@zwerschke (view raw or flat)
Thread:
Lists: pgsql-patches
Here is another suggested patch for PyGreSQL:


There is an ugly little problem with the DB wrapper class.

In pg.py the attributes of DB are defined as being the same as
the attributes of the corresponding pgobject "db", using the following

--------  piece of Python code in pg.py ----------
# Create convience methods ...
for e in ( 'query', ... , 'error', 'status', ...):
    if not hasattr(self,e) and hasattr(self.db,e):
        exec 'self.%s = self.db.%s' % ( e, e )
----------------------------------------------------
    
The problem is that the attributes of db (which are read only)
are not static (they are actually function calls to PostgreSQL),
especially "status" and "error", but those attributes are copied
and this is done only once when initializing the DB object.

So, in effect, only the attribute "db.error" of a DB instance
will be updated, but not the attribute "error". Same with "status".

The following Python session demonstrates the problem:

>>> from pg import DB
>>> db=DB('template1')
>>> db.query("this is an invalid query")
_pg.error: ERROR:  parser: parse error at or near "this"
>>> db.error
''
>>> db.db.error
'ERROR:  parser: parse error at or near "this"\n'


Suggested solution:

Don't copy the (read only) attributes of the pgobject to the
DB object, but only the methods, and all of them, like this:

--------------- change in pg.py ------------------
# Create convience methods, in a way that is still overridable.
for e in self.db.__methods__:
 setattr(self, e, getattr(self.db, e))
----------------------------------------------------

Furthermore, make an addition to the documentation of the
DB wrapper class (i.e. in pygresql-pg-db.html):
After the sentence "All pgobject methods are included in this class also."
add the following sentence "The pgobject read-only attributes can be
accessed py adding the prefix 'db.' to them."


Christoph Zwerschke
Zentrale Univerwaltung Heidelberg


Responses

pgsql-patches by date

Next:From: Tom LaneDate: 2002-12-09 22:13:02
Subject: Re: psql's \d commands --- end of the line for
Previous:From: Hannu KrosingDate: 2002-12-09 21:25:12
Subject: Re: psql's \d commands --- end of the line for

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group