Search Postgresql Archives

Can this pl/pgsql be simplified?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux