PostgreSQL 8.0.0beta3 Documentation | ||||
---|---|---|---|---|
Prev | Fast Backward | Chapter 37. PL/Perl - Perl Procedural Language | Fast Forward | Next |
Access to the database itself from your Perl function can be done via spi_exec_query, or via an experimental module DBD::PgSPI (also available at CPAN mirror sites). This module makes available a DBI-compliant database-handle named $pg_dbh that can be used to perform queries with normal DBI syntax.
PL/Perl itself presently provides two additional Perl commands:
spi_exec_query(
[ SELECT query [, max_rows]] | [non-SELECT query] )Here is an example of a SELECT query with the optional maximum number of rows.
$rv = spi_exec_query('SELECT * from my_table', 5);
This returns up to 5 rows from my_table.
If my_table has a column my_column, it would be accessed as
$foo = $rv->{rows}[$i]->{my_column};
The number of rows actually returned would be:
$nrows = @{$rv->{rows}};
Here is an example using a non-SELECT statement.
$query = "INSERT INTO my_table VALUES (1, 'test')"; $rv = spi_exec_query($query);
You can then access status (SPI_OK_INSERT, e.g.) like this.
$res = $rv->{status};
To get the rows affected, do:
$nrows = $rv->{rows};
elog
level, msgEmit a log or error message. Possible levels are DEBUG, LOG, INFO, NOTICE, WARNING, and ERROR. ERROR raises an error condition: further execution of the function is abandoned, and the current transaction is aborted.