Re: 8.0.3 regexp_replace()...

Поиск
Список
Период
Сортировка
От rlee0001
Тема Re: 8.0.3 regexp_replace()...
Дата
Msg-id 1138649243.775197.248880@g14g2000cwa.googlegroups.com
обсуждение исходный текст
Ответ на 8.0.3 regexp_replace()...  ("rlee0001" <robeddielee@hotmail.com>)
Ответы Re: 8.0.3 regexp_replace()...  (Martijn van Oosterhout <kleptog@svana.org>)
Re: 8.0.3 regexp_replace()...  (Stephan Szabo <sszabo@megazone.bigpanda.com>)
Список pgsql-general
I did get the code working. The function DDL follows:

CREATE OR REPLACE FUNCTION "webadmin"."regexp_replacex" (source
varchar, pattern varchar, replacement varchar) RETURNS varchar AS
$body$
DECLARE
 retvalue VARCHAR;
BEGIN
 retvalue = "source";
 LOOP
  retvalue = REPLACE(retvalue, COALESCE(SUBSTRING(retvalue FROM
"pattern"), ''), "replacement");
  EXIT WHEN retvalue = REPLACE(retvalue, COALESCE(SUBSTRING(retvalue
FROM "pattern"), ''), "replacement");
 END LOOP;
 RETURN retvalue;
END;
$body$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

The problem was that SUBSTRING returns NULL if it cannot find any
matches for the pattern and when the second parameter to REPLACE
returns NULL, REPLACE returns NULL (which is idiotic). Using COALESCE I
ensure that is SUBSTRING cannot find a match that '' (empty string) is
sent to REPLACE. REPLACE then behaves as expected and replaces nothing.

Debugging PostgreSQL's retarded behaviour around NULL values can be a
real pain. But at least I learned to use EMS PostgreSQL Manager for
Windows' function debugger, which can step through a function while
reporting the values of all variables, parameters and return values.
Very handy.

-Robert


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

Предыдущее
От: "rlee0001"
Дата:
Сообщение: Re: Little Offtopic: Database frontends
Следующее
От: "James Harper"
Дата:
Сообщение: libpq questions