ApiBootstraper / Rails-server

A Bootstrap to create REST API with Rails
http://apibootstraper.github.io/apps/rails-server
MIT License
1 stars 0 forks source link

PgSQL search with accents #5

Open nicolas-brousse opened 11 years ago

nicolas-brousse commented 11 years ago

Create a migration to add a pgsql function ?

nicolas-brousse commented 11 years ago

And add verification to check if database used is PostgreSQL.

nicolas-brousse commented 11 years ago

See this page : http://wiki.postgresql.org/wiki/Strip_accents_from_strings,_and_output_in_lowercase

CREATE OR REPLACE FUNCTION name2uri(text)
RETURNS text
IMMUTABLE
STRICT
LANGUAGE SQL
AS $$
SELECT REPLACE(trim(regexp_replace(translate(
    lower($1),
    'áàâãäåāăąèééêëēĕėęěìíîïìĩīĭḩóôõöōŏőùúûüũūŭůäàáâãåæçćĉčöòóôõøüùúûßéèêëýñîìíïş',
    'aaaaaaaaaeeeeeeeeeeiiiiiiiihooooooouuuuuuuuaaaaaaeccccoooooouuuuseeeeyniiiis'
), '[^a-z0-9\-]+', ' ', 'g')),' ', '-');
$$;

or

CREATE OR REPLACE FUNCTION unaccent_string(text)
RETURNS text
IMMUTABLE
STRICT
LANGUAGE SQL
AS $$
SELECT translate(
    $1,
    'âãäåāăąÁÂÃÄÅĀĂĄèééêëēĕėęěĒĔĖĘĚìíîïìĩīĭÌÍÎÏÌĨĪĬóôõöōŏőÒÓÔÕÖŌŎŐùúûüũūŭůÙÚÛÜŨŪŬŮ',
    'aaaaaaaaaaaaaaaeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiooooooooooooooouuuuuuuuuuuuuuuu'
);
$$;
nicolas-brousse commented 11 years ago

Create a fonction for search to do SQL in function of adapter used. And use pgsql function to unaccent string before use sql like.