[Zope-DB] dtml-let variables in sql queries

Andreas Tille tillea at rki.de
Thu Nov 23 10:06:07 EST 2006


I have defined the following table

    nameid   int,
    name     text,
    fake     int

INSERT INTO names values ( 1, 'Alfons',    0 );
INSERT INTO names values ( 2, 'Alberto',   0 );
INSERT INTO names values ( 3, 'Adam',      0 );
INSERT INTO names values ( 4, 'Anibal',    0 );
INSERT INTO names values ( 5, 'Bert',      0 );
INSERT INTO names values ( 6, 'Caesar',    0 );
INSERT INTO names values ( 7, 'Dagobert',  1 );
INSERT INTO names values ( 8, 'Kuno',      1 );

GRANT SELECT ON names TO zope ;

The following SQL-Method should extract all names where
fake = 0 except if there are further parameters like
name or nameid that should restrict the result set if they
are given:

<params>name nameid

WHERE fake = 0
<dtml-if name>
   AND <dtml-sqltest name op=like type=nb>
<dtml-if nameid>
   AND <dtml-sqltest nameid op=eq type=int>

This works in the ZMI test tab as expected.

The original problem is that I want to design a Form where
you can specify only first letters like 'Al' and the query
looks for "name like 'Al%'".  I tried to do this using:

<dtml-var standard_html_header>

<dtml-if "REQUEST.form.has_key('nameid')">
     <dtml-let nameid="REQUEST.form['nameid']">
        Selected nameid= <dtml-var nameid><br />
        <dtml-var print_cases>

   <dtml-if "REQUEST.form.has_key('namepart')">
     <dtml-let namepart="REQUEST.form['namepart']">
         Namepart = <dtml-var namepart><br />
         <dtml-let name="namepart + '%'">
           Seek for name = '<dtml-var name>'.
           <dtml-var print_cases>
     <form action="index_html" name="test" method="POST">
       <input name="namepart" type="text" size="10" />

<dtml-var standard_html_footer>

where namepart is constructed as name+'%' to enable the like
query.  Unfortunately
          <dtml-let namepart="namepart + '%'">
does not seem to work transparently in the SQL-Method, because
this does not work and just prints every single name.  If somebody
wants to try this code here is the print_cases method:

<dtml-in GetCases>
    <a href="<dtml-var URL0>?nameid=<dtml-var nameid>"><dtml-var name></a>

Any idea why the variable namepart is not known in the SQL-Method
while nameid is and works perfectly to select one name from the list?

Many thanks



More information about the Zope-DB mailing list