[Zope] SQLFile??

Terry Hancock hancock@anansispaceworks.com
Sat, 29 Jun 2002 07:41:55 -0700

You've probably already heard this by now, but there's
an API way to create a ZSQLMethod.  I don't know why,
but the Zope book calls this "ZSQLMethod", while it
appears to be called SQL in the actual Zope Source.

Here's an example snipped out of my current project.
I know the "import SQL as ZSQLMethod" is kind of weird
-- I did that because I had already typed ZSQLMethod
a whole bunch of times, and wasn't sure which was
the "right" name.  This code works, though.

dbc = 'NaryaDB'
from Products.ZSQLMethods.SQL import SQL as ZSQLMethod

getPostByNum = ZSQLMethod('addThreadStatus', 'Get a particular post',
        SELECT  post.username,  post.post_time, post.postnum,
                post.body,      user.tagline,
                user.avatar,    user.nrecent, user.nposts,
                user.signature, user.www
        FROM    post, user
        WHERE   post.postnum = <dtml-sqlvar number type="int">
        """ )


<method-object> = ZSQLMethod(<method-id>, <method-title>,
		<method-arguments (no commas!)>,
		<method-ZSQL code>)

Note that this creates the object -- I have this assigned
as a class method in a Zope Product.  I.e. it is assigned
here in a module "ZSQLRead", then I assign from the module
to the class method:

import ZSQLRead

class  my_folderish_product(Folder):
	# ... lots of other stuff ...

	getPostByNum = ZSQLRead.getPostByNum'

	# ... lots more stuff ...

You can call this from the web or from other Zope objects,
just as if it were contained in the folder, but it doesn't
appear in the mgmt interface and is not alterable. It also
will automatically be updated when the product is changed,
and it will exist for any number of actual instances of
the product.

If you want to see it in the management interface,
you'll want to use "manage_addZSQLMethod(...)" instead.
(But I don't explain that because I didn't do it that
way  -- it is mentioned in the Zope book as something


analogous to the above).

The book calls this the "ZSQLMethod" class, so I had assumed
the constructor would be invoked by "ZSQLMethod()", but I
couldn't get that to work without the weird import ... as
(This is Zope 2.5.1).

Sorry if this is redundant, but I wanted to reply
before I lost this message and forgot about it. :-)

Terry Hancock
Anansi Spaceworks                 
P.O. Box 60583                     
Pasadena, CA 91116-6583