Re: CSV hack - Mailing list pgsql-hackers

From david@fetter.org (David Fetter)
Subject Re: CSV hack
Date
Msg-id 200312110743.hBB7hYqa003302@fetter.org
Whole thread Raw
In response to Re: CSV hack  (Andrew Dunstan <andrew@dunslane.net>)
List 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 by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: CSV hack
Next
From: "Dave Page"
Date:
Subject: 7.4 build problem on Linux Vserver