[Zope] Separating assignments in Z SQL Updates

Bruce Eckel Bruce@EckelObjects.com
Mon, 14 May 2001 09:55:31 -0700


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.

After fooling around and hunting for awhile, the best I
could come up with is a separate 'update' statement inside
an 'if', for each field. So it looks like this:

<dtml-if due_date>
update Invoices set due_date = <dtml-sqlvar due_date
type="string">
where <dtml-sqltest invoice_id op=eq type=string><dtml-var
sql_delimiter>
</dtml-if>
<dtml-if purchase_order_id>
update Invoices set  purchase_order_id = <dtml-sqlvar
purchase_order_id type="string">
where <dtml-sqltest invoice_id op=eq type=string><dtml-var
sql_delimiter>
</dtml-if>
<dtml-if ship_name>
update Invoices set  ship_name = <dtml-sqlvar ship_name
type="string">
where <dtml-sqltest invoice_id op=eq type=string><dtml-var
sql_delimiter>
</dtml-if>

This works, but it seems awfully awkward to me. Is there a
way to insert commas instead, or a better solution in
general for this problem?

Thanks.
Bruce