From 88969ea11a52c1f92805d547e03446ab4374c2cd Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 19 Nov 2004 23:22:54 +0000 Subject: [PATCH] Fix incorrect documentation of plperl's method for accessing the number of rows processed by a SPI query (David Fetter); also some other minor editorial cleanup (Tom Lane). --- doc/src/sgml/plperl.sgml | 74 ++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/doc/src/sgml/plperl.sgml b/doc/src/sgml/plperl.sgml index 3adb8829fe..7893d26377 100644 --- a/doc/src/sgml/plperl.sgml +++ b/doc/src/sgml/plperl.sgml @@ -1,5 +1,5 @@ @@ -66,7 +66,7 @@ $$ LANGUAGE plperl; 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. @@ -77,7 +77,7 @@ $$ LANGUAGE plperl; CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$ - if ($_[0] > $_[1]) { return $_[0]; } + if ($_[0] > $_[1]) { return $_[0]; } return $_[1]; $$ LANGUAGE plperl; @@ -106,7 +106,7 @@ CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$ return $b; } if (! defined $b) { return $a; } - if ($a > $b) { return $a; } + if ($a > $b) { return $a; } return $b; $$ LANGUAGE plperl; @@ -129,7 +129,7 @@ CREATE TABLE employee ( 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; @@ -174,16 +174,17 @@ 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} @@ -196,11 +197,11 @@ $rv = spi_exec_query($query); 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}; @@ -260,13 +261,12 @@ INSERT INTO test (i, v) VALUES (4, 'immortal'); 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; @@ -283,7 +283,7 @@ SELECT * FROM test_munge(); 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; @@ -298,12 +298,12 @@ CREATE TYPE testsetperl AS (f1 integer, f2 text, f3 text); 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; - + @@ -359,7 +359,7 @@ SELECT get_var('sample'); 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; @@ -397,14 +397,14 @@ $$ 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 @@ -413,7 +413,7 @@ $$ LANGUAGE plperl; - $_TD->{old}{foo} + $_TD->{old}{foo} OLD value of column foo @@ -488,7 +488,7 @@ $$ LANGUAGE plperl; $_TD{argc} - Number of arguments of the trigger functions + Number of arguments of the trigger function @@ -521,7 +521,7 @@ $$ LANGUAGE plperl; "MODIFY" - Indicates that the NEW rows was modified by + Indicates that the NEW row was modified by the trigger function @@ -539,10 +539,10 @@ CREATE TABLE test ( ); 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