Re: Case insensitive hstore. - Mailing list pgsql-general

From Ian Lawrence Barwick
Subject Re: Case insensitive hstore.
Date
Msg-id CAB8KJ=iu02DupMkLe-gmQb+asvoQvBayma4x3ff4nLkf8W-tNg@mail.gmail.com
Whole thread Raw
In response to Case insensitive hstore.  (Glenn Pierce <glennpierce@gmail.com>)
List pgsql-general
2013/2/16 Glenn Pierce <glennpierce@gmail.com>:
> Hi
>
> Does anyone know how one would
> select from a table with a hstore field treating the key of the hstore as
> case insensitive.
>
> ie
>
> SELECT id, lower(additional_info->'type') AS type FROM table
>
> I would like this to work even if if the store tyoe is
>
> 'Type' -> 'original'

As far as I can see from looking at the docs, it's not possible (I
could be wrong though).

> failing that is there a way to lowercase the keys and values of the hstore
> field of the entire table ?

You could create a function like this:


CREATE OR REPLACE FUNCTION hstore_to_lower(val HSTORE)
  RETURNS HSTORE
  LANGUAGE plpgsql
AS $function$
  DECLARE
     hkey TEXT;
  BEGIN
     FOR hkey IN
       SELECT SKEYS(val)
     LOOP

       IF LOWER(hkey) != hkey THEN
         val := val || (LOWER(hkey) || '=>' ||
LOWER((val->hkey::TEXT)))::HSTORE;
         val := val - hkey;
       END IF;

     END LOOP;
     RETURN val;
  END;
$function$

No guarantee of suitability for a particular purpose or of it being
the optimal way of
doing this ;)
Note that any keys converted to lower case will overwrite existing
lower case keys.


HTH

Ian Barwick

pgsql-general by date:

Previous
From: David Kerr
Date:
Subject: PG9.2.3. Query hanging: SELECT count(*) FROM pg_catalog.pg_class...
Next
From: "René Romero Benavides"
Date:
Subject: Re: PG9.2.3. Query hanging: SELECT count(*) FROM pg_catalog.pg_class...