-<!-- $PostgreSQL: pgsql/doc/src/sgml/pltcl.sgml,v 2.39 2006/03/10 19:10:48 momjian Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/pltcl.sgml,v 2.40 2006/05/27 20:24:16 adunstan Exp $ -->
<chapter id="pltcl">
<title>PL/Tcl - Tcl Procedural Language</title>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><varname>$TG_table_name</varname></term>
+ <listitem>
+ <para>
+ The name of the table that caused the trigger procedure
+ to be invoked.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><varname>$TG_table_schema</varname></term>
+ <listitem>
+ <para>
+ The schema of the table that caused the trigger procedure
+ to be invoked.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><varname>$TG_relatts</varname></term>
<listitem>
2 | KEY2-9 | test key
(4 rows)
+-- show dump of trigger data
+insert into trigger_test values(1,'insert');
+NOTICE: NEW: {i: 1, v: insert}
+NOTICE: OLD: {}
+NOTICE: TG_level: ROW
+NOTICE: TG_name: show_trigger_data_trig
+NOTICE: TG_op: INSERT
+NOTICE: TG_relatts: {{} i v}
+NOTICE: TG_relid: bogus:12345
+NOTICE: TG_table_name: trigger_test
+NOTICE: TG_table_schema: public
+NOTICE: TG_when: BEFORE
+NOTICE: args: {23 skidoo}
+update trigger_test set v = 'update' where i = 1;
+NOTICE: NEW: {i: 1, v: update}
+NOTICE: OLD: {i: 1, v: insert}
+NOTICE: TG_level: ROW
+NOTICE: TG_name: show_trigger_data_trig
+NOTICE: TG_op: UPDATE
+NOTICE: TG_relatts: {{} i v}
+NOTICE: TG_relid: bogus:12345
+NOTICE: TG_table_name: trigger_test
+NOTICE: TG_table_schema: public
+NOTICE: TG_when: BEFORE
+NOTICE: args: {23 skidoo}
+delete from trigger_test;
+NOTICE: NEW: {}
+NOTICE: OLD: {i: 1, v: update}
+NOTICE: TG_level: ROW
+NOTICE: TG_name: show_trigger_data_trig
+NOTICE: TG_op: DELETE
+NOTICE: TG_relatts: {{} i v}
+NOTICE: TG_relid: bogus:12345
+NOTICE: TG_table_name: trigger_test
+NOTICE: TG_table_schema: public
+NOTICE: TG_when: BEFORE
+NOTICE: args: {23 skidoo}
+
}
return "f"
' language pltcl;
+-- dump trigger data
+CREATE TABLE trigger_test
+ (i int, v text );
+CREATE FUNCTION trigger_data() returns trigger language pltcl as $_$
+
+ if { [info exists TG_relid] } {
+ set TG_relid "bogus:12345"
+ }
+
+ set dnames [info locals {[a-zA-Z]*} ]
+
+ foreach key [lsort $dnames] {
+
+ if { [array exists $key] } {
+ set str "{"
+ foreach akey [lsort [ array names $key ] ] {
+ if {[string length $str] > 1} { set str "$str, " }
+ set cmd "($akey)"
+ set cmd "set val \$$key$cmd"
+ eval $cmd
+ set str "$str$akey: $val"
+ }
+ set str "$str}"
+ elog NOTICE "$key: $str"
+ } else {
+ set val [eval list "\$$key" ]
+ elog NOTICE "$key: $val"
+ }
+ }
+
+
+ return OK
+
+$_$;
+CREATE TRIGGER show_trigger_data_trig
+BEFORE INSERT OR UPDATE OR DELETE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
--
-- Trigger function on every change to T_pkey1
--
* pltcl.c - PostgreSQL support for Tcl as
* procedural language (PL)
*
- * $PostgreSQL: pgsql/src/pl/tcl/pltcl.c,v 1.102 2006/04/04 19:35:37 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/tcl/pltcl.c,v 1.103 2006/05/27 20:24:16 adunstan Exp $
*
**********************************************************************/
Tcl_DStringAppendElement(&tcl_cmd, stroid);
pfree(stroid);
+ /* The name of the table the trigger is acting on: TG_table_name */
+ stroid = SPI_getrelname(trigdata->tg_relation);
+ Tcl_DStringAppendElement(&tcl_cmd, stroid);
+ pfree(stroid);
+
+ /* The schema of the table the trigger is acting on: TG_table_schema */
+ stroid = SPI_getnspname(trigdata->tg_relation);
+ Tcl_DStringAppendElement(&tcl_cmd, stroid);
+ pfree(stroid);
+
/* A list of attribute names for argument TG_relatts */
Tcl_DStringAppendElement(&tcl_trigtup, "");
for (i = 0; i < tupdesc->natts; i++)
{
/* trigger procedure has fixed args */
strcpy(proc_internal_args,
- "TG_name TG_relid TG_relatts TG_when TG_level TG_op __PLTcl_Tup_NEW __PLTcl_Tup_OLD args");
+ "TG_name TG_relid TG_table_name TG_table_schema TG_relatts TG_when TG_level TG_op __PLTcl_Tup_NEW __PLTcl_Tup_OLD args");
}
/************************************************************
select * from T_pkey1 order by key1 using @<, key2;
select * from T_pkey2 order by key1 using @<, key2;
+
+-- show dump of trigger data
+insert into trigger_test values(1,'insert');
+update trigger_test set v = 'update' where i = 1;
+delete from trigger_test;
+
+
+
' language pltcl;
+-- dump trigger data
+
+CREATE TABLE trigger_test
+ (i int, v text );
+
+CREATE FUNCTION trigger_data() returns trigger language pltcl as $_$
+
+ if { [info exists TG_relid] } {
+ set TG_relid "bogus:12345"
+ }
+
+ set dnames [info locals {[a-zA-Z]*} ]
+
+ foreach key [lsort $dnames] {
+
+ if { [array exists $key] } {
+ set str "{"
+ foreach akey [lsort [ array names $key ] ] {
+ if {[string length $str] > 1} { set str "$str, " }
+ set cmd "($akey)"
+ set cmd "set val \$$key$cmd"
+ eval $cmd
+ set str "$str$akey: $val"
+ }
+ set str "$str}"
+ elog NOTICE "$key: $str"
+ } else {
+ set val [eval list "\$$key" ]
+ elog NOTICE "$key: $val"
+ }
+ }
+
+
+ return OK
+
+$_$;
+
+CREATE TRIGGER show_trigger_data_trig
+BEFORE INSERT OR UPDATE OR DELETE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+
+
--
-- Trigger function on every change to T_pkey1
--