Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit777f72c

Browse files
committed
Add table_name and table_schema to plperl trigger data. relname iskept but now deprecated. Patch from Adam Sjøgren. Add regression test toshow plperl trigger data (Andrew).TBD: apply similar changes to plpgsql, plpython and pltcl.
1 parent5d1a066 commit777f72c

File tree

4 files changed

+176
-3
lines changed

4 files changed

+176
-3
lines changed

‎doc/src/sgml/plperl.sgml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.52 2006/03/10 19:10:48 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.53 2006/05/26 17:34:16 adunstan Exp $ -->
22

33
<chapter id="plperl">
44
<title>PL/Perl - Perl Procedural Language</title>
@@ -728,14 +728,34 @@ $$ LANGUAGE plperl;
728728
</varlistentry>
729729

730730
<varlistentry>
731-
<term><literal>$_TD-&gt;{relname}</literal></term>
731+
<term><literal>$_TD-&gt;{table_name}</literal></term>
732732
<listitem>
733733
<para>
734734
Name of the table on which the trigger fired
735735
</para>
736736
</listitem>
737737
</varlistentry>
738738

739+
<varlistentry>
740+
<term><literal>$_TD-&gt;{relname}</literal></term>
741+
<listitem>
742+
<para>
743+
Name of the table on which the trigger fired. This has been deprecated,
744+
and could be removed in a future release.
745+
Please use $_TD-&gt;{table_name} instead.
746+
</para>
747+
</listitem>
748+
</varlistentry>
749+
750+
<varlistentry>
751+
<term><literal>$_TD-&gt;{table_schema}</literal></term>
752+
<listitem>
753+
<para>
754+
Name of the schema in which the table on which the trigger fired, is
755+
</para>
756+
</listitem>
757+
</varlistentry>
758+
739759
<varlistentry>
740760
<term><literal>$_TD-&gt;{argc}</literal></term>
741761
<listitem>

‎src/pl/plperl/expected/plperl_trigger.out

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,95 @@ CREATE TABLE trigger_test (
33
i int,
44
v varchar
55
);
6+
CREATE OR REPLACE FUNCTION trigger_data() RETURNS trigger LANGUAGE plperl AS $$
7+
8+
# make sure keys are sorted for consistent results - perl no longer
9+
# hashes in repeatable fashion across runs
10+
11+
foreach my $key (sort keys %$_TD)
12+
{
13+
14+
my $val = $_TD->{$key};
15+
16+
# relid is variable, so we can not use it repeatably
17+
$val = "bogus:12345" if $key eq 'relid';
18+
19+
if (! defined $val)
20+
{
21+
elog(NOTICE, "\$_TD->\{$key\} = NULL");
22+
}
23+
elsif (not ref $val)
24+
{
25+
elog(NOTICE, "\$_TD->\{$key\} = '$val'");
26+
}
27+
elsif (ref $val eq 'HASH')
28+
{
29+
my $str = "";
30+
foreach my $rowkey (sort keys %$val)
31+
{
32+
$str .= ", " if $str;
33+
my $rowval = $val->{$rowkey};
34+
$str .= "'$rowkey' => '$rowval'";
35+
}
36+
elog(NOTICE, "\$_TD->\{$key\} = \{$str\}");
37+
}
38+
elsif (ref $val eq 'ARRAY')
39+
{
40+
my $str = "";
41+
foreach my $argval (@$val)
42+
{
43+
$str .= ", " if $str;
44+
$str .= "'$argval'";
45+
}
46+
elog(NOTICE, "\$_TD->\{$key\} = \[$str\]");
47+
}
48+
}
49+
return undef; # allow statement to proceed;
50+
$$;
51+
CREATE TRIGGER show_trigger_data_trig
52+
BEFORE INSERT OR UPDATE OR DELETE ON trigger_test
53+
FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
54+
insert into trigger_test values(1,'insert');
55+
NOTICE: $_TD->{argc} = '2'
56+
NOTICE: $_TD->{args} = ['23', 'skidoo']
57+
NOTICE: $_TD->{event} = 'INSERT'
58+
NOTICE: $_TD->{level} = 'ROW'
59+
NOTICE: $_TD->{name} = 'show_trigger_data_trig'
60+
NOTICE: $_TD->{new} = {'i' => '1', 'v' => 'insert'}
61+
NOTICE: $_TD->{relid} = 'bogus:12345'
62+
NOTICE: $_TD->{relname} = 'trigger_test'
63+
NOTICE: $_TD->{table_name} = 'trigger_test'
64+
NOTICE: $_TD->{table_schema} = 'public'
65+
NOTICE: $_TD->{when} = 'BEFORE'
66+
update trigger_test set v = 'update' where i = 1;
67+
NOTICE: $_TD->{argc} = '2'
68+
NOTICE: $_TD->{args} = ['23', 'skidoo']
69+
NOTICE: $_TD->{event} = 'UPDATE'
70+
NOTICE: $_TD->{level} = 'ROW'
71+
NOTICE: $_TD->{name} = 'show_trigger_data_trig'
72+
NOTICE: $_TD->{new} = {'i' => '1', 'v' => 'update'}
73+
NOTICE: $_TD->{old} = {'i' => '1', 'v' => 'insert'}
74+
NOTICE: $_TD->{relid} = 'bogus:12345'
75+
NOTICE: $_TD->{relname} = 'trigger_test'
76+
NOTICE: $_TD->{table_name} = 'trigger_test'
77+
NOTICE: $_TD->{table_schema} = 'public'
78+
NOTICE: $_TD->{when} = 'BEFORE'
79+
delete from trigger_test;
80+
NOTICE: $_TD->{argc} = '2'
81+
NOTICE: $_TD->{args} = ['23', 'skidoo']
82+
NOTICE: $_TD->{event} = 'DELETE'
83+
NOTICE: $_TD->{level} = 'ROW'
84+
NOTICE: $_TD->{name} = 'show_trigger_data_trig'
85+
NOTICE: $_TD->{old} = {'i' => '1', 'v' => 'update'}
86+
NOTICE: $_TD->{relid} = 'bogus:12345'
87+
NOTICE: $_TD->{relname} = 'trigger_test'
88+
NOTICE: $_TD->{table_name} = 'trigger_test'
89+
NOTICE: $_TD->{table_schema} = 'public'
90+
NOTICE: $_TD->{when} = 'BEFORE'
91+
92+
DROP TRIGGER show_trigger_data_trig on trigger_test;
93+
94+
DROP FUNCTION trigger_data();
695
CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
796

897
if (($_TD->{new}{i}>=100) || ($_TD->{new}{i}<=0))

‎src/pl/plperl/plperl.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**********************************************************************
22
* plperl.c - perl as a procedural language for PostgreSQL
33
*
4-
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.108 2006/04/04 19:35:37 tgl Exp $
4+
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.109 2006/05/26 17:34:16 adunstan Exp $
55
*
66
**********************************************************************/
77

@@ -525,6 +525,12 @@ plperl_trigger_build_args(FunctionCallInfo fcinfo)
525525
hv_store(hv,"relname",7,
526526
newSVpv(SPI_getrelname(tdata->tg_relation),0),0);
527527

528+
hv_store(hv,"table_name",10,
529+
newSVpv(SPI_getrelname(tdata->tg_relation),0),0);
530+
531+
hv_store(hv,"table_schema",12,
532+
newSVpv(SPI_getnspname(tdata->tg_relation),0),0);
533+
528534
if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
529535
when="BEFORE";
530536
elseif (TRIGGER_FIRED_AFTER(tdata->tg_event))

‎src/pl/plperl/sql/plperl_trigger.sql

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,64 @@ CREATE TABLE trigger_test (
55
vvarchar
66
);
77

8+
CREATE OR REPLACEFUNCTIONtrigger_data() RETURNS trigger LANGUAGE plperlAS $$
9+
10+
# make sure keys are sorted for consistent results - perl no longer
11+
# hashes in repeatable fashion across runs
12+
13+
foreach my $key (sort keys %$_TD)
14+
{
15+
16+
my $val= $_TD->{$key};
17+
18+
# relid is variable, so we can not use it repeatably
19+
$val="bogus:12345" if $key eq'relid';
20+
21+
if (! defined $val)
22+
{
23+
elog(NOTICE,"\$_TD->\{$key\} = NULL");
24+
}
25+
elsif (not ref $val)
26+
{
27+
elog(NOTICE,"\$_TD->\{$key\} = '$val'");
28+
}
29+
elsif (ref $val eq'HASH')
30+
{
31+
my $str="";
32+
foreach my $rowkey (sort keys %$val)
33+
{
34+
$str .="," if $str;
35+
my $rowval= $val->{$rowkey};
36+
$str .="'$rowkey' => '$rowval'";
37+
}
38+
elog(NOTICE,"\$_TD->\{$key\} = \{$str\}");
39+
}
40+
elsif (ref $val eq'ARRAY')
41+
{
42+
my $str="";
43+
foreach my $argval (@$val)
44+
{
45+
$str .="," if $str;
46+
$str .="'$argval'";
47+
}
48+
elog(NOTICE,"\$_TD->\{$key\} = \[$str\]");
49+
}
50+
}
51+
return undef;# allow statement to proceed;
52+
$$;
53+
54+
CREATETRIGGERshow_trigger_data_trig
55+
BEFORE INSERTORUPDATEORDELETEON trigger_test
56+
FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
57+
58+
insert into trigger_testvalues(1,'insert');
59+
update trigger_testset v='update'where i=1;
60+
deletefrom trigger_test;
61+
62+
DROPTRIGGER show_trigger_data_trigon trigger_test;
63+
64+
DROPFUNCTION trigger_data();
65+
866
CREATE OR REPLACEFUNCTIONvalid_id() RETURNS triggerAS $$
967

1068
if (($_TD->{new}{i}>=100)|| ($_TD->{new}{i}<=0))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp