From: Andrew Dunstan Date: Sat, 27 May 2006 20:24:16 +0000 (+0000) Subject: TG_table_name and TG_table_schema for pl/tcl, plus regression test and docs. X-Git-Tag: REL8_2_BETA1~912 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=22b118b530154c386d850bfe533c6609568724c5;p=postgresql TG_table_name and TG_table_schema for pl/tcl, plus regression test and docs. --- diff --git a/doc/src/sgml/pltcl.sgml b/doc/src/sgml/pltcl.sgml index 2a12a5b26a..3ffaf81fe5 100644 --- a/doc/src/sgml/pltcl.sgml +++ b/doc/src/sgml/pltcl.sgml @@ -1,4 +1,4 @@ - + PL/Tcl - Tcl Procedural Language @@ -527,6 +527,26 @@ SELECT 'doesn''t' AS ret + + $TG_table_name + + + The name of the table that caused the trigger procedure + to be invoked. + + + + + + $TG_table_schema + + + The schema of the table that caused the trigger procedure + to be invoked. + + + + $TG_relatts diff --git a/src/pl/tcl/expected/pltcl_queries.out b/src/pl/tcl/expected/pltcl_queries.out index 600b15821b..6b02fb4b48 100644 --- a/src/pl/tcl/expected/pltcl_queries.out +++ b/src/pl/tcl/expected/pltcl_queries.out @@ -183,3 +183,41 @@ select * from T_pkey2 order by key1 using @<, key2; 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} + diff --git a/src/pl/tcl/expected/pltcl_setup.out b/src/pl/tcl/expected/pltcl_setup.out index 583fd4fe76..111d999af2 100644 --- a/src/pl/tcl/expected/pltcl_setup.out +++ b/src/pl/tcl/expected/pltcl_setup.out @@ -53,6 +53,43 @@ create function check_pkey1_exists(int4, bpchar) returns bool as E' } 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 -- diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c index 07cbcc2eb3..6705fd7ac6 100644 --- a/src/pl/tcl/pltcl.c +++ b/src/pl/tcl/pltcl.c @@ -2,7 +2,7 @@ * 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 $ * **********************************************************************/ @@ -657,6 +657,16 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS) 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++) @@ -1142,7 +1152,7 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid) { /* 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"); } /************************************************************ diff --git a/src/pl/tcl/sql/pltcl_queries.sql b/src/pl/tcl/sql/pltcl_queries.sql index 9cb059ed15..cc8b46842c 100644 --- a/src/pl/tcl/sql/pltcl_queries.sql +++ b/src/pl/tcl/sql/pltcl_queries.sql @@ -73,3 +73,11 @@ select 100 @< 4; 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; + + + diff --git a/src/pl/tcl/sql/pltcl_setup.sql b/src/pl/tcl/sql/pltcl_setup.sql index b57a77880e..062c9f1957 100644 --- a/src/pl/tcl/sql/pltcl_setup.sql +++ b/src/pl/tcl/sql/pltcl_setup.sql @@ -61,6 +61,48 @@ create function check_pkey1_exists(int4, bpchar) returns bool as E' ' 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 --