1 -- test plperl utility functions (defined in Util.xs)
3 create or replace function perl_quote_literal() returns setof text language plperl as $$
4 return_next "undef: ".quote_literal(undef);
5 return_next sprintf"$_: ".quote_literal($_)
6 for q{foo}, q{a'b}, q{a"b}, q{c''d}, q{e\f}, q{};
9 select perl_quote_literal();
21 -- test quote_nullable
22 create or replace function perl_quote_nullable() returns setof text language plperl as $$
23 return_next "undef: ".quote_nullable(undef);
24 return_next sprintf"$_: ".quote_nullable($_)
25 for q{foo}, q{a'b}, q{a"b}, q{c''d}, q{e\f}, q{};
28 select perl_quote_nullable();
41 create or replace function perl_quote_ident() returns setof text language plperl as $$
42 return_next "undef: ".quote_ident(undef); # generates undef warning if warnings enabled
43 return_next "$_: ".quote_ident($_)
44 for q{foo}, q{a'b}, q{a"b}, q{c''d}, q{e\f}, q{g.h}, q{};
47 select perl_quote_ident();
61 create or replace function perl_decode_bytea() returns setof text language plperl as $$
62 return_next "undef: ".decode_bytea(undef); # generates undef warning if warnings enabled
63 return_next "$_: ".decode_bytea($_)
64 for q{foo}, q{a\047b}, q{};
67 select perl_decode_bytea();
77 create or replace function perl_encode_bytea() returns setof text language plperl as $$
78 return_next encode_bytea(undef); # generates undef warning if warnings enabled
79 return_next encode_bytea($_)
80 for q{@}, qq{@\x01@}, qq{@\x00@}, q{};
83 select perl_encode_bytea();
93 -- test encode_array_literal
94 create or replace function perl_encode_array_literal() returns setof text language plperl as $$
95 return_next encode_array_literal(undef);
96 return_next encode_array_literal(0);
97 return_next encode_array_literal(42);
98 return_next encode_array_literal($_)
99 for [], [0], [1..5], [[]], [[1,2,[3]],4];
100 return_next encode_array_literal($_,'|')
101 for [], [0], [1..5], [[]], [[1,2,[3]],4];
104 select perl_encode_array_literal();
105 perl_encode_array_literal
106 ---------------------------
112 {"1", "2", "3", "4", "5"}
114 {{"1", "2", {"3"}}, "4"}
117 {"1"|"2"|"3"|"4"|"5"}
119 {{"1"|"2"|{"3"}}|"4"}
122 -- test encode_array_constructor
123 create or replace function perl_encode_array_constructor() returns setof text language plperl as $$
124 return_next encode_array_constructor(undef);
125 return_next encode_array_constructor(0);
126 return_next encode_array_constructor(42);
127 return_next encode_array_constructor($_)
128 for [], [0], [1..5], [[]], [[1,2,[3]],4];
131 select perl_encode_array_constructor();
132 perl_encode_array_constructor
133 -----------------------------------------
139 ARRAY['1', '2', '3', '4', '5']
141 ARRAY[ARRAY['1', '2', ARRAY['3']], '4']
144 -- test looks_like_number
145 create or replace function perl_looks_like_number() returns setof text language plperl as $$
146 return_next "undef is undef" if not defined looks_like_number(undef);
147 return_next quote_nullable($_).": ". (looks_like_number($_) ? "number" : "not number")
148 for 'foo', 0, 1, 1.3, '+3.e-4',
149 '42 x', # trailing garbage
150 '99 ', # trailing space
151 ' 99', # leading space
156 select perl_looks_like_number();
157 perl_looks_like_number
158 ------------------------
172 -- test encode_typed_literal
173 create type perl_foo as (a integer, b text[]);
174 create type perl_bar as (c perl_foo[]);
175 create domain perl_foo_pos as perl_foo check((value).a > 0);
176 create or replace function perl_encode_typed_literal() returns setof text language plperl as $$
177 return_next encode_typed_literal(undef, 'text');
178 return_next encode_typed_literal([[1,2,3],[3,2,1],[1,3,2]], 'integer[]');
179 return_next encode_typed_literal({a => 1, b => ['PL','/','Perl']}, 'perl_foo');
180 return_next encode_typed_literal({c => [{a => 9, b => ['PostgreSQL']}, {b => ['Postgres'], a => 1}]}, 'perl_bar');
181 return_next encode_typed_literal({a => 1, b => ['PL','/','Perl']}, 'perl_foo_pos');
183 select perl_encode_typed_literal();
184 perl_encode_typed_literal
185 -----------------------------------------------
187 {{1,2,3},{3,2,1},{1,3,2}}
189 ("{""(9,{PostgreSQL})"",""(1,{Postgres})""}")
193 create or replace function perl_encode_typed_literal() returns setof text language plperl as $$
194 return_next encode_typed_literal({a => 0, b => ['PL','/','Perl']}, 'perl_foo_pos');
196 select perl_encode_typed_literal(); -- fail
197 ERROR: value for domain perl_foo_pos violates check constraint "perl_foo_pos_check"
198 CONTEXT: PL/Perl function "perl_encode_typed_literal"