From: Erik Jones <erik@myemma.com>
To: Richard Broersma Jr <rabroersma@yahoo.com>
Cc: SQL Postgresql List <pgsql-sql@postgresql.org>
Subject: Re: Advice for generalizing trigger functions
Date: Thu, 27 Dec 2007 12:22:33 -0600
Message-ID: <75614809-F4CB-4D8B-86E6-2F600A43CF69@myemma.com> (raw)
In-Reply-To: <276465.18598.qm@web31801.mail.mud.yahoo.com>
References: <276465.18598.qm@web31801.mail.mud.yahoo.com>
On Dec 27, 2007, at 12:03 PM, Richard Broersma Jr wrote:
> --- On Thu, 12/27/07, Erik Jones <erik@myemma.com> wrote:
>
>> TG_TABLE_NAME will have the name of the table the trigger
>> was fired on. With that and using EXECUTE for your INSERT
>> statements, you'll probably be set.
>
> True the table name is the easy part, but how do I determine the
> Primary Key components of the table that will also be needed in the
> SQL string that will be executed?
That depends on how generalized you want this trigger function to
be. If you have a set number of tables you can branch on the table
name to determine the id column. That's brittle and would require
updating the the function every time you want to use if for a new
table type but if there's a limited number of tables it may work for
you. Another way to go would be to the table name to join across
pg_class, pg_attribute, and pg_constraint. Take a look at the table
layouts for those in the manual and it should be pretty clear how to
do that.
Erik Jones
Software Developer | Emma®
erik@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)
Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: erik@myemma.com, rabroersma@yahoo.com
Subject: Re: Advice for generalizing trigger functions
In-Reply-To: <75614809-F4CB-4D8B-86E6-2F600A43CF69@myemma.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox