Re: CSV hack

Поиск
Список
Период
Сортировка
От david@fetter.org (David Fetter)
Тема Re: CSV hack
Дата
Msg-id 200312110743.hBB7hYqa003302@fetter.org
обсуждение исходный текст
Ответ на Re: CSV hack  (Andrew Dunstan <andrew@dunslane.net>)
Список pgsql-hackers
In article <3FD7EC2A.6020903@dunslane.net> you wrote:
> 
> You also need to quote values containing the separator.

Roight!  Thanks for the heads-up :)  And now, version 2.

Cheers,
D

CREATE OR REPLACE FUNCTION csv(anyarray) RETURNS TEXT AS
'DECLARE   in_array ALIAS FOR $1;   temp_string TEXT;   quoted_string TEXT;   i INTEGER;
BEGIN   FOR i IN array_lower(in_array, 1)..array_upper(in_array, 1)   LOOP       IF in_array[i]::TEXT ~ ''[,"]''
THEN          temp_string := ''"'' || replace(in_array[i]::TEXT, ''"'', ''""'')  || ''"'';       ELSE
temp_string:= in_array[i]::TEXT;       END IF;       IF i = array_lower(in_array, 1)       THEN           quoted_string
:=temp_string;       ELSE           quoted_string := quoted_string || '','' || temp_string;       END IF;   END LOOP;
RETURNquoted_string;
 
END;
' LANGUAGE 'plpgsql';
-- 
David Fetter david@fetter.org http://fetter.org/
phone: +1 510 893 6100    cell: +1 415 235 3778

When a man tells you that he got rich through hard work, ask him:
'Whose?'       Don Marquis, quoted in Edward Anthony, O Rare Don Marquis


В списке pgsql-hackers по дате отправления:

Предыдущее
От: Andrew Dunstan
Дата:
Сообщение: Re: CSV hack
Следующее
От: "Dave Page"
Дата:
Сообщение: 7.4 build problem on Linux Vserver