[Zope] ZSQL method attribute name question

Robert Carey carey@gsi.org
Mon, 02 Aug 1999 14:28:30 -0400


I would like to be able to write generic handlers for data returned by
ZSQL methods.  To do this I need to know how to get access to the
metadata for rows returned by ZSQL methods.

If I define a class and associate it with a ZSQL method using the
Advanced tab of that method's management page, what is the right way for
that class to learn the names of the columns returned by the query?

The following code works, but I was unable to find the __record_schema__
attribute in any Zope documentation and I am a little wary of using it
unless I know it is legitimate, particularly since its name begins with
two underscores.

If there is a better way to do this I would really like to know.  (Did I
miss something obvious?)

If I have a handler that contains:

  class handler:
    def munge(self):
      ret = ""
      for i in self.__record_schema__.keys():
        ret = ret + "<br>" + str(i) + " = " + str(getattr(self, i))
      return ret

and the handler is associated with a ZSQL method named get_phonebook
which simply runs "select * from phonebook"

and I have a table in the database which looks like:

    create table phonebook (
      first_name     varchar,
      last_name      varchar,
      address        varchar,
      phonenumber    varchar)

then if I execute this page of DTML

    <!--#var standard_html_header-->

    <!--#in get_phonebook-->
        <!--#var munge-->
        <br>
    <!--#/in-->

    <!--#var standard_html_footer-->

It shows me something like this:

    ADDRESS = London
    PHONENUMBER = 999-9999
    LAST_NAME = Thomas
    FIRST_NAME = David

    ADDRESS = Berkeley
    PHONENUMBER = 888-8888
    LAST_NAME = Herman
    FIRST_NAME = Tom

which is what I want - the names of the columns and the values.

One of the things that is not clear to me (and this may be a Pythonism,
not a Zopism) is why I am able to retrieve the values for the columns by
calling getattr(self, i) when, if I try to retrieve the object's
attributes by calling dir(self) or just dir(), I don't see any of these
attribute names.

Obviously, this example is fairly contrived.  I just wanted to show the
mechanism I was after.  What I really want to be able to do is call
several ZSQL methods from an external method and play around with
combinations of the results without having to know in advance exactly
what to expect back from the ZSQL methods.

Rob