Thread: trigger : emulate "instead of" with before ?

trigger : emulate "instead of" with before ?

From
"Albrecht Berger"
Date:
Hello,
I need a trigger which is updating a delete flag of a row instead of
deleting it physically.

How do I implement a trigger which doesn't execute a delete without raising
an exception
in plsql ?

CREATE TRIGGER trigger1 BEFORE DELETE ON table1
FOR EACH ROW EXECUTE PROCEDURE setDeleteFlag ();

If I use
RAISE EXCEPTION "..."
the delete statement isn't executed, but I think the transaction is rolled
back too, am I right ?

So what is the best way to implement something (Oracle like) like that :
CREATE TRIGGER trigger1 INSTEAD OF DELETE ON table1
FOR EACH ROW EXECUTE PROCEDURE setDeleteFlag ();

thx



Re: trigger : emulate "instead of" with before ?

From
"Matthew Nuzum"
Date:
I'm not a postgres expert, and I certainly don't know the details of your
situation, but it seems to me you may want to use a rule instead of a
trigger.

Then, you can intercept the delete query and simply re-write it to be an
update query that sets your deleted flag.

--
Matthew Nuzum
www.bearfruit.org
cobalt@bearfruit.org
> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-
> owner@postgresql.org] On Behalf Of Albrecht Berger
> Sent: Friday, June 13, 2003 7:51 AM
> To: pgsql
> Subject: [SQL] trigger : emulate "instead of" with before ?
>
> Hello,
> I need a trigger which is updating a delete flag of a row instead of
> deleting it physically.
>
> How do I implement a trigger which doesn't execute a delete without
> raising
> an exception
> in plsql ?
>
> CREATE TRIGGER trigger1 BEFORE DELETE ON table1
> FOR EACH ROW EXECUTE PROCEDURE setDeleteFlag ();
>
> If I use
> RAISE EXCEPTION "..."
> the delete statement isn't executed, but I think the transaction is rolled
> back too, am I right ?
>
> So what is the best way to implement something (Oracle like) like that :
> CREATE TRIGGER trigger1 INSTEAD OF DELETE ON table1
> FOR EACH ROW EXECUTE PROCEDURE setDeleteFlag ();
>
> thx
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faqs/FAQ.html



Re: trigger : emulate "instead of" with before ?

From
Jeff Eckermann
Date:
I believe that if you return NULL from the trigger
function, the delete will not actually be done.  You
could set the value of the delete flag by reference to
OLD.fieldname.

I have not actually done this myself, so caveat
emptor.

--- Albrecht Berger <berger1517@gmx.ch> wrote:
> Hello,
> I need a trigger which is updating a delete flag of
> a row instead of
> deleting it physically.
> 
> How do I implement a trigger which doesn't execute a
> delete without raising
> an exception
> in plsql ?
> 
> CREATE TRIGGER trigger1 BEFORE DELETE ON table1
> FOR EACH ROW EXECUTE PROCEDURE setDeleteFlag ();
> 
> If I use
> RAISE EXCEPTION "..."
> the delete statement isn't executed, but I think the
> transaction is rolled
> back too, am I right ?
> 
> So what is the best way to implement something
> (Oracle like) like that :
> CREATE TRIGGER trigger1 INSTEAD OF DELETE ON table1
> FOR EACH ROW EXECUTE PROCEDURE setDeleteFlag ();
> 
> thx
> 
> 
> ---------------------------(end of
> broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
> 
> http://www.postgresql.org/docs/faqs/FAQ.html


__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com


Re: trigger : emulate "instead of" with before ?

From
Tom Lane
Date:
"Albrecht Berger" <berger1517@gmx.ch> writes:
> I need a trigger which is updating a delete flag of a row instead of
> deleting it physically.

> How do I implement a trigger which doesn't execute a delete without raising
> an exception in plsql ?

If the BEFORE DELETE trigger returns NULL, the delete is skipped.
        regards, tom lane