[Zope] ZSQLMethodFile?

Peter Sabaini sabaini@inode.at
Wed, 11 Jun 2003 16:04:52 +0200


Peter Bengtsson wrote:
> 
>>>> I usually just create ZSQL Methods as attributes of my (product) class:
>>>>
>>>> import Acquisition
>>>> from Products.ZSQLMethods.SQL import SQL
>>>>
>>>> class mySQLClass(Acquisition.Implicit):
>>>>     # We need Acquisition in order to find our db connection
>>>>
>>>>     # a database connection with this id must exist!
>>>>     dbconnection = "the_db_connection"
>>>>
>>>>     _sql_null = "select 1 as one"
>>>>     zsql_null = SQL('null',
>>>>                'A dumb zsql method for testing purposes',
>>>>                dbconnection,
>>>>                '',
>>>>                _sql_null,
>>>>                )
>>>>
>>>>
>>>>
>>>> An instance of "mySQLClass" then could call self.zsql_null just like 
>>>> a standard ZSQL Method
> 
> 
> I'm not sure I understand.
> Suppose my sql query string looks like this:
> 
> sqlstatement='''<params>admin_login_name
> admin_password</params>

Lose the <params />  part.

You specify the parameters to the ZSQL method when instantiating the 
ZSQL object.

The signature of the ZSQL __init__ looks like this:

     def __init__(self, id, title, connection_id, arguments, template):

self is self

id is the id of the zope object

title is the title string of the zope object

connection_id is the id of the database connection

arguments is a string specifying the keyword args, eg. 
"""admin_login_name:string admin_password:string"""

template is your sql statement incl. dtml


> SELECT
>  *
> FROM
>  service_provider
> WHERE
> <dtml-let admin_login_name="_.string.upper(admin_login_name)">
>  UPPER(admin_login_name) = <dtml-sqlvar admin_login_name type="string">
> </dtml-let>
>  AND
>  admin_password = <dtml-sqlvar admin_password type="string">'''
> 
> How do I then pass the keyword arguments?
> 
> 
>>>>
> 
>>
>>> If I do some os.listdir(...) on my files, I'll be able to hack-parse 
>>> the parameters and create the methods on the fly with parameters. 
>>> Added benefit is that I'll be able to use arguments instead of only 
>>> keyword arguments.
>>
>>
>>
>> I THINK you still have to pass keyword args (but I could be mistaken)
>>
> IF I wrap them are class methods, will it correctly look like this:
> 
> def Mysqlstuff(Aq...):
>     def zsql_method(self, name, age):
>         return SQL(..., ..., ...)(name=name, age=age)
> 
> Or do I need anything else?

This should about work, but it would be more efficient if you stored the 
ZSQL object somewhere.  Otherwise you'd have to reparse the dtml each 
time you call zsql_method() and won't be able to use zope caching.

hth,
peter.