Skip to content

Commit

Permalink
Support obtaining schema name in trigger functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alvherre committed May 21, 2011
1 parent 332c27f commit 74bab22
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions expected/trigger.out
Expand Up @@ -61,6 +61,16 @@ NOTICE: plphp: trigger: trigtest8 table: trigfunc_table, level: ROW event: AFTE
NOTICE: plphp: trigger: trigtest8 table: trigfunc_table, level: ROW event: AFTER DELETE old val: 2
NOTICE: plphp: trigger: trigtest8 table: trigfunc_table, level: ROW event: AFTER DELETE old val: 2
NOTICE: plphp: trigger: trigtest11 table: trigfunc_table, level: STATEMENT event: AFTER DELETE
-- Do we get the schema?
CREATE FUNCTION trigfunc2() RETURNS trigger LANGUAGE plphp AS $$
$extra = "";

pg_raise('notice', "schema: ${_TD['schemaname']} table: ${_TD['relname']}");
$$;
create schema "the schema" create table "the table" (a int);
create trigger trig2test after insert on "the schema"."the table" for each row execute procedure trigfunc2();
insert into "the schema"."the table" values (4);
NOTICE: plphp: schema: the schema table: the table
-- Add an additional attribute to NEW
CREATE FUNCTION trigfunc_test1() RETURNS trigger LANGUAGE plphp AS $$
$_TD['new']['a'] = 'foo';
Expand Down
1 change: 1 addition & 0 deletions plphp.c
Expand Up @@ -752,6 +752,7 @@ plphp_trig_build_args(FunctionCallInfo fcinfo)
add_assoc_string(retval, "name", tdata->tg_trigger->tgname, 1);
add_assoc_long(retval, "relid", tdata->tg_relation->rd_id);
add_assoc_string(retval, "relname", SPI_getrelname(tdata->tg_relation), 1);
add_assoc_string(retval, "schemaname", SPI_getnspname(tdata->tg_relation), 1);

/* EVENT */
if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
Expand Down
11 changes: 11 additions & 0 deletions sql/trigger.sql
Expand Up @@ -44,6 +44,17 @@ update trigfunc_table set a = 2 where a = 1;

delete from trigfunc_table where a = 2;

-- Do we get the schema?
CREATE FUNCTION trigfunc2() RETURNS trigger LANGUAGE plphp AS $$
$extra = "";

pg_raise('notice', "schema: ${_TD['schemaname']} table: ${_TD['relname']}");
$$;

create schema "the schema" create table "the table" (a int);
create trigger trig2test after insert on "the schema"."the table" for each row execute procedure trigfunc2();
insert into "the schema"."the table" values (4);

-- Add an additional attribute to NEW
CREATE FUNCTION trigfunc_test1() RETURNS trigger LANGUAGE plphp AS $$
$_TD['new']['a'] = 'foo';
Expand Down

0 comments on commit 74bab22

Please sign in to comment.