1 -- test plperl triggers
3 CREATE TABLE trigger_test (
8 CREATE OR REPLACE FUNCTION trigger_data() RETURNS trigger LANGUAGE plperl AS $$
10 # make sure keys are sorted for consistent results - perl no longer
11 # hashes in repeatable fashion across runs
13 foreach my $key (sort keys %$_TD)
16 my $val = $_TD->{$key};
18 # relid is variable, so we can not use it repeatably
19 $val = "bogus:12345" if $key eq 'relid';
23 elog(NOTICE, "\$_TD->\{$key\} = NULL");
27 elog(NOTICE, "\$_TD->\{$key\} = '$val'");
29 elsif (ref $val eq 'HASH')
32 foreach my $rowkey (sort keys %$val)
35 my $rowval = $val->{$rowkey};
36 $str .= "'$rowkey' => '$rowval'";
38 elog(NOTICE, "\$_TD->\{$key\} = \{$str\}");
40 elsif (ref $val eq 'ARRAY')
43 foreach my $argval (@$val)
48 elog(NOTICE, "\$_TD->\{$key\} = \[$str\]");
51 return undef; # allow statement to proceed;
54 CREATE TRIGGER show_trigger_data_trig
55 BEFORE INSERT OR UPDATE OR DELETE ON trigger_test
56 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
58 insert into trigger_test values(1,'insert');
59 update trigger_test set v = 'update' where i = 1;
60 delete from trigger_test;
62 DROP TRIGGER show_trigger_data_trig on trigger_test;
64 DROP FUNCTION trigger_data();
66 CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
68 if (($_TD->{new}{i}>=100) || ($_TD->{new}{i}<=0))
70 return "SKIP"; # Skip INSERT/UPDATE command
72 elsif ($_TD->{new}{v} ne "immortal")
74 $_TD->{new}{v} .= "(modified by trigger)";
75 return "MODIFY"; # Modify tuple and proceed INSERT/UPDATE command
79 return; # Proceed INSERT/UPDATE command
83 CREATE TRIGGER "test_valid_id_trig" BEFORE INSERT OR UPDATE ON trigger_test
84 FOR EACH ROW EXECUTE PROCEDURE "valid_id"();
86 INSERT INTO trigger_test (i, v) VALUES (1,'first line');
87 INSERT INTO trigger_test (i, v) VALUES (2,'second line');
88 INSERT INTO trigger_test (i, v) VALUES (3,'third line');
89 INSERT INTO trigger_test (i, v) VALUES (4,'immortal');
91 INSERT INTO trigger_test (i, v) VALUES (101,'bad id');
93 SELECT * FROM trigger_test;
95 UPDATE trigger_test SET i = 5 where i=3;
97 UPDATE trigger_test SET i = 100 where i=1;
99 SELECT * FROM trigger_test;
101 CREATE OR REPLACE FUNCTION immortal() RETURNS trigger AS $$
102 if ($_TD->{old}{v} eq $_TD->{args}[0])
104 return "SKIP"; # Skip DELETE command
108 return; # Proceed DELETE command
112 CREATE TRIGGER "immortal_trig" BEFORE DELETE ON trigger_test
113 FOR EACH ROW EXECUTE PROCEDURE immortal('immortal');
115 DELETE FROM trigger_test;
118 SELECT * FROM trigger_test;