Created
April 10, 2012 04:35
-
-
Save jotapepinheiro/2348335 to your computer and use it in GitHub Desktop.
SLUG URL POSTGRE FUNCTION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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