I'm trying to do something in a DTML-Method (in ancient Zope 2.6.2) that seems like it should be really simple, but I can't get it right.
I want to see if a value (defined via url-arg) is in a list which is generated from an RDBMS query (field type=integer).
The pseudo-code would be something like:
if foo in (select foo_col from table where x=y) then z='t' else z='f'
I tried putting the simple SELECT query into a zSql and then calling it like:
<dtml-call "REQUEST.set('foo_list',IfShowNextDoor())"> <dtml-if expr="foo in foo_list"> <dtml-call "REQUEST.set('z','t')"> <dtml-else> <dtml-call "REQUEST.set('z','f')"> </dtml-if>
But that doesn't work.
if foo in (select foo_col from table where x=y) then z='t' else z='f'
I tried putting the simple SELECT query into a zSql and then calling it like:
<dtml-call "REQUEST.set('foo_list',IfShowNextDoor())">
<dtml-if expr="foo in foo_list"> <dtml-call "REQUEST.set('z','t')"> <dtml-else> <dtml-call "REQUEST.set('z','f')"> </dtml-if>
How about checking the database table for foo rather than the result set:
ZSQL method name=select argument=foo select foo_col from table where foo_col =<dtml-sqlvar foo type="string">
then do: <dtml-in select> <dtml-call "REQUEST.set('z','t')">
<dtml-else> <dtml-call "REQUEST.set('z','f')"> </dtml-in>
If the variable is in table then dtml-in returns a result set and the <dtml-call "REQUEST.set('z','t')"> is called, if not the dtml-else is called.
HTH Regards Garry
Bill Seitz wrote at 2007-11-28 13:20 -0500:
I want to see if a value (defined via url-arg) is in a list which is generated from an RDBMS query (field type=integer).
The pseudo-code would be something like:
if foo in (select foo_col from table where x=y) then z='t' else z='f'
I tried putting the simple SELECT query into a zSql and then calling it like:
<dtml-call "REQUEST.set('foo_list',IfShowNextDoor())">
<dtml-if expr="foo in foo_list"> <dtml-call "REQUEST.set('z','t')"> <dtml-else> <dtml-call "REQUEST.set('z','f')"> </dtml-if>
But that doesn't work.
Off topic note: this is quite nasty and difficult to read code....
More helpful note:
The result of a Z SQL Method call behaves like a sequence of rows.
These rows are not strings; therefore "some_string == row" will fail (always return 'False') and correspondingly "some_string in sqlresult".
The rows (they are in fact 'Record' instances) allow access to the fields either through a subscription ("row[0]", "row[1]" ...) or an attribute access ("row.field1", "row.field2", ...) syntax.
Therefore, to get the list consisting of the first field of all rows, you may use
[row[0] for row in IfShowNextDoor()]
Your code above can then be replaced with:
<dtml-call "REQUEST.set( 'z', foo in [row[0] for row in IfShowNextDoor()] and 't' or 'f' )">
which is already a lot more readable than your code. If possible, you should get rid of the "REQUEST.set" ("dtml-let" is more readable).
And, of course, the advice from someone else to let the datebase perform the check for you it excellent: why do something inefficient and clumsy yourself when someone else (database) can do it more efficiently and more direct?
OK, here's a next rev that's close.
So I started out saying I have a url-arg value. Let's call that 'key'.
And note that foo_col is a boolean field.
So now zSql IfShowNextDoor contains SELECT foo_col FROM table where id=<dtml-sqlvar key type=int>
And then my DTML uses <dtml-call "REQUEST.set('z',IfShowNextDoor()[0][0])">
(I avoid dtml-let because when you need to do it multiple times, the nesting of the lets gets ugly.)
This works (setting value as 0 or 1).
(thanks to all for earlier replies: I tried posting this update earlier but it didn't go through somehow)
On Nov 28, 2007 1:20 PM, Bill Seitz fluxent@gmail.com wrote:
I'm trying to do something in a DTML-Method (in ancient Zope 2.6.2) that seems like it should be really simple, but I can't get it right.
I want to see if a value (defined via url-arg) is in a list which is generated from an RDBMS query (field type=integer).
The pseudo-code would be something like:
if foo in (select foo_col from table where x=y) then z='t' else z='f'
I tried putting the simple SELECT query into a zSql and then calling it like:
<dtml-call "REQUEST.set('foo_list',IfShowNextDoor())">
<dtml-if expr="foo in foo_list"> <dtml-call "REQUEST.set('z','t')"> <dtml-else> <dtml-call "REQUEST.set('z','f')"> </dtml-if>
But that doesn't work.