2009-08-12 12:37:26 -04:00
|
|
|
--
|
|
|
|
|
-- Test returning SETOF
|
|
|
|
|
--
|
|
|
|
|
|
2009-08-13 16:50:05 -04:00
|
|
|
CREATE FUNCTION test_setof_error() RETURNS SETOF text AS $$
|
|
|
|
|
return 37
|
|
|
|
|
$$ LANGUAGE plpythonu;
|
|
|
|
|
|
|
|
|
|
SELECT test_setof_error();
|
|
|
|
|
|
|
|
|
|
|
2009-08-12 12:37:26 -04:00
|
|
|
CREATE FUNCTION test_setof_as_list(count integer, content text) RETURNS SETOF text AS $$
|
|
|
|
|
return [ content ]*count
|
|
|
|
|
$$ LANGUAGE plpythonu;
|
|
|
|
|
|
|
|
|
|
CREATE FUNCTION test_setof_as_tuple(count integer, content text) RETURNS SETOF text AS $$
|
|
|
|
|
t = ()
|
2009-08-24 16:25:25 -04:00
|
|
|
for i in range(count):
|
2009-08-12 12:37:26 -04:00
|
|
|
t += ( content, )
|
|
|
|
|
return t
|
|
|
|
|
$$ LANGUAGE plpythonu;
|
|
|
|
|
|
|
|
|
|
CREATE FUNCTION test_setof_as_iterator(count integer, content text) RETURNS SETOF text AS $$
|
|
|
|
|
class producer:
|
|
|
|
|
def __init__ (self, icount, icontent):
|
|
|
|
|
self.icontent = icontent
|
|
|
|
|
self.icount = icount
|
|
|
|
|
def __iter__ (self):
|
|
|
|
|
return self
|
|
|
|
|
def next (self):
|
|
|
|
|
if self.icount == 0:
|
|
|
|
|
raise StopIteration
|
|
|
|
|
self.icount -= 1
|
|
|
|
|
return self.icontent
|
|
|
|
|
return producer(count, content)
|
|
|
|
|
$$ LANGUAGE plpythonu;
|
|
|
|
|
|
2010-11-15 14:26:55 -05:00
|
|
|
CREATE FUNCTION test_setof_spi_in_iterator() RETURNS SETOF text AS
|
|
|
|
|
$$
|
|
|
|
|
for s in ('Hello', 'Brave', 'New', 'World'):
|
|
|
|
|
plpy.execute('select 1')
|
|
|
|
|
yield s
|
|
|
|
|
plpy.execute('select 2')
|
|
|
|
|
$$
|
|
|
|
|
LANGUAGE plpythonu;
|
|
|
|
|
|
2009-08-12 12:37:26 -04:00
|
|
|
|
|
|
|
|
-- Test set returning functions
|
|
|
|
|
SELECT test_setof_as_list(0, 'list');
|
|
|
|
|
SELECT test_setof_as_list(1, 'list');
|
|
|
|
|
SELECT test_setof_as_list(2, 'list');
|
|
|
|
|
SELECT test_setof_as_list(2, null);
|
|
|
|
|
|
|
|
|
|
SELECT test_setof_as_tuple(0, 'tuple');
|
|
|
|
|
SELECT test_setof_as_tuple(1, 'tuple');
|
|
|
|
|
SELECT test_setof_as_tuple(2, 'tuple');
|
|
|
|
|
SELECT test_setof_as_tuple(2, null);
|
|
|
|
|
|
|
|
|
|
SELECT test_setof_as_iterator(0, 'list');
|
|
|
|
|
SELECT test_setof_as_iterator(1, 'list');
|
|
|
|
|
SELECT test_setof_as_iterator(2, 'list');
|
|
|
|
|
SELECT test_setof_as_iterator(2, null);
|
2010-11-15 14:26:55 -05:00
|
|
|
|
|
|
|
|
SELECT test_setof_spi_in_iterator();
|
2012-05-02 13:59:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
-- setof function with an SPI result set (used to crash because of
|
|
|
|
|
-- memory management issues across multiple calls)
|
|
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION get_user_records()
|
|
|
|
|
RETURNS SETOF users
|
|
|
|
|
AS $$
|
|
|
|
|
return plpy.execute("SELECT * FROM users ORDER BY username")
|
|
|
|
|
$$ LANGUAGE plpythonu;
|
|
|
|
|
|
|
|
|
|
SELECT get_user_records();
|