Re: autoupdating mtime column - Mailing list pgsql-sql

From Rodrigo De León
Subject Re: autoupdating mtime column
Date
Msg-id a55915760608040839w1a7caad9vd0aeb4e5e5271905@mail.gmail.com
Whole thread Raw
In response to Re: autoupdating mtime column  ("David Garamond" <davidgaramond@gmail.com>)
Responses Re: autoupdating mtime column
List pgsql-sql
On 8/4/06, David Garamond <davidgaramond@gmail.com> wrote:
> On 8/4/06, Richard Huxton <dev@archonet.com> wrote:
> > David Garamond wrote:
> > > Dear all,
> > >
> > > Please see SQL below. I'm already satisfied with everything except I
> > > wish in
> > > #4, mtime got automatically updated to NOW() if not explicitly SET in
> > > UPDATE
> > > statement. Is there a way to make the mtime column behave more like I
> > > wanted? Thanks in advance.
> >
> > Just check for OLD.mtime = NEW.mtime, or am I missing something here?
> >
>
>
> How do I differentiate between:
>
> UPDATE t SET mtime=mtime ...;
>
> in which mtime is specifically set and should not change, and
>
> UPDATE t SET foo=bar ...;
>
> in which mtime is not mentioned and should be updated automagically to
> NOW().

How about:

create or replace function
update_times()
returns trigger as $$begin    if TG_OP='INSERT' then        new.ctime = coalesce(new.ctime,now());        new.mtime =
coalesce(new.mtime,now());   elsif TG_OP='UPDATE' then        new.ctime = old.ctime;        new.mtime = now();    end
if;   return new;end;
 
$$ language plpgsql;

Regards,

Rodrigo


pgsql-sql by date:

Previous
From: "David Garamond"
Date:
Subject: Re: autoupdating mtime column
Next
From: "David Garamond"
Date:
Subject: Re: autoupdating mtime column