Thread: [GENERAL] Text value within composite function result not quoted

[GENERAL] Text value within composite function result not quoted

From
Guyren Howe
Date:
Define a couple of types:

CREATE TYPE request_in AS
(
path text[],
args jsonb,
server text,
port smallint,
headers jsonb,
body bytea,
type_requested text[]
);


CREATE TYPE request_out AS
(
status smallint,
headers jsonb,
body text
);

and a function:

CREATE OR REPLACE FUNCTION request(
req request_in)
    RETURNS "request_out"
    LANGUAGE 'plv8'
    COST 100.0
    VOLATILE 
AS $function$
return {'status': 200, 'headers': {}, 'body': "<body>Works!</body>"}
$function$;

call the function:

SELECT request(
(
'{}',
'{}'::jsonb,
'',
8080,
'{}'::jsonb,
''::bytea,
'{}')::request_in
)

get this result:

(200,{},<body>Works!</body>)

This is the textual representation of the result I get in psql and Ruby. Note that the textual final value is not quoted.

I imagine I can work out a way to deal with this, but this is not the most felicitous way of representing a text value that I can imagine.

Note that if I add a single space after “Works!”, I get quotes around the string.

This is 9.6.2 on MacOS.
Guyren Howe <guyren@gmail.com> writes:
> ... get this result:
> (200,{},<body>Works!</body>)
> This is the textual representation of the result I get in psql and Ruby. Note that the textual final value is not
quoted.
> I imagine I can work out a way to deal with this, but this is not the most felicitous way of representing a text
valuethat I can imagine. 
> Note that if I add a single space after “Works!”, I get quotes around the string.

As per spec:

https://www.postgresql.org/docs/current/static/arrays.html#ARRAYS-IO

            regards, tom lane


Re: [GENERAL] Text value within composite function result not quoted

From
Guyren Howe
Date:

On May 15, 2017, at 21:36 , Tom Lane <tgl@sss.pgh.pa.us> wrote:

... get this result:
(200,{},<body>Works!</body>)
This is the textual representation of the result I get in psql and Ruby. Note that the textual final value is not quoted.
I imagine I can work out a way to deal with this, but this is not the most felicitous way of representing a text value that I can imagine.
Note that if I add a single space after “Works!”, I get quotes around the string.

As per spec:

https://www.postgresql.org/docs/current/static/arrays.html#ARRAYS-IO

Thanks. This is… inconvenient. I see nothing about an option to force quoting of strings. Is there no such option? If not, I suggest that it would be a useful addition.

Re: [GENERAL] Text value within composite function result not quoted

From
"David G. Johnston"
Date:
On Monday, May 15, 2017, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Guyren Howe <guyren@gmail.com> writes:
> ... get this result:
> (200,{},<body>Works!</body>)
> This is the textual representation of the result I get in psql and Ruby. Note that the textual final value is not quoted.
> I imagine I can work out a way to deal with this, but this is not the most felicitous way of representing a text value that I can imagine.
> Note that if I add a single space after “Works!”, I get quotes around the string.

As per spec:

https://www.postgresql.org/docs/current/static/arrays.html#ARRAYS-IO
  

Right idea (same output rules), wrong link. The output is a composite, not an array.


David J.
Guyren Howe <guyren@gmail.com> writes:
> Thanks. This is… inconvenient. I see nothing about an option to force quoting of strings. Is there no such option? If
not,I suggest that it would be a useful addition. 

Force-quoting the elements would not move the goalposts all that much
concerning parse-ability of composite (or array) output.  If you're
complaining about that, I strongly suspect your code also fails to cope
with embedded quotes, nested structures, and/or nulls.

You might consider expanding the query's output so that the fields are
delivered separately.

            regards, tom lane