L3 Info : SGBD
 
◃  Ch. 11 PL/pgSQL  ▹
 

Combinaison de Triggers (1/2)

  • Code :
    CREATE TRIGGER trigOL_before
      BEFORE INSERT OR UPDATE ON outlaw FOR EACH ROW
      EXECUTE PROCEDURE trigOutlaw_5();
    
    CREATE TRIGGER trigOL_after
      AFTER INSERT OR UPDATE ON outlaw FOR EACH ROW
      EXECUTE PROCEDURE trigOutlaw_5();
    
    CREATE TRIGGER trigOL_after_all
      AFTER INSERT OR UPDATE ON outlaw FOR EACH STATEMENT
      EXECUTE PROCEDURE trigOutlaw_5();
    
    CREATE OR REPLACE FUNCTION trigOutlaw_5() RETURNS trigger AS $$
    DECLARE
    	nbol int;
    BEGIN
    	IF (TG_WHEN = 'BEFORE') THEN
    		IF (TG_OP = 'INSERT') THEN
    			RAISE NOTICE 'Insertion en cours : TG_OP=%', TG_OP;
    			SELECT COUNT(*) INTO nbol FROM outlaw;
    			RAISE NOTICE '% outlaws dans la table avant les insertions', nbol;
    		ELSE	
    			RAISE NOTICE 'Mis-à-jour en cours : TG_OP=%', TG_OP;
    			RAISE NOTICE 'Ancienne Valeur de l''enregistrement : %', OLD;	
    		END IF;
    	ELSE -- TG_WHEN = AFTER
    		IF (TG_OP = 'UPDATE' AND TG_LEVEL = 'ROW') THEN
    			RAISE NOTICE 'Nouvelle Valeur de l''enregistrement : %', NEW;
    		END IF;
    		IF (TG_OP = 'INSERT' AND TG_LEVEL = 'STATEMENT') THEN
    			SELECT COUNT(*) INTO nbol FROM outlaw;
    			RAISE NOTICE '% outlaws dans la table après les insertions', nbol;
    		END IF;
    	END IF;
    	RETURN NEW;
    END $$ LANGUAGE plpgsql;