@@ -69,7 +69,8 @@ SELECT * FROM perl_set_int(5);
6969 5
7070(6 rows)
7171
72- CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text);
72+ CREATE TYPE testnestperl AS (f5 integer[]);
73+ CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text, f4 testnestperl);
7374CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$
7475 return undef;
7576$$ LANGUAGE plperl;
@@ -80,24 +81,24 @@ SELECT perl_row();
8081(1 row)
8182
8283SELECT * FROM perl_row();
83- f1 | f2 | f3
84- ----+----+----
85- | |
84+ f1 | f2 | f3 | f4
85+ ----+----+----+----
86+ | | |
8687(1 row)
8788
8889CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$
89- return {f2 => 'hello', f1 => 1, f3 => 'world'};
90+ return {f2 => 'hello', f1 => 1, f3 => 'world', 'f4' => { 'f5' => [[1]] } };
9091$$ LANGUAGE plperl;
9192SELECT perl_row();
92- perl_row
93- -----------------
94- (1,hello,world)
93+ perl_row
94+ ---------------------------
95+ (1,hello,world,"({{1}})" )
9596(1 row)
9697
9798SELECT * FROM perl_row();
98- f1 | f2 | f3
99- ----+-------+-------
100- 1 | hello | world
99+ f1 | f2 | f3 | f4
100+ ----+-------+-------+---------
101+ 1 | hello | world | ({{1}})
101102(1 row)
102103
103104CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testrowperl AS $$
@@ -109,15 +110,18 @@ SELECT perl_set();
109110(0 rows)
110111
111112SELECT * FROM perl_set();
112- f1 | f2 | f3
113- ----+----+----
113+ f1 | f2 | f3 | f4
114+ ----+----+----+----
114115(0 rows)
115116
116117CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testrowperl AS $$
117118 return [
118119 { f1 => 1, f2 => 'Hello', f3 => 'World' },
119120 undef,
120- { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' }
121+ { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl', 'f4' => {} },
122+ { f1 => 4, f2 => 'Hello', f3 => 'PL/Perl', 'f4' => { 'f5' => undef }},
123+ { f1 => 5, f2 => 'Hello', f3 => 'PL/Perl', 'f4' => { 'f5' => '{1}' }},
124+ { f1 => 6, f2 => 'Hello', f3 => 'PL/Perl', 'f4' => { 'f5' => [1] }},
121125 ];
122126$$ LANGUAGE plperl;
123127SELECT perl_set();
@@ -129,25 +133,37 @@ CONTEXT: PL/Perl function "perl_set"
129133CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testrowperl AS $$
130134 return [
131135 { f1 => 1, f2 => 'Hello', f3 => 'World' },
132- { f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' },
133- { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' }
136+ { f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL', 'f4' => undef },
137+ { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl', 'f4' => {} },
138+ { f1 => 4, f2 => 'Hello', f3 => 'PL/Perl', 'f4' => { 'f5' => undef }},
139+ { f1 => 5, f2 => 'Hello', f3 => 'PL/Perl', 'f4' => { 'f5' => '{1}' }},
140+ { f1 => 6, f2 => 'Hello', f3 => 'PL/Perl', 'f4' => { 'f5' => [1] }},
141+ { f1 => 7, f2 => 'Hello', f3 => 'PL/Perl', 'f4' => '({1})' },
134142 ];
135143$$ LANGUAGE plperl;
136144SELECT perl_set();
137- perl_set
138- ----------------------
139- (1,Hello,World)
140- (2,Hello,PostgreSQL)
141- (3,Hello,PL/Perl)
142- (3 rows)
145+ perl_set
146+ ---------------------------
147+ (1,Hello,World,)
148+ (2,Hello,PostgreSQL,)
149+ (3,Hello,PL/Perl,"()")
150+ (4,Hello,PL/Perl,"()")
151+ (5,Hello,PL/Perl,"({1})")
152+ (6,Hello,PL/Perl,"({1})")
153+ (7,Hello,PL/Perl,"({1})")
154+ (7 rows)
143155
144156SELECT * FROM perl_set();
145- f1 | f2 | f3
146- ----+-------+------------
147- 1 | Hello | World
148- 2 | Hello | PostgreSQL
149- 3 | Hello | PL/Perl
150- (3 rows)
157+ f1 | f2 | f3 | f4
158+ ----+-------+------------+-------
159+ 1 | Hello | World |
160+ 2 | Hello | PostgreSQL |
161+ 3 | Hello | PL/Perl | ()
162+ 4 | Hello | PL/Perl | ()
163+ 5 | Hello | PL/Perl | ({1})
164+ 6 | Hello | PL/Perl | ({1})
165+ 7 | Hello | PL/Perl | ({1})
166+ (7 rows)
151167
152168CREATE OR REPLACE FUNCTION perl_record() RETURNS record AS $$
153169 return undef;
@@ -162,14 +178,14 @@ SELECT * FROM perl_record();
162178ERROR: a column definition list is required for functions returning "record"
163179LINE 1: SELECT * FROM perl_record();
164180 ^
165- SELECT * FROM perl_record() AS (f1 integer, f2 text, f3 text);
166- f1 | f2 | f3
167- ----+----+----
168- | |
181+ SELECT * FROM perl_record() AS (f1 integer, f2 text, f3 text, f4 testnestperl );
182+ f1 | f2 | f3 | f4
183+ ----+----+----+----
184+ | | |
169185(1 row)
170186
171187CREATE OR REPLACE FUNCTION perl_record() RETURNS record AS $$
172- return {f2 => 'hello', f1 => 1, f3 => 'world'};
188+ return {f2 => 'hello', f1 => 1, f3 => 'world', 'f4' => { 'f5' => [1] } };
173189$$ LANGUAGE plperl;
174190SELECT perl_record();
175191ERROR: function returning record called in context that cannot accept type record
@@ -178,10 +194,10 @@ SELECT * FROM perl_record();
178194ERROR: a column definition list is required for functions returning "record"
179195LINE 1: SELECT * FROM perl_record();
180196 ^
181- SELECT * FROM perl_record() AS (f1 integer, f2 text, f3 text);
182- f1 | f2 | f3
183- ----+-------+-------
184- 1 | hello | world
197+ SELECT * FROM perl_record() AS (f1 integer, f2 text, f3 text, f4 testnestperl );
198+ f1 | f2 | f3 | f4
199+ ----+-------+-------+-------
200+ 1 | hello | world | ({1})
185201(1 row)
186202
187203CREATE OR REPLACE FUNCTION perl_record_set() RETURNS SETOF record AS $$
@@ -474,7 +490,7 @@ SELECT * FROM recurse(3);
474490(5 rows)
475491
476492---
477- --- Test arrary return
493+ --- Test array return
478494---
479495CREATE OR REPLACE FUNCTION array_of_text() RETURNS TEXT[][]
480496LANGUAGE plperl as $$
@@ -555,6 +571,32 @@ $$ LANGUAGE plperl;
555571SELECT perl_spi_prepared_bad(4.35) as "double precision";
556572ERROR: type "does_not_exist" does not exist at line 2.
557573CONTEXT: PL/Perl function "perl_spi_prepared_bad"
574+ -- Test with a row type
575+ CREATE OR REPLACE FUNCTION perl_spi_prepared() RETURNS INTEGER AS $$
576+ my $x = spi_prepare('select $1::footype AS a', 'footype');
577+ my $q = spi_exec_prepared( $x, '(1, 2)');
578+ spi_freeplan($x);
579+ return $q->{rows}->[0]->{a}->{x};
580+ $$ LANGUAGE plperl;
581+ SELECT * from perl_spi_prepared();
582+ perl_spi_prepared
583+ -------------------
584+ 1
585+ (1 row)
586+
587+ CREATE OR REPLACE FUNCTION perl_spi_prepared_row(footype) RETURNS footype AS $$
588+ my $footype = shift;
589+ my $x = spi_prepare('select $1 AS a', 'footype');
590+ my $q = spi_exec_prepared( $x, {}, $footype );
591+ spi_freeplan($x);
592+ return $q->{rows}->[0]->{a};
593+ $$ LANGUAGE plperl;
594+ SELECT * from perl_spi_prepared_row('(1, 2)');
595+ x | y
596+ ---+---
597+ 1 | 2
598+ (1 row)
599+
558600-- simple test of a DO block
559601DO $$
560602 $a = 'This is a test';
0 commit comments