dbTalk Databases Forums  

Using OLD on INSERT

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Using OLD on INSERT in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Paul Makepeace
 
Posts: n/a

Default Using OLD on INSERT - 01-22-2004 , 07:29 AM






I have a trigger that sets an expires column to
last_access+expiry::interval if expires IS NULL or if the expires value
isn't being set or changed.

IF NEW.expires IS NULL OR NEW.expires = OLD.expires THEN
NEW.expires = NEW.last_access+NEW.expiry:interval;
END IF;

The problem here is OLD doesn't exist on the first INSERT which throws
an error. It seems PL/pgSQL doesn't have C's short-circuit booleans.

a) Is there a way around this?
b) is there a 'right' way to determine if a column is being changed?

Paul (total PL/pgSQL newbie)

--
Paul Makepeace ................................ http://paulm.com/ecademy

"If I had new shoes, then he wouldn't sing Halleighluha."
-- http://paulm.com/toys/surrealism/

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


Reply With Quote
  #2  
Old   
Terry Lee Tucker
 
Posts: n/a

Default Re: Using OLD on INSERT - 01-22-2004 , 08:26 AM






We check the value of TG_OP as in:
IF TG_OP = ''UPDATE'' THEN
Code that address OLD and NEW here;
ELSE
Code that addresses only NEW here.
END IF;

On Thursday 22 January 2004 08:29 am, Paul Makepeace wrote:
Quote:
I have a trigger that sets an expires column to
last_access+expiry::interval if expires IS NULL or if the expires value
isn't being set or changed.

IF NEW.expires IS NULL OR NEW.expires = OLD.expires THEN
NEW.expires = NEW.last_access+NEW.expiry:interval;
END IF;

The problem here is OLD doesn't exist on the first INSERT which throws
an error. It seems PL/pgSQL doesn't have C's short-circuit booleans.

a) Is there a way around this?
b) is there a 'right' way to determine if a column is being changed?

Paul (total PL/pgSQL newbie)
--
Quote: 8
"Even now politicians use the phrase 'federal dollars' as a synonym for
'free money.' It's a dangerous tendency, for it leaves the states
accountable to Washington rather than to their own voters. This mix of
state and federal governments is not just economically suspect but
politically corrosive; it undermines the essence of real federalism:
the integrity of both state and federal governments. If we're serious
about states' rights, then we need to get serious about states'
responsibilities. The two go together. As with love and marriage, you
can't have one without the other."

--Paul Greenberg

Work: 1-336-372-6812
Cell: 1-336-363-4719
email: terry (AT) esc1 (DOT) com

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
  #3  
Old   
Terry Lee Tucker
 
Posts: n/a

Default Re: Using OLD on INSERT - 01-22-2004 , 08:47 AM



After looking at this again, I see that my answer doesn't help you. We use the
same trigger code on INSERT OR UPDATE. According to the online docs I have:
OLD
Data type RECORD; variable holding the old database row for UPDATE/DELETE
operations in ROW level triggers.

OLD is only available for UPDATE/DELETE. As for workarounds, I don't know of
any.

On Thursday 22 January 2004 09:26 am, Terry Lee Tucker wrote:
Quote:
We check the value of TG_OP as in:
IF TG_OP = ''UPDATE'' THEN
Code that address OLD and NEW here;
ELSE
Code that addresses only NEW here.
END IF;

On Thursday 22 January 2004 08:29 am, Paul Makepeace wrote:
I have a trigger that sets an expires column to
last_access+expiry::interval if expires IS NULL or if the expires value
isn't being set or changed.

IF NEW.expires IS NULL OR NEW.expires = OLD.expires THEN
NEW.expires = NEW.last_access+NEW.expiry:interval;
END IF;

The problem here is OLD doesn't exist on the first INSERT which throws
an error. It seems PL/pgSQL doesn't have C's short-circuit booleans.

a) Is there a way around this?
b) is there a 'right' way to determine if a column is being changed?

Paul (total PL/pgSQL newbie)
--
Quote: 36
"War is an ugly thing, but not the ugliest of things. The person who
has nothing for which he is willing to fight, nothing which is more
important than his own personal safety, is a miserable creature and
has no chance of being free unless made and kept so by the exertions
of better men than himself."

--John Stuart Mill

Work: 1-336-372-6812
Cell: 1-336-363-4719
email: terry (AT) esc1 (DOT) com

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #4  
Old   
Marcus Andree S. Magalhaes
 
Posts: n/a

Default Re: Using OLD on INSERT - 01-22-2004 , 09:25 AM




I can't quite understand the problem. There are minor differences
between insert triggers and update triggers in postgresql...
AFAIK the values OLD and NEW are related to the data being
inserted/updated/modified right now and can't imagine why someone
would use 'old' in a simple insert statement, but here goes my
humble opinions:

1 - make two triggers. One for insertin and another for updating
2 - short-circuit OR can be "simulated" using if-then-else clauses:
if new.expires is null then
new.expires = //whatever//
else
if new.expires = old.expires then
new.expires = //whatever//
end if;
end if;

If you want to be sure that a column is being modified, write a
update trigger.

Hope this helps a bit.

Quote:
I have a trigger that sets an expires column to
last_access+expiry::interval if expires IS NULL or if the expires value
isn't being set or changed.

IF NEW.expires IS NULL OR NEW.expires = OLD.expires THEN
NEW.expires = NEW.last_access+NEW.expiry:interval;
END IF;

The problem here is OLD doesn't exist on the first INSERT which throws
an error. It seems PL/pgSQL doesn't have C's short-circuit booleans.

a) Is there a way around this?
b) is there a 'right' way to determine if a column is being changed?

Paul (total PL/pgSQL newbie)

--
Paul Makepeace ................................
http://paulm.com/ecademy

"If I had new shoes, then he wouldn't sing Halleighluha."
-- http://paulm.com/toys/surrealism/

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.