diff --git a/expected/function.out b/expected/function.out index 2f8b138..037dad7 100644 --- a/expected/function.out +++ b/expected/function.out @@ -94,3 +94,54 @@ SELECT polymorphic(ARRAY[10, 11]), polymorphic(ARRAY['foo', 'bar']); 10 | foo (1 row) +-- RECORD TYPES +CREATE TYPE rec AS (i integer, t text); +CREATE FUNCTION scalar_to_record(i integer, t text) RETURNS rec AS +$$ + return { "i": i, "t": t }; +$$ +LANGUAGE pljs; +SELECT scalar_to_record(1, 'a'); + scalar_to_record +------------------ + (1,a) +(1 row) + +CREATE FUNCTION record_to_text(x rec) RETURNS text AS +$$ + return JSON.stringify(x); +$$ +LANGUAGE pljs; +SELECT record_to_text('(1,a)'::rec); + record_to_text +----------------- + {"i":1,"t":"a"} +(1 row) + +CREATE FUNCTION return_record(i integer, t text) RETURNS record AS +$$ + return { "i": i, "t": t }; +$$ +LANGUAGE pljs; +SELECT * FROM return_record(1, 'a'); +ERROR: a column definition list is required for functions returning "record" +LINE 1: SELECT * FROM return_record(1, 'a'); + ^ +SELECT * FROM return_record(1, 'a') AS t(j integer, s text); + j | s +---+--- + | +(1 row) + +SELECT * FROM return_record(1, 'a') AS t(x text, y text); + x | y +---+--- + | +(1 row) + +SELECT * FROM return_record(1, 'a') AS t(i integer, t text); + i | t +---+--- + 1 | a +(1 row) + diff --git a/sql/function.sql b/sql/function.sql index 2b0e196..2bef8a6 100644 --- a/sql/function.sql +++ b/sql/function.sql @@ -65,3 +65,29 @@ $$ $$ LANGUAGE pljs; SELECT polymorphic(ARRAY[10, 11]), polymorphic(ARRAY['foo', 'bar']); + +-- RECORD TYPES +CREATE TYPE rec AS (i integer, t text); +CREATE FUNCTION scalar_to_record(i integer, t text) RETURNS rec AS +$$ + return { "i": i, "t": t }; +$$ +LANGUAGE pljs; +SELECT scalar_to_record(1, 'a'); + +CREATE FUNCTION record_to_text(x rec) RETURNS text AS +$$ + return JSON.stringify(x); +$$ +LANGUAGE pljs; +SELECT record_to_text('(1,a)'::rec); + +CREATE FUNCTION return_record(i integer, t text) RETURNS record AS +$$ + return { "i": i, "t": t }; +$$ +LANGUAGE pljs; +SELECT * FROM return_record(1, 'a'); +SELECT * FROM return_record(1, 'a') AS t(j integer, s text); +SELECT * FROM return_record(1, 'a') AS t(x text, y text); +SELECT * FROM return_record(1, 'a') AS t(i integer, t text);