Can this pl/pgsql be simplified? - Mailing list pgsql-general

From CSN
Subject Can this pl/pgsql be simplified?
Date
Msg-id 20051125201901.20388.qmail@web52902.mail.yahoo.com
Whole thread Raw
Responses Re: Can this pl/pgsql be simplified?  (Oliver Elphick <olly@lfix.co.uk>)
List 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/

pgsql-general by date:

Previous
From: John Taber
Date:
Subject: Re: pg_connect troubles on localhost
Next
From: "Thies C. Arntzen"
Date:
Subject: howto create dynamic table name in plpgsql function.