> 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

...

> 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".


> 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
This commit is contained in:
Bruce Momjian 2002-12-12 22:49:27 +00:00
parent 482ed836f7
commit d274105942
2 changed files with 16 additions and 15 deletions

View File

@ -1,4 +1,4 @@
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/Attic/pygresql.sgml,v 1.8 2002/09/23 23:20:38 tgl Exp $ --> <!-- $Header: /cvsroot/pgsql/doc/src/sgml/Attic/pygresql.sgml,v 1.9 2002/12/12 22:49:27 momjian Exp $ -->
<chapter id="pygresql"> <chapter id="pygresql">
<title><application>PyGreSQL</application> - <application>Python</application> Interface</title> <title><application>PyGreSQL</application> - <application>Python</application> Interface</title>
@ -2413,12 +2413,17 @@ loimport(<parameter>filename</parameter>)
<title>Database Wrapper Class: <classname>DB</classname></title> <title>Database Wrapper Class: <classname>DB</classname></title>
<para> <para>
<classname>pg</classname> module contains a class called The <literal>pg</literal> module contains a class called
<classname>DB</classname>. All <classname>pgobject</classname> <classname>DB</classname> wrapping a <classname>pgobject</classname>.
methods are included in this class also. A number of additional This <classname>pgobject</classname> can be addressed as a
<classname>DB</classname> class methods are described below. The <classname>DB</classname> class member named <varname>db</varname>
preferred way to use this module is as follows (See description of to get access to the read-only attributes of the corresponding connection
the initialization method below.): (e.g. <varname>db.error</varname>). All <classname>pgobject</classname>
methods (e.g. <function>query()</function>) are directly included as members
in the class <classname>DB</classname> also. A number of additional
higher level <classname>DB</classname> class methods are described below.
The preferred way to use this module is as follows (see description of the
initialization method below):
<programlisting> <programlisting>
import pg import pg

View File

@ -48,14 +48,10 @@ class DB:
def __init__(self, *args, **kw): def __init__(self, *args, **kw):
self.db = apply(connect, args, kw) self.db = apply(connect, args, kw)
# Create convience methods, in a way that is still overridable. # Create convience methods, in a way that is still overridable
for e in ( 'query', 'reset', 'close', 'getnotify', 'inserttable', # (members are not copied because they are actually functions)
'putline', 'getline', 'endcopy', for e in self.db.__methods__:
'host', 'port', 'db', 'options', setattr(self, e, getattr(self.db, e))
'tty', 'error', 'status', 'user',
'locreate', 'getlo', 'loimport' ):
if not hasattr(self,e) and hasattr(self.db,e):
exec 'self.%s = self.db.%s' % ( e, e )
self.__attnames__ = {} self.__attnames__ = {}
self.__pkeys__ = {} self.__pkeys__ = {}