Re: [Zope] silly DTML question: "in" test

2007-12-02 Thread Dieter Maurer
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:
>
>
>
>   
>
>   
>
>
>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:



  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?



-- 
Dieter
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] silly DTML question: "in" test

2007-11-28 Thread Garry Saddington

> 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:
>
> 
> 
>   
> 
>   
> 
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 =

then do:


> 
>   
> 

If the variable is in table then dtml-in returns a result set and the 
 is called, if not the dtml-else is called.

HTH
Regards
Garry
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )