Re: triggers: dynamic references to fields in NEW and OLD?

From: "Kerri Reno" <kreno(at)yumaed(dot)org>
To: "Vance Maverick" <vmaverick(at)pgpeng(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: triggers: dynamic references to fields in NEW and OLD?
Date: 2008-05-16 23:17:27
Message-ID: a5b8c7860805161617x698e1693m834a2d7fa5a171b5@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Vance,

I missed your earlier post, so I may be misunderstanding the situation, but
I think you could do this more easily in plpython, because TD['new'] and
TD['old'] are dictionaries, and you can traverse the dictionaries like this:

for k, v in TD['new'].items():
if tblfld == k:
plpy.notice('%s' % v)

This probably looks like gibberish if you're not used to python, but if
you'd like more help, email me back (with your original post) and I'll get
back to you next week.

Kerri

On 5/15/08, Vance Maverick <vmaverick(at)pgpeng(dot)com> wrote:
>
> Thanks! Your solution clearly works, but it requires the shared function
> to
> enumerate all possible column names. In my real case, there are 8-10
> distinct names, so that's a bit ugly....but it works.
>
> Vance
>
> -----Original Message-----
> If you just need which table triggered the function then |TG_TABLE_NAME|
> may
> be simpler than passing parameters.
>
> Something like this will probably work for you (replace the raise notice
> with whatever you have to do)
>
> create or replace function atest() returns trigger as $$ declare
> avalue int;
> tblfld text;
> begin
> tblfld := tg_argv[0];
> if tblfld = 'aa' then
> avalue := new.aa;
> else
> if tblfld = 'bb' then
> avalue := new.bb;
> end if;
> end if;
> raise notice '%',avalue;
> return new;
> end;
> $$ language plpgsql;
>
> klint.
>
> --
> Klint Gore
> Database Manager
> Sheep CRC
> A.G.B.U.
> University of New England
> Armidale NSW 2350
>
> Ph: 02 6773 3789
> Fax: 02 6773 3266
> EMail: kgore4(at)une(dot)edu(dot)au
>
>
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

--
Yuma Educational Computer Consortium
Compass Development Team
Kerri Reno
kreno(at)yumaed(dot)org (928) 502-4240
.·:*¨¨*:·. .·:*¨¨*:·. .·:*¨¨*:·.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2008-05-16 23:28:51 Re: writing a function without installing a language
Previous Message Gerald Quimpo 2008-05-16 22:39:20 Re: psql proxy