Commit 175a2421 authored by Andrew Dunstan's avatar Andrew Dunstan

Allow args to spi_prepare to be standard type aliaes as well as those known in...

Allow args to spi_prepare to be standard type aliaes as well as those known in pg_type. Fixes bug #2917. Add some regression tests for these cases.
parent 27552ce5
...@@ -438,7 +438,7 @@ SELECT array_of_text(); ...@@ -438,7 +438,7 @@ SELECT array_of_text();
-- Test spi_prepare/spi_exec_prepared/spi_freeplan -- Test spi_prepare/spi_exec_prepared/spi_freeplan
-- --
CREATE OR REPLACE FUNCTION perl_spi_prepared(INTEGER) RETURNS INTEGER AS $$ CREATE OR REPLACE FUNCTION perl_spi_prepared(INTEGER) RETURNS INTEGER AS $$
my $x = spi_prepare('select $1 AS a', 'INT4'); my $x = spi_prepare('select $1 AS a', 'INTEGER');
my $q = spi_exec_prepared( $x, $_[0] + 1); my $q = spi_exec_prepared( $x, $_[0] + 1);
spi_freeplan($x); spi_freeplan($x);
return $q->{rows}->[0]->{a}; return $q->{rows}->[0]->{a};
...@@ -468,3 +468,37 @@ SELECT * from perl_spi_prepared_set(1,2); ...@@ -468,3 +468,37 @@ SELECT * from perl_spi_prepared_set(1,2);
4 4
(2 rows) (2 rows)
--
-- Test prepare with a type with spaces
--
CREATE OR REPLACE FUNCTION perl_spi_prepared_double(double precision) RETURNS double precision AS $$
my $x = spi_prepare('SELECT 10.0 * $1 AS a', 'DOUBLE PRECISION');
my $q = spi_query_prepared($x,$_[0]);
my $result;
while (defined (my $y = spi_fetchrow($q))) {
$result = $y->{a};
}
spi_freeplan($x);
return $result;
$$ LANGUAGE plperl;
SELECT perl_spi_prepared_double(4.35) as "double precision";
double precision
------------------
43.5
(1 row)
--
-- Test with a bad type
--
CREATE OR REPLACE FUNCTION perl_spi_prepared_bad(double precision) RETURNS double precision AS $$
my $x = spi_prepare('SELECT 10.0 * $1 AS a', 'does_not_exist');
my $q = spi_query_prepared($x,$_[0]);
my $result;
while (defined (my $y = spi_fetchrow($q))) {
$result = $y->{a};
}
spi_freeplan($x);
return $result;
$$ LANGUAGE plperl;
SELECT perl_spi_prepared_bad(4.35) as "double precision";
ERROR: error from Perl function: type "does_not_exist" does not exist at line 2.
/********************************************************************** /**********************************************************************
* plperl.c - perl as a procedural language for PostgreSQL * plperl.c - perl as a procedural language for PostgreSQL
* *
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.123 2006/11/21 16:59:02 adunstan Exp $ * $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.124 2007/01/27 01:55:57 adunstan Exp $
* *
**********************************************************************/ **********************************************************************/
...@@ -2128,23 +2128,23 @@ plperl_spi_prepare(char *query, int argc, SV **argv) ...@@ -2128,23 +2128,23 @@ plperl_spi_prepare(char *query, int argc, SV **argv)
PG_TRY(); PG_TRY();
{ {
/************************************************************ /************************************************************
* Lookup the argument types by name in the system cache * Resolve argument type names and then look them up by oid
* and remember the required information for input conversion * in the system cache, and remember the required information
* for input conversion.
************************************************************/ ************************************************************/
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
{ {
List *names; Oid typId, typInput, typIOParam;
HeapTuple typeTup; int32 typmod;
/* Parse possibly-qualified type name and look it up in pg_type */ parseTypeString(SvPV(argv[i], PL_na), &typId, &typmod);
names = stringToQualifiedNameList(SvPV(argv[i], PL_na),
"plperl_spi_prepare"); getTypeInputInfo(typId, &typInput, &typIOParam);
typeTup = typenameType(NULL, makeTypeNameFromNameList(names));
qdesc->argtypes[i] = HeapTupleGetOid(typeTup); qdesc->argtypes[i] = typId;
perm_fmgr_info(((Form_pg_type) GETSTRUCT(typeTup))->typinput, perm_fmgr_info((Form_pg_type) typInput,
&(qdesc->arginfuncs[i])); &(qdesc->arginfuncs[i]));
qdesc->argtypioparams[i] = getTypeIOParam(typeTup); qdesc->argtypioparams[i] = typIOParam;
ReleaseSysCache(typeTup);
} }
/************************************************************ /************************************************************
......
...@@ -316,7 +316,7 @@ SELECT array_of_text(); ...@@ -316,7 +316,7 @@ SELECT array_of_text();
-- Test spi_prepare/spi_exec_prepared/spi_freeplan -- Test spi_prepare/spi_exec_prepared/spi_freeplan
-- --
CREATE OR REPLACE FUNCTION perl_spi_prepared(INTEGER) RETURNS INTEGER AS $$ CREATE OR REPLACE FUNCTION perl_spi_prepared(INTEGER) RETURNS INTEGER AS $$
my $x = spi_prepare('select $1 AS a', 'INT4'); my $x = spi_prepare('select $1 AS a', 'INTEGER');
my $q = spi_exec_prepared( $x, $_[0] + 1); my $q = spi_exec_prepared( $x, $_[0] + 1);
spi_freeplan($x); spi_freeplan($x);
return $q->{rows}->[0]->{a}; return $q->{rows}->[0]->{a};
...@@ -337,3 +337,33 @@ CREATE OR REPLACE FUNCTION perl_spi_prepared_set(INTEGER, INTEGER) RETURNS SETOF ...@@ -337,3 +337,33 @@ CREATE OR REPLACE FUNCTION perl_spi_prepared_set(INTEGER, INTEGER) RETURNS SETOF
$$ LANGUAGE plperl; $$ LANGUAGE plperl;
SELECT * from perl_spi_prepared_set(1,2); SELECT * from perl_spi_prepared_set(1,2);
--
-- Test prepare with a type with spaces
--
CREATE OR REPLACE FUNCTION perl_spi_prepared_double(double precision) RETURNS double precision AS $$
my $x = spi_prepare('SELECT 10.0 * $1 AS a', 'DOUBLE PRECISION');
my $q = spi_query_prepared($x,$_[0]);
my $result;
while (defined (my $y = spi_fetchrow($q))) {
$result = $y->{a};
}
spi_freeplan($x);
return $result;
$$ LANGUAGE plperl;
SELECT perl_spi_prepared_double(4.35) as "double precision";
--
-- Test with a bad type
--
CREATE OR REPLACE FUNCTION perl_spi_prepared_bad(double precision) RETURNS double precision AS $$
my $x = spi_prepare('SELECT 10.0 * $1 AS a', 'does_not_exist');
my $q = spi_query_prepared($x,$_[0]);
my $result;
while (defined (my $y = spi_fetchrow($q))) {
$result = $y->{a};
}
spi_freeplan($x);
return $result;
$$ LANGUAGE plperl;
SELECT perl_spi_prepared_bad(4.35) as "double precision";
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