Commit 88969ea1 authored by Tom Lane's avatar Tom Lane

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).
parent 8e579752
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.30 2004/11/06 14:32:10 petere Exp $ $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.31 2004/11/19 23:22:54 tgl Exp $
--> -->
<chapter id="plperl"> <chapter id="plperl">
...@@ -66,7 +66,7 @@ $$ LANGUAGE plperl; ...@@ -66,7 +66,7 @@ $$ LANGUAGE plperl;
<para> <para>
Arguments and results are handled as in any other Perl subroutine: Arguments and results are handled as in any other Perl subroutine:
Arguments are passed in <varname>@_</varname>, and a result value arguments are passed in <varname>@_</varname>, and a result value
is returned with <literal>return</> or as the last expression is returned with <literal>return</> or as the last expression
evaluated in the function. evaluated in the function.
</para> </para>
...@@ -77,7 +77,7 @@ $$ LANGUAGE plperl; ...@@ -77,7 +77,7 @@ $$ LANGUAGE plperl;
<programlisting> <programlisting>
CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$ CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
if ($_[0] > $_[1]) { return $_[0]; } if ($_[0] &gt; $_[1]) { return $_[0]; }
return $_[1]; return $_[1];
$$ LANGUAGE plperl; $$ LANGUAGE plperl;
</programlisting> </programlisting>
...@@ -106,7 +106,7 @@ CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$ ...@@ -106,7 +106,7 @@ CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
return $b; return $b;
} }
if (! defined $b) { return $a; } if (! defined $b) { return $a; }
if ($a > $b) { return $a; } if ($a &gt; $b) { return $a; }
return $b; return $b;
$$ LANGUAGE plperl; $$ LANGUAGE plperl;
</programlisting> </programlisting>
...@@ -129,7 +129,7 @@ CREATE TABLE employee ( ...@@ -129,7 +129,7 @@ CREATE TABLE employee (
CREATE FUNCTION empcomp(employee) RETURNS integer AS $$ CREATE FUNCTION empcomp(employee) RETURNS integer AS $$
my ($emp) = @_; my ($emp) = @_;
return $emp->{basesalary} + $emp->{bonus}; return $emp-&gt;{basesalary} + $emp-&gt;{bonus};
$$ LANGUAGE plperl; $$ LANGUAGE plperl;
SELECT name, empcomp(employee) FROM employee; SELECT name, empcomp(employee) FROM employee;
...@@ -174,16 +174,17 @@ SELECT name, empcomp(employee) FROM employee; ...@@ -174,16 +174,17 @@ SELECT name, empcomp(employee) FROM employee;
<programlisting> <programlisting>
$rv = spi_exec_query('SELECT * FROM my_table', 5); $rv = spi_exec_query('SELECT * FROM my_table', 5);
</programlisting> </programlisting>
This returns up to 5 rows from the table This returns up to 5 rows from the table
<literal>my_table</literal>. If <literal>my_table</literal> <literal>my_table</literal>. If <literal>my_table</literal>
has a column <literal>my_column</literal>, it could be accessed has a column <literal>my_column</literal>, you can get that
like this: value from row <literal>$i</literal> of the result like this:
<programlisting> <programlisting>
$foo = $rv->{rows}[$i]->{my_column}; $foo = $rv-&gt;{rows}[$i]-&gt;{my_column};
</programlisting> </programlisting>
The total number of rows returned can be accessed like this: The total number of rows returned from a <command>SELECT</command>
query can be accessed like this:
<programlisting> <programlisting>
$nrows = @{$rv->{rows}}; $nrows = $rv-&gt;{processed}
</programlisting> </programlisting>
</para> </para>
...@@ -196,11 +197,11 @@ $rv = spi_exec_query($query); ...@@ -196,11 +197,11 @@ $rv = spi_exec_query($query);
You can then access the command status (e.g., You can then access the command status (e.g.,
<literal>SPI_OK_INSERT</literal>) like this: <literal>SPI_OK_INSERT</literal>) like this:
<programlisting> <programlisting>
$res = $rv->{status}; $res = $rv-&gt;{status};
</programlisting> </programlisting>
To get the number of rows affected, do: To get the number of rows affected, do:
<programlisting> <programlisting>
$nrows = $rv->{rows}; $nrows = $rv-&gt;{processed};
</programlisting> </programlisting>
</para> </para>
</listitem> </listitem>
...@@ -260,13 +261,12 @@ INSERT INTO test (i, v) VALUES (4, 'immortal'); ...@@ -260,13 +261,12 @@ INSERT INTO test (i, v) VALUES (4, 'immortal');
CREATE FUNCTION test_munge() RETURNS SETOF test AS $$ CREATE FUNCTION test_munge() RETURNS SETOF test AS $$
my $res = []; my $res = [];
my $rv = spi_exec_query('select i, v from test;'); my $rv = spi_exec_query('select i, v from test;');
my $status = $rv->{status}; my $status = $rv-&gt;{status};
my $rows = @{$rv->{rows}}; my $nrows = $rv-&gt;{processed};
my $processed = $rv->{processed}; foreach my $rn (0 .. $nrows - 1) {
foreach my $rn (0 .. $rows - 1) { my $row = $rv-&gt;{rows}[$rn];
my $row = $rv->{rows}[$rn]; $row-&gt;{i} += 200 if defined($row-&gt;{i});
$row->{i} += 200 if defined($row->{i}); $row-&gt;{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row-&gt;{v}));
$row->{v} =~ tr/A-Za-z/a-zA-Z/ if (defined($row->{v}));
push @$res, $row; push @$res, $row;
} }
return $res; return $res;
...@@ -283,7 +283,7 @@ SELECT * FROM test_munge(); ...@@ -283,7 +283,7 @@ SELECT * FROM test_munge();
CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text); CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text);
CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$ CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$
return {f2 => 'hello', f1 => 1, f3 => 'world'}; return {f2 =&gt; 'hello', f1 =&gt; 1, f3 =&gt; 'world'};
$$ LANGUAGE plperl; $$ LANGUAGE plperl;
</programlisting> </programlisting>
</para> </para>
...@@ -298,12 +298,12 @@ CREATE TYPE testsetperl AS (f1 integer, f2 text, f3 text); ...@@ -298,12 +298,12 @@ CREATE TYPE testsetperl AS (f1 integer, f2 text, f3 text);
CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testsetperl AS $$ CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testsetperl AS $$
return [ return [
{ f1 => 1, f2 => 'Hello', f3 => 'World' }, { f1 =&gt; 1, f2 =&gt; 'Hello', f3 =&gt; 'World' },
{ f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' }, { f1 =&gt; 2, f2 =&gt; 'Hello', f3 =&gt; 'PostgreSQL' },
{ f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' } { f1 =&gt; 3, f2 =&gt; 'Hello', f3 =&gt; 'PL/Perl' }
]; ];
$$ LANGUAGE plperl; $$ LANGUAGE plperl;
</programlisting> </programlisting>
</para> </para>
</sect1> </sect1>
...@@ -359,7 +359,7 @@ SELECT get_var('sample'); ...@@ -359,7 +359,7 @@ SELECT get_var('sample');
system operations are not allowed for security reasons: system operations are not allowed for security reasons:
<programlisting> <programlisting>
CREATE FUNCTION badfunc() RETURNS integer AS $$ CREATE FUNCTION badfunc() RETURNS integer AS $$
open(TEMP, ">/tmp/badfile"); open(TEMP, "&gt;/tmp/badfile");
print TEMP "Gotcha!\n"; print TEMP "Gotcha!\n";
return 1; return 1;
$$ LANGUAGE plperl; $$ LANGUAGE plperl;
...@@ -397,14 +397,14 @@ $$ LANGUAGE plperl; ...@@ -397,14 +397,14 @@ $$ LANGUAGE plperl;
<title>PL/Perl Triggers</title> <title>PL/Perl Triggers</title>
<para> <para>
PL/Perl can be used to write trigger functions. The global hash PL/Perl can be used to write trigger functions. In a trigger function,
reference <varname>$_TD</varname> contains information about the the hash reference <varname>$_TD</varname> contains information about the
current trigger event. The parts of <varname>$_TD</varname> hash current trigger event. The fields of the <varname>$_TD</varname> hash
reference are: reference are:
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term><literal>$_TD->{new}{foo}</literal></term> <term><literal>$_TD-&gt;{new}{foo}</literal></term>
<listitem> <listitem>
<para> <para>
<literal>NEW</literal> value of column <literal>foo</literal> <literal>NEW</literal> value of column <literal>foo</literal>
...@@ -413,7 +413,7 @@ $$ LANGUAGE plperl; ...@@ -413,7 +413,7 @@ $$ LANGUAGE plperl;
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><literal>$_TD->{old}{foo}</literal></term> <term><literal>$_TD-&gt;{old}{foo}</literal></term>
<listitem> <listitem>
<para> <para>
<literal>OLD</literal> value of column <literal>foo</literal> <literal>OLD</literal> value of column <literal>foo</literal>
...@@ -488,7 +488,7 @@ $$ LANGUAGE plperl; ...@@ -488,7 +488,7 @@ $$ LANGUAGE plperl;
<term><literal>$_TD{argc}</literal></term> <term><literal>$_TD{argc}</literal></term>
<listitem> <listitem>
<para> <para>
Number of arguments of the trigger functions Number of arguments of the trigger function
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -521,7 +521,7 @@ $$ LANGUAGE plperl; ...@@ -521,7 +521,7 @@ $$ LANGUAGE plperl;
<term><literal>"MODIFY"</literal></term> <term><literal>"MODIFY"</literal></term>
<listitem> <listitem>
<para> <para>
Indicates that the <literal>NEW</literal> rows was modified by Indicates that the <literal>NEW</literal> row was modified by
the trigger function the trigger function
</para> </para>
</listitem> </listitem>
...@@ -539,10 +539,10 @@ CREATE TABLE test ( ...@@ -539,10 +539,10 @@ CREATE TABLE test (
); );
CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$ CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
if (($_TD->{new}{i} &gt;= 100) || ($_TD->{new}{i} &lt;= 0)) { if (($_TD-&gt;{new}{i} &gt;= 100) || ($_TD-&gt;{new}{i} &lt;= 0)) {
return "SKIP"; # skip INSERT/UPDATE command return "SKIP"; # skip INSERT/UPDATE command
} elsif ($_TD->{new}{v} ne "immortal") { } elsif ($_TD-&gt;{new}{v} ne "immortal") {
$_TD->{new}{v} .= "(modified by trigger)"; $_TD-&gt;{new}{v} .= "(modified by trigger)";
return "MODIFY"; # modify row and execute INSERT/UPDATE command return "MODIFY"; # modify row and execute INSERT/UPDATE command
} else { } else {
return; # execute INSERT/UPDATE command return; # execute INSERT/UPDATE command
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment