Arguments and results are handled as in any other Perl subroutine:
- Arguments are passed in @_, and a result value
+ arguments are passed in @_, and a result value
is returned with return> or as the last expression
evaluated in the function.
CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
- if ($_[0] > $_[1]) { return $_[0]; }
+ if ($_[0] > $_[1]) { return $_[0]; }
return $_[1];
$$ LANGUAGE plperl;
return $b;
}
if (! defined $b) { return $a; }
- if ($a > $b) { return $a; }
+ if ($a > $b) { return $a; }
return $b;
$$ LANGUAGE plperl;
CREATE FUNCTION empcomp(employee) RETURNS integer AS $$
my ($emp) = @_;
- return $emp->{basesalary} + $emp->{bonus};
+ return $emp->{basesalary} + $emp->{bonus};
$$ LANGUAGE plperl;
SELECT name, empcomp(employee) FROM employee;
$rv = spi_exec_query('SELECT * FROM my_table', 5);
- This returns up to 5 rows from the table
- my_table. If my_table
- has a column my_column, it could be accessed
- like this:
+ This returns up to 5 rows from the table
+ my_table. If my_table
+ has a column my_column, you can get that
+ value from row $i of the result like this:
-$foo = $rv->{rows}[$i]->{my_column};
+$foo = $rv->{rows}[$i]->{my_column};
- The total number of rows returned can be accessed like this:
+ The total number of rows returned from a SELECT
+ query can be accessed like this:
-$nrows = @{$rv->{rows}};
+$nrows = $rv->{processed}
You can then access the command status (e.g.,
SPI_OK_INSERT) like this:
-$res = $rv->{status};
+$res = $rv->{status};
To get the number of rows affected, do:
-$nrows = $rv->{rows};
+$nrows = $rv->{processed};
CREATE FUNCTION test_munge() RETURNS SETOF test AS $$
my $res = [];
my $rv = spi_exec_query('select i, v from test;');
- my $status = $rv->{status};
- my $rows = @{$rv->{rows}};
- my $processed = $rv->{processed};
- foreach my $rn (0 .. $rows - 1) {
- my $row = $rv->{rows}[$rn];
- $row->{i} += 200 if defined($row->{i});
- $row->{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row->{v}));
+ my $status = $rv->{status};
+ my $nrows = $rv->{processed};
+ foreach my $rn (0 .. $nrows - 1) {
+ my $row = $rv->{rows}[$rn];
+ $row->{i} += 200 if defined($row->{i});
+ $row->{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row->{v}));
push @$res, $row;
}
return $res;
CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text);
CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$
- return {f2 => 'hello', f1 => 1, f3 => 'world'};
+ return {f2 => 'hello', f1 => 1, f3 => 'world'};
$$ LANGUAGE plperl;
CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testsetperl AS $$
return [
- { f1 => 1, f2 => 'Hello', f3 => 'World' },
- { f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' },
- { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' }
+ { f1 => 1, f2 => 'Hello', f3 => 'World' },
+ { f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' },
+ { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' }
];
$$ LANGUAGE plperl;
-
+
system operations are not allowed for security reasons:
CREATE FUNCTION badfunc() RETURNS integer AS $$
- open(TEMP, ">/tmp/badfile");
+ open(TEMP, ">/tmp/badfile");
print TEMP "Gotcha!\n";
return 1;
$$ LANGUAGE plperl;
PL/Perl Triggers
- PL/Perl can be used to write trigger functions. The global hash
- reference $_TD contains information about the
- current trigger event. The parts of $_TD hash
+ PL/Perl can be used to write trigger functions. In a trigger function,
+ the hash reference $_TD contains information about the
+ current trigger event. The fields of the $_TD hash
reference are:
- $_TD->{new}{foo}
+ $_TD->{new}{foo}
NEW value of column foo
- $_TD->{old}{foo}
+ $_TD->{old}{foo}
OLD value of column foo
$_TD{argc}
- Number of arguments of the trigger functions
+ Number of arguments of the trigger function
"MODIFY"
- Indicates that the NEW rows was modified by
+ Indicates that the NEW row was modified by
the trigger function
);
CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
- if (($_TD->{new}{i} >= 100) || ($_TD->{new}{i} <= 0)) {
+ if (($_TD->{new}{i} >= 100) || ($_TD->{new}{i} <= 0)) {
return "SKIP"; # skip INSERT/UPDATE command
- } elsif ($_TD->{new}{v} ne "immortal") {
- $_TD->{new}{v} .= "(modified by trigger)";
+ } elsif ($_TD->{new}{v} ne "immortal") {
+ $_TD->{new}{v} .= "(modified by trigger)";
return "MODIFY"; # modify row and execute INSERT/UPDATE command
} else {
return; # execute INSERT/UPDATE command