Thread: arrays of varchar and "

arrays of varchar and "

From
Tim Kane
Date:
I don't recall seeing a response to this, so I'm posting it again.
Sorry if you've seen this twice!
(Assuming my original post made it here anyway).

I have.   create table my_table (       varchar    stuff[];            # An array of varchar   };


I want to insert a number of array elements here...
   insert into my_table values ('{"value 1", "value 2", "value 3"}');

Fine.

What do I do however, when I want to insert an array element that
contains a " character?
It seems I am unable to escape it it like \" as I normally would for a '
character.

Instead, the parser seems to chop out a portion of the query string...

Known bug?
Is there a workaround?

Thanks Heaps!!

Tim.



Re: [INTERFACES] arrays of varchar and "

From
Tom Lane
Date:
Tim Kane <timk@hotgames.com> writes:
> I want to insert a number of array elements here...
>     insert into my_table values ('{"value 1", "value 2", "value 3"}');
> Fine.
> What do I do however, when I want to insert an array element that
> contains a " character?

Hmm.  It looks like array_in tries to deal with backslashed quotes in
array elements; not sure why it's not working exactly.  Will put this
on the "bugs to look at" list.  Expect a fix in 7.0.
        regards, tom lane


Re: [INTERFACES] arrays of varchar and "

From
Tom Lane
Date:
I wrote:
> Tim Kane <timk@hotgames.com> writes:
>> I want to insert a number of array elements here...
>> insert into my_table values ('{"value 1", "value 2", "value 3"}');
>> Fine.
>> What do I do however, when I want to insert an array element that
>> contains a " character?

> Hmm.  It looks like array_in tries to deal with backslashed quotes in
> array elements; not sure why it's not working exactly.

Ah, I see ;-)  The correct expression if you want, say, `value "22"a'
in your middle array item is

insert into my_table values ('{"value 1", "value \\"22\\"a", "value 3"}');

You need double backslashes because one level of backslashing will get
stripped off when the lexer handles the string constant '{...}'.
Then what arrives at the array-datatype input converter will be{"value 1", "value \"22\"a", "value 3"}
which accomplishes what you want.

There is actually a bug here: when you write it with zero or one
backslashes, the array input converter is presented with{"value 1", "value "22"a", "value 3"}
which by all rights should trigger a "malformed array constant"
error message.  But it fails to do so; instead the `"value "22'
part is just dropped on the floor, and the "a" part is taken as
the second array element value.  The input converter should
complain about this input, I think --- though I'm not quite sure
what it should complain about and what it should accept as an
unquoted array element, as in {123.45, 456.66}.  Comments anyone?
        regards, tom lane

PS: if you want to comment, please reply to the pgsql-sql list,
which seems more appropriate for this issue.