[database] Merge queries

Larry Garfield larry at garfieldtech.com
Sat Jul 19 20:42:41 UTC 2008


On Saturday 19 July 2008 2:21:30 am David Timothy Strauss wrote:
> "Since the return value from db_update() is the number of rows *affected*,
> not *changed*, which means our usual update-or-insert routine doesn't
> work."
>
> I assume you mean it returns the number of rows *changed* rather than
> *matching* rows.

Er.  Yes.  Yes I do.

> The way I solve the issue is by doing the operations:
> (1) UPDATE
> (2) If updated changed zero rows, do a SELECT on the criteria
> (3) If the SELECT returned zero rows, do an INSERT
>
> This would all be in a transaction.

Three queries?  Yipes.  I guess that could work as a generic version, although 
I'd prefer to see something more Postgres-optimized in the Postgres driver if 
at all possible.  Remember we're talking about the code that is the bulk of 
variable_set() and potentially cache_set(), so it will frequently be called 
multiple times in rapid succession.

-- 
Larry Garfield
larry at garfieldtech.com


More information about the Database mailing list