Can this pl/pgsql be simplified?

From: CSN <cool_screen_name90001(at)yahoo(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Can this pl/pgsql be simplified?
Date: 2005-11-25 20:19:01
Message-ID: 20051125201901.20388.qmail@web52902.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a trigger function that simply updates item counts when the items table changes (member_id
or active changes). I'm curious if this bit of the code can be simplified? :)

thanks
csn

ELSIF TG_OP = 'UPDATE' THEN

IF (OLD.member_id is NULL and NEW.member_id is not null) or (OLD.member_id is not NULL and
NEW.member_id is null) or OLD.member_id <> NEW.member_id THEN
IF OLD.member_id is not null then
IF OLD.active is true then
update members set
items_submitted=items_submitted-1,
items_approved=items_approved-1
where id=OLD.member_id;
ELSE
update members set
items_submitted=items_submitted-1
where id=OLD.member_id;
END IF;
END IF;

IF NEW.member_id is not null then
IF NEW.active is true then
update members set
items_submitted=items_submitted+1,
items_approved=items_approved+1
where id=NEW.member_id;
ELSE
update members set
items_submitted=items_submitted+1
where id=NEW.member_id;
END IF;
END IF;
ELSIF OLD.active is false and NEW.active is true then
update members set
items_approved=items_approved+1
where id=NEW.member_id;
ELSIF OLD.active is true and NEW.active is false then
update members set
items_approved=items_approved-1
where id=NEW.member_id;
END IF;


__________________________________
Yahoo! Music Unlimited
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thies C. Arntzen 2005-11-25 20:23:33 howto create dynamic table name in plpgsql function.
Previous Message John Taber 2005-11-25 19:57:59 Re: pg_connect troubles on localhost