1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
--Fonction Explode qui transforme une chaine de caracteres en tableau
CREATE OR REPLACE FUNCTION explode(stringArray text, separator text) RETURNS text[] AS'
DECLARE
postgresArray text[];
restStringArray text;
beginning int;
ending int;
currentSeparatorPosition int;
currentValue text;
i int;
BEGIN
postgresArray:=''{}'';
IF position(''{'' in stringArray)>0 AND position(''}'' in stringArray)>0 THEN
beginning:=position(''{'' in stringArray)+1;
ending:=position(''}'' in stringArray)-beginning;
restStringArray:=substring(stringArray from beginning for ending);
currentSeparatorPosition:=position(separator in restStringArray)-1;
i:=1;
WHILE currentSeparatorPosition>0 LOOP
currentValue:=substring(restStringArray from 1 for currentSeparatorPosition-1);
postgresArray[i]:=currentValue;
restStringArray:=substring(restStringArray from currentSeparatorPosition+1);
i:=i+1;
currentSeparatorPosition:=position(separator in restStringArray);
END LOOP;
ELSE
postgresArray[1]:=''Error : '' || stringArray || '' is not formated as an array ({..,..,..}).'';
END IF;
RETURN postgresArray;
END;
'LANGUAGE 'plpgsql' ; |