[Zope] Separating assignments in Z SQL Updates

Thomas B. Passin tpassin@mitretek.org
Mon, 14 May 2001 13:46:41 -0400

[Bruce Eckel]

> I'm trying to write a ZSQL method that will do a general
> update to a record. I started out attempting to do what I
> thought was the 'normal' thing:
> update Table set name1='value1', name2='value2',
> name3='value3' [...] where id='idvalue';
> I wanted to use dtml to automatically build a statement
> that would only assign values if new values existed. The
> problem with this is the commas: you have to have commas
> between the assignments, but you can have a comma after the
> last assignment. And I couldn't figure out how to
> conditionally insert the commas.

Just for fun I came up with this approach.  The key is to have a dictionary
that returns a comma when you pass it a "1".  It's a lot cleaner than all
that conditional logic.  This has been tested and works.  The example
displays the string "A,B,C".

<dtml-let yesno="{0:'',1:','}">
   <dtml-let L="['A','B','C']">
      <dtml-let num="_.len(L)-1">
         <dtml-in L>
            <dtml-var "_['sequence-item']+yesno[_['sequence-index']<num]">


Tom P