Skip to content

Instantly share code, notes, and snippets.

@jotapepinheiro
Created April 10, 2012 04:35
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jotapepinheiro/2348335 to your computer and use it in GitHub Desktop.
Save jotapepinheiro/2348335 to your computer and use it in GitHub Desktop.
SLUG URL POSTGRE FUNCTION
CREATE OR REPLACE FUNCTION public.url_slug (
s_texto text
)
RETURNS varchar AS
$body$
DECLARE
total integer;
BEGIN
s_texto := replace(s_texto , 'U$', 'dolares');
s_texto := replace(s_texto , 'R$', 'reais');
s_texto := regexp_replace(translate(replace(lower(s_texto), ' ', '-'),
'áàâãäåāăąÁÂÃÄÅĀĂĄèééêëēĕėęěĒĔĖÉĘĚìíîïìĩīĭÌÍÎÏÌĨĪĬóôõöōŏőÒÓÔÕÖŌŎŐùúûüũūŭůÙÚÛÜŨŪŬŮçÇÿ&,.ñÑ',
'aaaaaaaaaaaaaaaaaeeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiooooooooooooooouuuuuuuuuuuuuuuuccy_--nn'), E'[^\\w -]', '', 'g');
SELECT COUNT(not_slug) INTO total FROM noticias WHERE not_slug = s_texto;
IF total > 0 THEN
RETURN s_texto || '-' || total+1;
ELSE
RETURN s_texto;
END IF;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY DEFINER
COST 100;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment